Skip to content

Commit

Permalink
Added context.Context as part of customresource.Context
Browse files Browse the repository at this point in the history
Signed-off-by: Abhijit Mukherjee <[email protected]>
  • Loading branch information
mabhi committed Feb 13, 2024
1 parent 84c0996 commit 90512b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
15 changes: 7 additions & 8 deletions pkg/customresource/customresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,22 +125,21 @@ 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)
}

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
}
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.")
Expand All @@ -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
}
Expand Down

0 comments on commit 90512b4

Please sign in to comment.