-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
568 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.