This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
/
update.go
85 lines (69 loc) · 2.77 KB
/
update.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
package clusterrolebinding
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/resource/crud"
apiv1 "k8s.io/api/rbac/v1beta1"
)
func (r *Resource) ApplyUpdateChange(ctx context.Context, obj, updateChange interface{}) error {
clusterRoleBindingsToUpdate, err := toClusterRoleBindings(updateChange)
if err != nil {
return microerror.Mask(err)
}
if len(clusterRoleBindingsToUpdate) != 0 {
r.logger.LogCtx(ctx, "level", "debug", "message", "updating the cluster role bindings in the Kubernetes API")
// Create the cluster role bindings in the Kubernetes API.
for _, clusterRoleBinding := range clusterRoleBindingsToUpdate {
_, err := r.k8sClient.RbacV1beta1().ClusterRoleBindings().Update(clusterRoleBinding)
if err != nil {
return microerror.Mask(err)
}
}
r.logger.LogCtx(ctx, "level", "debug", "message", "updated the cluster role bindings in the Kubernetes API")
} else {
r.logger.LogCtx(ctx, "level", "debug", "message", "the cluster role bindings do not need to be updated in the Kubernetes API")
}
return nil
}
func (r *Resource) NewUpdatePatch(ctx context.Context, obj, currentState, desiredState interface{}) (*crud.Patch, error) {
create, err := r.newCreateChange(ctx, obj, currentState, desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
update, err := r.newUpdateChange(ctx, obj, currentState, desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
patch := crud.NewPatch()
patch.SetCreateChange(create)
patch.SetUpdateChange(update)
return patch, nil
}
func (r *Resource) newUpdateChange(ctx context.Context, obj, currentState, desiredState interface{}) (interface{}, error) {
currentClusterRoleBindings, err := toClusterRoleBindings(currentState)
if err != nil {
return nil, microerror.Mask(err)
}
desiredClusterRoleBindings, err := toClusterRoleBindings(desiredState)
if err != nil {
return nil, microerror.Mask(err)
}
var clusterRoleBindingsToUpdate []*apiv1.ClusterRoleBinding
{
r.logger.LogCtx(ctx, "level", "debug", "message", "finding out which cluster role bindings have to be updated")
for _, clusterRoleBinding := range currentClusterRoleBindings {
desiredClusterRoleBinding, err := getClusterRoleBindingByName(desiredClusterRoleBindings, clusterRoleBinding.Name)
if IsNotFound(err) {
continue
} else if err != nil {
return nil, microerror.Mask(err)
}
if isClusterRoleBindingModified(desiredClusterRoleBinding, clusterRoleBinding) {
clusterRoleBindingsToUpdate = append(clusterRoleBindingsToUpdate, desiredClusterRoleBinding)
}
}
r.logger.LogCtx(ctx, "level", "debug", "message", fmt.Sprintf("found %d cluster role bindings that have to be updated", len(clusterRoleBindingsToUpdate)))
}
return clusterRoleBindingsToUpdate, nil
}