@@ -11,6 +11,187 @@ import (
1111 "k8s.io/apimachinery/pkg/runtime/schema"
1212)
1313
14+ func TestGenerateAgentRBACManifestsString (t * testing.T ) {
15+ testCases := []struct {
16+ description string
17+ dataGatherers []agent.DataGatherer
18+ expectedRBACManifests string
19+ }{
20+ {
21+ description : "Generate ClusterRole and ClusterRoleBinding for simple pod dg use case" ,
22+ dataGatherers : []agent.DataGatherer {
23+ {
24+ Name : "k8s/pods" ,
25+ Kind : "k8s-dynamic" ,
26+ Config : & k8s.ConfigDynamic {
27+ GroupVersionResource : schema.GroupVersionResource {
28+ Version : "v1" ,
29+ Resource : "pods" ,
30+ },
31+ },
32+ },
33+ },
34+ expectedRBACManifests : `apiVersion: rbac.authorization.k8s.io/v1
35+ kind: ClusterRole
36+ metadata:
37+ name: jetstack-secure-agent-pods-reader
38+ rules:
39+ - apiGroups:
40+ - ""
41+ resources:
42+ - pods
43+ verbs:
44+ - get
45+ - list
46+ - watch
47+ ---
48+ apiVersion: rbac.authorization.k8s.io/v1
49+ kind: ClusterRoleBinding
50+ metadata:
51+ name: jetstack-secure-agent-pods-reader
52+ roleRef:
53+ apiGroup: rbac.authorization.k8s.io
54+ kind: ClusterRole
55+ name: jetstack-secure-agent-pods-reader
56+ subjects:
57+ - kind: ServiceAccount
58+ name: agent
59+ namespace: jetstack-secure
60+ ---` ,
61+ },
62+ {
63+ description : "Generate ClusterRole and RoleBinding for simple pod dg with include namespace \" foobar\" " ,
64+ dataGatherers : []agent.DataGatherer {
65+ {
66+ Name : "k8s/pods" ,
67+ Kind : "k8s-dynamic" ,
68+ Config : & k8s.ConfigDynamic {
69+ IncludeNamespaces : []string {"foobar" },
70+ GroupVersionResource : schema.GroupVersionResource {
71+ Version : "v1" ,
72+ Resource : "pods" ,
73+ },
74+ },
75+ },
76+ },
77+ expectedRBACManifests : `apiVersion: rbac.authorization.k8s.io/v1
78+ kind: ClusterRole
79+ metadata:
80+ name: jetstack-secure-agent-pods-reader
81+ rules:
82+ - apiGroups:
83+ - ""
84+ resources:
85+ - pods
86+ verbs:
87+ - get
88+ - list
89+ - watch
90+ ---
91+ apiVersion: rbac.authorization.k8s.io/v1
92+ kind: RoleBinding
93+ metadata:
94+ name: jetstack-secure-agent-pods-reader
95+ namespace: foobar
96+ roleRef:
97+ apiGroup: rbac.authorization.k8s.io
98+ kind: ClusterRole
99+ name: jetstack-secure-agent-pods-reader
100+ subjects:
101+ - kind: ServiceAccount
102+ name: agent
103+ namespace: jetstack-secure
104+ ---` ,
105+ },
106+ {
107+ description : "Generate multiple ClusterRoles and ClusterRoleBindings for simple pod and nodes dg use case" ,
108+ dataGatherers : []agent.DataGatherer {
109+ {
110+ Name : "k8s/pods" ,
111+ Kind : "k8s-dynamic" ,
112+ Config : & k8s.ConfigDynamic {
113+ GroupVersionResource : schema.GroupVersionResource {
114+ Version : "v1" ,
115+ Resource : "pods" ,
116+ },
117+ },
118+ },
119+ {
120+ Name : "k8s/nodes" ,
121+ Kind : "k8s-dynamic" ,
122+ Config : & k8s.ConfigDynamic {
123+ GroupVersionResource : schema.GroupVersionResource {
124+ Version : "v1" ,
125+ Resource : "nodes" ,
126+ },
127+ },
128+ },
129+ },
130+ expectedRBACManifests : `apiVersion: rbac.authorization.k8s.io/v1
131+ kind: ClusterRole
132+ metadata:
133+ name: jetstack-secure-agent-pods-reader
134+ rules:
135+ - apiGroups:
136+ - ""
137+ resources:
138+ - pods
139+ verbs:
140+ - get
141+ - list
142+ - watch
143+ ---
144+ apiVersion: rbac.authorization.k8s.io/v1
145+ kind: ClusterRole
146+ metadata:
147+ name: jetstack-secure-agent-nodes-reader
148+ rules:
149+ - apiGroups:
150+ - ""
151+ resources:
152+ - nodes
153+ verbs:
154+ - get
155+ - list
156+ - watch
157+ ---
158+ apiVersion: rbac.authorization.k8s.io/v1
159+ kind: ClusterRoleBinding
160+ metadata:
161+ name: jetstack-secure-agent-pods-reader
162+ roleRef:
163+ apiGroup: rbac.authorization.k8s.io
164+ kind: ClusterRole
165+ name: jetstack-secure-agent-pods-reader
166+ subjects:
167+ - kind: ServiceAccount
168+ name: agent
169+ namespace: jetstack-secure
170+ ---
171+ apiVersion: rbac.authorization.k8s.io/v1
172+ kind: ClusterRoleBinding
173+ metadata:
174+ name: jetstack-secure-agent-nodes-reader
175+ roleRef:
176+ apiGroup: rbac.authorization.k8s.io
177+ kind: ClusterRole
178+ name: jetstack-secure-agent-nodes-reader
179+ subjects:
180+ - kind: ServiceAccount
181+ name: agent
182+ namespace: jetstack-secure
183+ ---` ,
184+ },
185+ }
186+
187+ for _ , input := range testCases {
188+ got := GenerateFullManifest (input .dataGatherers )
189+ if input .expectedRBACManifests != got {
190+ t .Errorf ("value mismatch, \n **********expected:******************************\n %s\n **********got:******************************\n %s" , input .expectedRBACManifests , got )
191+ }
192+ }
193+ }
194+
14195func TestGenerateAgentRBACManifests (t * testing.T ) {
15196 testCases := []struct {
16197 description string
0 commit comments