Skip to content

Commit 33c9ef9

Browse files
authored
Merge pull request #2585 from ykakarap/awsclustertemplate
add AWSClusterTemplate type
2 parents 11e8246 + 51164fb commit 33c9ef9

14 files changed

+701
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ generate: ## Generate code
276276

277277
.PHONY: generate-go
278278
generate-go: $(MOCKGEN)
279-
go generate ./...
280279
$(MAKE) generate-go-core
281280
$(MAKE) generate-go-eks-bootstrap
282281
$(MAKE) generate-go-eks-controlplane
282+
go generate ./...
283283

284284
.PHONY: generate-go-core
285285
generate-go-core: ## Runs Go related generate targets

PROJECT

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ resources:
3838
- group: infrastructure
3939
version: v1alpha4
4040
kind: AWSClusterControllerIdentity
41+
- group: infrastructure
42+
version: v1alpha4
43+
kind: AWSClusterTemplate

api/v1alpha4/awscluster_webhook.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,21 @@ func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {
144144

145145
// Default satisfies the defaulting webhook interface.
146146
func (r *AWSCluster) Default() {
147-
SetDefaults_Bastion(&r.Spec.Bastion)
148-
SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
147+
SetDefaultsAWSClusterSpec(&r.Spec)
148+
}
149+
150+
func (r *AWSCluster) validateSSHKeyName() field.ErrorList {
151+
return validateSSHKeyName(r.Spec.SSHKeyName)
152+
}
149153

150-
if r.Spec.IdentityRef == nil {
151-
r.Spec.IdentityRef = &AWSIdentityReference{
154+
func SetDefaultsAWSClusterSpec(s *AWSClusterSpec) {
155+
SetDefaults_Bastion(&s.Bastion)
156+
SetDefaults_NetworkSpec(&s.NetworkSpec)
157+
158+
if s.IdentityRef == nil {
159+
s.IdentityRef = &AWSIdentityReference{
152160
Kind: ControllerIdentityKind,
153161
Name: AWSClusterControllerIdentityName,
154162
}
155163
}
156164
}
157-
158-
func (r *AWSCluster) validateSSHKeyName() field.ErrorList {
159-
return validateSSHKeyName(r.Spec.SSHKeyName)
160-
}
Lines changed: 55 additions & 0 deletions
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+
// AWSClusterTemplateSpec defines the desired state of AWSClusterTemplate.
24+
type AWSClusterTemplateSpec struct {
25+
Template AWSClusterTemplateResource `json:"template"`
26+
}
27+
28+
// +kubebuilder:object:root=true
29+
// +kubebuilder:resource:path=awsclustertemplates,scope=Namespaced,categories=cluster-api,shortName=awsct
30+
// +kubebuilder:storageversion
31+
32+
// AWSClusterTemplate is the Schema for the awsclustertemplates API.
33+
type AWSClusterTemplate struct {
34+
metav1.TypeMeta `json:",inline"`
35+
metav1.ObjectMeta `json:"metadata,omitempty"`
36+
37+
Spec AWSClusterTemplateSpec `json:"spec,omitempty"`
38+
}
39+
40+
//+kubebuilder:object:root=true
41+
42+
// AWSClusterTemplateList contains a list of AWSClusterTemplate.
43+
type AWSClusterTemplateList struct {
44+
metav1.TypeMeta `json:",inline"`
45+
metav1.ListMeta `json:"metadata,omitempty"`
46+
Items []AWSClusterTemplate `json:"items"`
47+
}
48+
49+
func init() {
50+
SchemeBuilder.Register(&AWSClusterTemplate{}, &AWSClusterTemplateList{})
51+
}
52+
53+
type AWSClusterTemplateResource struct {
54+
Spec AWSClusterSpec `json:"spec"`
55+
}
Lines changed: 70 additions & 0 deletions
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+
"reflect"
21+
22+
apierrors "k8s.io/apimachinery/pkg/api/errors"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/util/validation/field"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook"
27+
)
28+
29+
func (r *AWSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
30+
return ctrl.NewWebhookManagedBy(mgr).
31+
For(r).
32+
Complete()
33+
}
34+
35+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha4-awsclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1alpha4,name=validation.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
36+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-awsclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1alpha4,name=default.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
37+
38+
var _ webhook.Defaulter = &AWSClusterTemplate{}
39+
40+
// Default implements webhook.Defaulter so a webhook will be registered for the type.
41+
func (r *AWSClusterTemplate) Default() {
42+
SetDefaultsAWSClusterSpec(&r.Spec.Template.Spec)
43+
}
44+
45+
var _ webhook.Validator = &AWSClusterTemplate{}
46+
47+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
48+
func (r *AWSClusterTemplate) ValidateCreate() error {
49+
var allErrs field.ErrorList
50+
51+
allErrs = append(allErrs, r.Spec.Template.Spec.Bastion.Validate()...)
52+
allErrs = append(allErrs, validateSSHKeyName(r.Spec.Template.Spec.SSHKeyName)...)
53+
54+
return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
55+
}
56+
57+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
58+
func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
59+
old := oldRaw.(*AWSClusterTemplate)
60+
61+
if !reflect.DeepEqual(r.Spec, old.Spec) {
62+
return apierrors.NewBadRequest("AWSClusterTempalate.Spec is immutable")
63+
}
64+
return nil
65+
}
66+
67+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
68+
func (r *AWSClusterTemplate) ValidateDelete() error {
69+
return nil
70+
}

api/v1alpha4/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)