Skip to content

Commit 1b9dfe7

Browse files
committed
format
1 parent 52843e3 commit 1b9dfe7

22 files changed

+1178
-51
lines changed

pkg/resources/clusterrole.go

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

6+
"sigs.k8s.io/controller-runtime/pkg/client"
7+
68
v1 "k8s.io/api/rbac/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
)
911

1012
type ClusterRoleMutator struct {
11-
Name string
12-
Labels map[string]string
13-
Rules []v1.PolicyRule
13+
Name string
14+
Rules []v1.PolicyRule
15+
meta Mutator[client.Object]
1416
}
1517

1618
var _ Mutator[*v1.ClusterRole] = &ClusterRoleMutator{}
1719

20+
func NewClusterRoleMutator(name string, rules []v1.PolicyRule, labels map[string]string, annotations map[string]string) Mutator[*v1.ClusterRole] {
21+
return &ClusterRoleMutator{
22+
Name: name,
23+
Rules: rules,
24+
meta: NewMetadataMutator(labels, annotations),
25+
}
26+
}
27+
1828
func (m *ClusterRoleMutator) String() string {
1929
return fmt.Sprintf("clusterrole %s", m.Name)
2030
}
@@ -32,7 +42,6 @@ func (m *ClusterRoleMutator) Empty() *v1.ClusterRole {
3242
}
3343

3444
func (m *ClusterRoleMutator) Mutate(r *v1.ClusterRole) error {
35-
r.ObjectMeta.Labels = m.Labels
3645
r.Rules = m.Rules
37-
return nil
46+
return m.meta.Mutate(r)
3847
}

pkg/resources/clusterrole_test.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,84 @@
1-
package resources
1+
package resources_test
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
v1 "k8s.io/api/rbac/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/openmcp-project/controller-utils/pkg/resources"
13+
"github.com/openmcp-project/controller-utils/pkg/testing"
14+
)
15+
16+
var _ = Describe("ClusterRoleMutator", func() {
17+
var (
18+
ctx context.Context
19+
fakeClient client.WithWatch
20+
scheme *runtime.Scheme
21+
rules []v1.PolicyRule
22+
labels map[string]string
23+
annotations map[string]string
24+
mutator resources.Mutator[*v1.ClusterRole]
25+
)
26+
27+
BeforeEach(func() {
28+
ctx = context.TODO()
29+
30+
// Create a scheme and register the rbac/v1 API
31+
scheme = runtime.NewScheme()
32+
Expect(v1.AddToScheme(scheme)).To(Succeed())
33+
34+
// Initialize the fake client
35+
var err error
36+
fakeClient, err = testing.GetFakeClient(scheme)
37+
Expect(err).ToNot(HaveOccurred())
38+
39+
// Define rules, labels, and annotations
40+
rules = []v1.PolicyRule{
41+
{
42+
APIGroups: []string{""},
43+
Resources: []string{"pods"},
44+
Verbs: []string{"get", "list"},
45+
},
46+
}
47+
labels = map[string]string{"key1": "value1"}
48+
annotations = map[string]string{"annotation1": "value1"}
49+
50+
// Create a cluster role mutator
51+
mutator = resources.NewClusterRoleMutator("test-clusterrole", rules, labels, annotations)
52+
})
53+
54+
It("should create an empty cluster role with correct metadata", func() {
55+
clusterRole := mutator.Empty()
56+
57+
Expect(clusterRole.Name).To(Equal("test-clusterrole"))
58+
Expect(clusterRole.APIVersion).To(Equal("rbac.authorization.k8s.io/v1"))
59+
Expect(clusterRole.Kind).To(Equal("ClusterRole"))
60+
})
61+
62+
It("should apply rules using Mutate", func() {
63+
clusterRole := mutator.Empty()
64+
65+
// Apply the mutator's Mutate method
66+
Expect(mutator.Mutate(clusterRole)).To(Succeed())
67+
68+
// Verify that the rules are applied
69+
Expect(clusterRole.Rules).To(Equal(rules))
70+
})
71+
72+
It("should create and retrieve the cluster role using the fake client", func() {
73+
clusterRole := mutator.Empty()
74+
Expect(mutator.Mutate(clusterRole)).To(Succeed())
75+
76+
// Create the cluster role in the fake client
77+
Expect(fakeClient.Create(ctx, clusterRole)).To(Succeed())
78+
79+
// Retrieve the cluster role from the fake client and verify it
80+
retrievedClusterRole := &v1.ClusterRole{}
81+
Expect(fakeClient.Get(ctx, client.ObjectKey{Name: "test-clusterrole"}, retrievedClusterRole)).To(Succeed())
82+
Expect(retrievedClusterRole).To(Equal(clusterRole))
83+
})
84+
})

