Skip to content

Commit 9a849c8

Browse files
authored
Merge pull request #937 from Daimler/tobiasgiese/v1alpha4-multi-tenancy
✨ Add conversion for SecretReference to string
2 parents 5dece8f + de98749 commit 9a849c8

24 files changed

+363
-134
lines changed

api/v1alpha3/conversion.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20+
corev1 "k8s.io/api/core/v1"
2021
conversion "k8s.io/apimachinery/pkg/conversion"
2122
ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion"
2223

@@ -113,20 +114,37 @@ func Convert_v1alpha3_OpenStackClusterSpec_To_v1alpha4_OpenStackClusterSpec(in *
113114
return autoConvert_v1alpha3_OpenStackClusterSpec_To_v1alpha4_OpenStackClusterSpec(in, out, s)
114115
}
115116

117+
// Convert_v1alpha4_OpenStackClusterSpec_To_v1alpha3_OpenStackClusterSpec has to be added by us because we have to
118+
// convert the Type of CloudsSecret from SecretReference to string.
119+
func Convert_v1alpha4_OpenStackClusterSpec_To_v1alpha3_OpenStackClusterSpec(in *v1alpha4.OpenStackClusterSpec, out *OpenStackClusterSpec, s conversion.Scope) error {
120+
if in.IdentityRef != nil {
121+
out.CloudsSecret = &corev1.SecretReference{
122+
Name: in.IdentityRef.Name,
123+
}
124+
}
125+
return autoConvert_v1alpha4_OpenStackClusterSpec_To_v1alpha3_OpenStackClusterSpec(in, out, s)
126+
}
127+
116128
// Convert_v1alpha3_OpenStackMachineSpec_To_v1alpha4_OpenStackMachineSpec is an autogenerated conversion function.
117129
// v1alpha4 drops the field .UserDataSecret which is why we reuqire to define the function here.
118130
func Convert_v1alpha3_OpenStackMachineSpec_To_v1alpha4_OpenStackMachineSpec(in *OpenStackMachineSpec, out *v1alpha4.OpenStackMachineSpec, s conversion.Scope) error {
131+
if in.CloudsSecret != nil {
132+
out.IdentityRef = &v1alpha4.OpenStackIdentityReference{
133+
Name: in.CloudsSecret.Name,
134+
}
135+
}
119136
return autoConvert_v1alpha3_OpenStackMachineSpec_To_v1alpha4_OpenStackMachineSpec(in, out, s)
120137
}
121138

122-
// Convert_v1alpha3_OpenStackClusterSpec_To_v1alpha4_OpenStackClusterSpec has to be added by us for the new portOpts
139+
// Convert_v1alpha4_Network_To_v1alpha3_Network has to be added by us for the new portOpts
123140
// parameter in v1alpha4. There is no intention to support this parameter in v1alpha3, so the field is just dropped.
124141
func Convert_v1alpha4_Network_To_v1alpha3_Network(in *v1alpha4.Network, out *Network, s conversion.Scope) error {
125142
return autoConvert_v1alpha4_Network_To_v1alpha3_Network(in, out, s)
126143
}
127144

128-
// Convert_v1alpha3_OpenStackClusterSpec_To_v1alpha4_OpenStackClusterSpec has to be added by us for the new ports
145+
// Convert_v1alpha4_OpenStackMachineSpec_To_v1alpha3_OpenStackMachineSpec has to be added by us for the new ports
129146
// parameter in v1alpha4. There is no intention to support this parameter in v1alpha3, so the field is just dropped.
147+
// Further, we want to convert the Type of CloudsSecret from SecretReference to string.
130148
func Convert_v1alpha4_OpenStackMachineSpec_To_v1alpha3_OpenStackMachineSpec(in *v1alpha4.OpenStackMachineSpec, out *OpenStackMachineSpec, s conversion.Scope) error {
131149
return autoConvert_v1alpha4_OpenStackMachineSpec_To_v1alpha3_OpenStackMachineSpec(in, out, s)
132150
}

