forked from goharbor/harbor-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation.go
157 lines (121 loc) · 4.27 KB
/
mutation.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
package controller
import (
"context"
"fmt"
"strings"
goharborv1 "github.com/plotly/harbor-operator/apis/goharbor.io/v1beta1"
"github.com/plotly/harbor-operator/pkg/controller/mutation"
"github.com/plotly/harbor-operator/pkg/factories/logger"
"github.com/plotly/harbor-operator/pkg/factories/owner"
"github.com/plotly/harbor-operator/pkg/graph"
"github.com/plotly/harbor-operator/pkg/resources"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
var ErrorOwnerNotFound = errors.New("owner not found")
const (
ImmutableAnnotation = "goharbor.io/immutable"
WarningAnnotation = "goharbor.io/warning"
WarningValueTmpl = "⚠️ This Resource is managed by *%s* ⚠️"
)
const (
OperatorNameLabel = "goharbor.io/operator-controller"
OperatorVersionLabel = "goharbor.io/operator-version"
)
func (c *Controller) GlobalMutateFn(ctx context.Context) (resources.Mutable, error) {
var mutate resources.Mutable = mutation.NoOp
className, err := c.GetClassName(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot get class name")
}
if className != "" {
mutate.AppendMutation(mutation.GetAnnotationsMutation(goharborv1.HarborClassAnnotation, className))
}
mutate.AppendMutation(mutation.GetAnnotationsMutation(WarningAnnotation, fmt.Sprintf(WarningValueTmpl, c.GetName())))
mutate.AppendMutation(mutation.GetLabelsMutation(OperatorNameLabel, c.GetName(), OperatorVersionLabel, c.GetVersion()))
o := owner.Get(ctx)
if o == nil {
return nil, ErrorOwnerNotFound
}
mutate.AppendMutation(mutation.GetOwnerMutation(c.Scheme, o))
return mutate, nil
}
func (c *Controller) GetFQDN() string {
return fmt.Sprintf("%s.goharbor.io", strings.ToLower(c.GetName()))
}
func (c *Controller) Label(suffix ...string) string {
return c.LabelWithPrefix("", suffix...)
}
func (c *Controller) LabelWithPrefix(prefix string, suffix ...string) string {
var suffixString string
if len(suffix) > 0 {
suffixString = "/" + strings.Join(suffix, "-")
}
if prefix != "" {
prefix = "." + prefix
}
return fmt.Sprintf("%s%s%s", prefix, c.GetFQDN(), suffixString)
}
func (c *Controller) DeploymentMutateFn(ctx context.Context, dependencies ...graph.Resource) (resources.Mutable, error) {
mutate, err := c.GlobalMutateFn(ctx)
if err != nil {
return nil, err
}
mutate.AppendMutation(mutation.GetTemplateAnnotationsMutation(WarningAnnotation, fmt.Sprintf(WarningValueTmpl, c.GetName())))
mutate.AppendMutation(mutation.GetTemplateLabelsMutation(OperatorNameLabel, c.GetName(), OperatorVersionLabel, c.GetVersion()))
fqdn := c.GetFQDN()
mutate.AppendMutation(func(ctx context.Context, obj runtime.Object) error {
for i, dep := range dependencies {
res, ok := dep.(*Resource)
if !ok {
logger.Get(ctx).Info("Cannot add dependency checksum", "resource", dep)
continue
}
depRemote, ok := res.resource.DeepCopyObject().(resources.Resource)
if !ok {
logger.Get(ctx).Info("Cannot add dependency checksum", "resource", dep)
continue
}
name := res.resource.GetName()
namespace := res.resource.GetNamespace()
err := c.Client.Get(ctx, types.NamespacedName{
Name: name,
Namespace: namespace,
}, depRemote)
if err != nil {
return errors.Wrapf(err, "%d: cannot get dependency", i)
}
kind := strings.ToLower(depRemote.GetObjectKind().GroupVersionKind().Kind)
err = mutation.GetTemplateAnnotationsMutation(
fmt.Sprintf("%s.%s.%s.%s/uuid", name, namespace, kind, fqdn), string(depRemote.GetUID()),
fmt.Sprintf("%s.%s.%s.%s/version", name, namespace, kind, fqdn), depRemote.GetResourceVersion(),
)(ctx, obj)
if err != nil {
return errors.Wrapf(err, "%d: annotation mutation", i)
}
}
return nil
})
return mutate, nil
}
func (c *Controller) SecretMutateFn(ctx context.Context, immutable *bool) (resources.Mutable, error) {
mutate, err := c.GlobalMutateFn(ctx)
if err != nil {
return nil, err
}
if immutable != nil && *immutable {
mutate.AppendMutation(mutation.GetAnnotationsMutation(ImmutableAnnotation, "true"))
}
return mutate, nil
}
func isImmutableResource(res resources.Resource) bool {
annotations := res.GetAnnotations()
if len(annotations) == 0 {
return false
}
if value, ok := annotations[ImmutableAnnotation]; ok && value == "true" {
return true
}
return false
}