pkg/resources/clusterrolebinding.go

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

6+
"sigs.k8s.io/controller-runtime/pkg/client"
7+
68
v1 "k8s.io/api/rbac/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
)
911

1012
type ClusterRoleBindingMutator struct {
11-
ClusterRoleBindingName string
12-
ClusterRoleName string
13-
ServiceAccountName string
14-
ServiceAccountNamespace string
15-
Labels map[string]string
13+
ClusterRoleBindingName string
14+
RoleRef v1.RoleRef
15+
Subjects []v1.Subject
16+
meta Mutator[client.Object]
1617
}
1718

1819
var _ Mutator[*v1.ClusterRoleBinding] = &ClusterRoleBindingMutator{}
1920

21+
func NewClusterRoleBindingMutator(clusterRoleBindingName string, subjects []v1.Subject, roleRef v1.RoleRef, labels map[string]string, annotations map[string]string) Mutator[*v1.ClusterRoleBinding] {
22+
return &ClusterRoleBindingMutator{
23+
ClusterRoleBindingName: clusterRoleBindingName,
24+
RoleRef: roleRef,
25+
Subjects: subjects,
26+
meta: NewMetadataMutator(labels, annotations),
27+
}
28+
}
29+
2030
func (m *ClusterRoleBindingMutator) String() string {
2131
return fmt.Sprintf("clusterrolebinding %s", m.ClusterRoleBindingName)
2232
}
@@ -34,18 +44,7 @@ func (m *ClusterRoleBindingMutator) Empty() *v1.ClusterRoleBinding {
3444
}
3545

