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
/
role.go
159 lines (141 loc) · 4.74 KB
/
role.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
/*
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 (
"reflect"
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"
)
// Role stores the configuration to add or delete the role
type Role struct {
config *CommonConfig
deployer *util.DeployerHelper
roles []*components.Role
oldRoles map[string]rbacv1.Role
newRoles map[string]*rbacv1.Role
}
// NewRole returns the role
func NewRole(config *CommonConfig, roles []*components.Role) (*Role, error) {
deployer, err := util.NewDeployer(config.kubeConfig)
if err != nil {
return nil, errors.Annotatef(err, "unable to get deployer object for %s", config.namespace)
}
newRoles := append([]*components.Role{}, roles...)
for i := 0; i < len(newRoles); i++ {
if !isLabelsExist(config.expectedLabels, newRoles[i].Labels) {
newRoles = append(newRoles[:i], newRoles[i+1:]...)
i--
}
}
return &Role{
config: config,
deployer: deployer,
roles: newRoles,
oldRoles: make(map[string]rbacv1.Role, 0),
newRoles: make(map[string]*rbacv1.Role, 0),
}, nil
}
// buildNewAndOldObject builds the old and new role
func (c *Role) buildNewAndOldObject() error {
// build old role
oldRs, err := c.list()
if err != nil {
return errors.Annotatef(err, "unable to get roles for %s", c.config.namespace)
}
for _, oldR := range oldRs.(*rbacv1.RoleList).Items {
c.oldRoles[oldR.GetName()] = oldR
}
// build new role
for _, newR := range c.roles {
c.newRoles[newR.GetName()] = newR.Role
}
return nil
}
// add adds the role
func (c *Role) add(isPatched bool) (bool, error) {
isAdded := false
for _, role := range c.roles {
if _, ok := c.oldRoles[role.GetName()]; !ok {
c.deployer.Deployer.AddComponent(horizonapi.RoleComponent, role)
isAdded = true
} else {
_, err := c.patch(role, 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 in %s", c.config.namespace)
}
}
return false, nil
}
// get gets the role
func (c *Role) get(name string) (interface{}, error) {
return util.GetRole(c.config.kubeClient, c.config.namespace, name)
}
// list lists all the roles
func (c *Role) list() (interface{}, error) {
return util.ListRoles(c.config.kubeClient, c.config.namespace, c.config.labelSelector)
}
// delete deletes the role
func (c *Role) delete(name string) error {
log.Infof("deleting the role: %s", name)
return util.DeleteRole(c.config.kubeClient, c.config.namespace, name)
}
// remove removes the role
func (c *Role) remove() error {
// compare the old and new role and delete if needed
for _, oldRole := range c.oldRoles {
if _, ok := c.newRoles[oldRole.GetName()]; !ok {
err := c.delete(oldRole.GetName())
if err != nil {
return errors.Annotatef(err, "unable to delete role %s in namespace %s", oldRole.GetName(), c.config.namespace)
}
}
}
return nil
}
// patch patches the role
func (c *Role) patch(cr interface{}, isPatched bool) (bool, error) {
role := cr.(*components.Role)
roleName := role.GetName()
oldrole := c.oldRoles[roleName]
newRole := c.newRoles[roleName]
if !reflect.DeepEqual(sortPolicyRule(oldrole.Rules), sortPolicyRule(newRole.Rules)) && !c.config.dryRun {
log.Infof("updating the role %s for %s namespace", roleName, c.config.namespace)
getR, err := c.get(roleName)
if err != nil {
return false, errors.Annotatef(err, "unable to get the role %s for namespace %s", roleName, c.config.namespace)
}
oldLatestRole := getR.(*rbacv1.Role)
oldLatestRole.Rules = newRole.Rules
_, err = util.UpdateRole(c.config.kubeClient, c.config.namespace, oldLatestRole)
if err != nil {
return false, errors.Annotatef(err, "unable to update the role %s for namespace %s", roleName, c.config.namespace)
}
}
return false, nil
}