Skip to content

Commit 3470c2b

Browse files
authored
expand metadata mutator for finalizers and owner references (#49)
* expand metadata mutator for finalizers and owner references * fix bug and add metadata mutator test * move labels and annotations in resource mutators into metadata mutator
1 parent c913430 commit 3470c2b

24 files changed

+337
-84
lines changed

docs/libs/resource.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ Create or update a `ConfigMap`, a `ServiceAccount` and a `Deployment` using the
99

1010
```go
1111
type myDeploymentMutator struct {
12+
meta MetadataMutator
1213
}
1314

1415
var _ resource.Mutator[*appsv1.Deployment] = &myDeploymentMutator{}
1516

16-
func newDeploymentMutator() resources.Mutator[*appsv1.Deployment] {
17-
return &MyDeploymentMutator{}
17+
func newDeploymentMutator() resource.Mutator[*appsv1.Deployment] {
18+
return &MyDeploymentMutator{
19+
meta: NewMetadataMutator()
20+
}
1821
}
1922

2023
func (m *MyDeploymentMutator) String() string {
@@ -38,17 +41,22 @@ func (m *MyDeploymentMutator) Mutate(deployment *appsv1.Deployment) error {
3841
Image: "test-image:latest",
3942
},
4043
}
41-
return nil
44+
return m.meta.Mutate(deployment)
45+
}
46+
47+
func (m *MyDeploymentMutator) MetadataMutator() resource.MetadataMutator {
48+
return m.meta
4249
}
4350

4451

4552
func ReconcileResources(ctx context.Context, client client.Client) error {
46-
configMapResource := resource.NewConfigMap("my-configmap", "my-namespace", map[string]string{
53+
configMapResource := resource.NewConfigMapMutator("my-configmap", "my-namespace")
54+
configMapResource.MetadataMutator().WithLabels(map[string]string{
4755
"label1": "value1",
4856
"label2": "value2",
49-
}, nil)
57+
})
5058

51-
serviceAccountResource := resource.NewServiceAccount("my-serviceaccount", "my-namespace", nil, nil)
59+
serviceAccountResource := resource.NewServiceAccountMutator("my-serviceaccount", "my-namespace")
5260

5361
myDeploymentMutator := newDeploymentMutator()
5462

pkg/crds/crds.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func (m *CRDManager) CreateOrUpdateCRDs(ctx context.Context, log *logging.Logger
5757
if log != nil {
5858
log.Info("creating/updating CRD", "name", crd.Name, "cluster", c.ID())
5959
}
60-
err = resources.CreateOrUpdateResource(ctx, c.Client(), resources.NewCRDMutator(crd, crd.Labels, crd.Annotations))
60+
m := resources.NewCRDMutator(crd)
61+
m.MetadataMutator().WithLabels(crd.Labels).WithAnnotations(crd.Annotations)
62+
err = resources.CreateOrUpdateResource(ctx, c.Client(), m)
6163
errs = errors.Join(errs, err)
6264
}
6365

pkg/resources/clusterrole.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ package resources
33
import (
44
"fmt"
55

6-
"sigs.k8s.io/controller-runtime/pkg/client"
7-
86
v1 "k8s.io/api/rbac/v1"
97
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
108
)
119

1210
type ClusterRoleMutator struct {
1311
Name string
1412
Rules []v1.PolicyRule
15-
meta Mutator[client.Object]
13+
meta MetadataMutator
1614
}
1715

1816
var _ Mutator[*v1.ClusterRole] = &ClusterRoleMutator{}
1917

20-
func NewClusterRoleMutator(name string, rules []v1.PolicyRule, labels map[string]string, annotations map[string]string) Mutator[*v1.ClusterRole] {
18+
func NewClusterRoleMutator(name string, rules []v1.PolicyRule) Mutator[*v1.ClusterRole] {
2119
return &ClusterRoleMutator{
2220
Name: name,
2321
Rules: rules,
24-
meta: NewMetadataMutator(labels, annotations),
22+
meta: NewMetadataMutator(),
2523
}
2624
}
2725

@@ -45,3 +43,7 @@ func (m *ClusterRoleMutator) Mutate(r *v1.ClusterRole) error {
4543
r.Rules = m.Rules
4644
return m.meta.Mutate(r)
4745
}
46+
47+
func (m *ClusterRoleMutator) MetadataMutator() MetadataMutator {
48+
return m.meta
49+
}

pkg/resources/clusterrole_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ var _ = Describe("ClusterRoleMutator", func() {
4848
annotations = map[string]string{"annotation1": "value1"}
4949

5050
// Create a cluster role mutator
51-
mutator = resources.NewClusterRoleMutator("test-clusterrole", rules, labels, annotations)
51+
mutator = resources.NewClusterRoleMutator("test-clusterrole", rules)
52+
mutator.MetadataMutator().WithLabels(labels).WithAnnotations(annotations)
5253
})
5354

5455
It("should create an empty cluster role with correct metadata", func() {

pkg/resources/clusterrolebinding.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package resources
33
import (
44
"fmt"
55

6-
"sigs.k8s.io/controller-runtime/pkg/client"
7-
86
v1 "k8s.io/api/rbac/v1"
97
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
108
)
@@ -13,17 +11,17 @@ type ClusterRoleBindingMutator struct {
1311
ClusterRoleBindingName string
1412
RoleRef v1.RoleRef
1513
Subjects []v1.Subject
16-
meta Mutator[client.Object]
14+
meta MetadataMutator
1715
}
1816

1917
var _ Mutator[*v1.ClusterRoleBinding] = &ClusterRoleBindingMutator{}
2018

21-
func NewClusterRoleBindingMutator(clusterRoleBindingName string, subjects []v1.Subject, roleRef v1.RoleRef, labels map[string]string, annotations map[string]string) Mutator[*v1.ClusterRoleBinding] {
19+
func NewClusterRoleBindingMutator(clusterRoleBindingName string, subjects []v1.Subject, roleRef v1.RoleRef) Mutator[*v1.ClusterRoleBinding] {
2220
return &ClusterRoleBindingMutator{
2321
ClusterRoleBindingName: clusterRoleBindingName,
2422
RoleRef: roleRef,
2523
Subjects: subjects,
26-
meta: NewMetadataMutator(labels, annotations),
24+
meta: NewMetadataMutator(),
2725
}
2826
}
2927

@@ -48,3 +46,7 @@ func (m *ClusterRoleBindingMutator) Mutate(r *v1.ClusterRoleBinding) error {
4846
r.Subjects = m.Subjects
4947
return m.meta.Mutate(r)
5048
}
49+
50+
func (m *ClusterRoleBindingMutator) MetadataMutator() MetadataMutator {
51+
return m.meta
52+
}

pkg/resources/clusterrolebinding_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ var _ = Describe("ClusterRoleBindingMutator", func() {
5050
annotations = map[string]string{"annotation1": "value1"}
5151

5252
// Create a cluster role binding mutator
53-
mutator = resources.NewClusterRoleBindingMutator("test-clusterrolebinding", subjects, roleRef, labels, annotations)
53+
mutator = resources.NewClusterRoleBindingMutator("test-clusterrolebinding", subjects, roleRef)
54+
mutator.MetadataMutator().WithLabels(labels).WithAnnotations(annotations)
5455
})
5556

5657
It("should create an empty cluster role binding with correct metadata", func() {

pkg/resources/configmap.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ import (
55

66
core "k8s.io/api/core/v1"
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8-
"sigs.k8s.io/controller-runtime/pkg/client"
98
)
109

1110
type ConfigMapMutator struct {
1211
Name string
1312
Namespace string
1413
Data map[string]string
15-
meta Mutator[client.Object]
14+
meta MetadataMutator
1615
}
1716

1817
var _ Mutator[*core.ConfigMap] = &ConfigMapMutator{}
1918

20-
func NewConfigMapMutator(name, namespace string, data map[string]string, labels map[string]string, annotations map[string]string) Mutator[*core.ConfigMap] {
19+
func NewConfigMapMutator(name, namespace string, data map[string]string) Mutator[*core.ConfigMap] {
2120
return &ConfigMapMutator{
2221
Name: name,
2322
Namespace: namespace,
2423
Data: data,
25-
meta: NewMetadataMutator(labels, annotations),
24+
meta: NewMetadataMutator(),
2625
}
2726
}
2827

@@ -53,3 +52,7 @@ func (m *ConfigMapMutator) Mutate(cm *core.ConfigMap) error {
5352
}
5453
return m.meta.Mutate(cm)
5554
}
55+
56+
func (m *ConfigMapMutator) MetadataMutator() MetadataMutator {
57+
return m.meta
58+
}

pkg/resources/configmap_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var _ = Describe("ConfigMapMutator", func() {
4242
annotations = map[string]string{"annotation1": "value1"}
4343

4444
// Create a ConfigMap mutator
45-
mutator = resources.NewConfigMapMutator("test-configmap", "test-namespace", data, labels, annotations)
45+
mutator = resources.NewConfigMapMutator("test-configmap", "test-namespace", data)
46+
mutator.MetadataMutator().WithLabels(labels).WithAnnotations(annotations)
4647
})
4748

4849
It("should create an empty ConfigMap with correct metadata", func() {

pkg/resources/crd.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ package resources
33
import (
44
"fmt"
55

6-
"sigs.k8s.io/controller-runtime/pkg/client"
7-
86
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
97
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
108
)
119

1210
type crdMutator struct {
1311
crd *apiextv1.CustomResourceDefinition
14-
meta Mutator[client.Object]
12+
meta MetadataMutator
1513
}
1614

1715
var _ Mutator[*apiextv1.CustomResourceDefinition] = &crdMutator{}
1816

19-
func NewCRDMutator(crd *apiextv1.CustomResourceDefinition, labels map[string]string, annotations map[string]string) Mutator[*apiextv1.CustomResourceDefinition] {
20-
return &crdMutator{crd: crd, meta: NewMetadataMutator(labels, annotations)}
17+
func NewCRDMutator(crd *apiextv1.CustomResourceDefinition) Mutator[*apiextv1.CustomResourceDefinition] {
18+
return &crdMutator{crd: crd, meta: NewMetadataMutator()}
2119
}
2220

2321
func (m *crdMutator) String() string {
@@ -40,3 +38,7 @@ func (m *crdMutator) Mutate(r *apiextv1.CustomResourceDefinition) error {
4038
m.crd.Spec.DeepCopyInto(&r.Spec)
4139
return m.meta.Mutate(r)
4240
}
41+
42+
func (m *crdMutator) MetadataMutator() MetadataMutator {
43+
return m.meta
44+
}

pkg/resources/crd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ var _ = Describe("CRDMutator", func() {
6161
}
6262

6363
// Create a CRD mutator
64-
mutator = resources.NewCRDMutator(crd, labels, annotations)
64+
mutator = resources.NewCRDMutator(crd)
65+
mutator.MetadataMutator().WithLabels(labels).WithAnnotations(annotations)
6566
})
6667

6768
It("should create an empty CRD with correct metadata", func() {

0 commit comments

Comments
 (0)