This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
rolebinding.go
174 lines (156 loc) · 6.04 KB
/
rolebinding.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
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package crdupdater
import (
"fmt"
"strings"
horizonapi "github.com/blackducksoftware/horizon/pkg/api"
"github.com/blackducksoftware/horizon/pkg/components"
"github.com/blackducksoftware/synopsys-operator/pkg/util"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
rbacv1 "k8s.io/api/rbac/v1"
)
// RoleBinding stores the configuration to add or delete the role binding
type RoleBinding struct {
config *CommonConfig
deployer *util.DeployerHelper
roleBindings []*components.RoleBinding
oldRoleBindings map[string]rbacv1.RoleBinding
newRoleBindings map[string]*rbacv1.RoleBinding
}
// NewRoleBinding returns the role binding
func NewRoleBinding(config *CommonConfig, roleBindings []*components.RoleBinding) (*RoleBinding, error) {
deployer, err := util.NewDeployer(config.kubeConfig)
if err != nil {
return nil, errors.Annotatef(err, "unable to get deployer object for %s", config.namespace)
}
newRoleBindings := append([]*components.RoleBinding{}, roleBindings...)
for i := 0; i < len(newRoleBindings); i++ {
if !isLabelsExist(config.expectedLabels, newRoleBindings[i].Labels) {
newRoleBindings = append(newRoleBindings[:i], newRoleBindings[i+1:]...)
i--
}
}
return &RoleBinding{
config: config,
deployer: deployer,
roleBindings: newRoleBindings,
oldRoleBindings: make(map[string]rbacv1.RoleBinding, 0),
newRoleBindings: make(map[string]*rbacv1.RoleBinding, 0),
}, nil
}
// buildNewAndOldObject builds the old and new role binding
func (c *RoleBinding) buildNewAndOldObject() error {
// build old role binding
oldRbs, err := c.list()
if err != nil {
return errors.Annotatef(err, "unable to get role bindings for %s", c.config.namespace)
}
for _, oldRb := range oldRbs.(*rbacv1.RoleBindingList).Items {
c.oldRoleBindings[oldRb.GetName()] = oldRb
}
// build new role binding
for _, newRb := range c.roleBindings {
c.newRoleBindings[newRb.GetName()] = newRb.RoleBinding
}
return nil
}
// add adds the role binding
func (c *RoleBinding) add(isPatched bool) (bool, error) {
isAdded := false
var err error
for _, roleBinding := range c.roleBindings {
if _, ok := c.oldRoleBindings[roleBinding.GetName()]; !ok {
c.deployer.Deployer.AddComponent(horizonapi.RoleBindingComponent, roleBinding)
isAdded = true
} else {
_, err = c.patch(roleBinding, isPatched)
if err != nil {
return false, errors.Annotatef(err, "patch role")
}
}
}
if isAdded && !c.config.dryRun {
err := c.deployer.Deployer.Run()
if err != nil {
return false, errors.Annotatef(err, "unable to deploy role binding in %s", c.config.namespace)
}
}
return false, nil
}
// get gets the role binding
func (c *RoleBinding) get(name string) (interface{}, error) {
return util.GetRoleBinding(c.config.kubeClient, c.config.namespace, name)
}
// list lists all the role bindings
func (c *RoleBinding) list() (interface{}, error) {
return util.ListRoleBindings(c.config.kubeClient, c.config.namespace, c.config.labelSelector)
}
// delete deletes the role binding
func (c *RoleBinding) delete(name string) error {
log.Infof("deleting the role binding: %s", name)
return util.DeleteRoleBinding(c.config.kubeClient, c.config.namespace, name)
}
// remove removes the role binding
func (c *RoleBinding) remove() error {
// compare the old and new role binding and delete if needed
for _, oldRoleBinding := range c.oldRoleBindings {
if _, ok := c.newRoleBindings[oldRoleBinding.GetName()]; !ok {
err := c.delete(oldRoleBinding.GetName())
if err != nil {
return errors.Annotatef(err, "unable to delete role binding %s in namespace %s", oldRoleBinding.GetName(), c.config.namespace)
}
}
}
return nil
}
// patch patches the role binding
func (c *RoleBinding) patch(crb interface{}, isPatched bool) (bool, error) {
roleBinding := crb.(*components.RoleBinding)
roleBindingName := roleBinding.GetName()
oldroleBinding := c.oldRoleBindings[roleBindingName]
newRoleBinding := c.newRoleBindings[roleBindingName]
isChanged := false
// check for role ref changes
if (!strings.EqualFold(oldroleBinding.RoleRef.Name, newRoleBinding.RoleRef.Name) || !strings.EqualFold(oldroleBinding.RoleRef.Kind, newRoleBinding.RoleRef.Kind)) && !c.config.dryRun {
isChanged = true
}
// check for subject changes
for _, subject := range newRoleBinding.Subjects {
if !util.IsSubjectExist(oldroleBinding.Subjects, subject.Namespace, subject.Name) {
oldroleBinding.Subjects = append(oldroleBinding.Subjects, rbacv1.Subject{Name: subject.Name, Namespace: subject.Namespace, Kind: "ServiceAccount"})
isChanged = true
}
}
if isChanged {
log.Infof("updating the role binding %s for %s namespace", roleBindingName, c.config.namespace)
getRb, err := c.get(roleBindingName)
if err != nil {
return false, errors.Annotatef(err, "unable to get the role binding %s for namespace %s", roleBindingName, c.config.namespace)
}
oldLatestRoleBinding := getRb.(*rbacv1.RoleBinding)
oldLatestRoleBinding.RoleRef = newRoleBinding.RoleRef
oldLatestRoleBinding.Subjects = oldroleBinding.Subjects
_, err = util.UpdateRoleBinding(c.config.kubeClient, c.config.namespace, oldLatestRoleBinding)
if err != nil {
return false, errors.Annotate(err, fmt.Sprintf("failed to update %s role binding for namespace %s", roleBinding.GetName(), c.config.namespace))
}
}
return false, nil
}