Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions pkg/helpers/apply/applierbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package apply

import (
"context"
"text/template"

"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
Expand All @@ -20,6 +21,7 @@ type Applier struct {
scheme *runtime.Scheme
owner runtime.Object
cache resourceapply.ResourceCache
context context.Context
controller *bool
blockOwnerDeletion *bool
}
Expand All @@ -43,6 +45,14 @@ type iApplierBuilder interface {
WithOwner(owner runtime.Object, blockOwnerDeletion, controller bool, scheme *runtime.Scheme) *ApplierBuilder
//WithCache add cache
WithCache(cache resourceapply.ResourceCache) *ApplierBuilder
//WithContext use a context or use a new one if not provided
WithContext(ctx context.Context) *ApplierBuilder
//GetKubeClient returns the kubeclient
GetKubeClient() kubernetes.Interface
//GetAPIExtensionClient returns the APIExtensionClient
GetAPIExtensionClient() apiextensionsclient.Interface
//GetDynamicClient returns the dynamicClient
GetDynamicClient() dynamic.Interface
}

var _ iApplierBuilder = NewApplierBuilder()
Expand All @@ -57,6 +67,9 @@ func (a *ApplierBuilder) Build() Applier {
if a.applier.cache == nil {
a.applier.cache = NewResourceCache()
}
if a.applier.context == nil {
a.applier.context = context.Background()
}
return a.applier
}

Expand Down Expand Up @@ -92,6 +105,24 @@ func (a *ApplierBuilder) WithCache(cache resourceapply.ResourceCache) *ApplierBu
return a
}

//WithContext set a the cache instead of using the default cache created on the Build()
func (a *ApplierBuilder) WithContext(ctx context.Context) *ApplierBuilder {
a.applier.context = ctx
return a
}

func (a *ApplierBuilder) GetKubeClient() kubernetes.Interface {
return a.applier.kubeClient
}

func (a *ApplierBuilder) GetAPIExtensionClient() apiextensionsclient.Interface {
return a.applier.apiExtensionsClient
}

func (a *ApplierBuilder) GetDynamicClient() dynamic.Interface {
return a.applier.dynamicClient
}

func NewResourceCache() resourceapply.ResourceCache {
return resourceapply.NewResourceCache()
}
11 changes: 5 additions & 6 deletions pkg/helpers/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package apply

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -88,7 +87,7 @@ func (a *Applier) ApplyDeployment(
if err != nil {
return output, fmt.Errorf("%q: %v %v", name, sch, err)
}
_, _, err = resourceapply.ApplyDeployment(context.TODO(),
_, _, err = resourceapply.ApplyDeployment(a.context,
a.kubeClient.AppsV1(),
recorder,
deployment.(*appsv1.Deployment), 0)
Expand All @@ -112,7 +111,7 @@ func (a *Applier) ApplyDirectly(
output := make([]string, 0)
//Apply resources
clients := resourceapply.NewClientHolder().WithAPIExtensionsClient(a.apiExtensionsClient).WithDynamicClient(a.dynamicClient).WithKubernetes(a.kubeClient)
resourceResults := resourceapply.ApplyDirectly(context.TODO(), clients, recorder, a.cache, func(name string) ([]byte, error) {
resourceResults := resourceapply.ApplyDirectly(a.context, clients, recorder, a.cache, func(name string) ([]byte, error) {
out, err := a.MustTemplateAsset(reader, values, headerFile, name)
if err != nil {
return nil, err
Expand Down Expand Up @@ -188,12 +187,12 @@ func (a *Applier) ApplyCustomResource(
return output, err
}
dr := a.dynamicClient.Resource(mapping.Resource)
existing, err := dr.Namespace(required.GetNamespace()).Get(context.TODO(), required.GetName(), metav1.GetOptions{})
existing, err := dr.Namespace(required.GetNamespace()).Get(a.context, required.GetName(), metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
required := required.DeepCopy()
actual, err := dr.Namespace(required.GetNamespace()).
Create(context.TODO(), required, metav1.CreateOptions{})
Create(a.context, required, metav1.CreateOptions{})
a.GetCache().UpdateCachedResourceMetadata(required, actual)
return output, err
}
Expand All @@ -206,7 +205,7 @@ func (a *Applier) ApplyCustomResource(

required.SetResourceVersion(existing.GetResourceVersion())
actual, err := dr.Namespace(required.GetNamespace()).
Update(context.TODO(), required, metav1.UpdateOptions{})
Update(a.context, required, metav1.UpdateOptions{})
a.GetCache().UpdateCachedResourceMetadata(required, actual)

if err != nil {
Expand Down