-
Notifications
You must be signed in to change notification settings - Fork 797
/
controller.go
179 lines (155 loc) · 6.05 KB
/
controller.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
package analysis
import (
"time"
log "github.com/sirupsen/logrus"
batchv1 "k8s.io/api/batch/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/wait"
batchinformers "k8s.io/client-go/informers/batch/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"github.com/argoproj/argo-rollouts/controller/metrics"
"github.com/argoproj/argo-rollouts/metricproviders"
register "github.com/argoproj/argo-rollouts/pkg/apis/rollouts"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
clientset "github.com/argoproj/argo-rollouts/pkg/client/clientset/versioned"
informers "github.com/argoproj/argo-rollouts/pkg/client/informers/externalversions/rollouts/v1alpha1"
listers "github.com/argoproj/argo-rollouts/pkg/client/listers/rollouts/v1alpha1"
controllerutil "github.com/argoproj/argo-rollouts/utils/controller"
logutil "github.com/argoproj/argo-rollouts/utils/log"
)
// AnalysisController is the controller implementation for Analysis resources
type AnalysisController struct {
// kubeclientset is a standard kubernetes clientset
kubeclientset kubernetes.Interface
// analysisclientset is a clientset for our own API group
argoProjClientset clientset.Interface
analysisRunLister listers.AnalysisRunLister
analysisRunSynced cache.InformerSynced
jobInformer batchinformers.JobInformer
metricsServer *metrics.MetricsServer
newProvider func(logCtx log.Entry, metric v1alpha1.Metric) (metricproviders.Provider, error)
// used for unit testing
enqueueAnalysis func(obj interface{})
enqueueAnalysisAfter func(obj interface{}, duration time.Duration)
// workqueue is a rate limited work queue. This is used to queue work to be
// processed instead of performing it as soon as a change happens. This
// means we can ensure we only process a fixed amount of resources at a
// time, and makes it easy to ensure we are never processing the same item
// simultaneously in two different workers.
analysisRunWorkQueue workqueue.RateLimitingInterface
// recorder is an event recorder for recording Event resources to the
// Kubernetes API.
recorder record.EventRecorder
resyncPeriod time.Duration
}
// NewAnalysisController returns a new analysis controller
func NewAnalysisController(
kubeclientset kubernetes.Interface,
argoProjClientset clientset.Interface,
analysisRunInformer informers.AnalysisRunInformer,
jobInformer batchinformers.JobInformer,
resyncPeriod time.Duration,
analysisRunWorkQueue workqueue.RateLimitingInterface,
metricsServer *metrics.MetricsServer,
recorder record.EventRecorder) *AnalysisController {
controller := &AnalysisController{
kubeclientset: kubeclientset,
argoProjClientset: argoProjClientset,
analysisRunLister: analysisRunInformer.Lister(),
metricsServer: metricsServer,
analysisRunWorkQueue: analysisRunWorkQueue,
jobInformer: jobInformer,
analysisRunSynced: analysisRunInformer.Informer().HasSynced,
recorder: recorder,
resyncPeriod: resyncPeriod,
}
controller.enqueueAnalysis = func(obj interface{}) {
controllerutil.Enqueue(obj, analysisRunWorkQueue)
}
controller.enqueueAnalysisAfter = func(obj interface{}, duration time.Duration) {
controllerutil.EnqueueAfter(obj, duration, analysisRunWorkQueue)
}
providerFactory := metricproviders.ProviderFactory{
KubeClient: controller.kubeclientset,
JobLister: jobInformer.Lister(),
}
controller.newProvider = providerFactory.NewProvider
jobInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
controller.enqueueIfCompleted(obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
controller.enqueueIfCompleted(newObj)
},
DeleteFunc: func(obj interface{}) {
controller.enqueueIfCompleted(obj)
},
})
log.Info("Setting up analysis event handlers")
// Set up an event handler for when analysis resources change
analysisRunInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAnalysis,
UpdateFunc: func(old, new interface{}) {
controller.enqueueAnalysis(new)
},
DeleteFunc: controller.enqueueAnalysis,
})
return controller
}
func (c *AnalysisController) Run(threadiness int, stopCh <-chan struct{}) error {
log.Info("Starting analysis workers")
for i := 0; i < threadiness; i++ {
go wait.Until(func() {
controllerutil.RunWorker(c.analysisRunWorkQueue, logutil.AnalysisRunKey, c.syncHandler, c.metricsServer)
}, time.Second, stopCh)
}
log.Infof("Started %d analysis workers", threadiness)
<-stopCh
log.Info("Shutting down analysis workers")
return nil
}
func (c *AnalysisController) syncHandler(key string) error {
startTime := time.Now()
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
log.WithField(logutil.AnalysisRunKey, name).WithField(logutil.NamespaceKey, namespace).Infof("Started syncing Analysis at (%v)", startTime)
run, err := c.analysisRunLister.AnalysisRuns(namespace).Get(name)
if k8serrors.IsNotFound(err) {
log.WithField(logutil.AnalysisRunKey, name).WithField(logutil.NamespaceKey, namespace).Info("Analysis has been deleted")
return nil
}
if err != nil {
return err
}
defer func() {
duration := time.Since(startTime)
//TODO(jessesuen) Add metrics for analysis
//arc.metricsServer.IncReconcile(r, duration)
logCtx := logutil.WithAnalysisRun(run).WithField("time_ms", duration.Seconds()*1e3)
logCtx.Info("Reconciliation completed")
}()
if run.DeletionTimestamp != nil {
logutil.WithAnalysisRun(run).Info("No reconciliation as analysis marked for deletion")
return nil
}
newRun := c.reconcileAnalysisRun(run)
return c.persistAnalysisRunStatus(run, newRun.Status)
}
func (c *AnalysisController) enqueueIfCompleted(obj interface{}) {
job, ok := obj.(*batchv1.Job)
if !ok {
return
}
for _, condition := range job.Status.Conditions {
switch condition.Type {
case batchv1.JobFailed, batchv1.JobComplete:
controllerutil.EnqueueParentObject(job, register.AnalysisRunKind, c.enqueueAnalysis)
return
}
}
}