Skip to content

Commit f07c27e

Browse files
authored
Merge pull request #933 from Daimler/tobiasgiese/add-openstackclustertemplate
✨ Add OpenStackClusterTemplates Type
2 parents 7b0420e + 0322214 commit f07c27e

11 files changed

+952
-3
lines changed

PROJECT

+3
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ resources:
2020
- group: infrastructure
2121
version: v1alpha4
2222
kind: OpenStackMachineTemplate
23+
- group: infrastructure
24+
kind: OpenStackClusterTemplate
25+
version: v1alpha4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// OpenStackClusterTemplateResource describes the data needed to create a OpenStackCluster from a template.
24+
type OpenStackClusterTemplateResource struct {
25+
Spec OpenStackClusterSpec `json:"spec"`
26+
}
27+
28+
// OpenStackClusterTemplateSpec defines the desired state of OpenStackClusterTemplate.
29+
type OpenStackClusterTemplateSpec struct {
30+
Template OpenStackClusterTemplateResource `json:"template"`
31+
}
32+
33+
//+kubebuilder:object:root=true
34+
//+kubebuilder:resource:path=openstackclustertemplates,scope=Namespaced,categories=cluster-api,shortName=osct
35+
36+
// OpenStackClusterTemplate is the Schema for the openstackclustertemplates API.
37+
type OpenStackClusterTemplate struct {
38+
metav1.TypeMeta `json:",inline"`
39+
metav1.ObjectMeta `json:"metadata,omitempty"`
40+
41+
Spec OpenStackClusterTemplateSpec `json:"spec,omitempty"`
42+
}
43+
44+
//+kubebuilder:object:root=true
45+
46+
// OpenStackClusterTemplateList contains a list of OpenStackClusterTemplate.
47+
type OpenStackClusterTemplateList struct {
48+
metav1.TypeMeta `json:",inline"`
49+
metav1.ListMeta `json:"metadata,omitempty"`
50+
Items []OpenStackClusterTemplate `json:"items"`
51+
}
52+
53+
func init() {
54+
SchemeBuilder.Register(&OpenStackClusterTemplate{}, &OpenStackClusterTemplateList{})
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
import (
20+
"reflect"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/apimachinery/pkg/util/validation/field"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
const openStackClusterTemplateImmutableMsg = "OpenStackClusterTemplate spec.template.spec field is immutable. Please create new resource instead."
29+
30+
func (r *OpenStackClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
31+
return ctrl.NewWebhookManagedBy(mgr).
32+
For(r).
33+
Complete()
34+
}
35+
36+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackclustertemplates,versions=v1alpha4,name=default.openstackclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
37+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackclustertemplates,versions=v1alpha4,name=validation.openstackclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
38+
39+
var (
40+
_ webhook.Defaulter = &OpenStackClusterTemplate{}
41+
_ webhook.Validator = &OpenStackClusterTemplate{}
42+
)
43+
44+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
45+
func (r *OpenStackClusterTemplate) Default() {
46+
if r.Spec.Template.Spec.IdentityRef != nil && r.Spec.Template.Spec.IdentityRef.Kind == "" {
47+
r.Spec.Template.Spec.IdentityRef.Kind = defaultIdentityRefKind
48+
}
49+
}
50+
51+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
52+
func (r *OpenStackClusterTemplate) ValidateCreate() error {
53+
var allErrs field.ErrorList
54+
55+
if r.Spec.Template.Spec.IdentityRef != nil && r.Spec.Template.Spec.IdentityRef.Kind != defaultIdentityRefKind {
56+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "template", "spec", "identityRef", "kind"), "must be a Secret"))
57+
}
58+
59+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
60+
}
61+
62+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
63+
func (r *OpenStackClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
64+
var allErrs field.ErrorList
65+
66+
old := oldRaw.(*OpenStackClusterTemplate)
67+
if !reflect.DeepEqual(r.Spec.Template.Spec, old.Spec.Template.Spec) {
68+
allErrs = append(allErrs,
69+
field.Invalid(field.NewPath("OpenStackClusterTemplate", "spec", "template", "spec"), r, openStackClusterTemplateImmutableMsg),
70+
)
71+
}
72+
73+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
74+
}
75+
76+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
77+
func (r *OpenStackClusterTemplate) ValidateDelete() error {
78+
return nil
79+
}

api/v1alpha4/openstackmachinetemplate_webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (r *OpenStackMachineTemplate) SetupWebhookWithManager(mgr manager.Manager)
3535
Complete()
3636
}
3737

38-
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackmachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=openstackmachinetemplates,versions=v1alpha4,name=validation.openstackmachinetemplate.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
38+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-openstackmachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.x-k8s.io,resources=openstackmachinetemplates,versions=v1alpha4,name=validation.openstackmachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
3939

4040
var _ webhook.Validator = &OpenStackMachineTemplate{}
4141

api/v1alpha4/zz_generated.deepcopy.go

+90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)