forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
67 lines (59 loc) · 1.69 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
package config
import (
"context"
"fmt"
"strings"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
)
type Controller interface {
Get(context.Context) (*Config, error)
GetName() string
}
type controller struct {
namespace string
// name of the config map
configMap string
kubeclientset kubernetes.Interface
}
func NewController(namespace, name string, kubeclientset kubernetes.Interface) Controller {
return &controller{
namespace: namespace,
configMap: name,
kubeclientset: kubeclientset,
}
}
func parseConfigMap(cm *apiv1.ConfigMap, config *Config) error {
// The key in the configmap to retrieve workflow configuration from.
// Content encoding is expected to be YAML.
rawConfig, ok := cm.Data["config"]
if ok && len(cm.Data) != 1 {
return fmt.Errorf("if you have an item in your config map named 'config', you must only have one item")
}
if !ok {
for name, value := range cm.Data {
if strings.Contains(value, "\n") {
// this mucky code indents with two spaces
rawConfig = rawConfig + name + ":\n " + strings.Join(strings.Split(strings.Trim(value, "\n"), "\n"), "\n ") + "\n"
} else {
rawConfig = rawConfig + name + ": " + value + "\n"
}
}
}
err := yaml.UnmarshalStrict([]byte(rawConfig), config)
return err
}
func (cc *controller) Get(ctx context.Context) (*Config, error) {
config := &Config{}
cmClient := cc.kubeclientset.CoreV1().ConfigMaps(cc.namespace)
cm, err := cmClient.Get(ctx, cc.configMap, metav1.GetOptions{})
if err != nil {
return nil, err
}
return config, parseConfigMap(cm, config)
}
func (cc *controller) GetName() string {
return cc.configMap
}