forked from argoproj/argo-workflows
/
config_controller.go
169 lines (155 loc) · 5.3 KB
/
config_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
package controller
import (
"context"
"fmt"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/argoproj/argo/errors"
"github.com/argoproj/argo/workflow/common"
"github.com/argoproj/argo/workflow/config"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)
// ResyncConfig reloads the controller config from the configmap
func (wfc *WorkflowController) ResyncConfig() error {
cmClient := wfc.kubeclientset.CoreV1().ConfigMaps(wfc.namespace)
cm, err := cmClient.Get(wfc.configMap, metav1.GetOptions{})
if err != nil {
return errors.InternalWrapError(err)
}
return wfc.updateConfig(cm)
}
func (wfc *WorkflowController) updateConfig(cm *apiv1.ConfigMap) error {
configStr, ok := cm.Data[common.WorkflowControllerConfigMapKey]
if !ok {
log.Warnf("ConfigMap '%s' does not have key '%s'", wfc.configMap, common.WorkflowControllerConfigMapKey)
return nil
}
var config config.WorkflowControllerConfig
err := yaml.Unmarshal([]byte(configStr), &config)
if err != nil {
return errors.InternalWrapError(err)
}
log.Printf("workflow controller configuration from %s:\n%s", wfc.configMap, configStr)
if wfc.cliExecutorImage == "" && config.ExecutorImage == "" {
return errors.Errorf(errors.CodeBadRequest, "ConfigMap '%s' does not have executorImage", wfc.configMap)
}
wfc.Config = config
if wfc.Config.Persistence != nil {
log.Info("Persistence configuration enabled")
wfc.wfDBctx, err = wfc.createPersistenceContext()
if err != nil {
log.Errorf("Error Creating Persistence context. %v", err)
} else {
log.Info("Persistence Session created successfully")
}
} else {
log.Info("Persistence configuration disabled")
wfc.wfDBctx = nil
}
wfc.throttler.SetParallelism(config.Parallelism)
return nil
}
// executorImage returns the image to use for the workflow executor
func (wfc *WorkflowController) executorImage() string {
if wfc.cliExecutorImage != "" {
return wfc.cliExecutorImage
}
return wfc.Config.ExecutorImage
}
// executorImagePullPolicy returns the imagePullPolicy to use for the workflow executor
func (wfc *WorkflowController) executorImagePullPolicy() apiv1.PullPolicy {
if wfc.cliExecutorImagePullPolicy != "" {
return apiv1.PullPolicy(wfc.cliExecutorImagePullPolicy)
} else if wfc.Config.Executor != nil && wfc.Config.Executor.ImagePullPolicy != "" {
return wfc.Config.Executor.ImagePullPolicy
} else {
return apiv1.PullPolicy(wfc.Config.ExecutorImagePullPolicy)
}
}
func (wfc *WorkflowController) watchControllerConfigMap(ctx context.Context) (cache.Controller, error) {
source := wfc.newControllerConfigMapWatch()
_, controller := cache.NewInformer(
source,
&apiv1.ConfigMap{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if cm, ok := obj.(*apiv1.ConfigMap); ok {
log.Infof("Detected ConfigMap update. Updating the controller config.")
err := wfc.updateConfig(cm)
if err != nil {
log.Errorf("Update of config failed due to: %v", err)
}
}
},
UpdateFunc: func(old, new interface{}) {
oldCM := old.(*apiv1.ConfigMap)
newCM := new.(*apiv1.ConfigMap)
if oldCM.ResourceVersion == newCM.ResourceVersion {
return
}
if newCm, ok := new.(*apiv1.ConfigMap); ok {
log.Infof("Detected ConfigMap update. Updating the controller config.")
err := wfc.updateConfig(newCm)
if err != nil {
log.Errorf("Update of config failed due to: %v", err)
}
}
},
})
go controller.Run(ctx.Done())
return controller, nil
}
func (wfc *WorkflowController) newControllerConfigMapWatch() *cache.ListWatch {
c := wfc.kubeclientset.CoreV1().RESTClient()
resource := "configmaps"
name := wfc.configMap
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", name))
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fieldSelector.String()
req := c.Get().
Namespace(wfc.namespace).
Resource(resource).
VersionedParams(&options, metav1.ParameterCodec)
return req.Do().Get()
}
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
options.FieldSelector = fieldSelector.String()
req := c.Get().
Namespace(wfc.namespace).
Resource(resource).
VersionedParams(&options, metav1.ParameterCodec)
return req.Watch()
}
return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
}
func ReadConfigMapValue(clientset kubernetes.Interface, namespace string, name string, key string) (string, error) {
cm, err := clientset.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return "", err
}
value, ok := cm.Data[key]
if !ok {
return "", errors.InternalErrorf("Key %s was not found in the %s configMap.", key, name)
}
return value, nil
}
func getArtifactRepositoryRef(wfc *WorkflowController, configMapName string, key string) (*config.ArtifactRepository, error) {
configStr, err := ReadConfigMapValue(wfc.kubeclientset, wfc.namespace, configMapName, key)
if err != nil {
return nil, err
}
var config config.ArtifactRepository
err = yaml.Unmarshal([]byte(configStr), &config)
if err != nil {
return nil, errors.InternalWrapError(err)
}
return &config, nil
}