/
configuration.go
94 lines (72 loc) · 2.38 KB
/
configuration.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
package istio
import (
"encoding/json"
operatorv1alpha1 "github.com/kyma-project/istio/operator/api/v1alpha1"
)
// IstioCRChange represents difference since last reconciliation of IstioCR
type IstioCRChange int
const (
NoChange IstioCRChange = 0
Create IstioCRChange = 1
VersionUpdate IstioCRChange = 2
ConfigurationUpdate IstioCRChange = 4
Delete IstioCRChange = 8
)
func (r IstioCRChange) requireInstall() bool {
return r == Create || r&VersionUpdate > 0 || r&ConfigurationUpdate > 0
}
func (r IstioCRChange) requireIstioDeletion() bool {
return r == Delete
}
type appliedConfig struct {
operatorv1alpha1.IstioSpec
IstioTag string
}
// EvaluateIstioCRChanges returns IstioCRChange that happened since LastAppliedConfiguration
func EvaluateIstioCRChanges(istioCR operatorv1alpha1.Istio, istioTag string) (trigger IstioCRChange, err error) {
if !istioCR.DeletionTimestamp.IsZero() {
return Delete, nil
}
lastAppliedConfigAnnotation, ok := istioCR.Annotations[LastAppliedConfiguration]
if !ok {
return Create, nil
}
trigger = NoChange
var lastAppliedConfig appliedConfig
err = json.Unmarshal([]byte(lastAppliedConfigAnnotation), &lastAppliedConfig)
if err != nil {
return trigger, err
}
if lastAppliedConfig.IstioTag != istioTag {
trigger = trigger | VersionUpdate
}
lastAppliedNotNil := lastAppliedConfig.Config.NumTrustedProxies != nil
newNotNil := istioCR.Spec.Config.NumTrustedProxies != nil
if lastAppliedNotNil != newNotNil {
return trigger | ConfigurationUpdate, nil
}
if !lastAppliedNotNil {
return trigger, nil
}
if *lastAppliedConfig.Config.NumTrustedProxies != *istioCR.Spec.Config.NumTrustedProxies {
return trigger | ConfigurationUpdate, nil
}
return trigger, nil
}
// UpdateLastAppliedConfiguration annotates the passed CR with LastAppliedConfiguration, which holds information about last applied
// IstioCR spec and IstioTag (IstioVersion-IstioImageBase)
func UpdateLastAppliedConfiguration(cr operatorv1alpha1.Istio, istioTag string) (operatorv1alpha1.Istio, error) {
if cr.Annotations == nil {
cr.Annotations = make(map[string]string)
}
newAppliedConfig := appliedConfig{
IstioSpec: cr.Spec,
IstioTag: istioTag,
}
config, err := json.Marshal(newAppliedConfig)
if err != nil {
return operatorv1alpha1.Istio{}, err
}
cr.Annotations[LastAppliedConfiguration] = string(config)
return cr, nil
}