Skip to content

Commit

Permalink
Merge pull request #2585 from ykakarap/awsclustertemplate
Browse files Browse the repository at this point in the history
add AWSClusterTemplate type
  • Loading branch information
k8s-ci-robot committed Jul 19, 2021
2 parents 11e8246 + 51164fb commit 33c9ef9
Show file tree
Hide file tree
Showing 14 changed files with 701 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ generate: ## Generate code

.PHONY: generate-go
generate-go: $(MOCKGEN)
go generate ./...
$(MAKE) generate-go-core
$(MAKE) generate-go-eks-bootstrap
$(MAKE) generate-go-eks-controlplane
go generate ./...

.PHONY: generate-go-core
generate-go-core: ## Runs Go related generate targets
Expand Down
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ resources:
- group: infrastructure
version: v1alpha4
kind: AWSClusterControllerIdentity
- group: infrastructure
version: v1alpha4
kind: AWSClusterTemplate
20 changes: 12 additions & 8 deletions api/v1alpha4/awscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,21 @@ func (r *AWSCluster) ValidateUpdate(old runtime.Object) error {

// Default satisfies the defaulting webhook interface.
func (r *AWSCluster) Default() {
SetDefaults_Bastion(&r.Spec.Bastion)
SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
SetDefaultsAWSClusterSpec(&r.Spec)
}

func (r *AWSCluster) validateSSHKeyName() field.ErrorList {
return validateSSHKeyName(r.Spec.SSHKeyName)
}

if r.Spec.IdentityRef == nil {
r.Spec.IdentityRef = &AWSIdentityReference{
func SetDefaultsAWSClusterSpec(s *AWSClusterSpec) {
SetDefaults_Bastion(&s.Bastion)
SetDefaults_NetworkSpec(&s.NetworkSpec)

if s.IdentityRef == nil {
s.IdentityRef = &AWSIdentityReference{
Kind: ControllerIdentityKind,
Name: AWSClusterControllerIdentityName,
}
}
}

func (r *AWSCluster) validateSSHKeyName() field.ErrorList {
return validateSSHKeyName(r.Spec.SSHKeyName)
}
55 changes: 55 additions & 0 deletions api/v1alpha4/awsclustertemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha4

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AWSClusterTemplateSpec defines the desired state of AWSClusterTemplate.
type AWSClusterTemplateSpec struct {
Template AWSClusterTemplateResource `json:"template"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=awsclustertemplates,scope=Namespaced,categories=cluster-api,shortName=awsct
// +kubebuilder:storageversion

// AWSClusterTemplate is the Schema for the awsclustertemplates API.
type AWSClusterTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec AWSClusterTemplateSpec `json:"spec,omitempty"`
}

//+kubebuilder:object:root=true

// AWSClusterTemplateList contains a list of AWSClusterTemplate.
type AWSClusterTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []AWSClusterTemplate `json:"items"`
}

func init() {
SchemeBuilder.Register(&AWSClusterTemplate{}, &AWSClusterTemplateList{})
}

type AWSClusterTemplateResource struct {
Spec AWSClusterSpec `json:"spec"`
}
70 changes: 70 additions & 0 deletions api/v1alpha4/awsclustertemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha4

import (
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

func (r *AWSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// +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
// +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

var _ webhook.Defaulter = &AWSClusterTemplate{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *AWSClusterTemplate) Default() {
SetDefaultsAWSClusterSpec(&r.Spec.Template.Spec)
}

var _ webhook.Validator = &AWSClusterTemplate{}

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

allErrs = append(allErrs, r.Spec.Template.Spec.Bastion.Validate()...)
allErrs = append(allErrs, validateSSHKeyName(r.Spec.Template.Spec.SSHKeyName)...)

return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
old := oldRaw.(*AWSClusterTemplate)

if !reflect.DeepEqual(r.Spec, old.Spec) {
return apierrors.NewBadRequest("AWSClusterTempalate.Spec is immutable")
}
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterTemplate) ValidateDelete() error {
return nil
}
90 changes: 90 additions & 0 deletions api/v1alpha4/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33c9ef9

Please sign in to comment.