Skip to content
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
17 changes: 0 additions & 17 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,26 +558,9 @@ func (ac *AdmissionController) mutate(ctx context.Context, kind metav1.GroupVers
}
}

if err := validateMetadata(newObj); err != nil {
logger.Error("Failed to validate", zap.Error(err))
return nil, fmt.Errorf("Failed to validate: %s", err)
}
return json.Marshal(patches)
}

func validateMetadata(new GenericCRD) error {
name := new.GetObjectMeta().GetName()

if strings.Contains(name, ".") {
return errors.New("Invalid resource name: special character . must not be present")
}

if len(name) > 63 {
return errors.New("Invalid resource name: length must be no more than 63 characters")
}
return nil
}

// updateGeneration sets the generation by following this logic:
// if there's no old object, it's create, set generation to 1
// if there's an old object and spec has changed, set generation to oldGeneration + 1
Expand Down
29 changes: 0 additions & 29 deletions webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,6 @@ func TestUnknownKindFails(t *testing.T) {
expectFailsWith(t, ac.admit(TestContextWithLogger(t), &req), "unhandled kind")
}

func TestInvalidNameFails(t *testing.T) {
_, ac := newNonRunningTestAdmissionController(t, newDefaultOptions())
req := &admissionv1beta1.AdmissionRequest{
Operation: admissionv1beta1.Create,
Kind: metav1.GroupVersionKind{
Group: "pkg.knative.dev",
Version: "v1alpha1",
Kind: "Resource",
},
}
invalidName := "an.example"
config := createResource(0, invalidName)
marshaled, err := json.Marshal(config)
if err != nil {
t.Fatalf("Failed to marshal resource: %s", err)
}
req.Object.Raw = marshaled
expectFailsWith(t, ac.admit(TestContextWithLogger(t), req), "Invalid resource name")

invalidName = strings.Repeat("a", 64)
config = createResource(0, invalidName)
marshaled, err = json.Marshal(config)
if err != nil {
t.Fatalf("Failed to marshal resource: %s", err)
}
req.Object.Raw = marshaled
expectFailsWith(t, ac.admit(TestContextWithLogger(t), req), "Invalid resource name")
}

func TestValidCreateResourceSucceeds(t *testing.T) {
r := createResource(1234, "a name")
r.SetDefaults() // Fill in defaults to check that there are no patches.
Expand Down