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

[0.6] Make sure to update the name in the mutator #535

Merged
merged 2 commits into from
Oct 31, 2024
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
6 changes: 3 additions & 3 deletions pkg/resources/management.cattle.io/v3/project/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ func (m *Mutator) createProjectNamespace(project *v3.Project) (*v3.Project, erro
if project.Name == "" {
// If err is nil, (meaning "project exists", see below) we need to repeat the generation process to find a project name and backing namespace that isn't taken
for err == nil {
newName := names.SimpleNameGenerator.GenerateName(project.GenerateName)
_, err = m.projectClient.Get(newProject.Spec.ClusterName, newName, v1.GetOptions{})
newProject.Name = names.SimpleNameGenerator.GenerateName(project.GenerateName)
_, err = m.projectClient.Get(newProject.Spec.ClusterName, newProject.Name, v1.GetOptions{})
if err == nil {
// A project with this name already exists. Generate a new name.
continue
} else if !apierrors.IsNotFound(err) {
return nil, err
}

backingNamespace = name.SafeConcatName(newProject.Spec.ClusterName, strings.ToLower(newName))
backingNamespace = name.SafeConcatName(newProject.Spec.ClusterName, strings.ToLower(newProject.Name))
_, err = m.namespaceClient.Get(backingNamespace, v1.GetOptions{})

// If the backing namespace already exists, generate a new project name
Expand Down
65 changes: 57 additions & 8 deletions pkg/resources/management.cattle.io/v3/project/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import (
"encoding/json"
"fmt"
"strings"
"testing"

jsonpatch "github.com/evanphx/json-patch"
Expand Down Expand Up @@ -39,14 +40,15 @@ var (
func TestAdmit(t *testing.T) {
t.Parallel()
tests := []struct {
name string
operation admissionv1.Operation
dryRun bool
oldProject func() *v3.Project
newProject func() *v3.Project
indexer func() ([]*v3.RoleTemplate, error)
wantProject func() *v3.Project
wantErr bool
name string
operation admissionv1.Operation
dryRun bool
oldProject func() *v3.Project
newProject func() *v3.Project
indexer func() ([]*v3.RoleTemplate, error)
wantProject func() *v3.Project
wantErr bool
generateName bool
}{
{
name: "dry run returns allowed",
Expand Down Expand Up @@ -158,6 +160,45 @@ func TestAdmit(t *testing.T) {
return p
},
},
{
name: "create with generated name",
operation: admissionv1.Create,
newProject: func() *v3.Project {
p := defaultProject.DeepCopy()
p.Name = ""
p.GenerateName = "p-"
return p
},
wantProject: func() *v3.Project {
p := defaultProject.DeepCopy()
p.Name = "p-"
p.GenerateName = "p-"
p.Annotations = map[string]string{
"authz.management.cattle.io/creator-role-bindings": "{\"required\":[\"project-owner\"]}",
}
p.Status.BackingNamespace = "testcluster-p-"
return p
},
generateName: true,
},
{
name: "when creating with generated name and name, prioritize name",
operation: admissionv1.Create,
newProject: func() *v3.Project {
p := defaultProject.DeepCopy()
p.GenerateName = "p-"
return p
},
wantProject: func() *v3.Project {
p := defaultProject.DeepCopy()
p.GenerateName = "p-"
p.Annotations = map[string]string{
"authz.management.cattle.io/creator-role-bindings": "{\"required\":[\"project-owner\"]}",
}
p.Status.BackingNamespace = "testcluster-testproject"
return p
},
},
}

roleTemplates := []*v3.RoleTemplate{
Expand Down Expand Up @@ -223,6 +264,14 @@ func TestAdmit(t *testing.T) {
err = json.Unmarshal(patchedJS, gotObj)
assert.NoError(t, err, "failed to unmarshal patched Object")

if test.generateName {
// Since the name is a random string, confirm it has the right prefix and set it back to p- before checking equality.
assert.True(t, strings.HasPrefix(gotObj.Name, newProject.GenerateName))
gotObj.Name = newProject.GenerateName
// The backing namespace will also have a random string. Confirm that it has the prefix "ClusterName-GenerateName" and reset it before checking equality.
assert.True(t, strings.HasPrefix(gotObj.Status.BackingNamespace, fmt.Sprintf("%s-%s", newProject.Spec.ClusterName, newProject.GenerateName)))
gotObj.Status.BackingNamespace = fmt.Sprintf("%s-%s", newProject.Spec.ClusterName, newProject.GenerateName)
}
assert.Equal(t, test.wantProject(), gotObj)
} else {
assert.Nil(t, resp.Patch, "unexpected patch request received")
Expand Down
Loading