forked from noobaa/noobaa-operator
/
operator.go
316 lines (293 loc) · 9.96 KB
/
operator.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package operator
import (
"fmt"
"os"
"strings"
"github.com/noobaa/noobaa-operator/v5/pkg/bundle"
"github.com/noobaa/noobaa-operator/v5/pkg/options"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
secv1 "github.com/openshift/api/security/v1"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/cli-runtime/pkg/printers"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Cmd returns a CLI command
func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operator",
Short: "Deployment using operator",
}
cmd.AddCommand(
CmdInstall(),
CmdUninstall(),
CmdStatus(),
CmdYaml(),
CmdRun(),
)
return cmd
}
// CmdInstall returns a CLI command
func CmdInstall() *cobra.Command {
cmd := &cobra.Command{
Use: "install",
Short: "Install noobaa-operator",
Run: RunInstall,
Args: cobra.NoArgs,
}
cmd.Flags().Bool("no-deploy", false, "Install only the needed resources but do not create the operator deployment")
return cmd
}
// CmdUninstall returns a CLI command
func CmdUninstall() *cobra.Command {
cmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall noobaa-operator",
Run: RunUninstall,
Args: cobra.NoArgs,
}
cmd.Flags().Bool("cleanup", false, "Enable deletion of the Namespace")
return cmd
}
// CmdStatus returns a CLI command
func CmdStatus() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Status of a noobaa-operator",
Run: RunStatus,
Args: cobra.NoArgs,
}
return cmd
}
// CmdYaml returns a CLI command
func CmdYaml() *cobra.Command {
cmd := &cobra.Command{
Use: "yaml",
Short: "Show bundled noobaa-operator yaml",
Run: RunYaml,
Args: cobra.NoArgs,
}
return cmd
}
// CmdRun returns a CLI command
func CmdRun() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Runs the noobaa-operator",
Run: RunOperator,
Args: cobra.NoArgs,
}
return cmd
}
// RunInstall runs a CLI command
func RunInstall(cmd *cobra.Command, args []string) {
c := LoadOperatorConf(cmd)
util.KubeCreateSkipExisting(c.NS)
util.KubeCreateSkipExisting(c.SA)
util.KubeCreateSkipExisting(c.SAEndpoint)
util.KubeCreateSkipExisting(c.Role)
util.KubeCreateSkipExisting(c.RoleEndpoint)
util.KubeCreateSkipExisting(c.RoleBinding)
util.KubeCreateSkipExisting(c.RoleBindingEndpoint)
util.KubeCreateSkipExisting(c.ClusterRole)
util.KubeCreateSkipExisting(c.ClusterRoleBinding)
util.KubeCreateOptional(c.SecurityContextConstraints)
util.KubeCreateOptional(c.SCCEndpoint)
noDeploy, _ := cmd.Flags().GetBool("no-deploy")
if !noDeploy {
util.KubeCreateSkipExisting(c.Deployment)
}
}
// RunUninstall runs a CLI command
func RunUninstall(cmd *cobra.Command, args []string) {
c := LoadOperatorConf(cmd)
noDeploy, _ := cmd.Flags().GetBool("no-deploy")
if !noDeploy {
util.KubeDelete(c.Deployment)
}
cleanup, _ := cmd.Flags().GetBool("cleanup")
if !cleanup {
log.Printf("SCC Delete: currently disabled (enable with \"--cleanup\")")
log.Printf("SCC Status:")
util.KubeCheck(c.NS)
} else {
util.KubeDelete(c.SCCEndpoint)
util.KubeDelete(c.SecurityContextConstraints)
}
util.KubeDelete(c.ClusterRoleBinding)
util.KubeDelete(c.ClusterRole)
util.KubeDelete(c.RoleBindingEndpoint)
util.KubeDelete(c.RoleBinding)
util.KubeDelete(c.RoleEndpoint)
util.KubeDelete(c.Role)
util.KubeDelete(c.SAEndpoint)
util.KubeDelete(c.SA)
reservedNS := c.NS.Name == "default" ||
strings.HasPrefix(c.NS.Name, "openshift-") ||
strings.HasPrefix(c.NS.Name, "kubernetes-") ||
strings.HasPrefix(c.NS.Name, "kube-")
if reservedNS {
log.Printf("Namespace Delete: disabled for reserved namespace")
log.Printf("Namespace Status:")
util.KubeCheck(c.NS)
} else if !cleanup {
log.Printf("Namespace Delete: currently disabled (enable with \"--cleanup\")")
log.Printf("Namespace Status:")
util.KubeCheck(c.NS)
} else {
log.Printf("Namespace Delete:")
util.KubeDelete(c.NS)
}
}
// RunStatus runs a CLI command
func RunStatus(cmd *cobra.Command, args []string) {
c := LoadOperatorConf(cmd)
util.KubeCheck(c.NS)
if util.KubeCheck(c.SA) && util.KubeCheck(c.SAEndpoint) {
// in OLM deployment the roles and bindings have generated names
// so we list and lookup bindings to our service account to discover the actual names
DetectRole(c)
DetectClusterRole(c)
}
util.KubeCheck(c.Role)
util.KubeCheck(c.RoleEndpoint)
util.KubeCheck(c.RoleBinding)
util.KubeCheck(c.RoleBindingEndpoint)
util.KubeCheck(c.ClusterRole)
util.KubeCheck(c.ClusterRoleBinding)
noDeploy, _ := cmd.Flags().GetBool("no-deploy")
if !noDeploy {
util.KubeCheck(c.Deployment)
}
}
// RunYaml runs a CLI command
func RunYaml(cmd *cobra.Command, args []string) {
c := LoadOperatorConf(cmd)
p := printers.YAMLPrinter{}
util.Panic(p.PrintObj(c.NS, os.Stdout))
util.Panic(p.PrintObj(c.SA, os.Stdout))
util.Panic(p.PrintObj(c.SAEndpoint, os.Stdout))
util.Panic(p.PrintObj(c.Role, os.Stdout))
util.Panic(p.PrintObj(c.RoleEndpoint, os.Stdout))
util.Panic(p.PrintObj(c.RoleBinding, os.Stdout))
util.Panic(p.PrintObj(c.RoleBindingEndpoint, os.Stdout))
util.Panic(p.PrintObj(c.ClusterRole, os.Stdout))
util.Panic(p.PrintObj(c.ClusterRoleBinding, os.Stdout))
noDeploy, _ := cmd.Flags().GetBool("no-deploy")
if !noDeploy {
util.Panic(p.PrintObj(c.Deployment, os.Stdout))
}
}
// Conf struct holds all the objects needed to install the operator
type Conf struct {
NS *corev1.Namespace
SA *corev1.ServiceAccount
SAEndpoint *corev1.ServiceAccount
Role *rbacv1.Role
RoleEndpoint *rbacv1.Role
RoleBinding *rbacv1.RoleBinding
RoleBindingEndpoint *rbacv1.RoleBinding
ClusterRole *rbacv1.ClusterRole
ClusterRoleBinding *rbacv1.ClusterRoleBinding
SecurityContextConstraints *secv1.SecurityContextConstraints
SCCEndpoint *secv1.SecurityContextConstraints
Deployment *appsv1.Deployment
IsForODF bool
}
// LoadOperatorConf loads and initializes all the objects needed to install the operator
func LoadOperatorConf(cmd *cobra.Command) *Conf {
c := &Conf{}
c.NS = util.KubeObject(bundle.File_deploy_namespace_yaml).(*corev1.Namespace)
c.SA = util.KubeObject(bundle.File_deploy_service_account_yaml).(*corev1.ServiceAccount)
c.SAEndpoint = util.KubeObject(bundle.File_deploy_service_account_endpoint_yaml).(*corev1.ServiceAccount)
c.Role = util.KubeObject(bundle.File_deploy_role_yaml).(*rbacv1.Role)
c.RoleEndpoint = util.KubeObject(bundle.File_deploy_role_endpoint_yaml).(*rbacv1.Role)
c.RoleBinding = util.KubeObject(bundle.File_deploy_role_binding_yaml).(*rbacv1.RoleBinding)
c.RoleBindingEndpoint = util.KubeObject(bundle.File_deploy_role_binding_endpoint_yaml).(*rbacv1.RoleBinding)
c.ClusterRole = util.KubeObject(bundle.File_deploy_cluster_role_yaml).(*rbacv1.ClusterRole)
c.ClusterRoleBinding = util.KubeObject(bundle.File_deploy_cluster_role_binding_yaml).(*rbacv1.ClusterRoleBinding)
c.SecurityContextConstraints = util.KubeObject(bundle.File_deploy_scc_yaml).(*secv1.SecurityContextConstraints)
c.SCCEndpoint = util.KubeObject(bundle.File_deploy_scc_endpoint_yaml).(*secv1.SecurityContextConstraints)
c.Deployment = util.KubeObject(bundle.File_deploy_operator_yaml).(*appsv1.Deployment)
c.IsForODF = false
c.NS.Name = options.Namespace
c.SA.Namespace = options.Namespace
c.SAEndpoint.Namespace = options.Namespace
c.Role.Namespace = options.Namespace
c.RoleEndpoint.Namespace = options.Namespace
c.RoleBinding.Namespace = options.Namespace
c.RoleBindingEndpoint.Namespace = options.Namespace
c.ClusterRole.Namespace = options.Namespace
c.Deployment.Namespace = options.Namespace
c.ClusterRole.Name = options.SubDomainNS()
c.ClusterRoleBinding.Name = c.ClusterRole.Name
c.ClusterRoleBinding.RoleRef.Name = c.ClusterRole.Name
for i := range c.ClusterRoleBinding.Subjects {
c.ClusterRoleBinding.Subjects[i].Namespace = options.Namespace
}
c.Deployment.Spec.Template.Spec.Containers[0].Image = options.OperatorImage
if options.ImagePullSecret != "" {
c.Deployment.Spec.Template.Spec.ImagePullSecrets =
[]corev1.LocalObjectReference{{Name: options.ImagePullSecret}}
}
c.SecurityContextConstraints.Users[0] = fmt.Sprintf("system:serviceaccount:%s:%s", options.Namespace, c.SA.Name)
c.SCCEndpoint.Users[0] = fmt.Sprintf("system:serviceaccount:%s:%s", options.Namespace, c.SAEndpoint.Name)
return c
}
// DetectRole looks up a role binding referencing our service account
func DetectRole(c *Conf) {
roleBindings := &rbacv1.RoleBindingList{}
selector := labels.SelectorFromSet(labels.Set{
"olm.owner.kind": "ClusterServiceVersion",
"olm.owner.namespace": c.SA.Namespace,
})
util.KubeList(roleBindings, &client.ListOptions{
Namespace: c.SA.Namespace,
LabelSelector: selector,
})
for i := range roleBindings.Items {
b := &roleBindings.Items[i]
for j := range b.Subjects {
s := b.Subjects[j]
if s.Kind == "ServiceAccount" &&
s.Name == c.SA.Name &&
s.Namespace == c.SA.Namespace {
c.Role.Name = b.RoleRef.Name
c.RoleBinding.Name = b.Name
}
if s.Kind == "ServiceAccount" &&
s.Name == c.SAEndpoint.Name &&
s.Namespace == c.SAEndpoint.Namespace {
c.RoleEndpoint.Name = b.RoleRef.Name
c.RoleBindingEndpoint.Name = b.Name
}
}
}
}
// DetectClusterRole looks up a cluster role binding referencing our service account
func DetectClusterRole(c *Conf) {
clusterRoleBindings := &rbacv1.ClusterRoleBindingList{}
selector := labels.SelectorFromSet(labels.Set{
"olm.owner.kind": "ClusterServiceVersion",
"olm.owner.namespace": c.SA.Namespace,
})
util.KubeList(clusterRoleBindings, &client.ListOptions{
LabelSelector: selector,
})
for i := range clusterRoleBindings.Items {
b := &clusterRoleBindings.Items[i]
for j := range b.Subjects {
s := b.Subjects[j]
if s.Kind == "ServiceAccount" &&
s.Name == c.SA.Name &&
s.Namespace == c.SA.Namespace {
c.ClusterRole.Name = b.RoleRef.Name
c.ClusterRoleBinding.Name = b.Name
return
}
}
}
}