Skip to content

Commit

Permalink
[0.6] Make sure to update the name in the mutator (#535)
Browse files Browse the repository at this point in the history
* Make sure to update the name in the mutator

* add unit test
  • Loading branch information
JonCrowther authored Oct 31, 2024
1 parent b174447 commit 7e0627b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
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

0 comments on commit 7e0627b

Please sign in to comment.