forked from argoproj/argo-rollouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb.go
114 lines (104 loc) · 3.43 KB
/
alb.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
package ingress
import (
"context"
"encoding/json"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"k8s.io/utils/pointer"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
ingressutil "github.com/argoproj/argo-rollouts/utils/ingress"
jsonutil "github.com/argoproj/argo-rollouts/utils/json"
logutil "github.com/argoproj/argo-rollouts/utils/log"
)
func (c *Controller) syncALBIngress(ingress *ingressutil.Ingress, rollouts []*v1alpha1.Rollout) error {
ctx := context.TODO()
annotations := ingress.GetAnnotations()
managedActions, err := ingressutil.NewManagedALBActions(annotations[ingressutil.ManagedActionsAnnotation])
if err != nil {
return nil
}
actionHasExistingRollout := map[string]bool{}
for i := range rollouts {
rollout := rollouts[i]
if _, ok := managedActions[rollout.Name]; ok {
actionHasExistingRollout[rollout.Name] = true
c.enqueueRollout(rollout)
}
}
newIngress := ingress.DeepCopy()
modified := false
for roName := range managedActions {
if _, ok := actionHasExistingRollout[roName]; !ok {
modified = true
actionKey := managedActions[roName]
delete(managedActions, roName)
resetALBAction, err := getResetALBActionStr(ingress, actionKey)
if err != nil {
log.WithField(logutil.RolloutKey, roName).
WithField(logutil.IngressKey, ingress.GetName()).
WithField(logutil.NamespaceKey, ingress.GetNamespace()).
Error(err)
return nil
}
annotations := newIngress.GetAnnotations()
annotations[actionKey] = resetALBAction
newIngress.SetAnnotations(annotations)
}
}
if !modified {
return nil
}
newManagedStr := managedActions.String()
newAnnotations := newIngress.GetAnnotations()
newAnnotations[ingressutil.ManagedActionsAnnotation] = newManagedStr
newIngress.SetAnnotations(newAnnotations)
if newManagedStr == "" {
delete(newIngress.GetAnnotations(), ingressutil.ManagedActionsAnnotation)
}
_, err = c.ingressWrapper.Update(ctx, ingress.GetNamespace(), newIngress)
return err
}
func getResetALBActionStr(ingress *ingressutil.Ingress, action string) (string, error) {
parts := strings.Split(action, ingressutil.ALBActionPrefix)
if len(parts) != 2 {
return "", fmt.Errorf("unable to parse action to get the service %s", action)
}
service := parts[1]
annotations := ingress.GetAnnotations()
previousActionStr := annotations[action]
var previousAction ingressutil.ALBAction
err := json.Unmarshal([]byte(previousActionStr), &previousAction)
if err != nil {
return "", fmt.Errorf("unable to unmarshal previous ALB action")
}
var port string
for _, tg := range previousAction.ForwardConfig.TargetGroups {
if tg.ServiceName == service {
port = tg.ServicePort
}
}
if port == "" {
return "", fmt.Errorf("unable to reset annotation due to missing port")
}
albAction := ingressutil.ALBAction{
Type: "forward",
ForwardConfig: ingressutil.ALBForwardConfig{
TargetGroups: []ingressutil.ALBTargetGroup{
{
ServiceName: service,
ServicePort: port,
Weight: pointer.Int64Ptr(int64(100)),
},
},
},
}
if previousAction.ForwardConfig.TargetGroupStickinessConfig != nil {
albAction.ForwardConfig.TargetGroupStickinessConfig = &ingressutil.ALBTargetGroupStickinessConfig{
Enabled: previousAction.ForwardConfig.TargetGroupStickinessConfig.Enabled,
DurationSeconds: previousAction.ForwardConfig.TargetGroupStickinessConfig.DurationSeconds,
}
}
bytes := jsonutil.MustMarshal(albAction)
return string(bytes), nil
}