diff --git a/pkg/customresource/customresource.go b/pkg/customresource/customresource.go index 88167837e2..37793589db 100644 --- a/pkg/customresource/customresource.go +++ b/pkg/customresource/customresource.go @@ -65,6 +65,7 @@ type Context struct { APIExtensionClientset apiextensionsclient.Interface Interval time.Duration Timeout time.Duration + ParentContext contextpkg.Context } // CreateCustomResources creates the given custom resources and waits for them to initialize @@ -124,8 +125,7 @@ func createCRD(context Context, resource CustomResource) error { return errors.Wrap(err, "Getting CRD object from CRD bytes") } - ctx := contextpkg.Background() - _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{}) + _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Create(context.ParentContext, crd, metav1.CreateOptions{}) if err != nil { if !apierrors.IsAlreadyExists(err) { return errors.Errorf("Failed to create %s CRD. %+v", resource.Name, err) @@ -133,13 +133,13 @@ func createCRD(context Context, resource CustomResource) error { err := retry.RetryOnConflict(retry.DefaultRetry, func() error { // if CRD already exists, get the resource version and create the CRD with that resource version - c, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd.Name, metav1.GetOptions{}) + c, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(context.ParentContext, crd.Name, metav1.GetOptions{}) if err != nil { return err } crd.ResourceVersion = c.ResourceVersion - _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{}) + _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Update(context.ParentContext, crd, metav1.UpdateOptions{}) if err != nil { return err } @@ -157,11 +157,10 @@ func rawCRDFromFile(path string) ([]byte, error) { return yamls.ReadFile(path) } -func waitForCRDInit(crcontext Context, resource CustomResource) error { +func waitForCRDInit(context Context, resource CustomResource) error { crdName := fmt.Sprintf("%s.%s", resource.Plural, resource.Group) - ctx := contextpkg.TODO() - return wait.PollUntilContextTimeout(ctx, crcontext.Interval, crcontext.Timeout, false, func(contextpkg.Context) (bool, error) { - crd, err := crcontext.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crdName, metav1.GetOptions{}) + return wait.PollUntilContextTimeout(context.ParentContext, context.Interval, context.Timeout, false, func(contextpkg.Context) (bool, error) { + crd, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(context.ParentContext, crdName, metav1.GetOptions{}) if err != nil { return false, err } diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index d19f9a6b17..223af2675d 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -38,7 +38,7 @@ const ( // CreateCustomResources creates the given custom resources and waits for them to initialize func CreateCustomResources(ctx context.Context, config *rest.Config) error { - crCTX, err := newOpKitContext(config) + crCTX, err := newOpKitContext(config, ctx) if err != nil { return err } @@ -50,7 +50,7 @@ func CreateCustomResources(ctx context.Context, config *rest.Config) error { return customresource.CreateCustomResources(*crCTX, resources) } -func newOpKitContext(config *rest.Config) (*customresource.Context, error) { +func newOpKitContext(config *rest.Config, ctx context.Context) (*customresource.Context, error) { clientset, err := kubernetes.NewForConfig(config) if err != nil { return nil, errors.Wrap(err, "failed to get k8s client.") @@ -64,12 +64,13 @@ func newOpKitContext(config *rest.Config) (*customresource.Context, error) { APIExtensionClientset: apiExtClientset, Interval: 500 * time.Millisecond, Timeout: 60 * time.Second, + ParentContext: ctx, }, nil } // CreateRepoServerCustomResource creates the kopia repository server custom resource func CreateRepoServerCustomResource(ctx context.Context, config *rest.Config) error { - crCTX, err := newOpKitContext(config) + crCTX, err := newOpKitContext(config, ctx) if err != nil { return err }