-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patryk Strusiewicz-Surmacki <[email protected]>
- Loading branch information
1 parent
1c662f1
commit 8834b66
Showing
21 changed files
with
1,010 additions
and
13 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
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 v1beta1 | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// Metal3ClusterTemplateSpec defines the desired state of Metal3ClusterTemplate. | ||
type Metal3ClusterTemplateSpec struct { | ||
Template Metal3ClusterTemplateResource `json:"template"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:resource:path=metal3clustertemplates,scope=Namespaced,categories=cluster-api | ||
// +kubebuilder:storageversion | ||
|
||
// Metal3ClusterTemplate is the Schema for the metal3clustertemplates API. | ||
type Metal3ClusterTemplate struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec Metal3ClusterTemplateSpec `json:"spec,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
// Metal3ClusterTemplateList contains a list of Metal3ClusterTemplate. | ||
type Metal3ClusterTemplateList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []Metal3ClusterTemplate `json:"items"` | ||
} | ||
|
||
func init() { | ||
objectTypes = append(objectTypes, &Metal3ClusterTemplate{}, &Metal3ClusterTemplateList{}) | ||
} | ||
|
||
// Metal3ClusterTemplateResource describes the data for creating a Metal3Cluster from a template. | ||
type Metal3ClusterTemplateResource struct { | ||
Spec Metal3ClusterSpec `json:"spec"` | ||
} |
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,66 @@ | ||
/* | ||
Copyright 2020 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 v1beta1 | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
func (c *Metal3ClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(c). | ||
Complete() | ||
} | ||
|
||
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-metal3clustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=metal3clustertemplates,versions=v1beta1,name=validation.metal3clustertemplate.infrastructure.cluster.x-k8s.io,matchPolicy=Equivalent,sideEffects=None,admissionReviewVersions=v1;v1beta1 | ||
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-metal3clustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=metal3clustertemplates,versions=v1beta1,name=default.metal3clustertemplate.infrastructure.cluster.x-k8s.io,matchPolicy=Equivalent,sideEffects=None,admissionReviewVersions=v1;v1beta1 | ||
|
||
var _ webhook.Defaulter = &Metal3ClusterTemplate{} | ||
var _ webhook.Validator = &Metal3ClusterTemplate{} | ||
|
||
func (c *Metal3ClusterTemplate) Default() { | ||
} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type. | ||
func (c *Metal3ClusterTemplate) ValidateCreate() (admission.Warnings, error) { | ||
return nil, c.validate() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. | ||
func (c *Metal3ClusterTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { | ||
oldM3ct, ok := old.(*Metal3ClusterTemplate) | ||
if !ok || oldM3ct == nil { | ||
return nil, apierrors.NewInternalError(errors.New("unable to convert existing object")) | ||
} | ||
|
||
if err := oldM3ct.Spec.Template.Spec.IsValid(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, c.validate() | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type. | ||
func (c *Metal3ClusterTemplate) ValidateDelete() (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (c *Metal3ClusterTemplate) validate() error { | ||
return c.Spec.Template.Spec.IsValid() | ||
} |
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,179 @@ | ||
/* | ||
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 v1beta1 | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestMetal3ClusterTemplateDefault(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
c := &Metal3ClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
}, | ||
Spec: Metal3ClusterTemplateSpec{}, | ||
} | ||
c.Default() | ||
|
||
g.Expect(c.Spec).To(Equal(Metal3ClusterTemplateSpec{})) | ||
} | ||
|
||
func TestMetal3ClusterTemplateValidation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectErr bool | ||
c *Metal3ClusterTemplate | ||
}{ | ||
{ | ||
name: "should succeed when values and templates correct", | ||
expectErr: false, | ||
c: &Metal3ClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
}, | ||
Spec: Metal3ClusterTemplateSpec{ | ||
Template: Metal3ClusterTemplateResource{ | ||
Spec: Metal3ClusterSpec{ | ||
ControlPlaneEndpoint: APIEndpoint{ | ||
Host: "127.0.0.1", | ||
Port: 4242, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
if tt.expectErr { | ||
_, err := tt.c.ValidateCreate() | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
_, err := tt.c.ValidateCreate() | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
_, err := tt.c.ValidateDelete() | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
} | ||
} | ||
|
||
func TestMetal3ClusterTemplateUpdateValidation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expectErr bool | ||
new *Metal3ClusterTemplateSpec | ||
old *Metal3ClusterTemplateSpec | ||
}{ | ||
{ | ||
name: "should succeed when values and templates correct", | ||
expectErr: false, | ||
|
||
new: &Metal3ClusterTemplateSpec{ | ||
Template: Metal3ClusterTemplateResource{ | ||
Spec: Metal3ClusterSpec{ | ||
ControlPlaneEndpoint: APIEndpoint{ | ||
Host: "127.0.0.1", | ||
Port: 4242, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
old: &Metal3ClusterTemplateSpec{ | ||
Template: Metal3ClusterTemplateResource{ | ||
Spec: Metal3ClusterSpec{ | ||
ControlPlaneEndpoint: APIEndpoint{ | ||
Host: "127.0.0.1", | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "should fail if old template is invalid (e.g. missing host or port)", | ||
expectErr: true, | ||
|
||
new: &Metal3ClusterTemplateSpec{ | ||
Template: Metal3ClusterTemplateResource{ | ||
Spec: Metal3ClusterSpec{ | ||
ControlPlaneEndpoint: APIEndpoint{ | ||
Host: "127.0.0.1", | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
}, | ||
old: &Metal3ClusterTemplateSpec{}, | ||
}, | ||
{ | ||
name: "should fail if new template is invalid (e.g. missing host or port)", | ||
expectErr: true, | ||
|
||
new: &Metal3ClusterTemplateSpec{}, | ||
old: &Metal3ClusterTemplateSpec{ | ||
Template: Metal3ClusterTemplateResource{ | ||
Spec: Metal3ClusterSpec{ | ||
ControlPlaneEndpoint: APIEndpoint{ | ||
Host: "127.0.0.1", | ||
Port: 8080, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var newCT, oldCT *Metal3ClusterTemplate | ||
g := NewWithT(t) | ||
newCT = &Metal3ClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
}, | ||
Spec: *tt.new, | ||
} | ||
|
||
if tt.old != nil { | ||
oldCT = &Metal3ClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
}, | ||
Spec: *tt.old, | ||
} | ||
} else { | ||
oldCT = nil | ||
} | ||
|
||
if tt.expectErr { | ||
_, err := newCT.ValidateUpdate(oldCT) | ||
g.Expect(err).To(HaveOccurred()) | ||
} else { | ||
_, err := newCT.ValidateUpdate(oldCT) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.