-
Notifications
You must be signed in to change notification settings - Fork 797
/
alb.go
226 lines (207 loc) · 7.29 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package alb
import (
"context"
"fmt"
"strconv"
"github.com/sirupsen/logrus"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
extensionslisters "k8s.io/client-go/listers/extensions/v1beta1"
"k8s.io/utils/pointer"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/utils/aws"
"github.com/argoproj/argo-rollouts/utils/diff"
ingressutil "github.com/argoproj/argo-rollouts/utils/ingress"
jsonutil "github.com/argoproj/argo-rollouts/utils/json"
logutil "github.com/argoproj/argo-rollouts/utils/log"
"github.com/argoproj/argo-rollouts/utils/record"
)
const (
// Type holds this controller type
Type = "ALB"
)
// ReconcilerConfig describes static configuration data for the ALB Ingress reconciler
type ReconcilerConfig struct {
Rollout *v1alpha1.Rollout
Client kubernetes.Interface
Recorder record.EventRecorder
ControllerKind schema.GroupVersionKind
IngressLister extensionslisters.IngressLister
VerifyWeight *bool
}
// Reconciler holds required fields to reconcile ALB Ingress resources
type Reconciler struct {
cfg ReconcilerConfig
log *logrus.Entry
aws aws.Client
}
var (
defaultVerifyWeight = false
)
// SetDefaultVerifyWeight sets the default setWeight verification when instantiating the reconciler
func SetDefaultVerifyWeight(b bool) {
defaultVerifyWeight = b
}
// NewReconciler returns a reconciler struct that brings the ALB Ingress into the desired state
func NewReconciler(cfg ReconcilerConfig) (*Reconciler, error) {
awsClient, err := aws.NewClient()
if err != nil {
return nil, err
}
reconciler := Reconciler{
cfg: cfg,
log: logutil.WithRollout(cfg.Rollout).WithField(logutil.IngressKey, cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.ALB.Ingress),
aws: awsClient,
}
return &reconciler, nil
}
// Type indicates this reconciler is an ALB ingress reconciler
func (r *Reconciler) Type() string {
return Type
}
// SetWeight modifies ALB Ingress resources to reach desired state
func (r *Reconciler) SetWeight(desiredWeight int32) error {
ctx := context.TODO()
rollout := r.cfg.Rollout
ingressName := rollout.Spec.Strategy.Canary.TrafficRouting.ALB.Ingress
ingress, err := r.cfg.IngressLister.Ingresses(rollout.Namespace).Get(ingressName)
if err != nil {
return err
}
actionService := r.cfg.Rollout.Spec.Strategy.Canary.StableService
if r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.ALB.RootService != "" {
actionService = r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.ALB.RootService
}
port := r.cfg.Rollout.Spec.Strategy.Canary.TrafficRouting.ALB.ServicePort
if !ingressutil.HasRuleWithService(ingress, actionService) {
return fmt.Errorf("ingress does not have service `%s` in rules", actionService)
}
desired, err := getDesiredAnnotations(ingress, rollout, port, desiredWeight)
if err != nil {
return err
}
patch, modified, err := calculatePatch(ingress, desired)
if err != nil {
return nil
}
if !modified {
r.log.Info("no changes to the ALB Ingress")
return nil
}
r.log.WithField("patch", string(patch)).Debug("applying ALB Ingress patch")
r.log.WithField("desiredWeight", desiredWeight).Info("updating ALB Ingress")
r.cfg.Recorder.Eventf(r.cfg.Rollout, record.EventOptions{EventReason: "PatchingALBIngress"}, "Updating Ingress `%s` to desiredWeight '%d'", ingressName, desiredWeight)
_, err = r.cfg.Client.ExtensionsV1beta1().Ingresses(ingress.Namespace).Patch(ctx, ingress.Name, types.MergePatchType, patch, metav1.PatchOptions{})
if err != nil {
r.log.WithField("err", err.Error()).Error("error patching alb ingress")
return fmt.Errorf("error patching alb ingress `%s`: %v", ingressName, err)
}
return nil
}
func (r *Reconciler) shouldVerifyWeight() bool {
if r.cfg.VerifyWeight != nil {
return *r.cfg.VerifyWeight
}
return defaultVerifyWeight
}
func (r *Reconciler) VerifyWeight(desiredWeight int32) (bool, error) {
if !r.shouldVerifyWeight() {
return true, nil
}
ctx := context.TODO()
rollout := r.cfg.Rollout
ingressName := rollout.Spec.Strategy.Canary.TrafficRouting.ALB.Ingress
ingress, err := r.cfg.IngressLister.Ingresses(rollout.Namespace).Get(ingressName)
if err != nil {
return false, err
}
canaryService := rollout.Spec.Strategy.Canary.CanaryService
resourceID := aws.BuildV2TargetGroupID(rollout.Namespace, ingress.Name, canaryService, rollout.Spec.Strategy.Canary.TrafficRouting.ALB.ServicePort)
if len(ingress.Status.LoadBalancer.Ingress) == 0 {
r.log.Infof("LoadBalancer not yet allocated")
}
for _, lbIngress := range ingress.Status.LoadBalancer.Ingress {
if lbIngress.Hostname == "" {
continue
}
lb, err := r.aws.FindLoadBalancerByDNSName(ctx, lbIngress.Hostname)
if err != nil {
return false, err
}
if lb == nil || lb.LoadBalancerArn == nil {
r.log.Infof("LoadBalancer %s not found", lbIngress.Hostname)
return false, nil
}
lbTargetGroups, err := r.aws.GetTargetGroupMetadata(ctx, *lb.LoadBalancerArn)
if err != nil {
return false, err
}
logCtx := r.log.WithField("lb", *lb.LoadBalancerArn)
for _, tg := range lbTargetGroups {
if tg.Tags[aws.AWSLoadBalancerV2TagKeyResourceID] == resourceID {
if tg.Weight != nil {
logCtx := logCtx.WithField("tg", *tg.TargetGroupArn)
logCtx.Infof("canary weight of %s (desired: %d, current: %d)", resourceID, desiredWeight, *tg.Weight)
return *tg.Weight == desiredWeight, nil
}
}
}
}
return false, nil
}
func calculatePatch(current *extensionsv1beta1.Ingress, desiredAnnotations map[string]string) ([]byte, bool, error) {
// only compare Annotations
return diff.CreateTwoWayMergePatch(
&extensionsv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Annotations: current.Annotations,
},
},
&extensionsv1beta1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Annotations: desiredAnnotations,
},
}, extensionsv1beta1.Ingress{})
}
func getForwardActionString(r *v1alpha1.Rollout, port int32, desiredWeight int32) string {
stableService := r.Spec.Strategy.Canary.StableService
canaryService := r.Spec.Strategy.Canary.CanaryService
portStr := strconv.Itoa(int(port))
action := ingressutil.ALBAction{
Type: "forward",
ForwardConfig: ingressutil.ALBForwardConfig{
TargetGroups: []ingressutil.ALBTargetGroup{
{
ServiceName: stableService,
ServicePort: portStr,
Weight: pointer.Int64Ptr(100 - int64(desiredWeight)),
}, {
ServiceName: canaryService,
ServicePort: portStr,
Weight: pointer.Int64Ptr(int64(desiredWeight)),
},
},
},
}
bytes := jsonutil.MustMarshal(action)
return string(bytes)
}
func getDesiredAnnotations(current *extensionsv1beta1.Ingress, r *v1alpha1.Rollout, port int32, desiredWeight int32) (map[string]string, error) {
desired := current.DeepCopy().Annotations
key := ingressutil.ALBActionAnnotationKey(r)
desired[key] = getForwardActionString(r, port, desiredWeight)
m, err := ingressutil.NewManagedALBActions(desired[ingressutil.ManagedActionsAnnotation])
if err != nil {
return nil, err
}
m[r.Name] = key
desired[ingressutil.ManagedActionsAnnotation] = m.String()
return desired, nil
}
// UpdateHash informs a traffic routing reconciler about new canary/stable pod hashes
func (r *Reconciler) UpdateHash(canaryHash, stableHash string) error {
return nil
}