/
job.go
156 lines (139 loc) · 4.08 KB
/
job.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
// Copyright (c) 2016 Pulcy.
//
// 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 kubernetes
import (
"encoding/json"
"fmt"
"strings"
"time"
k8s "github.com/YakLabs/k8s-client"
)
const (
jobStartTimeout = time.Minute
)
// Job is a wrapper for a kubernetes batch.Job that implements
// scheduler.UnitData.
type Job struct {
k8s.Job
}
// Name returns a name of the resource
func (ds *Job) Name() string {
return ds.Job.ObjectMeta.Name
}
// Namespace returns the namespace the resource should be added to.
func (ds *Job) Namespace() string {
return ds.Job.ObjectMeta.Namespace
}
// GetCurrent loads the current version of the object on the cluster
func (ds *Job) GetCurrent(cs k8s.Client) (interface{}, error) {
x, err := cs.GetJob(ds.Namespace(), ds.Name())
if err != nil {
return nil, maskAny(err)
}
return &Job{*x}, nil
}
// IsEqual returns true of all values configured in myself are the same in the other object.
func (ds *Job) IsEqual(other interface{}) ([]string, bool, error) {
ods, ok := other.(*Job)
if !ok {
return nil, false, maskAny(fmt.Errorf("Expected other to by *Job"))
}
if diffs, eq := isSameObjectMeta(ds.Job.ObjectMeta, ods.Job.ObjectMeta); !eq {
return diffs, false, nil
}
diffs, eq := isSameJobSpec(ds.Spec, ods.Spec)
return diffs, eq, nil
}
func isSameJobSpec(self, other *k8s.JobSpec) ([]string, bool) {
if diffs, eq := isSamePodTemplateSpec(&self.Template, &other.Template, "controller-uid", "job-name"); !eq {
return diffs, eq
}
diffs, eq := diff(self, other, func(path string) bool {
switch path {
case ".Selector":
return true
}
if strings.HasPrefix(path, ".Template") {
return true
}
return false
})
return diffs, eq
}
// IsValidState returns true if the current state of the resource on the cluster is OK.
func (ds *Job) IsValidState(cs k8s.Client) (bool, string, error) {
current, err := cs.GetJob(ds.Namespace(), ds.Name())
if err != nil {
return false, "", maskAny(err)
}
ok := false
status := current.Status
msg := ""
if status != nil {
ok = status.Failed == 0
msg = fmt.Sprintf("%d pods active, %d succeeded, %d failed", status.Active, status.Succeeded, status.Failed)
}
return ok, msg, nil
}
// ObjectMeta returns the ObjectMeta of the resource.
func (ds *Job) ObjectMeta() *k8s.ObjectMeta {
return &ds.Job.ObjectMeta
}
// Content returns a JSON representation of the resource.
func (ds *Job) Content() string {
x := ds.Job
x.Status = nil
return mustRender(x)
}
// Destroy deletes the job from the cluster.
func (ds *Job) Destroy(cs k8s.Client, events chan string) error {
// Fetch current deployment
current, err := cs.GetJob(ds.Namespace(), ds.Name())
if err != nil {
return maskAny(err)
}
labelSelector := createLabelSelector(current.ObjectMeta)
// Delete deployment itself
events <- "deleting job"
if err := cs.DeleteJob(ds.Namespace(), ds.Name()); err != nil {
return maskAny(err)
}
// Delete created pods.
events <- "deleting pods"
if err := deletePods(cs, ds.Namespace(), labelSelector); err != nil {
return maskAny(err)
}
return nil
}
// Start creates/updates the job
func (ds *Job) Start(cs k8s.Client, events chan string) error {
current, err := cs.GetJob(ds.Namespace(), ds.Name())
if err == nil {
// Update
events <- "updating"
updateMetadataFromCurrent(ds.ObjectMeta(), current.ObjectMeta)
if _, err := cs.UpdateJob(ds.Namespace(), &ds.Job); err != nil {
m, _ := json.Marshal(err)
fmt.Printf("Error=%s\n", string(m))
return maskAny(err)
}
} else {
// Create
events <- "creating"
if _, err := cs.CreateJob(ds.Namespace(), &ds.Job); err != nil {
return maskAny(err)
}
}
return nil
}