api/v1alpha3/openstackcluster_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type OpenStackClusterSpec struct {
3333

3434
// The name of the secret containing the openstack credentials
3535
// +optional
36+
// +k8s:conversion-gen=false
3637
CloudsSecret *corev1.SecretReference `json:"cloudsSecret"`
3738

3839
// The name of the cloud to use from the clouds secret

api/v1alpha3/openstackmachine_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type OpenStackMachineSpec struct {
3838

3939
// The name of the secret containing the openstack credentials
4040
// +optional
41+
// +k8s:conversion-gen=false
4142
CloudsSecret *corev1.SecretReference `json:"cloudsSecret"`
4243

4344
// The name of the cloud to use from the clouds secret

api/v1alpha3/zz_generated.conversion.go

Lines changed: 9 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha4/identity_types.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha4
18+
19+
const defaultIdentityRefKind = "Secret"
20+
21+
// OpenStackIdentityReference is a reference to an infrastructure
22+
// provider identity to be used to provision cluster resources.
23+
type OpenStackIdentityReference struct {
24+
// Kind of the identity. Must be supported by the infrastructure
25+
// provider and may be either cluster or namespace-scoped.
26+
// +kubebuilder:validation:MinLength=1
27+
Kind string `json:"kind"`
28+
29+
// Name of the infrastructure identity to be used.
30+
// Must be either a cluster-scoped resource, or namespaced-scoped
31+
// resource the same namespace as the resource(s) being provisioned.
32+
Name string `json:"name"`
33+
}

api/v1alpha4/openstackcluster_types.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1alpha4
1818

1919
import (
20-
corev1 "k8s.io/api/core/v1"
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
2322
capierrors "sigs.k8s.io/cluster-api/errors"
@@ -31,11 +30,6 @@ const (
3130

3231
// OpenStackClusterSpec defines the desired state of OpenStackCluster.
3332
type OpenStackClusterSpec struct {
34-
35-
// The name of the secret containing the openstack credentials
36-
// +optional
37-
CloudsSecret *corev1.SecretReference `json:"cloudsSecret"`
38-
3933
// The name of the cloud to use from the clouds secret
4034
// +optional
4135
CloudName string `json:"cloudName"`
@@ -106,6 +100,11 @@ type OpenStackClusterSpec struct {
106100
// Bastion is the OpenStack instance to login the nodes
107101
//+optional
108102
Bastion *Bastion `json:"bastion,omitempty"`
103+
104+
// IdentityRef is a reference to a identity to be used when reconciling this cluster
105+
// +optional
106+
// +k8s:conversion-gen=false
107+
IdentityRef *OpenStackIdentityReference `json:"identityRef,omitempty"`
109108
}
110109

111110
// OpenStackClusterStatus defines the observed state of OpenStackCluster.

api/v1alpha4/openstackcluster_webhook.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package v1alpha4
1818

1919
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
"k8s.io/apimachinery/pkg/util/validation/field"
2022
"sigs.k8s.io/controller-runtime/pkg/builder"
2123
logf "sigs.k8s.io/controller-runtime/pkg/log"
2224
"sigs.k8s.io/controller-runtime/pkg/manager"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
2326
)
2427

2528
// log is for logging in this package.
@@ -30,3 +33,45 @@ func (r *OpenStackCluster) SetupWebhookWithManager(mgr manager.Manager) error {
3033
For(r).
3134
Complete()
3235
}
36+
37+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackcluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackcluster,versions=v1alpha4,name=validation.openstackcluster.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
38+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackcluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackcluster,versions=v1alpha4,name=default.openstackcluster.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
39+
40+
var (
41+
_ webhook.Defaulter = &OpenStackCluster{}
42+
_ webhook.Validator = &OpenStackCluster{}
43+
)
44+
45+
// Default satisfies the defaulting webhook interface.
46+
func (r *OpenStackCluster) Default() {
47+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind == "" {
48+
r.Spec.IdentityRef.Kind = defaultIdentityRefKind
49+
}
50+
}
51+
52+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
53+
func (r *OpenStackCluster) ValidateCreate() error {
54+
var allErrs field.ErrorList
55+
56+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
57+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "identityRef", "kind"), "must be a Secret"))
58+
}
59+
60+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
61+
}
62+
63+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
64+
func (r *OpenStackCluster) ValidateUpdate(old runtime.Object) error {
65+
var allErrs field.ErrorList
66+
67+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
68+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "identityRef", "kind"), "must be a Secret"))
69+
}
70+
71+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
72+
}
73+
74+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
75+
func (r *OpenStackCluster) ValidateDelete() error {
76+
return nil
77+
}

