-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
k8sapi.go
72 lines (60 loc) · 2.48 KB
/
k8sapi.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
package k8sapi
import (
"context"
"io"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"github.com/argoproj/argo-workflows/v3/errors"
"github.com/argoproj/argo-workflows/v3/workflow/common"
execcommon "github.com/argoproj/argo-workflows/v3/workflow/executor/common"
)
type K8sAPIExecutor struct {
client *k8sAPIClient
}
func NewK8sAPIExecutor(clientset kubernetes.Interface, config *restclient.Config, podName, namespace string) *K8sAPIExecutor {
client := newK8sAPIClient(clientset, config, podName, namespace)
return &K8sAPIExecutor{
client: client,
}
}
func (k *K8sAPIExecutor) GetFileContents(containerName string, sourcePath string) (string, error) {
return "", errors.Errorf(errors.CodeNotImplemented, "GetFileContents() is not implemented in the k8sapi executor.")
}
func (k *K8sAPIExecutor) CopyFile(containerName string, sourcePath string, destPath string, compressionLevel int) error {
return errors.Errorf(errors.CodeNotImplemented, "CopyFile() is not implemented in the k8sapi executor.")
}
func (k *K8sAPIExecutor) GetOutputStream(ctx context.Context, containerName string, combinedOutput bool) (io.ReadCloser, error) {
log.Infof("Getting output of %s", containerName)
if !combinedOutput {
log.Warn("non combined output unsupported")
}
return k.client.getLogsAsStream(ctx, containerName)
}
// Wait for the container to complete
func (k *K8sAPIExecutor) Wait(ctx context.Context, containerNames []string) error {
return k.Until(ctx, func(pod *corev1.Pod) bool {
return execcommon.AllTerminated(pod.Status.ContainerStatuses, containerNames) || pod.Status.Reason == common.ErrDeadlineExceeded
})
}
func (k *K8sAPIExecutor) Until(ctx context.Context, f func(pod *corev1.Pod) bool) error {
return k.client.until(ctx, f)
}
// Kill kills a list of containers first with a SIGTERM then with a SIGKILL after a grace period
func (k *K8sAPIExecutor) Kill(ctx context.Context, containerNames []string, terminationGracePeriodDuration time.Duration) error {
log.Infof("Killing containers %v", containerNames)
return k.client.killGracefully(ctx, containerNames, terminationGracePeriodDuration)
}
func (k *K8sAPIExecutor) ListContainerNames(ctx context.Context) ([]string, error) {
pod, err := k.client.getPod(ctx)
if err != nil {
return nil, err
}
var containerNames []string
for _, c := range pod.Status.ContainerStatuses {
containerNames = append(containerNames, c.Name)
}
return containerNames, nil
}