Skip to content

Commit

Permalink
fix: Only validate manifests for certain resource actions. Fixes #9418 (
Browse files Browse the repository at this point in the history
#9419)

* fix: Only validate manifests for certain resource actions. Fixes #9418

Signed-off-by: Yuan Tang <[email protected]>

* chore: fix lint

Signed-off-by: Yuan Tang <[email protected]>

* fix: lint

Signed-off-by: Yuan Tang <[email protected]>

Signed-off-by: Yuan Tang <[email protected]>
  • Loading branch information
terrytangyuan committed Aug 24, 2022
1 parent a91e004 commit 3ddbb5e
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,30 +679,32 @@ func (ctx *templateValidationCtx) validateLeaf(scope map[string]interface{}, tmp
return errors.Errorf(errors.CodeBadRequest, "templates.%s.resource.action must be one of: get, create, apply, delete, replace, patch", tmpl.Name)
}
}
if tmpl.Resource.Manifest == "" && tmpl.Resource.ManifestFrom == nil {
return errors.Errorf(errors.CodeBadRequest, "either templates.%s.resource.manifest or templates.%s.resource.manifestFrom must be specified", tmpl.Name, tmpl.Name)
}
if tmpl.Resource.Manifest != "" && tmpl.Resource.ManifestFrom != nil {
return errors.Errorf(errors.CodeBadRequest, "shouldn't have both `manifest` and `manifestFrom` specified in `Manifest` for resource template")
}
if tmpl.Resource.ManifestFrom != nil && tmpl.Resource.ManifestFrom.Artifact != nil {
var found bool
for _, art := range tmpl.Inputs.Artifacts {
if tmpl.Resource.ManifestFrom.Artifact.Name == art.Name {
found = true
break
}
if tmpl.Resource.Action != "delete" && tmpl.Resource.Action != "get" {
if tmpl.Resource.Manifest == "" && tmpl.Resource.ManifestFrom == nil {
return errors.Errorf(errors.CodeBadRequest, "either templates.%s.resource.manifest or templates.%s.resource.manifestFrom must be specified", tmpl.Name, tmpl.Name)
}
if !found {
return errors.Errorf(errors.CodeBadRequest, "artifact %s in `manifestFrom` refer to a non-exist artifact", tmpl.Resource.ManifestFrom.Artifact.Name)
if tmpl.Resource.Manifest != "" && tmpl.Resource.ManifestFrom != nil {
return errors.Errorf(errors.CodeBadRequest, "shouldn't have both `manifest` and `manifestFrom` specified in `Manifest` for resource template")
}
}
if tmpl.Resource.Manifest != "" && !placeholderGenerator.IsPlaceholder(tmpl.Resource.Manifest) {
// Try to unmarshal the given manifest, just ensuring it's a valid YAML.
var obj interface{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.resource.manifest must be a valid yaml", tmpl.Name)
if tmpl.Resource.ManifestFrom != nil && tmpl.Resource.ManifestFrom.Artifact != nil {
var found bool
for _, art := range tmpl.Inputs.Artifacts {
if tmpl.Resource.ManifestFrom.Artifact.Name == art.Name {
found = true
break
}
}
if !found {
return errors.Errorf(errors.CodeBadRequest, "artifact %s in `manifestFrom` refer to a non-exist artifact", tmpl.Resource.ManifestFrom.Artifact.Name)
}
}
if tmpl.Resource.Manifest != "" && !placeholderGenerator.IsPlaceholder(tmpl.Resource.Manifest) {
// Try to unmarshal the given manifest, just ensuring it's a valid YAML.
var obj interface{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.resource.manifest must be a valid yaml", tmpl.Name)
}
}
}
}
Expand Down

0 comments on commit 3ddbb5e

Please sign in to comment.