-
Notifications
You must be signed in to change notification settings - Fork 71
/
expose_lb.go
169 lines (153 loc) · 5.57 KB
/
expose_lb.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
package changedetector
import (
"context"
"fmt"
"github.com/armory/spinnaker-operator/pkg/apis/spinnaker/interfaces"
"github.com/armory/spinnaker-operator/pkg/util"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
)
type exposeLbChangeDetector struct {
client client.Client
log logr.Logger
evtRecorder record.EventRecorder
}
type exposeLbChangeDetectorGenerator struct {
}
func (g *exposeLbChangeDetectorGenerator) NewChangeDetector(client client.Client, log logr.Logger, evtRecorder record.EventRecorder) (ChangeDetector, error) {
return &exposeLbChangeDetector{client: client, log: log, evtRecorder: evtRecorder}, nil
}
// IsSpinnakerUpToDate returns true if expose spinnaker configuration matches actual exposed services
func (ch *exposeLbChangeDetector) IsSpinnakerUpToDate(ctx context.Context, svc interfaces.SpinnakerService) (bool, error) {
exp := svc.GetExposeConfig()
switch strings.ToLower(exp.Type) {
case "":
return true, nil
case "service":
isDeckSSLEnabled, err := svc.GetSpinnakerConfig().GetHalConfigPropBool(util.DeckSSLEnabledProp, false)
if err != nil {
isDeckSSLEnabled = false
}
upToDateDeck, err := ch.isExposeServiceUpToDate(ctx, svc, util.DeckServiceName, isDeckSSLEnabled)
if !upToDateDeck || err != nil {
return false, err
}
isGateSSLEnabled, err := svc.GetSpinnakerConfig().GetHalConfigPropBool(util.GateSSLEnabledProp, false)
if err != nil {
isGateSSLEnabled = false
}
upToDateGate, err := ch.isExposeServiceUpToDate(ctx, svc, util.GateServiceName, isGateSSLEnabled)
if !upToDateGate || err != nil {
return false, err
}
return true, nil
default:
return false, fmt.Errorf("expose type %s not supported. Valid types: \"service\"", exp.Type)
}
}
func (ch *exposeLbChangeDetector) AlwaysRun() bool {
return false
}
func (ch *exposeLbChangeDetector) isExposeServiceUpToDate(ctx context.Context, spinSvc interfaces.SpinnakerService, serviceName string, hcSSLEnabled bool) (bool, error) {
rLogger := ch.log.WithValues("Service", spinSvc.GetName())
ns := spinSvc.GetNamespace()
svc, err := util.GetService(serviceName, ns, ch.client)
if err != nil {
return false, err
}
// we need a service to exist, therefore it's not "up to date"
if svc == nil {
return false, nil
}
// service type is different, redeploy
if upToDate, err := ch.exposeServiceTypeUpToDate(serviceName, spinSvc, svc); !upToDate || err != nil {
return false, err
}
// port is different, redeploy
if upToDate, err := ch.exposePortUpToDate(ctx, serviceName, spinSvc, svc); !upToDate || err != nil {
return false, err
}
// annotations are different, redeploy
simpleServiceName := serviceName[len("spin-"):]
exp := spinSvc.GetExposeConfig()
expectedAnnotations := exp.GetAggregatedAnnotations(simpleServiceName)
if !ch.areAnnotationsEqual(svc.Annotations, expectedAnnotations) {
rLogger.Info(fmt.Sprintf("Service annotations for %s: expected: %s, actual: %s", serviceName,
expectedAnnotations, svc.Annotations))
return false, nil
}
// status url is available but not set yet, redeploy
st := spinSvc.GetStatus()
statusUrl := st.APIUrl
if serviceName == "spin-deck" {
statusUrl = st.UIUrl
}
if statusUrl == "" {
lbUrl, err := util.FindLoadBalancerUrl(serviceName, ns, ch.client, hcSSLEnabled)
if err != nil {
return false, err
}
if lbUrl != "" {
rLogger.Info(fmt.Sprintf("Status url of %s is not set and load balancer url is ready", serviceName))
return false, nil
}
}
return true, nil
}
func (ch *exposeLbChangeDetector) exposeServiceTypeUpToDate(serviceName string, spinSvc interfaces.SpinnakerService, svc *corev1.Service) (bool, error) {
rLogger := ch.log.WithValues("Service", spinSvc.GetName())
formattedServiceName := serviceName[len("spin-"):]
exp := spinSvc.GetExposeConfig()
if c, ok := exp.Service.Overrides[formattedServiceName]; ok && c.Type != "" {
if string(svc.Spec.Type) != c.Type {
rLogger.Info(fmt.Sprintf("Service type for %s: expected: %s, actual: %s", serviceName,
c.Type, string(svc.Spec.Type)))
return false, nil
}
} else {
if string(svc.Spec.Type) != exp.Service.Type {
rLogger.Info(fmt.Sprintf("Service type for %s: expected: %s, actual: %s", serviceName,
exp.Service.Type, string(svc.Spec.Type)))
return false, nil
}
}
return true, nil
}
func (ch *exposeLbChangeDetector) exposePortUpToDate(ctx context.Context, serviceName string, spinSvc interfaces.SpinnakerService, svc *corev1.Service) (bool, error) {
rLogger := ch.log.WithValues("Service", spinSvc.GetName())
if len(svc.Spec.Ports) < 1 {
rLogger.Info(fmt.Sprintf("No exposed port for %s found", serviceName))
return false, nil
}
svcNameWithoutPrefix := serviceName[len("spin-"):]
portName := fmt.Sprintf("%s-tcp", svcNameWithoutPrefix)
publicPort, _ := ch.getSvcPorts(portName, svc)
desiredPort := util.GetDesiredExposePort(ctx, svcNameWithoutPrefix, int32(80), spinSvc)
if desiredPort != publicPort {
rLogger.Info(fmt.Sprintf("Service port for %s: expected: %d, actual: %d", serviceName,
desiredPort, publicPort))
return false, nil
}
return true, nil
}
func (ch *exposeLbChangeDetector) getSvcPorts(portName string, svc *corev1.Service) (int32, int32) {
for _, p := range svc.Spec.Ports {
if p.Name == portName {
return p.Port, p.TargetPort.IntVal
}
}
return 0, 0
}
func (ch *exposeLbChangeDetector) areAnnotationsEqual(first map[string]string, other map[string]string) bool {
if len(first) != len(other) {
return false
}
if first == nil || other == nil {
return true
}
return reflect.DeepEqual(first, other)
}