Skip to content

Commit 3e0d3fc

Browse files
feat: Adding support for DeploymentRuntimeConfig (#74)
* Adding support for DeploymentRuntimeConfig On-behalf-of: Gergely Szabo (SAP) <[email protected]> Signed-off-by: Gergely Szabo (SAP) <[email protected]> * fixing linter errors On-behalf-of: Gergely Szabo (SAP) <[email protected]> Signed-off-by: Gergely Szabo (SAP) <[email protected]> * adding test On-behalf-of: Gergely Szabo (SAP) <[email protected]> Signed-off-by: Gergely Szabo (SAP) <[email protected]> --------- Signed-off-by: Gergely Szabo (SAP) <[email protected]>
1 parent 7791604 commit 3e0d3fc

File tree

4 files changed

+272
-18
lines changed

4 files changed

+272
-18
lines changed

pkg/setup/setup.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ func (c CrossplaneSetup) Options() []xpenvfuncs.CrossplaneOpt {
5555

5656
// ClusterSetup help with a default kind setup for crossplane, with crossplane and a provider
5757
type ClusterSetup struct {
58-
ProviderName string
59-
Images images.ProviderImages
60-
CrossplaneSetup CrossplaneSetup
61-
ControllerConfig *vendored.ControllerConfig
62-
ProviderCredential *ProviderCredentials
63-
AddToSchemaFuncs []func(s *runtime.Scheme) error
64-
postSetupFuncs []ClusterAwareFunc
65-
ProviderConfigDir *string
58+
ProviderName string
59+
Images images.ProviderImages
60+
CrossplaneSetup CrossplaneSetup
61+
ControllerConfig *vendored.ControllerConfig
62+
DeploymentRuntimeConfig *vendored.DeploymentRuntimeConfig
63+
ProviderCredential *ProviderCredentials
64+
AddToSchemaFuncs []func(s *runtime.Scheme) error
65+
postSetupFuncs []ClusterAwareFunc
66+
ProviderConfigDir *string
6667
}
6768

6869
// Configure optionally creates the kind cluster and takes care about the rest of the setup,
@@ -99,10 +100,11 @@ func (s *ClusterSetup) Configure(testEnv env.Environment, cluster *kind.Cluster)
99100
xpenvfuncs.InstallCrossplane(name, s.CrossplaneSetup.Options()...),
100101
xpenvfuncs.InstallCrossplaneProvider(
101102
name, xpenvfuncs.InstallCrossplaneProviderOptions{
102-
Name: s.ProviderName,
103-
Package: s.Images.Package,
104-
ControllerImage: s.Images.ControllerImage,
105-
ControllerConfig: s.ControllerConfig,
103+
Name: s.ProviderName,
104+
Package: s.Images.Package,
105+
ControllerImage: s.Images.ControllerImage,
106+
ControllerConfig: s.ControllerConfig,
107+
DeploymentRuntimeConfig: s.DeploymentRuntimeConfig,
106108
}),
107109
), firstSetup),
108110
setupProviderCredentials(s),
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package vendored
2+
3+
import (
4+
appsv1 "k8s.io/api/apps/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
"k8s.io/api/apps/v1"
7+
)
8+
9+
// ObjectMeta is metadata contains the configurable metadata fields for the
10+
// runtime objects.
11+
type ObjectMeta struct {
12+
// Name is the name of the object.
13+
// +optional
14+
Name *string `json:"name,omitempty"`
15+
// Annotations is an unstructured key value map stored with a resource that
16+
// may be set by external tools to store and retrieve arbitrary metadata.
17+
// They are not queryable and should be preserved when modifying objects.
18+
// More info: http:https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
19+
// +optional
20+
Annotations map[string]string `json:"annotations,omitempty"`
21+
22+
// Map of string keys and values that can be used to organize and categorize
23+
// (scope and select) objects. Labels will be merged with internal labels
24+
// used by crossplane, and labels with a crossplane.io key might be
25+
// overwritten.
26+
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
27+
// +optional
28+
Labels map[string]string `json:"labels,omitempty"`
29+
}
30+
31+
// DeploymentTemplate is the template for the Deployment object.
32+
type DeploymentTemplate struct {
33+
// Metadata contains the configurable metadata fields for the Deployment.
34+
// +optional
35+
Metadata *ObjectMeta `json:"metadata,omitempty"`
36+
37+
// Spec contains the configurable spec fields for the Deployment object.
38+
// +optional
39+
Spec *appsv1.DeploymentSpec `json:"spec,omitempty"`
40+
}
41+
42+
// ServiceTemplate is the template for the Service object.
43+
type ServiceTemplate struct {
44+
// Metadata contains the configurable metadata fields for the Service.
45+
// +optional
46+
Metadata *ObjectMeta `json:"metadata,omitempty"`
47+
}
48+
49+
// ServiceAccountTemplate is the template for the ServiceAccount object.
50+
type ServiceAccountTemplate struct {
51+
// Metadata contains the configurable metadata fields for the ServiceAccount.
52+
// +optional
53+
Metadata *ObjectMeta `json:"metadata,omitempty"`
54+
}
55+
56+
// DeploymentRuntimeConfigSpec specifies the configuration for a packaged controller.
57+
// Values provided will override package manager defaults. Labels and
58+
// annotations are passed to both the controller Deployment and ServiceAccount.
59+
type DeploymentRuntimeConfigSpec struct {
60+
// DeploymentTemplate is the template for the Deployment object.
61+
// +optional
62+
DeploymentTemplate *DeploymentTemplate `json:"deploymentTemplate,omitempty"`
63+
// ServiceTemplate is the template for the Service object.
64+
// +optional
65+
ServiceTemplate *ServiceTemplate `json:"serviceTemplate,omitempty"`
66+
// ServiceAccountTemplate is the template for the ServiceAccount object.
67+
// +optional
68+
ServiceAccountTemplate *ServiceAccountTemplate `json:"serviceAccountTemplate,omitempty"`
69+
}
70+
71+
// The DeploymentRuntimeConfig provides settings for the Kubernetes Deployment
72+
// of a Provider or composition function package.
73+
//
74+
// Read the Crossplane documentation for
75+
// [more information about DeploymentRuntimeConfigs](https://docs.crossplane.io/latest/concepts/providers/#runtime-configuration).
76+
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
77+
// +kubebuilder:resource:scope=Cluster,categories={crossplane}
78+
type DeploymentRuntimeConfig struct {
79+
metav1.TypeMeta `json:",inline"`
80+
metav1.ObjectMeta `json:"metadata,omitempty"`
81+
82+
Spec DeploymentRuntimeConfigSpec `json:"spec,omitempty"`
83+
}
84+
85+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
86+
func (in *ObjectMeta) DeepCopyInto(out *ObjectMeta) {
87+
*out = *in
88+
if in.Name != nil {
89+
in, out := &in.Name, &out.Name
90+
*out = new(string)
91+
**out = **in
92+
}
93+
if in.Annotations != nil {
94+
in, out := &in.Annotations, &out.Annotations
95+
*out = make(map[string]string, len(*in))
96+
for key, val := range *in {
97+
(*out)[key] = val
98+
}
99+
}
100+
if in.Labels != nil {
101+
in, out := &in.Labels, &out.Labels
102+
*out = make(map[string]string, len(*in))
103+
for key, val := range *in {
104+
(*out)[key] = val
105+
}
106+
}
107+
}
108+
109+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
110+
func (in *DeploymentTemplate) DeepCopyInto(out *DeploymentTemplate) {
111+
*out = *in
112+
if in.Metadata != nil {
113+
in, out := &in.Metadata, &out.Metadata
114+
*out = new(ObjectMeta)
115+
(*in).DeepCopyInto(*out)
116+
}
117+
if in.Spec != nil {
118+
in, out := &in.Spec, &out.Spec
119+
*out = new(v1.DeploymentSpec)
120+
(*in).DeepCopyInto(*out)
121+
}
122+
}
123+
124+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
125+
func (in *ServiceTemplate) DeepCopyInto(out *ServiceTemplate) {
126+
*out = *in
127+
if in.Metadata != nil {
128+
in, out := &in.Metadata, &out.Metadata
129+
*out = new(ObjectMeta)
130+
(*in).DeepCopyInto(*out)
131+
}
132+
}
133+
134+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
135+
func (in *ServiceAccountTemplate) DeepCopyInto(out *ServiceAccountTemplate) {
136+
*out = *in
137+
if in.Metadata != nil {
138+
in, out := &in.Metadata, &out.Metadata
139+
*out = new(ObjectMeta)
140+
(*in).DeepCopyInto(*out)
141+
}
142+
}
143+
144+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
145+
func (in *DeploymentRuntimeConfigSpec) DeepCopyInto(out *DeploymentRuntimeConfigSpec) {
146+
*out = *in
147+
if in.DeploymentTemplate != nil {
148+
in, out := &in.DeploymentTemplate, &out.DeploymentTemplate
149+
*out = new(DeploymentTemplate)
150+
(*in).DeepCopyInto(*out)
151+
}
152+
if in.ServiceTemplate != nil {
153+
in, out := &in.ServiceTemplate, &out.ServiceTemplate
154+
*out = new(ServiceTemplate)
155+
(*in).DeepCopyInto(*out)
156+
}
157+
if in.ServiceAccountTemplate != nil {
158+
in, out := &in.ServiceAccountTemplate, &out.ServiceAccountTemplate
159+
*out = new(ServiceAccountTemplate)
160+
(*in).DeepCopyInto(*out)
161+
}
162+
}
163+
164+
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
165+
func (in *DeploymentRuntimeConfig) DeepCopyInto(out *DeploymentRuntimeConfig) {
166+
*out = *in
167+
out.TypeMeta = in.TypeMeta
168+
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
169+
in.Spec.DeepCopyInto(&out.Spec)
170+
}
171+
172+
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DeploymentRuntimeConfig.
173+
func (in *DeploymentRuntimeConfig) DeepCopy() *DeploymentRuntimeConfig {
174+
if in == nil {
175+
return nil
176+
}
177+
out := new(DeploymentRuntimeConfig)
178+
in.DeepCopyInto(out)
179+
return out
180+
}

