Skip to content

Commit

Permalink
Merge pull request #402 from shivi28/gcp_397
Browse files Browse the repository at this point in the history
GCPClusterTemplate CRD added
  • Loading branch information
k8s-ci-robot authored Jul 27, 2021
2 parents 5286fc0 + 392e9a9 commit 5fed20f
Show file tree
Hide file tree
Showing 13 changed files with 568 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ resources:
- group: infrastructure
version: v1alpha4
kind: GCPMachineTemplate
- group: infrastructure
version: v1alpha4
kind: GCPClusterTemplate
56 changes: 56 additions & 0 deletions api/v1alpha4/gcpclustertemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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"
)

// GCPClusterTemplateSpec defines the desired state of GCPClusterTemplate.
type GCPClusterTemplateSpec struct {
Template GCPClusterTemplateResource `json:"template"`
}

// GCPClusterTemplateResource contains spec for GCPClusterSpec.
type GCPClusterTemplateResource struct {
Spec GCPClusterSpec `json:"spec"`
}

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

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

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

//+kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&GCPClusterTemplate{}, &GCPClusterTemplateList{})
}
76 changes: 76 additions & 0 deletions api/v1alpha4/gcpclustertemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
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 (
"fmt"
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var gcpclustertemplatelog = logf.Log.WithName("gcpclustertemplate-resource")

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

//+kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha4-gcpclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=gcpclustertemplates,versions=v1alpha4,name=default.gcpclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1

var _ webhook.Defaulter = &GCPClusterTemplate{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *GCPClusterTemplate) Default() {
gcpclustertemplatelog.Info("default", "name", r.Name)
}

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

var _ webhook.Validator = &GCPClusterTemplate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *GCPClusterTemplate) ValidateCreate() error {
gcpclustertemplatelog.Info("validate create", "name", r.Name)

return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *GCPClusterTemplate) ValidateUpdate(oldRaw runtime.Object) error {
old, ok := oldRaw.(*GCPClusterTemplate)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected an GCPClusterTemplate but got a %T", oldRaw))
}

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

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *GCPClusterTemplate) ValidateDelete() error {
gcpclustertemplatelog.Info("validate delete", "name", r.Name)
return nil
}
86 changes: 86 additions & 0 deletions api/v1alpha4/gcpclustertemplate_webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
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 (
"testing"

. "github.com/onsi/gomega"
)

func TestGCPClusterTemplate_ValidateUpdate(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
newTemplate *GCPClusterTemplate
oldTemplate *GCPClusterTemplate
wantErr bool
}{
{
name: "GCPClusterTemplated with immutable spec",
newTemplate: &GCPClusterTemplate{
Spec: GCPClusterTemplateSpec{
Template: GCPClusterTemplateResource{GCPClusterSpec{
Project: "test-gcp-cluster",
Region: "ap-south-1",
}},
},
},
oldTemplate: &GCPClusterTemplate{
Spec: GCPClusterTemplateSpec{
Template: GCPClusterTemplateResource{GCPClusterSpec{
Project: "test-gcp-cluster",
Region: "ap-south-1",
}},
},
},
wantErr: false,
},
{
name: "GCPClusterTemplated with mutable spec",
newTemplate: &GCPClusterTemplate{
Spec: GCPClusterTemplateSpec{
Template: GCPClusterTemplateResource{GCPClusterSpec{
Project: "test-gcp-cluster",
Region: "ap-south-1",
}},
},
},
oldTemplate: &GCPClusterTemplate{
Spec: GCPClusterTemplateSpec{
Template: GCPClusterTemplateResource{GCPClusterSpec{
Project: "test-gcp-cluster",
Region: "ap-east-1",
}},
},
},
wantErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
err := test.newTemplate.ValidateUpdate(test.oldTemplate)
if test.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).NotTo(HaveOccurred())
}
})
}
}
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.

Loading

0 comments on commit 5fed20f

Please sign in to comment.