Skip to content

Commit

Permalink
Fix validations
Browse files Browse the repository at this point in the history
Co-authored-by: Marty Spiewak <mspiewak@vmware.com>
  • Loading branch information
emmjohnson and Marty Spiewak committed Mar 24, 2022
1 parent 77f2494 commit 3c71ccd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/apis/v1alpha1/cluster_run_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -96,12 +97,12 @@ func (c *ClusterRunTemplate) ValidateDelete() error {
}

func (t *RunTemplateSpec) validate() error {
obj := metav1.PartialObjectMetadata{}
obj := unstructured.Unstructured{}
if err := json.Unmarshal(t.Template.Raw, &obj); err != nil {
return fmt.Errorf("invalid template: failed to parse object metadata: %w", err)
}

if obj.Namespace != metav1.NamespaceNone {
if obj.GetNamespace() != metav1.NamespaceNone {
return errors.New("invalid template: template should not set metadata.namespace on the child object")
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/v1alpha1/cluster_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/json"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -94,11 +95,11 @@ func (t *TemplateSpec) validate() error {
return fmt.Errorf("invalid template: must specify one of template or ytt, found both")
}
if t.Template != nil {
obj := metav1.PartialObjectMetadata{}
obj := unstructured.Unstructured{}
if err := json.Unmarshal(t.Template.Raw, &obj); err != nil {
return fmt.Errorf("invalid template: failed to parse object metadata: %w", err)
}
if obj.Namespace != metav1.NamespaceNone {
if obj.GetNamespace() != metav1.NamespaceNone {
return errors.New("invalid template: template should not set metadata.namespace on the child object")
}
}
Expand Down

0 comments on commit 3c71ccd

Please sign in to comment.