pkg/xpenvfuncs/xpenvfuncs.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ spec:
7878
{{- if .ControllerConfig }}
7979
controllerConfigRef:
8080
name: {{.ControllerConfig}}
81-
{{end}}`
81+
{{end}}
82+
{{- if .RuntimeConfig }}
83+
runtimeConfigRef:
84+
name: {{.RuntimeConfig}}
85+
{{end}}
86+
`
8287

8388
const (
8489
helmRepoName = "e2e_crossplane-stable"
@@ -88,7 +93,8 @@ const (
8893
)
8994

9095
var (
91-
controllerConfigSchema = schema.GroupVersionResource{Group: "pkg.crossplane.io", Version: "v1alpha1", Resource: "controllerconfigs"}
96+
controllerConfigSchema = schema.GroupVersionResource{Group: "pkg.crossplane.io", Version: "v1alpha1", Resource: "controllerconfigs"}
97+
deploymentRuntimeConfigSchema = schema.GroupVersionResource{Group: "pkg.crossplane.io", Version: "v1beta1", Resource: "deploymentruntimeconfigs"}
9298
)
9399

94100
// InstallCrossplane returns an env.Func that is used to install crossplane into the given cluster
@@ -163,10 +169,11 @@ func ApplySecretInCrossplaneNamespace(name string, data map[string]string) env.F
163169

164170
// InstallCrossplaneProviderOptions hols information on the tested provider
165171
type InstallCrossplaneProviderOptions struct {
166-
Name string
167-
Package string
168-
ControllerImage *string // TODO read from package
169-
ControllerConfig *vendored.ControllerConfig
172+
Name string
173+
Package string
174+
ControllerImage *string // TODO read from package
175+
ControllerConfig *vendored.ControllerConfig
176+
DeploymentRuntimeConfig *vendored.DeploymentRuntimeConfig
170177
}
171178

172179
// InstallCrossplaneProvider returns an env.Func that is used to
@@ -298,6 +305,7 @@ func installCrossplaneProviderEnvFunc(_ string, opts InstallCrossplaneProviderOp
298305
Name string
299306
Package string
300307
ControllerConfig string
308+
RuntimeConfig string
301309
}{
302310
Name: opts.Name,
303311
Package: opts.Package,
@@ -311,6 +319,13 @@ func installCrossplaneProviderEnvFunc(_ string, opts InstallCrossplaneProviderOp
311319
return ctx, err
312320
}
313321
}
322+
if opts.DeploymentRuntimeConfig != nil {
323+
data.RuntimeConfig = opts.DeploymentRuntimeConfig.ObjectMeta.Name
324+
err := applyDeploymentRuntimeConfig(ctx, cfg, opts)
325+
if err != nil {
326+
return ctx, err
327+
}
328+
}
314329

315330
crs, err := renderTemplate(
316331
crsCrossplaneProviderTemplate, data,
@@ -345,6 +360,26 @@ func applyControllerConfig(ctx context.Context, cfg *envconf.Config, opts Instal
345360
return err
346361
}
347362

363+
func applyDeploymentRuntimeConfig(ctx context.Context, cfg *envconf.Config, opts InstallCrossplaneProviderOptions) error {
364+
klog.V(4).Info("Installing DeploymentRuntimeConfig")
365+
config := opts.DeploymentRuntimeConfig.DeepCopy()
366+
config.TypeMeta.Kind = "DeploymentRuntimeConfig"
367+
config.TypeMeta.APIVersion = deploymentRuntimeConfigSchema.GroupVersion().Identifier()
368+
369+
cl, err := dynamic.NewForConfig(cfg.Client().RESTConfig())
370+
if err != nil {
371+
return err
372+
}
373+
res := cl.Resource(deploymentRuntimeConfigSchema)
374+
data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(config)
375+
if err != nil {
376+
return err
377+
}
378+
unstruc := unstructured.Unstructured{Object: data}
379+
_, err = res.Create(ctx, &unstruc, metav1.CreateOptions{})
380+
return err
381+
}
382+
348383
func awaitProviderHealthy(opts InstallCrossplaneProviderOptions) env.Func {
349384
return func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
350385
r, err := resources.New(cfg.Client().RESTConfig())

pkg/xpenvfuncs/xpenvfuncs_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,43 @@ spec:
233233
packagePullPolicy: Never`,
234234
},
235235
},
236+
{
237+
description: "with runtimeConfigRef",
238+
args: args{
239+
template: `apiVersion: pkg.crossplane.io/v1
240+
kind: Provider
241+
metadata:
242+
name: {{.Name}}
243+
spec:
244+
package: {{.Package}}
245+
packagePullPolicy: Never
246+
{{- if .ControllerConfig }}
247+
runtimeConfigRef:
248+
name: {{.RuntimeConfig}}
249+
{{end}}`,
250+
data: struct {
251+
Name string
252+
Package string
253+
RuntimeConfig string
254+
}{
255+
Name: "my-provider",
256+
Package: "my-registry.local/path/to/my-provider:1.2.3",
257+
RuntimeConfig: "my-runtime-config-ref",
258+
},
259+
},
260+
expects: expects{
261+
rendered: `apiVersion: pkg.crossplane.io/v1
262+
kind: Provider
263+
metadata:
264+
name: my-provider
265+
spec:
266+
package: my-registry.local/path/to/my-provider:1.2.3
267+
packagePullPolicy: Never
268+
runtimeConfigRef:
269+
name: my-runtime-config-ref
270+
`,
271+
},
272+
},
236273
}
237274

238275
for _, test := range tests {

0 commit comments

Comments
 (0)