-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
configmap.go
70 lines (65 loc) · 1.62 KB
/
configmap.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
package plugin
import (
"fmt"
"strings"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
"github.com/argoproj/argo-workflows/v3/pkg/plugins/spec"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
func ToConfigMap(p *spec.Plugin) (*apiv1.ConfigMap, error) {
if err := p.Validate(); err != nil {
return nil, err
}
data, err := yaml.Marshal(p.Spec.Sidecar.Container)
if err != nil {
return nil, err
}
cm := &apiv1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-executor-plugin", p.Name),
Annotations: map[string]string{},
Labels: map[string]string{
common.LabelKeyConfigMapType: p.Kind,
},
},
Data: map[string]string{
"sidecar.container": string(data),
},
}
for k, v := range p.Annotations {
cm.Annotations[k] = v
}
for k, v := range p.Labels {
cm.Labels[k] = v
}
return cm, nil
}
func FromConfigMap(cm *apiv1.ConfigMap) (*spec.Plugin, error) {
p := &spec.Plugin{
TypeMeta: metav1.TypeMeta{
Kind: cm.Labels[common.LabelKeyConfigMapType],
},
ObjectMeta: metav1.ObjectMeta{
Name: strings.TrimSuffix(cm.Name, "-executor-plugin"),
Annotations: map[string]string{},
Labels: map[string]string{},
},
}
for k, v := range cm.Annotations {
p.Annotations[k] = v
}
for k, v := range cm.Labels {
p.Labels[k] = v
}
delete(p.Labels, common.LabelKeyConfigMapType)
if err := yaml.UnmarshalStrict([]byte(cm.Data["sidecar.container"]), &p.Spec.Sidecar.Container); err != nil {
return nil, err
}
return p, p.Validate()
}