3646
func (m *ClusterRoleBindingMutator) Mutate(r *v1.ClusterRoleBinding) error {
37-
r.ObjectMeta.Labels = m.Labels
38-
r.RoleRef = v1.RoleRef{
39-
APIGroup: "rbac.authorization.k8s.io",
40-
Kind: "ClusterRole",
41-
Name: m.ClusterRoleName,
42-
}
43-
r.Subjects = []v1.Subject{
44-
{
45-
Kind: "ServiceAccount",
46-
Name: m.ServiceAccountName,
47-
Namespace: m.ServiceAccountNamespace,
48-
},
49-
}
50-
return nil
47+
r.RoleRef = m.RoleRef
48+
r.Subjects = m.Subjects
49+
return m.meta.Mutate(r)
5150
}
Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
package resources
1+
package resources_test
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
v1 "k8s.io/api/rbac/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/openmcp-project/controller-utils/pkg/resources"
13+
"github.com/openmcp-project/controller-utils/pkg/testing"
14+
)
15+
16+
var _ = Describe("ClusterRoleBindingMutator", func() {
17+
var (
18+
ctx context.Context
19+
fakeClient client.WithWatch
20+
scheme *runtime.Scheme
21+
subjects []v1.Subject
22+
roleRef v1.RoleRef
23+
labels map[string]string
24+
annotations map[string]string
25+
mutator resources.Mutator[*v1.ClusterRoleBinding]
26+
)
27+
28+
BeforeEach(func() {
29+
ctx = context.TODO()
30+
31+
// Create a scheme and register the rbac/v1 API
32+
scheme = runtime.NewScheme()
33+
Expect(v1.AddToScheme(scheme)).To(Succeed())
34+
35+
// Initialize the fake client
36+
var err error
37+
fakeClient, err = testing.GetFakeClient(scheme)
38+
Expect(err).ToNot(HaveOccurred())
39+
40+
// Define subjects, roleRef, labels, and annotations
41+
subjects = []v1.Subject{
42+
{
43+
Kind: "User",
44+
Name: "test-user",
45+
Namespace: "test-namespace",
46+
},
47+
}
48+
roleRef = resources.NewClusterRoleRef("test-role")
49+
labels = map[string]string{"key1": "value1"}
50+
annotations = map[string]string{"annotation1": "value1"}
51+
52+
// Create a cluster role binding mutator
53+
mutator = resources.NewClusterRoleBindingMutator("test-clusterrolebinding", subjects, roleRef, labels, annotations)
54+
})
55+
56+
It("should create an empty cluster role binding with correct metadata", func() {
57+
clusterRoleBinding := mutator.Empty()
58+
59+
Expect(clusterRoleBinding.Name).To(Equal("test-clusterrolebinding"))
60+
Expect(clusterRoleBinding.APIVersion).To(Equal("rbac.authorization.k8s.io/v1"))
61+
Expect(clusterRoleBinding.Kind).To(Equal("ClusterRoleBinding"))
62+
})
63+
64+
It("should apply subjects and roleRef using Mutate", func() {
65+
clusterRoleBinding := mutator.Empty()
66+
67+
// Apply the mutator's Mutate method
68+
Expect(mutator.Mutate(clusterRoleBinding)).To(Succeed())
69+
70+
// Verify that the subjects and roleRef are applied
71+
Expect(clusterRoleBinding.Subjects).To(Equal(subjects))
72+
Expect(clusterRoleBinding.RoleRef).To(Equal(roleRef))
73+
})
74+
75+
It("should create and retrieve the cluster role binding using the fake client", func() {
76+
clusterRoleBinding := mutator.Empty()
77+
Expect(mutator.Mutate(clusterRoleBinding)).To(Succeed())
78+
79+
// Create the cluster role binding in the fake client
80+
Expect(fakeClient.Create(ctx, clusterRoleBinding)).To(Succeed())
81+
82+
// Retrieve the cluster role binding from the fake client and verify it
83+
retrievedClusterRoleBinding := &v1.ClusterRoleBinding{}
84+
Expect(fakeClient.Get(ctx, client.ObjectKey{Name: "test-clusterrolebinding"}, retrievedClusterRoleBinding)).To(Succeed())
85+
Expect(retrievedClusterRoleBinding).To(Equal(clusterRoleBinding))
86+
})
87+
})

pkg/resources/configmap.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
package resources
2+
3+
import (
4+
"fmt"
5+
6+
core "k8s.io/api/core/v1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
)
10+
11+
type ConfigMapMutator struct {
12+
Name string
13+
Namespace string
14+
Data map[string]string
15+
meta Mutator[client.Object]
16+
}
17+
18+
var _ Mutator[*core.ConfigMap] = &ConfigMapMutator{}
19+
20+
func NewConfigMapMutator(name, namespace string, data map[string]string, labels map[string]string, annotations map[string]string) Mutator[*core.ConfigMap] {
21+
return &ConfigMapMutator{
22+
Name: name,
23+
Namespace: namespace,
24+
Data: data,
25+
meta: NewMetadataMutator(labels, annotations),
26+
}
27+
}
28+
29+
func (m *ConfigMapMutator) String() string {
30+
return fmt.Sprintf("configmap %s/%s", m.Namespace, m.Name)
31+
}
32+
33+
func (m *ConfigMapMutator) Empty() *core.ConfigMap {
34+
return &core.ConfigMap{
35+
TypeMeta: metav1.TypeMeta{
36+
APIVersion: "v1",
37+
Kind: "ConfigMap",
38+
},
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: m.Name,
41+
Namespace: m.Namespace,
42+
},
43+
Data: m.Data,
44+
}
45+
}
46+
47+
func (m *ConfigMapMutator) Mutate(cm *core.ConfigMap) error {
48+
if cm.Data == nil {
49+
cm.Data = make(map[string]string)
50+
}
51+
for key, value := range m.Data {
52+
cm.Data[key] = value
53+
}
54+
return m.meta.Mutate(cm)
55+
}

0 commit comments

Comments
 (0)