forked from openshift-kni/eco-goinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterrolebinding.go
271 lines (202 loc) · 7.52 KB
/
clusterrolebinding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package rbac
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
"golang.org/x/exp/slices"
rbacv1 "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ClusterRoleBindingBuilder provides struct for clusterrolebinding object
// containing connection to the cluster and the clusterrolebinding definitions.
type ClusterRoleBindingBuilder struct {
// Clusterrolebinding definition. Used to create a clusterrolebinding object.
Definition *rbacv1.ClusterRoleBinding
// Created clusterrolebinding object
Object *rbacv1.ClusterRoleBinding
// Used in functions that define or mutate clusterrolebinding definition.
// errorMsg is processed before the clusterrolebinding object is created.
errorMsg string
apiClient *clients.Settings
}
// ClusterRoleBindingAdditionalOptions additional options for ClusterRoleBinding object.
type ClusterRoleBindingAdditionalOptions func(builder *ClusterRoleBindingBuilder) (*ClusterRoleBindingBuilder, error)
// NewClusterRoleBindingBuilder creates a new instance of ClusterRoleBindingBuilder.
func NewClusterRoleBindingBuilder(
apiClient *clients.Settings, name, clusterRole string, subject rbacv1.Subject) *ClusterRoleBindingBuilder {
glog.V(100).Infof(
"Initializing new clusterrolebinding structure with the following params: "+
"name: %s, clusterrole: %s, subject %v",
name, clusterRole, subject)
builder := &ClusterRoleBindingBuilder{
apiClient: apiClient,
Definition: &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Name: clusterRole,
Kind: "ClusterRole",
},
},
}
builder.WithSubjects([]rbacv1.Subject{subject})
if name == "" {
glog.V(100).Infof("The name of the clusterrolebinding is empty")
builder.errorMsg = "clusterrolebinding 'name' cannot be empty"
return builder
}
return builder
}
// WithSubjects appends additional subjects to clusterrolebinding definition.
func (builder *ClusterRoleBindingBuilder) WithSubjects(subjects []rbacv1.Subject) *ClusterRoleBindingBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof("Appending to the definition of clusterrolebinding %s these additional subjects %v",
builder.Definition.Name, subjects)
if len(subjects) == 0 {
glog.V(100).Infof("The list of subjects is empty")
builder.errorMsg = "cannot accept nil or empty slice as subjects"
return builder
}
for _, subject := range subjects {
if !slices.Contains(allowedSubjectKinds(), subject.Kind) {
glog.V(100).Infof("The clusterrolebinding subject kind must be one of 'ServiceAccount', 'User', or 'Group'")
builder.errorMsg = "clusterrolebinding subject kind must be one of 'ServiceAccount', 'User', or 'Group'"
return builder
}
if subject.Name == "" {
glog.V(100).Infof("The clusterrolebinding subject name cannot be empty")
builder.errorMsg = "clusterrolebinding subject name cannot be empty"
return builder
}
}
builder.Definition.Subjects = append(builder.Definition.Subjects, subjects...)
return builder
}
// WithOptions creates ClusterRoleBinding with generic mutation options.
func (builder *ClusterRoleBindingBuilder) WithOptions(
options ...ClusterRoleBindingAdditionalOptions) *ClusterRoleBindingBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof("Setting ClusterRoleBinding additional options")
for _, option := range options {
if option != nil {
builder, err := option(builder)
if err != nil {
glog.V(100).Infof("Error occurred in mutation function")
builder.errorMsg = err.Error()
return builder
}
}
}
return builder
}
// PullClusterRoleBinding pulls existing clusterrolebinding from cluster.
func PullClusterRoleBinding(apiClient *clients.Settings, name string) (*ClusterRoleBindingBuilder, error) {
glog.V(100).Infof("Pulling existing clusterrolebinding name %s from cluster", name)
builder := ClusterRoleBindingBuilder{
apiClient: apiClient,
Definition: &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
},
}
if name == "" {
glog.V(100).Infof("The name of the clusterrolebinding is empty")
return nil, fmt.Errorf("clusterrolebinding 'name' cannot be empty")
}
if !builder.Exists() {
return nil, fmt.Errorf("clusterrolebinding object %s does not exist", name)
}
builder.Definition = builder.Object
return &builder, nil
}
// Create generates a clusterrolebinding in the cluster and stores the created object in struct.
func (builder *ClusterRoleBindingBuilder) Create() (*ClusterRoleBindingBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Creating clusterrolebinding %s",
builder.Definition.Name)
var err error
if !builder.Exists() {
builder.Object, err = builder.apiClient.ClusterRoleBindings().Create(
context.TODO(), builder.Definition, metav1.CreateOptions{})
}
return builder, err
}
// Delete removes a clusterrolebinding from the cluster.
func (builder *ClusterRoleBindingBuilder) Delete() error {
if valid, err := builder.validate(); !valid {
return err
}
glog.V(100).Infof("Removing clusterrolebinding %s",
builder.Definition.Name)
if !builder.Exists() {
glog.V(100).Infof("ClusterRoleBinding object %s does not exist",
builder.Definition.Name)
builder.Object = nil
return nil
}
err := builder.apiClient.ClusterRoleBindings().Delete(
context.TODO(), builder.Definition.Name, metav1.DeleteOptions{})
if err != nil {
return err
}
builder.Object = nil
return nil
}
// Update modifies a clusterrolebinding object in the cluster.
func (builder *ClusterRoleBindingBuilder) Update() (*ClusterRoleBindingBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Updating clusterrolebinding %s",
builder.Definition.Name)
var err error
builder.Object, err = builder.apiClient.ClusterRoleBindings().Update(
context.TODO(), builder.Definition, metav1.UpdateOptions{})
return builder, err
}
// Exists checks if clusterrolebinding exists in the cluster.
func (builder *ClusterRoleBindingBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}
glog.V(100).Infof("Checking if clusterrolebinding %s exists",
builder.Definition.Name)
var err error
builder.Object, err = builder.apiClient.ClusterRoleBindings().Get(
context.TODO(), builder.Definition.Name, metav1.GetOptions{})
return err == nil || !k8serrors.IsNotFound(err)
}
// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *ClusterRoleBindingBuilder) validate() (bool, error) {
resourceCRD := "ClusterRoleBinding"
if builder == nil {
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD)
return false, fmt.Errorf("error: received nil %s builder", resourceCRD)
}
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD))
}
if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD)
}
if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg)
return false, fmt.Errorf(builder.errorMsg)
}
return true, nil
}