Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webhook: Don't check common template namespace #747

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions tests/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,6 @@ var _ = Describe("Validation webhook", func() {
strategy.RevertToOriginalSspCr()
})

It("[test_id:6056] should fail to create SSP CR with invalid commonTemplates.namespace", func() {
newSsp.Spec.CommonTemplates.Namespace = "nonexisting-templates-namespace"

err := apiClient.Create(ctx, newSsp, client.DryRunAll)
if err == nil {
Fail("SSP CR with invalid commonTemplates.namespace created.")
return
}
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf(
"creation failed, the configured namespace for common templates does not exist: %v",
newSsp.Spec.CommonTemplates.Namespace,
)))
})

Context("Placement API validation", func() {
It("[test_id:5988]should succeed with valid template-validator placement fields", func() {
newSsp.Spec.TemplateValidator = &sspv1beta2.TemplateValidator{
Expand Down
40 changes: 12 additions & 28 deletions webhooks/ssp_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,7 @@ func (s *sspValidator) ValidateCreate(ctx context.Context, obj runtime.Object) e
return fmt.Errorf("creation failed, an SSP CR already exists in namespace %v: %v", ssps.Items[0].ObjectMeta.Namespace, ssps.Items[0].ObjectMeta.Name)
}

// Check if the common templates namespace exists
namespaceName := sspObj.Spec.CommonTemplates.Namespace
var namespace v1.Namespace
err = s.apiClient.Get(ctx, client.ObjectKey{Name: namespaceName}, &namespace)
if err != nil {
return fmt.Errorf("creation failed, the configured namespace for common templates does not exist: %v", namespaceName)
}

if err = s.validatePlacement(ctx, sspObj); err != nil {
return fmt.Errorf("placement api validation error: %w", err)
}

if err := validateDataImportCronTemplates(sspObj); err != nil {
return fmt.Errorf("dataImportCronTemplates validation error: %w", err)
}

if err := validateCommonInstancetypes(sspObj); err != nil {
return fmt.Errorf("commonInstancetypes validation error: %w", err)
}

return nil
return s.validateSspObject(ctx, sspObj)
}

func (s *sspValidator) ValidateUpdate(ctx context.Context, _, newObj runtime.Object) error {
Expand All @@ -110,25 +90,29 @@ func (s *sspValidator) ValidateUpdate(ctx context.Context, _, newObj runtime.Obj

ssplog.Info("validate update", "name", newSsp.Name)

if err := s.validatePlacement(ctx, newSsp); err != nil {
return s.validateSspObject(ctx, newSsp)
}

func (s *sspValidator) ValidateDelete(_ context.Context, _ runtime.Object) error {
return nil
}

func (s *sspValidator) validateSspObject(ctx context.Context, ssp *sspv1beta2.SSP) error {
if err := s.validatePlacement(ctx, ssp); err != nil {
return fmt.Errorf("placement api validation error: %w", err)
}

if err := validateDataImportCronTemplates(newSsp); err != nil {
if err := validateDataImportCronTemplates(ssp); err != nil {
return fmt.Errorf("dataImportCronTemplates validation error: %w", err)
}

if err := validateCommonInstancetypes(newSsp); err != nil {
if err := validateCommonInstancetypes(ssp); err != nil {
return fmt.Errorf("commonInstancetypes validation error: %w", err)
}

return nil
}

func (s *sspValidator) ValidateDelete(_ context.Context, _ runtime.Object) error {
return nil
}

func (s *sspValidator) validatePlacement(ctx context.Context, ssp *sspv1beta2.SSP) error {
if ssp.Spec.TemplateValidator == nil {
return nil
Expand Down
38 changes: 0 additions & 38 deletions webhooks/ssp_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,6 @@ var _ = Describe("SSP Validation", func() {
})
})

It("should fail if template namespace does not exist", func() {
const nonexistingNamespace = "nonexisting-namespace"
ssp := &sspv1beta2.SSP{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ssp",
Namespace: "test-ns",
},
Spec: sspv1beta2.SSPSpec{
CommonTemplates: sspv1beta2.CommonTemplates{
Namespace: nonexistingNamespace,
},
},
}
err := validator.ValidateCreate(ctx, toUnstructured(ssp))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("creation failed, the configured namespace for common templates does not exist: " + nonexistingNamespace))
})

It("should accept old v1beta1 SSP CR", func() {
ssp := &sspv1beta1.SSP{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -166,26 +148,6 @@ var _ = Describe("SSP Validation", func() {
})
})

It("should allow update of commonTemplates.namespace", func() {
oldSsp := &sspv1beta2.SSP{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ssp",
Namespace: "test-ns",
},
Spec: sspv1beta2.SSPSpec{
CommonTemplates: sspv1beta2.CommonTemplates{
Namespace: "old-ns",
},
},
}

newSsp := oldSsp.DeepCopy()
newSsp.Spec.CommonTemplates.Namespace = "new-ns"

err := validator.ValidateUpdate(ctx, toUnstructured(oldSsp), toUnstructured(newSsp))
Expect(err).ToNot(HaveOccurred())
})

Context("DataImportCronTemplates", func() {
const (
templatesNamespace = "test-templates-ns"
Expand Down
Loading