Skip to content

Commit

Permalink
Merge pull request #84 from rawlingsj/master
Browse files Browse the repository at this point in the history
chore: handle both old and new style jx requirements file
  • Loading branch information
jenkins-x-bot authored Nov 27, 2020
2 parents a66332d + c57ddab commit 8b410fa
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 12 deletions.
49 changes: 37 additions & 12 deletions pkg/apis/core/v4beta1/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ func LoadRequirementsConfig(dir string, failOnValidationErrors bool) (*Requireme

// LoadRequirementsConfigFile loads a specific project YAML configuration file
func LoadRequirementsConfigFile(fileName string, failOnValidationErrors bool) (*RequirementsConfig, error) {

config := &RequirementsConfig{}
_, err := os.Stat(fileName)
if err != nil {
Expand All @@ -481,22 +482,46 @@ func LoadRequirementsConfigFile(fileName string, failOnValidationErrors bool) (*
return nil, fmt.Errorf("failed to load file %s due to %s", fileName, err)
}

validationErrors, err := util.ValidateYaml(config, data)
if err != nil {
return nil, fmt.Errorf("failed to validate YAML file %s due to %s", fileName, err)
}
s := string(data)
// //check whether new or old jx requirements
if strings.Contains(s, "apiVersion") && strings.Contains(s, "kind") && strings.Contains(s, "spec") {
requirements := &Requirements{}
validationErrors, err := util.ValidateYaml(requirements, data)
if err != nil {
return nil, fmt.Errorf("failed to validate YAML file %s due to %s", fileName, err)
}

if len(validationErrors) > 0 {
log.Logger().Warnf("validation failures in YAML file %s: %s", fileName, strings.Join(validationErrors, ", "))
if len(validationErrors) > 0 {
log.Logger().Warnf("validation failures in YAML file %s: %s", fileName, strings.Join(validationErrors, ", "))
if failOnValidationErrors {
return nil, fmt.Errorf("validation failures in YAML file %s:\n%s", fileName, strings.Join(validationErrors, "\n"))
}
}

if failOnValidationErrors {
return nil, fmt.Errorf("validation failures in YAML file %s:\n%s", fileName, strings.Join(validationErrors, "\n"))
err = yaml.Unmarshal(data, requirements)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML file %s due to %s", fileName, err)
}
}

err = yaml.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML file %s due to %s", fileName, err)
config = &requirements.Spec
} else {
validationErrors, err := util.ValidateYaml(config, data)
if err != nil {
return nil, fmt.Errorf("failed to validate YAML file %s due to %s", fileName, err)
}

if len(validationErrors) > 0 {
log.Logger().Warnf("validation failures in YAML file %s: %s", fileName, strings.Join(validationErrors, ", "))

if failOnValidationErrors {
return nil, fmt.Errorf("validation failures in YAML file %s:\n%s", fileName, strings.Join(validationErrors, "\n"))
}
}

err = yaml.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML file %s due to %s", fileName, err)
}
}

config.addDefaults()
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/core/v4beta1/requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,21 @@ func TestLoadRequirementsConfig_load_invalid_yaml(t *testing.T) {
requirementsConfigPath := path.Join(absolute, v4beta1.RequirementsConfigFileName)
assert.EqualError(t, err, fmt.Sprintf("validation failures in YAML file %s:\nenvironments.0: Additional property namespace is not allowed", requirementsConfigPath))
}

func TestBackwardsCompatibleRequirementsFile(t *testing.T) {
t.Parallel()
oldRequirementsDir := path.Join(testDataDir, "backwards_compatible_requirements_file", "old")
newRequirementsDir := path.Join(testDataDir, "backwards_compatible_requirements_file", "new")

validateRequirements(t, oldRequirementsDir)
validateRequirements(t, newRequirementsDir)

}

func validateRequirements(t *testing.T, oldRequirementsDir string) {
requirements, fileName, err := v4beta1.LoadRequirementsConfig(oldRequirementsDir, true)
assert.NoError(t, err, "failed to load old style jx-requirements.yml")
assert.NotEmpty(t, fileName, "requirements filename should not be empty")
assert.NotNil(t, requirements, "requirements should not be empty")
assert.Equal(t, v4beta1.WebhookTypeLighthouse, requirements.Webhook, "failed to find requirement")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: core.jenkins.io/v4beta1
kind: Requirements
spec:
autoUpdate:
enabled: true
schedule: 0 0 * * *
cluster:
environmentGitPublic: true
gitKind: github
gitName: github
gitPublic: true
gitServer: https://github.com
gke:
projectNumber: "1055835833001"
provider: gke
registry: gcr.io
zone: us-east1-c
repository: nexus
secretStorage: vault
webhook: lighthouse
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
autoUpdate:
enabled: true
schedule: 0 0 * * *
cluster:
environmentGitPublic: true
gitKind: github
gitName: github
gitPublic: true
gitServer: https://github.com
gke:
projectNumber: "1055835833001"
provider: gke
registry: gcr.io
zone: us-east1-c
environments:
- ingress:
cloud_dns_secret_name: external-dns-gcp-sa
namespaceSubDomain: -jx.
tls:
enabled: true
production: false
key: dev
repository: nexus
secretStorage: vault
webhook: lighthouse

0 comments on commit 8b410fa

Please sign in to comment.