/
applierbuilder.go
158 lines (137 loc) · 4.91 KB
/
applierbuilder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright Red Hat
package apply
import (
"context"
"text/template"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type Applier struct {
kubeClient kubernetes.Interface
apiExtensionsClient apiextensionsclient.Interface
dynamicClient dynamic.Interface
templateFuncMap template.FuncMap
scheme *runtime.Scheme
owner runtime.Object
cache resourceapply.ResourceCache
context context.Context
controller *bool
blockOwnerDeletion *bool
kindOrder KindsOrder
}
// ApplierBuilder a builder to build the applier
type ApplierBuilder struct {
applier Applier
}
type iApplierBuilder interface {
// Build returns the builded applier
Build() Applier
// WithRestConfig adds the necessary clients using a rest.Config
WithRestConfig(cfg *rest.Config) *ApplierBuilder
// WithClient adds the several clients to the applier
WithClient(
kubeClient kubernetes.Interface,
apiExtensionsClient apiextensionsclient.Interface,
dynamicClient dynamic.Interface) *ApplierBuilder
// WithTemplateFuncMap add template.FuncMap to the applier.
WithTemplateFuncMap(fm template.FuncMap) *ApplierBuilder
// WithOwner add an ownerref to the object
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
// WithKindOrder define in which order to the files must be applied
WithKindOrder(kindOrder KindsOrder) *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()
// New ApplyBuilder
func NewApplierBuilder() *ApplierBuilder {
return &ApplierBuilder{}
}
// Build returns the builded applier
func (a *ApplierBuilder) Build() Applier {
if a.applier.cache == nil {
a.applier.cache = NewResourceCache()
}
if a.applier.context == nil {
a.applier.context = context.Background()
}
if a.applier.kindOrder == nil {
a.applier.kindOrder = DefaultCreateUpdateKindsOrder
}
return a.applier
}
// WithRestConfig adds the clients based on the provided rest.Config
func (a *ApplierBuilder) WithRestConfig(cfg *rest.Config) *ApplierBuilder {
kubeClient := kubernetes.NewForConfigOrDie(cfg)
apiExtensionsClient := apiextensionsclient.NewForConfigOrDie(cfg)
dynamicClient := dynamic.NewForConfigOrDie(cfg)
a.applier.kubeClient = kubeClient
a.applier.apiExtensionsClient = apiExtensionsClient
a.applier.dynamicClient = dynamicClient
return a
}
// WithClient adds the several clients to the applier
func (a *ApplierBuilder) WithClient(
kubeClient kubernetes.Interface,
apiExtensionsClient apiextensionsclient.Interface,
dynamicClient dynamic.Interface) *ApplierBuilder {
a.applier.kubeClient = kubeClient
a.applier.apiExtensionsClient = apiExtensionsClient
a.applier.dynamicClient = dynamicClient
return a
}
// WithTemplateFuncMap add template.FuncMap to the applier.
func (a *ApplierBuilder) WithTemplateFuncMap(fm template.FuncMap) *ApplierBuilder {
a.applier.templateFuncMap = fm
return a
}
// WithOwner add an ownerref to the object
func (a *ApplierBuilder) WithOwner(owner runtime.Object,
blockOwnerDeletion,
controller bool,
scheme *runtime.Scheme) *ApplierBuilder {
a.applier.owner = owner
a.applier.blockOwnerDeletion = &blockOwnerDeletion
a.applier.controller = &controller
a.applier.scheme = scheme
return a
}
// WithCache set a the cache instead of using the default cache created on the Build()
func (a *ApplierBuilder) WithCache(cache resourceapply.ResourceCache) *ApplierBuilder {
a.applier.cache = cache
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
}
// WithKindOrder defines the order in which the files must be applied.
func (a *ApplierBuilder) WithKindOrder(kindsOrder KindsOrder) *ApplierBuilder {
a.applier.kindOrder = kindsOrder
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()
}