From a10055bd3529f3a14f16bb4111bf827297d722d6 Mon Sep 17 00:00:00 2001 From: Daniil Fedotov Date: Thu, 5 Dec 2024 15:15:38 -0500 Subject: [PATCH] fix: use controller namespace by default in MultiContainerRun --- pkg/function/multi_container_run.go | 10 ++++ pkg/function/multi_container_run_test.go | 66 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/pkg/function/multi_container_run.go b/pkg/function/multi_container_run.go index 7dffab95e0..316f046849 100644 --- a/pkg/function/multi_container_run.go +++ b/pkg/function/multi_container_run.go @@ -242,9 +242,19 @@ func (ktpf *multiContainerRunFunc) Exec(ctx context.Context, tp param.TemplatePa if err = OptArg(args, MultiContainerRunInitCommandArg, &ktpf.initCommand, nil); err != nil { return nil, err } + if err = OptArg(args, MultiContainerRunNamespaceArg, &ktpf.namespace, ""); err != nil { return nil, err } + + if ktpf.namespace == "" { + controllerNamespace, err := kube.GetControllerNamespace() + if err != nil { + return nil, errkit.Wrap(err, "Failed to get controller namespace") + } + ktpf.namespace = controllerNamespace + } + if err = OptArg(args, MultiContainerRunVolumeMediumArg, &ktpf.storageMedium, ""); err != nil { return nil, err } diff --git a/pkg/function/multi_container_run_test.go b/pkg/function/multi_container_run_test.go index 23237101c4..005323150d 100644 --- a/pkg/function/multi_container_run_test.go +++ b/pkg/function/multi_container_run_test.go @@ -202,3 +202,69 @@ func (s *MultiContainerRunSuite) TestMultiContainerRunWithInit(c *C) { } } } + +func multiContainerRunPhaseWithoutNamespace() crv1alpha1.BlueprintPhase { + return crv1alpha1.BlueprintPhase{ + Name: "testMultiContainerRun", + Func: MultiContainerRunFuncName, + Args: map[string]interface{}{ + MultiContainerRunBackgroundImageArg: consts.LatestKanisterToolsImage, + MultiContainerRunBackgroundCommandArg: []string{ + "sh", + "-c", + "echo foo > /tmp/file", + }, + MultiContainerRunOutputImageArg: consts.LatestKanisterToolsImage, + MultiContainerRunOutputCommandArg: []string{ + "sh", + "-c", + "while [ ! -e /tmp/file ]; do sleep 1; done; kando output value $(cat /tmp/file)", + }, + }, + } +} + +func (s *MultiContainerRunSuite) TestMultiContainerRunWithoutNamespace(c *C) { + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + defer cancel() + tp := param.TemplateParams{ + StatefulSet: ¶m.StatefulSetParams{ + Namespace: s.namespace, + }, + PodOverride: crv1alpha1.JSONMap{ + "containers": []map[string]interface{}{ + { + "name": "background", + "imagePullPolicy": "Always", + }, + { + "name": "output", + "imagePullPolicy": "Always", + }, + }, + }, + } + action := "test" + for _, tc := range []struct { + bp *crv1alpha1.Blueprint + outs []map[string]interface{} + }{ + { + bp: newTaskBlueprint(multiContainerRunPhaseWithoutNamespace()), + outs: []map[string]interface{}{ + { + "value": "foo", + }, + }, + }, + } { + phases, err := kanister.GetPhases(*tc.bp, action, kanister.DefaultVersion, tp) + c.Assert(err, IsNil) + c.Assert(phases, HasLen, len(tc.outs)) + for i, p := range phases { + out, err := p.Exec(ctx, *tc.bp, action, tp) + c.Assert(err, IsNil, Commentf("Phase %s failed", p.Name())) + c.Assert(out, DeepEquals, tc.outs[i]) + } + } +}