This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
243 lines (219 loc) · 7.22 KB
/
run.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright © 2017 RooFoods LTD
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pipeline
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"io/ioutil"
"k8s.io/api/core/v1"
k8errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
"log"
"time"
)
type runCmdFlagsStruct struct {
StepName string
BucketName string
ImageTag string
StepBranch string
StepVersion string
OverrideInputs bool
TailLogs bool
Secrets []string
Env []string
DeletePollInterval time.Duration
StartTimeout time.Duration
}
const defaultDeletePollInterval = 2 * time.Second
const deleteTimeout = 120 * time.Second
const defaultStartTimeout = 10 * time.Minute
var runCmdFlags *runCmdFlagsStruct
var clientset kubernetes.Interface
var logFatalf = log.Fatalf
var runCmd = &cobra.Command{
Use: "run [pipeline_yaml]",
Short: "Run a pipeline or a pipeline step",
Args: cobra.ExactArgs(1),
Long: `Run a pipeline (or a single step) on the Kubernetes cluster.
Example:
$ paddle pipeline run test_pipeline.yaml
`,
Run: func(cmd *cobra.Command, args []string) {
runPipeline(args[0], runCmdFlags)
},
}
func init() {
runCmdFlags = &runCmdFlagsStruct{}
runCmd.Flags().StringVarP(&runCmdFlags.StepName, "step", "s", "", "Single step to execute")
runCmd.Flags().StringVarP(&runCmdFlags.BucketName, "bucket", "b", "", "Bucket name")
runCmd.Flags().StringVarP(&runCmdFlags.ImageTag, "tag", "t", "", "Image tag (overrides the one defined in the pipeline)")
runCmd.Flags().StringVarP(&runCmdFlags.StepBranch, "step-branch", "B", "", "Step branch (overrides the one defined in the pipeline)")
runCmd.Flags().StringVarP(&runCmdFlags.StepVersion, "step-version", "V", "", "Step version (overrides the one defined in the pipeline)")
runCmd.Flags().BoolVarP(&runCmdFlags.TailLogs, "logs", "l", true, "Tail logs")
runCmd.Flags().BoolVarP(&runCmdFlags.OverrideInputs, "override-inputs", "I", false, "Override input version/branch (only makes sense to use with -B or -V)")
runCmd.Flags().StringSliceVarP(&runCmdFlags.Secrets, "secret", "S", []string{}, "Secret to pull into the environment (in the form ENV_VAR:secret_store:key_name)")
runCmd.Flags().StringSliceVarP(&runCmdFlags.Env, "env", "e", []string{}, "Environment variables to set (in the form name:value)")
runCmdFlags.DeletePollInterval = defaultDeletePollInterval
runCmdFlags.StartTimeout = defaultStartTimeout
config, err := getKubernetesConfig()
if err != nil {
panic(err.Error())
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
}
func runPipeline(path string, flags *runCmdFlagsStruct) {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err.Error())
}
pipeline := parsePipeline(data)
if flags.BucketName != "" {
pipeline.Bucket = flags.BucketName
}
for _, step := range pipeline.Steps {
if flags.StepName != "" && step.Step != flags.StepName {
continue
}
if flags.ImageTag != "" {
step.OverrideTag(flags.ImageTag)
}
if flags.StepBranch != "" {
step.OverrideBranch(flags.StepBranch, flags.OverrideInputs)
}
if flags.StepVersion != "" {
step.OverrideVersion(flags.StepVersion, flags.OverrideInputs)
}
err = runPipelineStep(pipeline, &step, flags)
if err != nil {
logFatalf("[paddle] %s", err.Error())
}
}
}
func runPipelineStep(pipeline *PipelineDefinition, step *PipelineDefinitionStep, flags *runCmdFlagsStruct) error {
log.Printf("[paddle] Running step %s", step.Step)
podDefinition := NewPodDefinition(pipeline, step)
podDefinition.parseSecrets(flags.Secrets)
podDefinition.parseEnv(flags.Env)
stepPodBuffer := podDefinition.compile()
pod := &v1.Pod{}
yaml.NewYAMLOrJSONDecoder(stepPodBuffer, 4096).Decode(pod)
pods := clientset.CoreV1().Pods(pipeline.Namespace)
err := deleteAndWait(clientset, podDefinition, flags)
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
watch, err := Watch(ctx, clientset, pod)
if err != nil {
return err
}
pod, err = pods.Create(pod)
if err != nil {
return err
}
containers := make(map[string]bool)
go func() {
time.Sleep(flags.StartTimeout)
if len(containers) < len(pod.Spec.Containers) {
cancel()
}
}()
for {
select {
case e := <-watch:
switch e.Type {
case Added:
log.Printf("[paddle] Container %s/%s starting", pod.Name, e.Container)
containers[e.Container] = true
if flags.TailLogs {
TailLogs(ctx, clientset, e.Pod, e.Container)
}
case Deleted:
log.Println("[paddle] Pod deleted")
return errors.New("Pod was deleted unexpectedly.")
case Removed:
log.Printf("[paddle] Container removed: %s", e.Container)
continue
case Completed:
log.Printf("[paddle] Pod execution completed")
return nil
case Failed:
var msg string
if e.Container != "" {
if e.Message != "" {
msg = fmt.Sprintf("Container %s/%s failed: '%s'", pod.Name, e.Container, e.Message)
} else {
msg = fmt.Sprintf("Container %s/%s failed", pod.Name, e.Container)
}
_, present := containers[e.Container]
if !present && flags.TailLogs { // container died before being added
TailLogs(ctx, clientset, e.Pod, e.Container)
time.Sleep(3 * time.Second) // give it time to tail logs
}
} else {
msg = "Pod failed"
}
return errors.New(msg)
}
case <-ctx.Done():
pod, _ := pods.Get(podDefinition.PodName, metav1.GetOptions{})
reason := "Timed out waiting for pod to start. Cluster might not have sufficient resources."
if pod != nil {
for _, container := range pod.Status.ContainerStatuses {
if container.State.Waiting != nil {
reason = container.State.Waiting.Message
}
}
}
pods.Delete(podDefinition.PodName, &metav1.DeleteOptions{})
return errors.New(reason)
}
}
log.Printf("[paddle] Finishing pod execution")
return nil
}
func deleteAndWait(c kubernetes.Interface, podDefinition *PodDefinition, flags *runCmdFlagsStruct) error {
pods := clientset.CoreV1().Pods(podDefinition.Namespace)
deleting := false
var gracePeriod int64
opts := metav1.DeleteOptions{GracePeriodSeconds: &gracePeriod}
err := wait.PollImmediate(flags.DeletePollInterval, deleteTimeout, func() (bool, error) {
var err error
err = pods.Delete(podDefinition.PodName, &opts)
if err != nil {
if k8errors.IsNotFound(err) {
if deleting {
log.Printf("[paddle] deleted pod %s", podDefinition.PodName)
}
return true, nil
} else {
return true, err
}
}
if !deleting {
log.Printf("[paddle] deleting pod %s", podDefinition.PodName)
deleting = true
}
return false, nil
})
return err
}