Skip to content

Commit c46cbfd

Browse files
author
Yuvaraj Kakaraparthi
committed
CAPD: add DockerClusterTemplate type
1 parent 37c862b commit c46cbfd

13 files changed

+587
-18
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,19 @@
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+
func (s *DockerClusterSpec) setDefaults() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 "k8s.io/apimachinery/pkg/util/validation/field"
20+
21+
func (s *DockerClusterSpec) validate() field.ErrorList {
22+
return nil
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
ctrl "sigs.k8s.io/controller-runtime"
23+
logf "sigs.k8s.io/controller-runtime/pkg/log"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook"
25+
)
26+
27+
// log is for logging in this package.
28+
var dockerclusterlog = logf.Log.WithName("dockercluster-resource")
29+
30+
func (r *DockerCluster) 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-dockercluster,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclusters,versions=v1alpha4,name=validation.dockercluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
37+
38+
var _ webhook.Defaulter = &DockerCluster{}
39+
40+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
41+
func (r *DockerCluster) Default() {
42+
dockerclusterlog.Info("default", "name", r.Name)
43+
r.Spec.setDefaults()
44+
}
45+
46+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-dockercluster,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclusters,versions=v1alpha4,name=validation.dockercluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
47+
48+
var _ webhook.Validator = &DockerCluster{}
49+
50+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
51+
func (r *DockerCluster) ValidateCreate() error {
52+
dockerclusterlog.Info("validate create", "name", r.Name)
53+
allErrs := r.Spec.validate()
54+
if len(allErrs) > 0 {
55+
return apierrors.NewInvalid(GroupVersion.WithKind("DockerCluster").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 *DockerCluster) ValidateUpdate(old runtime.Object) error {
62+
dockerclusterlog.Info("validate update", "name", r.Name)
63+
return nil
64+
}
65+
66+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
67+
func (r *DockerCluster) ValidateDelete() error {
68+
dockerclusterlog.Info("validate delete", "name", r.Name)
69+
return nil
70+
}
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,77 @@
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+
"errors"
21+
"reflect"
22+
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
24+
"k8s.io/apimachinery/pkg/runtime"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
logf "sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/webhook"
28+
)
29+
30+
// log is for logging in this package.
31+
var dockerclustertemplatelog = logf.Log.WithName("dockerclustertemplate-resource")
32+
33+
func (r *DockerClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
34+
return ctrl.NewWebhookManagedBy(mgr).
35+
For(r).
36+
Complete()
37+
}
38+
39+
//+kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-dockerclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1alpha4,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
40+
41+
var _ webhook.Defaulter = &DockerClusterTemplate{}
42+
43+
// Default implements webhook.Defaulter so a webhook will be registered for the type
44+
func (r *DockerClusterTemplate) Default() {
45+
dockerclustertemplatelog.Info("default", "name", r.Name)
46+
r.Spec.Template.Spec.setDefaults()
47+
}
48+
49+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-dockerclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1alpha4,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
50+
51+
var _ webhook.Validator = &DockerClusterTemplate{}
52+
53+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
54+
func (r *DockerClusterTemplate) ValidateCreate() error {
55+
dockerclustertemplatelog.Info("validate create", "name", r.Name)
56+
allErrs := r.Spec.Template.Spec.validate()
57+
if len(allErrs) > 0 {
58+
return apierrors.NewInvalid(GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), r.Name, allErrs)
59+
}
60+
return nil
61+
}
62+
63+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
64+
func (r *DockerClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
65+
dockerclustertemplatelog.Info("validate update", "name", r.Name)
66+
old := oldRaw.(*DockerClusterTemplate)
67+
if !reflect.DeepEqual(r.Spec, old.Spec) {
68+
return errors.New("DockerClusterTemplateSpec is immutable")
69+
}
70+
return nil
71+
}
72+
73+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
74+
func (r *DockerClusterTemplate) ValidateDelete() error {
75+
dockerclustertemplatelog.Info("validate delete", "name", r.Name)
76+
return nil
77+
}

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)