Skip to content

Commit 565286f

Browse files
author
Yuvaraj Kakaraparthi
committed
CAPD: add DockerClusterTemplate type
1 parent 109abc5 commit 565286f

13 files changed

+578
-25
lines changed

test/infrastructure/docker/PROJECT

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

test/infrastructure/docker/api/v1alpha4/dockermachinetemplate_webhook.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ limitations under the License.
1717
package v1alpha4
1818

1919
import (
20-
"errors"
20+
"fmt"
2121
"reflect"
2222

23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2324
runtime "k8s.io/apimachinery/pkg/runtime"
25+
"k8s.io/apimachinery/pkg/util/validation/field"
2426
ctrl "sigs.k8s.io/controller-runtime"
2527
"sigs.k8s.io/controller-runtime/pkg/webhook"
2628
)
2729

30+
const dockerMachineTemplateImmutableMsg = "DockerMachineTemplate spec.template.spec field is immutable. Please create a new resource instead."
31+
2832
func (m *DockerMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
2933
return ctrl.NewWebhookManagedBy(mgr).
3034
For(m).
@@ -41,12 +45,19 @@ func (m *DockerMachineTemplate) ValidateCreate() error {
4145
}
4246

4347
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
44-
func (m *DockerMachineTemplate) ValidateUpdate(old runtime.Object) error {
45-
oldCRS := old.(*DockerMachineTemplate)
46-
if !reflect.DeepEqual(m.Spec, oldCRS.Spec) {
47-
return errors.New("DockerMachineTemplateSpec is immutable")
48+
func (m *DockerMachineTemplate) ValidateUpdate(oldRaw runtime.Object) error {
49+
var allErrs field.ErrorList
50+
old, ok := oldRaw.(*DockerMachineTemplate)
51+
if !ok {
52+
return apierrors.NewBadRequest(fmt.Sprintf("expected a DockerMachineTemplate but got a %T", oldRaw))
4853
}
49-
return nil
54+
if !reflect.DeepEqual(m.Spec.Template.Spec, old.Spec.Template.Spec) {
55+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "template", "spec"), m, dockerMachineTemplateImmutableMsg))
56+
}
57+
if len(allErrs) == 0 {
58+
return nil
59+
}
60+
return apierrors.NewInvalid(GroupVersion.WithKind("DockerMachineTemplate").GroupKind(), m.Name, allErrs)
5061
}
5162

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

test/infrastructure/docker/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)