-
Notifications
You must be signed in to change notification settings - Fork 880
/
ingress.go
190 lines (166 loc) · 5.47 KB
/
ingress.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
package ingress
import (
"context"
"fmt"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/kubernetes/cmd/kubeadm/app/util"
"github.com/argoproj/argo-rollouts/controller/metrics"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
informers "github.com/argoproj/argo-rollouts/pkg/client/informers/externalversions/rollouts/v1alpha1"
controllerutil "github.com/argoproj/argo-rollouts/utils/controller"
ingressutil "github.com/argoproj/argo-rollouts/utils/ingress"
logutil "github.com/argoproj/argo-rollouts/utils/log"
unstructuredutil "github.com/argoproj/argo-rollouts/utils/unstructured"
)
const (
// ingressIndexName is the index by which Ingress resources are cached
ingressIndexName = "byIngress"
)
// ControllerConfig describes the data required to instantiate a new ingress controller
type ControllerConfig struct {
Client kubernetes.Interface
IngressWrap *ingressutil.IngressWrap
IngressWorkQueue workqueue.RateLimitingInterface
RolloutsInformer informers.RolloutInformer
RolloutWorkQueue workqueue.RateLimitingInterface
MetricsServer *metrics.MetricsServer
ALBClasses []string
NGINXClasses []string
}
// Controller describes an ingress controller
type Controller struct {
client kubernetes.Interface
rolloutsIndexer cache.Indexer
ingressWrapper IngressWrapper
ingressWorkqueue workqueue.RateLimitingInterface
metricServer *metrics.MetricsServer
enqueueRollout func(obj any)
albClasses []string
nginxClasses []string
}
type IngressWrapper interface {
GetCached(namespace, name string) (*ingressutil.Ingress, error)
Update(ctx context.Context, namespace string, ingress *ingressutil.Ingress) (*ingressutil.Ingress, error)
}
// NewController returns a new ingress controller
func NewController(cfg ControllerConfig) *Controller {
controller := &Controller{
client: cfg.Client,
rolloutsIndexer: cfg.RolloutsInformer.Informer().GetIndexer(),
ingressWrapper: cfg.IngressWrap,
ingressWorkqueue: cfg.IngressWorkQueue,
metricServer: cfg.MetricsServer,
albClasses: cfg.ALBClasses,
nginxClasses: cfg.NGINXClasses,
}
util.CheckErr(cfg.RolloutsInformer.Informer().AddIndexers(cache.Indexers{
ingressIndexName: func(obj any) ([]string, error) {
if ro := unstructuredutil.ObjectToRollout(obj); ro != nil {
return ingressutil.GetRolloutIngressKeys(ro), nil
}
return []string{}, nil
},
}))
cfg.IngressWrap.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj any) {
controllerutil.Enqueue(obj, cfg.IngressWorkQueue)
},
UpdateFunc: func(oldObj, newObj any) {
controllerutil.Enqueue(newObj, cfg.IngressWorkQueue)
},
DeleteFunc: func(obj any) {
controllerutil.Enqueue(obj, cfg.IngressWorkQueue)
},
})
controller.enqueueRollout = func(obj any) {
controllerutil.EnqueueRateLimited(obj, cfg.RolloutWorkQueue)
}
return controller
}
// Run starts the controller threads
func (c *Controller) Run(ctx context.Context, threadiness int) error {
log.Info("Starting Ingress workers")
wg := sync.WaitGroup{}
for i := 0; i < threadiness; i++ {
wg.Add(1)
go wait.Until(func() {
controllerutil.RunWorker(ctx, c.ingressWorkqueue, logutil.IngressKey, c.syncIngress, c.metricServer)
wg.Done()
log.Debug("Ingress worker has stopped")
}, time.Second, ctx.Done())
}
log.Info("Started Ingress workers")
<-ctx.Done()
wg.Wait()
log.Info("All ingress workers have stopped")
return nil
}
// syncIngress queues all rollouts referencing the Ingress for reconciliation
func (c *Controller) syncIngress(ctx context.Context, key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
ingress, err := c.ingressWrapper.GetCached(namespace, name)
if err != nil {
if !errors.IsNotFound(err) {
// Unknown error occurred
return err
}
if !strings.HasSuffix(name, ingressutil.CanaryIngressSuffix) {
// a primary ingress was deleted, simply ignore the event
log.WithField(logutil.IngressKey, key).Warn("primary ingress has been deleted")
}
return nil
}
rollouts, err := c.getRolloutsByIngress(ingress.GetNamespace(), ingress.GetName())
if err != nil {
return nil
}
class := ingress.GetClass()
switch {
case hasClass(c.albClasses, class):
return c.syncALBIngress(ingress, rollouts)
case hasClass(c.nginxClasses, class):
return c.syncNginxIngress(name, namespace, rollouts)
default:
return nil
}
}
func hasClass(classes []string, class string) bool {
for _, str := range classes {
if str == class {
return true
}
}
return false
}
func (c *Controller) syncNginxIngress(name, namespace string, rollouts []*v1alpha1.Rollout) error {
for i := range rollouts {
// reconciling the Rollout will ensure the canaryIngress is updated or created
c.enqueueRollout(rollouts[i])
}
return nil
}
// getRolloutsByIngress returns all rollouts which are referencing specified ingress
func (c *Controller) getRolloutsByIngress(namespace string, ingressName string) ([]*v1alpha1.Rollout, error) {
objs, err := c.rolloutsIndexer.ByIndex(ingressIndexName, fmt.Sprintf("%s/%s", namespace, ingressName))
if err != nil {
return nil, err
}
var rollouts []*v1alpha1.Rollout
for _, obj := range objs {
if ro := unstructuredutil.ObjectToRollout(obj); ro != nil {
rollouts = append(rollouts, ro)
}
}
return rollouts, nil
}