/
kube_task.go
151 lines (133 loc) · 4.48 KB
/
kube_task.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
// Copyright 2019 The Kanister Authors.
//
// 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 function
import (
"context"
"path"
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
kanister "github.com/kanisterio/kanister/pkg"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/output"
"github.com/kanisterio/kanister/pkg/param"
"github.com/kanisterio/kanister/pkg/progress"
)
const (
jobPrefix = "kanister-job-"
jobIDSuffix = "JobID"
// KubeTaskFuncName gives the function name
KubeTaskFuncName = "KubeTask"
KubeTaskNamespaceArg = "namespace"
KubeTaskImageArg = "image"
KubeTaskCommandArg = "command"
KubeTaskPodOverrideArg = "podOverride"
)
func init() {
_ = kanister.Register(&kubeTaskFunc{})
}
var _ kanister.Func = (*kubeTaskFunc)(nil)
type kubeTaskFunc struct {
progressPercent string
}
func (*kubeTaskFunc) Name() string {
return KubeTaskFuncName
}
func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image string, command []string, podOverride crv1alpha1.JSONMap) (map[string]interface{}, error) {
options := &kube.PodOptions{
Namespace: namespace,
GenerateName: jobPrefix,
Image: image,
Command: command,
PodOverride: podOverride,
}
// Mark pod with label having key `kanister.io/JobID`, the value of which is a reference to the origin of the pod.
kube.AddLabelsToPodOptionsFromContext(ctx, options, path.Join(consts.LabelPrefix, jobIDSuffix))
pr := kube.NewPodRunner(cli, options)
podFunc := kubeTaskPodFunc()
return pr.Run(ctx, podFunc)
}
func kubeTaskPodFunc() func(ctx context.Context, pc kube.PodController) (map[string]interface{}, error) {
return func(ctx context.Context, pc kube.PodController) (map[string]interface{}, error) {
if err := pc.WaitForPodReady(ctx); err != nil {
return nil, errors.Wrapf(err, "Failed while waiting for Pod %s to be ready", pc.PodName())
}
ctx = field.Context(ctx, consts.LogKindKey, consts.LogKindDatapath)
// Fetch logs from the pod
r, err := pc.StreamPodLogs(ctx)
if err != nil {
return nil, errors.Wrapf(err, "Failed to fetch logs from the pod")
}
out, err := output.LogAndParse(ctx, r)
if err != nil {
return nil, err
}
// Wait for pod completion
if err := pc.WaitForPodCompletion(ctx); err != nil {
return nil, errors.Wrapf(err, "Failed while waiting for Pod %s to complete", pc.PodName())
}
return out, err
}
}
func (ktf *kubeTaskFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {
// Set progress percent
ktf.progressPercent = progress.StartedPercent
defer func() { ktf.progressPercent = progress.CompletedPercent }()
var namespace, image string
var command []string
var err error
if err = Arg(args, KubeTaskImageArg, &image); err != nil {
return nil, err
}
if err = Arg(args, KubeTaskCommandArg, &command); err != nil {
return nil, err
}
if err = OptArg(args, KubeTaskNamespaceArg, &namespace, ""); err != nil {
return nil, err
}
podOverride, err := GetPodSpecOverride(tp, args, KubeTaskPodOverrideArg)
if err != nil {
return nil, err
}
cli, err := kube.NewClient()
if err != nil {
return nil, errors.Wrapf(err, "Failed to create Kubernetes client")
}
return kubeTask(ctx, cli, namespace, image, command, podOverride)
}
func (*kubeTaskFunc) RequiredArgs() []string {
return []string{
KubeTaskImageArg,
KubeTaskCommandArg,
}
}
func (*kubeTaskFunc) Arguments() []string {
return []string{
KubeTaskImageArg,
KubeTaskCommandArg,
KubeTaskNamespaceArg,
KubeTaskPodOverrideArg,
}
}
func (k *kubeTaskFunc) ExecutionProgress() (crv1alpha1.PhaseProgress, error) {
metav1Time := metav1.NewTime(time.Now())
return crv1alpha1.PhaseProgress{
ProgressPercent: k.progressPercent,
LastTransitionTime: &metav1Time,
}, nil
}