/
manager.go
138 lines (123 loc) · 3.99 KB
/
manager.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 jobs
import (
"context"
"fmt"
"strings"
"github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils"
batchV1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type Manager interface {
CreateAndWaitForJobToComplete(ctx context.Context, jobs *batchV1.Job) (*batchV1.Job, error)
CreateJobAndWaitForJobToRun(ctx context.Context, jobs *batchV1.Job) (*batchV1.Job, error)
DeleteAndWaitTillJobIsDeleted(ctx context.Context, jobs *batchV1.Job) error
}
func NewManager(k8sClient client.Client) Manager {
return &defaultManager{k8sClient: k8sClient}
}
type defaultManager struct {
k8sClient client.Client
}
func (m *defaultManager) CreateJobAndWaitForJobToRun(ctx context.Context,
jobs *batchV1.Job) (*batchV1.Job, error) {
err := m.k8sClient.Create(ctx, jobs)
if err != nil {
return nil, err
}
observedJob := &batchV1.Job{}
return observedJob, wait.PollUntil(utils.PollIntervalShort, func() (bool, error) {
podList := &v1.PodList{}
err = m.k8sClient.List(ctx, podList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(jobs.Spec.Template.Labels),
})
if err != nil {
return false, err
}
if err := m.k8sClient.Get(ctx, utils.NamespacedName(jobs), observedJob); err != nil {
return false, err
}
if observedJob.Status.Succeeded > 0 || observedJob.Status.Failed > 0 {
return false, fmt.Errorf("some jobs has either"+
" succeeded or failed :%v", observedJob.Status)
} else if observedJob.Status.Active > 0 {
var count int
for _, pod := range podList.Items {
if pod.Status.Phase == v1.PodRunning && strings.Contains(pod.Name, jobs.Name) {
count++
}
}
return count == int(*jobs.Spec.Parallelism), nil
}
return false, nil
}, ctx.Done())
}
func (m *defaultManager) CreateAndWaitForJobToComplete(ctx context.Context,
jobs *batchV1.Job) (*batchV1.Job, error) {
err := m.k8sClient.Create(ctx, jobs)
if err != nil {
return nil, err
}
observedJob := &batchV1.Job{}
return observedJob, wait.PollUntil(utils.PollIntervalShort, func() (bool, error) {
if err := m.k8sClient.Get(ctx, utils.NamespacedName(jobs), observedJob); err != nil {
return false, err
}
if observedJob.Status.Failed > 0 {
return false, fmt.Errorf("failed to execute job :%v", observedJob.Status)
} else if observedJob.Status.Succeeded == (*jobs.Spec.Parallelism) {
return true, nil
}
return false, nil
}, ctx.Done())
}
func (m *defaultManager) DeleteAndWaitTillJobIsDeleted(ctx context.Context, job *batchV1.Job) error {
err := m.k8sClient.Delete(ctx, job)
if err != nil {
return err
}
observedJob := &batchV1.Job{}
err = wait.PollUntil(utils.PollIntervalShort, func() (bool, error) {
if err := m.k8sClient.Get(ctx, utils.NamespacedName(job), observedJob); err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
}, ctx.Done())
if err != nil {
return err
}
// Deleting the Job will not removed the Job Pods, they have to be manually deleted
jobPods := &v1.PodList{}
err = m.k8sClient.List(ctx, jobPods, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(job.ObjectMeta.Labels),
Namespace: v1.NamespaceAll,
})
if err != nil {
return err
}
for _, pod := range jobPods.Items {
err = m.k8sClient.Delete(ctx, &pod)
if err != nil {
return err
}
}
return nil
}