Skip to content

Commit

Permalink
Propagate context to child function (#1036)
Browse files Browse the repository at this point in the history
* Propogate context to child function

* Refactor controller package

* Refactor kube package

* Refactor function package

* Refactor param package

* Refactor package kanctl

* Refactor testing package

* Fix lint

* Fix Lint

* Fix typos in param package

* Use context.background for default invocation

* Apply suggestions from code review

Co-authored-by: Pavan Navarathna <pavan@kasten.io>

* Address review comment

* Apply suggestions from code review

Co-authored-by: Pavan Navarathna <pavan@kasten.io>

* Apply suggestions from code review

Co-authored-by: Pavan Navarathna <pavan@kasten.io>

* Address review comments

* Fix lint

* Change Snapshot Interface methods

Co-authored-by: Pavan Navarathna <pavan@kasten.io>
  • Loading branch information
akankshakumari393 and pavannd1 committed Aug 5, 2021
1 parent c847927 commit bc29ee4
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 158 deletions.
10 changes: 5 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Controller) StartWatch(ctx context.Context, namespace string) error {
if err != nil {
return errors.Wrap(err, "failed to get a CustomResource client")
}
if err := checkCRAccess(crClient, namespace); err != nil {
if err := checkCRAccess(ctx, crClient, namespace); err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(c.config)
Expand Down Expand Up @@ -121,14 +121,14 @@ func (c *Controller) StartWatch(ctx context.Context, namespace string) error {
return nil
}

func checkCRAccess(cli versioned.Interface, ns string) error {
if _, err := cli.CrV1alpha1().ActionSets(ns).List(context.TODO(), v1.ListOptions{}); err != nil {
func checkCRAccess(ctx context.Context, cli versioned.Interface, ns string) error {
if _, err := cli.CrV1alpha1().ActionSets(ns).List(ctx, v1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list ActionSets")
}
if _, err := cli.CrV1alpha1().Blueprints(ns).List(context.TODO(), v1.ListOptions{}); err != nil {
if _, err := cli.CrV1alpha1().Blueprints(ns).List(ctx, v1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list Blueprints")
}
if _, err := cli.CrV1alpha1().Profiles(ns).List(context.TODO(), v1.ListOptions{}); err != nil {
if _, err := cli.CrV1alpha1().Profiles(ns).List(ctx, v1.ListOptions{}); err != nil {
return errors.Wrap(err, "Could not list Profiles")
}
return nil
Expand Down
50 changes: 28 additions & 22 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,31 @@ func (s *ControllerSuite) SetUpSuite(c *C) {
GenerateName: "kanistercontrollertest-",
},
}
cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
ctx := context.Background()
cns, err := s.cli.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.Name

sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(context.TODO(), sec, metav1.CreateOptions{})
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(ctx, sec, metav1.CreateOptions{})
c.Assert(err, IsNil)

p := testutil.NewTestProfile(s.namespace, sec.GetName())
_, err = s.crCli.Profiles(s.namespace).Create(context.TODO(), p, metav1.CreateOptions{})
_, err = s.crCli.Profiles(s.namespace).Create(ctx, p, metav1.CreateOptions{})
c.Assert(err, IsNil)

ss := testutil.NewTestStatefulSet(1)
ss, err = s.cli.AppsV1().StatefulSets(s.namespace).Create(context.TODO(), ss, metav1.CreateOptions{})
ss, err = s.cli.AppsV1().StatefulSets(s.namespace).Create(ctx, ss, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.ss = ss

d := testutil.NewTestDeployment(1)
d, err = s.cli.AppsV1().Deployments(s.namespace).Create(context.TODO(), d, metav1.CreateOptions{})
d, err = s.cli.AppsV1().Deployments(s.namespace).Create(ctx, d, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.deployment = d

cm := testutil.NewTestConfigMap()
cm, err = s.cli.CoreV1().ConfigMaps(s.namespace).Create(context.TODO(), cm, metav1.CreateOptions{})
cm, err = s.cli.CoreV1().ConfigMaps(s.namespace).Create(ctx, cm, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.confimap = cm
}
Expand Down Expand Up @@ -369,7 +370,8 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
// Add a blueprint with a mocked kanister function.
bp := testutil.NewTestBlueprint(pok, tc.funcNames...)
bp = testutil.BlueprintWithConfigMap(bp)
bp, err = s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
ctx := context.Background()
bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

var n string
Expand All @@ -385,7 +387,7 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {
// Add an actionset that references that blueprint.
as := testutil.NewTestActionSet(s.namespace, bp.GetName(), pok, n, s.namespace, tc.version)
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err = s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil, Commentf("Failed case: %s", tc.name))

err = s.waitOnActionSetState(c, as, crv1alpha1.StateRunning)
Expand Down Expand Up @@ -435,6 +437,7 @@ func (s *ControllerSuite) TestExecActionSet(c *C) {

func (s *ControllerSuite) TestRuntimeObjEventLogs(c *C) {
c.Skip("This may not work in MiniKube")
ctx := context.Background()
// Create ActionSet
as := &crv1alpha1.ActionSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -448,7 +451,7 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *C) {
},
},
}
as, err := s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err := s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil)
msg := "Unit testing event logs"
reason := "Test Logs"
Expand All @@ -458,11 +461,10 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *C) {

// Create Blueprint
bp := testutil.NewTestBlueprint("StatefulSet", testutil.WaitFuncName)
bp, err = s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
bp, err = s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

//Test the logAndErrorEvent function
ctx := context.Background()
ctx = field.Context(ctx, consts.ActionsetNameKey, as.GetName())
config, err := kube.LoadConfig()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -497,16 +499,17 @@ func (s *ControllerSuite) TestRuntimeObjEventLogs(c *C) {
}

func (s *ControllerSuite) TestPhaseOutputAsArtifact(c *C) {
ctx := context.Background()
// Create a blueprint that uses func output as artifact
bp := newBPWithOutputArtifact()
bp = testutil.BlueprintWithConfigMap(bp)
bp, err := s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

// Add an actionset that references that blueprint.
as := testutil.NewTestActionSet(s.namespace, bp.GetName(), "Deployment", s.deployment.GetName(), s.namespace, kanister.DefaultVersion)
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err = s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil)

err = s.waitOnActionSetState(c, as, crv1alpha1.StateRunning)
Expand All @@ -519,7 +522,7 @@ func (s *ControllerSuite) TestPhaseOutputAsArtifact(c *C) {
c.Assert(err, IsNil)

// Check if the artifacts got updated correctly
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(context.TODO(), as.GetName(), metav1.GetOptions{})
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(ctx, as.GetName(), metav1.GetOptions{})
arts := as.Status.Actions[0].Artifacts
c.Assert(arts, NotNil)
c.Assert(arts, HasLen, 1)
Expand All @@ -528,16 +531,17 @@ func (s *ControllerSuite) TestPhaseOutputAsArtifact(c *C) {
}

func (s *ControllerSuite) TestPhaseOutputAsKopiaSnapshot(c *C) {
ctx := context.Background()
// Create a blueprint that uses func output as kopia snapshot
bp := newBPWithKopiaSnapshotOutputArtifact()
bp = testutil.BlueprintWithConfigMap(bp)
bp, err := s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

// Add an actionset that references that blueprint.
as := testutil.NewTestActionSet(s.namespace, bp.GetName(), "Deployment", s.deployment.GetName(), s.namespace, kanister.DefaultVersion)
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err = s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil)

err = s.waitOnActionSetState(c, as, crv1alpha1.StateRunning)
Expand All @@ -550,7 +554,7 @@ func (s *ControllerSuite) TestPhaseOutputAsKopiaSnapshot(c *C) {
c.Assert(err, IsNil)

// Check if the artifacts got updated correctly
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(context.TODO(), as.GetName(), metav1.GetOptions{})
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(ctx, as.GetName(), metav1.GetOptions{})
arts := as.Status.Actions[0].Artifacts
c.Assert(arts, NotNil)
c.Assert(arts, HasLen, 1)
Expand All @@ -559,10 +563,11 @@ func (s *ControllerSuite) TestPhaseOutputAsKopiaSnapshot(c *C) {
}

func (s *ControllerSuite) TestActionSetExecWithoutProfile(c *C) {
ctx := context.Background()
// Create a blueprint that uses func output as artifact
bp := newBPWithOutputArtifact()
bp = testutil.BlueprintWithConfigMap(bp)
bp, err := s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

// Add an actionset that references that blueprint.
Expand All @@ -587,7 +592,7 @@ func (s *ControllerSuite) TestActionSetExecWithoutProfile(c *C) {
},
}
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err = s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil)

err = s.waitOnActionSetState(c, as, crv1alpha1.StateRunning)
Expand All @@ -600,7 +605,7 @@ func (s *ControllerSuite) TestActionSetExecWithoutProfile(c *C) {
c.Assert(err, IsNil)

// Check if the artifacts got updated correctly
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(context.TODO(), as.GetName(), metav1.GetOptions{})
as, _ = s.crCli.ActionSets(as.GetNamespace()).Get(ctx, as.GetName(), metav1.GetOptions{})
arts := as.Status.Actions[0].Artifacts
c.Assert(arts, NotNil)
c.Assert(arts, HasLen, 1)
Expand All @@ -609,15 +614,16 @@ func (s *ControllerSuite) TestActionSetExecWithoutProfile(c *C) {
}

func (s *ControllerSuite) TestRenderArtifactsFailure(c *C) {
ctx := context.Background()
bp := newBPWithFakeOutputArtifact()
bp = testutil.BlueprintWithConfigMap(bp)
bp, err := s.crCli.Blueprints(s.namespace).Create(context.TODO(), bp, metav1.CreateOptions{})
bp, err := s.crCli.Blueprints(s.namespace).Create(ctx, bp, metav1.CreateOptions{})
c.Assert(err, IsNil)

// Add an actionset that references that blueprint.
as := testutil.NewTestActionSet(s.namespace, bp.GetName(), "Deployment", s.deployment.GetName(), s.namespace, kanister.DefaultVersion)
as = testutil.ActionSetWithConfigMap(as, s.confimap.GetName())
as, err = s.crCli.ActionSets(s.namespace).Create(context.TODO(), as, metav1.CreateOptions{})
as, err = s.crCli.ActionSets(s.namespace).Create(ctx, as, metav1.CreateOptions{})
c.Assert(err, IsNil)

err = s.waitOnActionSetState(c, as, crv1alpha1.StateRunning)
Expand Down
7 changes: 4 additions & 3 deletions pkg/function/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ func (s *DataSuite) SetUpSuite(c *C) {
s.crCli = crCli
s.osCli = osCli

ctx := context.Background()
ns := testutil.NewTestNamespace()
ns.GenerateName = "kanister-datatest-"

cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
cns, err := s.cli.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.GetName()

sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(context.TODO(), sec, metav1.CreateOptions{})
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(ctx, sec, metav1.CreateOptions{})
c.Assert(err, IsNil)

p := testutil.NewTestProfile(s.namespace, sec.GetName())
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(context.TODO(), p, metav1.CreateOptions{})
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(ctx, p, metav1.CreateOptions{})
c.Assert(err, IsNil)

var location crv1alpha1.Location
Expand Down
8 changes: 4 additions & 4 deletions pkg/function/e2e_volume_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *VolumeSnapshotTestSuite) SetUpTest(c *C) {
volToPvc := kube.StatefulSetVolumes(s.cli, ss, &pods[0])
pvc := volToPvc[pods[0].Spec.Containers[0].VolumeMounts[0].Name]
c.Assert(len(pvc) > 0, Equals, true)
id, secret, locationType, err := s.getCreds(c, s.cli, s.namespace, pvc)
id, secret, locationType, err := s.getCreds(c, ctx, s.cli, s.namespace, pvc)
c.Assert(err, IsNil)
if id == "" || secret == "" {
c.Skip("Skipping the test since storage type not supported")
Expand Down Expand Up @@ -331,13 +331,13 @@ func (s *VolumeSnapshotTestSuite) TestVolumeSnapshot(c *C) {
}
}

func (s *VolumeSnapshotTestSuite) getCreds(c *C, cli kubernetes.Interface, namespace string, pvcname string) (string, string, crv1alpha1.LocationType, error) {
pvc, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), pvcname, metav1.GetOptions{})
func (s *VolumeSnapshotTestSuite) getCreds(c *C, ctx context.Context, cli kubernetes.Interface, namespace string, pvcname string) (string, string, crv1alpha1.LocationType, error) {
pvc, err := cli.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcname, metav1.GetOptions{})
if err != nil {
return "", "", "", err
}
pvName := pvc.Spec.VolumeName
pv, err := cli.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{})
pv, err := cli.CoreV1().PersistentVolumes().Get(ctx, pvName, metav1.GetOptions{})
if err != nil {
return "", "", "", err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/function/kube_exec_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,22 @@ func (s *KubeExecAllTest) SetUpSuite(c *C) {
s.crCli = crCli
s.osCli = osCli

ctx := context.Background()
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "kubeexecall-",
},
}
cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
cns, err := s.cli.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.Name

sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(context.TODO(), sec, metav1.CreateOptions{})
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(ctx, sec, metav1.CreateOptions{})
c.Assert(err, IsNil)

p := testutil.NewTestProfile(s.namespace, sec.GetName())
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(context.TODO(), p, metav1.CreateOptions{})
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(ctx, p, metav1.CreateOptions{})
c.Assert(err, IsNil)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/function/kube_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,22 @@ func (s *KubeExecTest) SetUpSuite(c *C) {
s.crCli = crCli
s.osCli = osCli

ctx := context.Background()
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "kanisterkubeexectest-",
},
}
cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
cns, err := s.cli.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.Name

sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(context.TODO(), sec, metav1.CreateOptions{})
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(ctx, sec, metav1.CreateOptions{})
c.Assert(err, IsNil)

p := testutil.NewTestProfile(s.namespace, sec.GetName())
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(context.TODO(), p, metav1.CreateOptions{})
_, err = s.crCli.CrV1alpha1().Profiles(s.namespace).Create(ctx, p, metav1.CreateOptions{})
c.Assert(err, IsNil)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/function/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *ScaleSuite) SetUpTest(c *C) {
s.cli = cli
s.crCli = crCli
s.osCli = osCli

ctx := context.Background()
err = resource.CreateCustomResources(context.Background(), config)
c.Assert(err, IsNil)

Expand All @@ -66,16 +66,16 @@ func (s *ScaleSuite) SetUpTest(c *C) {
GenerateName: "kanister-scale-test-",
},
}
cns, err := s.cli.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})
cns, err := s.cli.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
c.Assert(err, IsNil)
s.namespace = cns.Name

sec := testutil.NewTestProfileSecret()
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(context.TODO(), sec, metav1.CreateOptions{})
sec, err = s.cli.CoreV1().Secrets(s.namespace).Create(ctx, sec, metav1.CreateOptions{})
c.Assert(err, IsNil)

p := testutil.NewTestProfile(s.namespace, sec.GetName())
_, err = crCli.CrV1alpha1().Profiles(s.namespace).Create(context.TODO(), p, metav1.CreateOptions{})
_, err = crCli.CrV1alpha1().Profiles(s.namespace).Create(ctx, p, metav1.CreateOptions{})
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (s *ScaleSuite) TestScaleDeployment(c *C) {
c.Assert(ok, Equals, true)
}

pods, err := s.cli.CoreV1().Pods(s.namespace).List(context.TODO(), metav1.ListOptions{})
pods, err := s.cli.CoreV1().Pods(s.namespace).List(ctx, metav1.ListOptions{})
c.Assert(err, IsNil)
c.Assert(pods.Items, HasLen, 0)
}
Expand Down Expand Up @@ -224,7 +224,7 @@ func (s *ScaleSuite) TestScaleStatefulSet(c *C) {
c.Assert(ok, Equals, true)
}

pods, err := s.cli.CoreV1().Pods(s.namespace).List(context.TODO(), metav1.ListOptions{})
pods, err := s.cli.CoreV1().Pods(s.namespace).List(ctx, metav1.ListOptions{})
c.Assert(err, IsNil)

// This check can flake on underprovisioned clusters so we exit early.
Expand Down
Loading

0 comments on commit bc29ee4

Please sign in to comment.