api/v1alpha4/openstackmachine_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ type OpenStackMachineSpec struct {
3636
// InstanceID is the OpenStack instance ID for this machine.
3737
InstanceID *string `json:"instanceID,omitempty"`
3838

39-
// The name of the secret containing the openstack credentials
40-
// +optional
41-
CloudsSecret *corev1.SecretReference `json:"cloudsSecret"`
42-
4339
// The name of the cloud to use from the clouds secret
4440
// +optional
4541
CloudName string `json:"cloudName"`
@@ -90,6 +86,11 @@ type OpenStackMachineSpec struct {
9086

9187
// The server group to assign the machine to
9288
ServerGroupID string `json:"serverGroupID,omitempty"`
89+
90+
// IdentityRef is a reference to a identity to be used when reconciling this cluster
91+
// +optional
92+
// +k8s:conversion-gen=false
93+
IdentityRef *OpenStackIdentityReference `json:"identityRef,omitempty"`
9394
}
9495

9596
// OpenStackMachineStatus defines the observed state of OpenStackMachine.

api/v1alpha4/openstackmachine_webhook.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,28 @@ func (r *OpenStackMachine) SetupWebhookWithManager(mgr manager.Manager) error {
3939
}
4040

4141
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackmachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackmachines,versions=v1alpha4,name=validation.openstackmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
42+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackmachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackmachines,versions=v1alpha4,name=default.openstackmachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
4243

43-
var _ webhook.Validator = &OpenStackMachine{}
44+
var (
45+
_ webhook.Defaulter = &OpenStackMachine{}
46+
_ webhook.Validator = &OpenStackMachine{}
47+
)
48+
49+
// Default satisfies the defaulting webhook interface.
50+
func (r *OpenStackMachine) Default() {
51+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind == "" {
52+
r.Spec.IdentityRef.Kind = defaultIdentityRefKind
53+
}
54+
}
4455

4556
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
4657
func (r *OpenStackMachine) ValidateCreate() error {
4758
var allErrs field.ErrorList
4859

60+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
61+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "identityRef", "kind"), "must be a Secret"))
62+
}
63+
4964
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
5065
}
5166

@@ -66,6 +81,10 @@ func (r *OpenStackMachine) ValidateUpdate(old runtime.Object) error {
6681

6782
var allErrs field.ErrorList
6883

84+
if r.Spec.IdentityRef != nil && r.Spec.IdentityRef.Kind != defaultIdentityRefKind {
85+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "identityRef", "kind"), "must be a Secret"))
86+
}
87+
6988
newOpenStackMachineSpec := newOpenStackMachine["spec"].(map[string]interface{})
7089
oldOpenStackMachineSpec := oldOpenStackMachine["spec"].(map[string]interface{})
7190

api/v1alpha4/openstackmachinetemplate_webhook.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ func (r *OpenStackMachineTemplate) ValidateUpdate(old runtime.Object) error {
6161
)
6262
}
6363

64-
if len(allErrs) != 0 {
65-
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
66-
}
67-
68-
return nil
64+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
6965
}
7066

7167
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.

0 commit comments

Comments
 (0)