-
Notifications
You must be signed in to change notification settings - Fork 31
/
step_spec.go
165 lines (155 loc) · 6.08 KB
/
step_spec.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
package v1alpha1
import (
"encoding/json"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
)
type StepSpec struct {
// +kubebuilder:default=default
Name string `json:"name" protobuf:"bytes,6,opt,name=name"`
Cat *Cat `json:"cat,omitempty" protobuf:"bytes,15,opt,name=cat"`
Container *Container `json:"container,omitempty" protobuf:"bytes,1,opt,name=container"`
Handler *Handler `json:"handler,omitempty" protobuf:"bytes,7,opt,name=handler"`
Git *Git `json:"git,omitempty" protobuf:"bytes,12,opt,name=git"`
Filter Filter `json:"filter,omitempty" protobuf:"bytes,8,opt,name=filter,casttype=Filter"`
Map Map `json:"map,omitempty" protobuf:"bytes,9,opt,name=map,casttype=Map"`
Group *Group `json:"group,omitempty" protobuf:"bytes,11,opt,name=group"`
Flatten *Flatten `json:"flatten,omitempty" protobuf:"bytes,25,opt,name=flatten"`
Expand *Expand `json:"expand,omitempty" protobuf:"bytes,26,opt,name=expand"`
Replicas *uint32 `json:"replicas,omitempty" protobuf:"varint,23,opt,name=replicas"`
Scale *Scale `json:"scale,omitempty" protobuf:"bytes,24,opt,name=scale"`
// +patchStrategy=merge
// +patchMergeKey=name
Sources Sources `json:"sources,omitempty" protobuf:"bytes,3,rep,name=sources"`
// +patchStrategy=merge
// +patchMergeKey=name
Sinks []Sink `json:"sinks,omitempty" protobuf:"bytes,4,rep,name=sinks"`
// +kubebuilder:default=OnFailure
RestartPolicy corev1.RestartPolicy `json:"restartPolicy,omitempty" protobuf:"bytes,5,opt,name=restartPolicy,casttype=k8s.io/api/core/v1.RestartPolicy"`
Terminator bool `json:"terminator,omitempty" protobuf:"varint,10,opt,name=terminator"` // if this step terminates, terminate all steps in the pipeline
// +patchStrategy=merge
// +patchMergeKey=name
Volumes []corev1.Volume `json:"volumes,omitempty" protobuf:"bytes,13,rep,name=volumes"`
// +kubebuilder:default=pipeline
ServiceAccountName string `json:"serviceAccountName,omitempty" protobuf:"bytes,14,opt,name=serviceAccountName"`
Metadata *Metadata `json:"metadata,omitempty" protobuf:"bytes,16,opt,name=metadata"`
NodeSelector map[string]string `json:"nodeSelector,omitempty" protobuf:"bytes,17,rep,name=nodeSelector"`
Affinity *corev1.Affinity `json:"affinity,omitempty" protobuf:"bytes,18,opt,name=affinity"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty" protobuf:"bytes,19,rep,name=tolerations"`
}
// +kubebuilder:skipversion
type GetPodSpecReq struct {
PipelineName string `protobuf:"bytes,1,opt,name=pipelineName"`
Namespace string `protobuf:"bytes,2,opt,name=namespace"`
Replica int32 `protobuf:"varint,3,opt,name=replica"`
ImageFormat string `protobuf:"bytes,4,opt,name=imageFormat"`
RunnerImage string `protobuf:"bytes,5,opt,name=runnerImage"`
PullPolicy corev1.PullPolicy `protobuf:"bytes,6,opt,name=pullPolicy,casttype=k8s.io/api/core/v1.PullPolicy"`
UpdateInterval time.Duration `protobuf:"varint,7,opt,name=updateInterval,casttype=time.Duration"`
}
func (in *StepSpec) GetPodSpec(req GetPodSpecReq) corev1.PodSpec {
volume := corev1.Volume{
Name: "var-run-argo-dataflow",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}
data, _ := json.Marshal(in)
volumeMounts := []corev1.VolumeMount{{Name: volume.Name, MountPath: PathVarRun}}
envVars := []corev1.EnvVar{
{Name: EnvPipelineName, Value: req.PipelineName},
{Name: EnvNamespace, Value: req.Namespace},
{Name: EnvReplica, Value: strconv.Itoa(int(req.Replica))},
{Name: EnvStepSpec, Value: string(data)},
{Name: EnvUpdateInterval, Value: req.UpdateInterval.String()},
}
return corev1.PodSpec{
Volumes: append(in.Volumes, volume),
RestartPolicy: in.RestartPolicy,
NodeSelector: in.NodeSelector,
ServiceAccountName: in.ServiceAccountName,
SecurityContext: &corev1.PodSecurityContext{
RunAsNonRoot: pointer.BoolPtr(true),
RunAsUser: pointer.Int64Ptr(9653),
},
Affinity: in.Affinity,
Tolerations: in.Tolerations,
InitContainers: []corev1.Container{
{
Name: CtrInit,
Image: req.RunnerImage,
ImagePullPolicy: req.PullPolicy,
Args: []string{"init"},
Env: envVars,
VolumeMounts: volumeMounts,
Resources: SmallResourceRequirements,
},
},
Containers: []corev1.Container{
{
Name: CtrSidecar,
Image: req.RunnerImage,
ImagePullPolicy: req.PullPolicy,
Args: []string{"sidecar"},
Env: envVars,
VolumeMounts: volumeMounts,
Resources: SmallResourceRequirements,
Ports: []corev1.ContainerPort{
{ContainerPort: 3569},
},
},
in.GetContainer(
req.ImageFormat,
req.RunnerImage,
req.PullPolicy,
corev1.VolumeMount{Name: "var-run-argo-dataflow", MountPath: "/var/run/argo-dataflow"},
),
},
}
}
func (in *StepSpec) GetIn() *Interface {
if in.Container != nil {
return in.Container.GetIn()
}
return DefaultInterface
}
func (in *StepSpec) GetContainer(imageFormat, runnerImage string, policy corev1.PullPolicy, mnt corev1.VolumeMount) corev1.Container {
return in.getType().getContainer(getContainerReq{
imageFormat: imageFormat,
runnerImage: runnerImage,
imagePullPolicy: policy,
volumeMount: mnt,
})
}
func (in *StepSpec) getType() containerSupplier {
if x := in.Cat; x != nil {
return x
} else if x := in.Container; x != nil {
return x
} else if x := in.Expand; x != nil {
return x
} else if x := in.Filter; x != "" {
return x
} else if x := in.Flatten; x != nil {
return x
} else if x := in.Git; x != nil {
return x
} else if x := in.Group; x != nil {
return x
} else if x := in.Handler; x != nil {
return x
} else if x := in.Map; x != "" {
return x
} else {
panic("invalid step spec")
}
}
func (in *StepSpec) CalculateReplicas(pending int) int {
if in.Replicas != nil {
return int(*in.Replicas)
}
if in.Scale == nil {
return 1
}
return in.Scale.Calculate(pending)
}