/
deploy.go
110 lines (97 loc) · 3.23 KB
/
deploy.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
package service
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/unanet/eve-sch/internal/config"
"github.com/unanet/eve/pkg/eve"
)
// Public CONST
const TimeoutExitCode = 124
func intContains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func expandSuccessExitCodes(successExitCodes string) ([]int, error) {
var r []int
var last int
for _, part := range strings.Split(successExitCodes, ",") {
if i := strings.Index(part[1:], "-"); i == -1 {
n, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("success_exit_code parse error, parts are not a valid int: %s", err.Error())
}
if len(r) > 0 {
if last == n {
return nil, fmt.Errorf("success_exit_code parse error, duplicate value: %d", n)
} else if last > n {
return nil, fmt.Errorf("success_exit_code parse error, values not ordered: %d", n)
}
}
r = append(r, n)
last = n
} else {
n1, err := strconv.Atoi(part[:i+1])
if err != nil {
return nil, fmt.Errorf("success_exit_code parse error, parts are not a valid int: %s", err.Error())
}
n2, err := strconv.Atoi(part[i+2:])
if err != nil {
return nil, fmt.Errorf("success_exit_code parse error, parts are not a valid int: %s", err.Error())
}
if n2 < n1+2 {
return nil, fmt.Errorf("success_exit_code parse error, invalid range: %s", part)
}
if len(r) > 0 {
if last == n1 {
return nil, fmt.Errorf("success_exit_code parse error, duplicate value: %d", n1)
} else if last > n1 {
return nil, fmt.Errorf("success_exit_code parse error, values not ordered: %d", n1)
}
}
for i = n1; i <= n2; i++ {
r = append(r, i)
}
last = n2
}
}
return r, nil
}
// deploy is the entry point for Eve Jobs and Eve Services
func (s *Scheduler) deploy(ctx context.Context, deployment eve.DeploymentSpec, plan *eve.NSDeploymentPlan) {
failNLog := s.failAndLogFn(ctx, deployment.GetName(), deployment.GetArtifact(), plan)
logFn := s.logMessageFn(deployment.GetName(), deployment.GetArtifact(), plan)
if err := s.deployCRDs(ctx, deployment, plan); err != nil {
failNLog(err, "failed to deploy resource definitions")
return
}
// We wait/watch for 1 successful pod to come up
if err := s.watchPods(ctx, deployment, plan.Namespace.Name); err != nil {
failNLog(err, "an error occurred while watching k8s deployment pods")
return
}
if deployment.GetExitCode() != 0 {
if deployment.GetExitCode() == TimeoutExitCode {
logFn("timeout of %d seconds exceeded while waiting for the deployment to start", config.GetConfig().K8sDeployTimeoutSec)
} else {
logFn("pod failed to start and returned a non zero exit code: %d", deployment.GetExitCode())
}
validExitCodes, err := expandSuccessExitCodes(deployment.GetSuccessCodes())
if err != nil {
failNLog(err, "an error occurred parsing valid exit codes for the deployment")
return
}
if !intContains(validExitCodes, deployment.GetExitCode()) {
deployment.SetDeployResult(eve.DeployArtifactResultFailed)
}
}
// if we've set it to a failure above somewhere, we don't want to now state it's succeeded.
if deployment.GetDeployResult() == eve.DeployArtifactResultNoop {
deployment.SetDeployResult(eve.DeployArtifactResultSuccess)
}
}