Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark ephemeral pods created as part of kanister functions with JobID label #2778

Merged
merged 22 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion pkg/function/kube_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package function

import (
"context"
"path"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -33,7 +35,9 @@ import (
)

const (
jobPrefix = "kanister-job-"
jobPrefix = "kanister-job-"
jobIDSuffix = "JobID"

// KubeTaskFuncName gives the function name
KubeTaskFuncName = "KubeTask"
KubeTaskNamespaceArg = "namespace"
Expand Down Expand Up @@ -64,6 +68,9 @@ func kubeTask(ctx context.Context, cli kubernetes.Interface, namespace, image st
Command: command,
PodOverride: podOverride,
}
// Mark labels to pods with prefix `kanister.io`. Add the jobID as reference to the origin for the pod.
mabhi marked this conversation as resolved.
Show resolved Hide resolved
validateFunc := validateLabelKeyIsPresentFunc(consts.LabelPrefix)
kube.AddLabelsToPodOptionsFromContext(ctx, options, path.Join(consts.LabelPrefix, jobIDSuffix), validateFunc)
mabhi marked this conversation as resolved.
Show resolved Hide resolved

pr := kube.NewPodRunner(cli, options)
podFunc := kubeTaskPodFunc()
Expand Down Expand Up @@ -93,6 +100,23 @@ func kubeTaskPodFunc() func(ctx context.Context, pc kube.PodController) (map[str
}
}

// validateLabelKeyIsPresentFunc: This is a helper validation function used by kubetask to validate the presence of
// label key. Result of this is used to add target label selector to the pod
func validateLabelKeyIsPresentFunc(keyPrefix string) func(ctx context.Context) (bool, string) {
return func(ctx context.Context) (bool, string) {
fields := field.FromContext(ctx)
if fields == nil {
return false, ""
}
for _, f := range fields.Fields() {
if strings.HasPrefix(f.Key(), keyPrefix) {
return true, f.Value().(string)
}
}
return false, ""
}
}

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
Expand Down
95 changes: 95 additions & 0 deletions pkg/kube/pod_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ import (
"context"
"os"

"github.com/pkg/errors"
. "gopkg.in/check.v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/testing"

"github.com/kanisterio/kanister/pkg/consts"
"github.com/kanisterio/kanister/pkg/field"
"path"
)

type PodRunnerTestSuite struct{}
Expand Down Expand Up @@ -109,9 +114,99 @@ func (s *PodRunnerTestSuite) TestPodRunnerForSuccessCase(c *C) {
cancel()
}

// TestPodRunnerWithJobIDDebugLabelForSuccessCase: This test adds a debug entry (kanister.io/JobID) into the context and verifies the
// pod got created with corresponding label using the entry or not.
func (s *PodRunnerTestSuite) TestPodRunnerWithJobIDDebugLabelForSuccessCase(c *C) {
randomUUID := "xyz123"
for _, tc := range []struct {
name string
validateFn func(_ context.Context) (bool, string)
afterPodRunTestFn func(labelKey, labelValue string, ch chan struct{}) func(ctx context.Context, pc PodController) (map[string]interface{}, error)
}{
{
name: "test key not present in context",
validateFn: func(_ context.Context) (bool, string) {
return false, ""
},
afterPodRunTestFn: afterPodRunTestKeyAbsentFunc,
},
{
name: "test key is present in context",
validateFn: func(_ context.Context) (bool, string) {
return true, randomUUID
},
afterPodRunTestFn: afterPodRunTestKeyPresentFunc,
},
} {
ctx, cancel := context.WithCancel(context.Background())
ctx = field.Context(ctx, consts.LabelPrefix+"JobID", randomUUID)
ctx = field.Context(ctx, "some-test-key", "some-test-value")

cli := fake.NewSimpleClientset()
cli.PrependReactor("create", "pods", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
return false, nil, nil
})
cli.PrependReactor("get", "pods", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
p := &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
}
return true, p, nil
})
po := &PodOptions{
Namespace: podRunnerNS,
Name: podName,
Command: []string{"sh", "-c", "tail -f /dev/null"},
}
deleted := make(chan struct{})
cli.PrependReactor("delete", "pods", func(action testing.Action) (handled bool, ret runtime.Object, err error) {
c.Log("Pod deleted due to Context Cancelled")
close(deleted)
return true, nil, nil
})
var targetKey = path.Join(consts.LabelPrefix, "JobID")
mabhi marked this conversation as resolved.
Show resolved Hide resolved
AddLabelsToPodOptionsFromContext(ctx, po, targetKey, tc.validateFn)
pr := NewPodRunner(cli, po)
errorCh := make(chan error)
go func() {
_, err := pr.Run(ctx, tc.afterPodRunTestFn(targetKey, randomUUID, deleted))
errorCh <- err
}()
deleted <- struct{}{}
c.Assert(<-errorCh, IsNil)
cancel()
}
}

func makePodRunnerTestFunc(ch chan struct{}) func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
return func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
<-ch
return nil, nil
}
}

func afterPodRunTestKeyPresentFunc(labelKey, labelValue string, ch chan struct{}) func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
return func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
<-ch
value, ok := pc.Pod().Labels[labelKey]
if !ok {
return nil, errors.New("Key not present")
}
if value != labelValue {
return nil, errors.New("Value mismatch")
}
return nil, nil
}
}

func afterPodRunTestKeyAbsentFunc(labelKey, labelValue string, ch chan struct{}) func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
return func(ctx context.Context, pc PodController) (map[string]interface{}, error) {
<-ch
_, present := pc.Pod().Labels[labelKey]
if present {
return nil, errors.New("Key should not be present")
}
return nil, nil
}
}
18 changes: 18 additions & 0 deletions pkg/kube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,21 @@ func PVCContainsReadOnlyAccessMode(pvc *corev1.PersistentVolumeClaim) bool {

return false
}

// AddLabelsToPodOptionsFromContext adds additional label selector to `PodOptions`,
// provided the validationFunc passes successfully.
func AddLabelsToPodOptionsFromContext(
ctx context.Context,
options *PodOptions,
targetKey string,
validateFn func(context.Context) (bool, string),
) {
ok, value := validateFn(ctx)
if !ok {
return
}
if options.Labels == nil {
options.Labels = make(map[string]string)
}
options.Labels[targetKey] = value
}