Skip to content

Commit b96f452

Browse files
committed
Move defaulting to webhook rather than openapi
Attemping to use openapi defaulting for replicas resulted in the following error: STEP: bootstrapping test environment Failure [9.838 seconds] [BeforeSuite] BeforeSuite /home/prow/go/src/sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2/suite_test.go:56 Unexpected error: <*errors.StatusError | 0xc0003d4280>: { ErrStatus: { TypeMeta: {Kind: "", APIVersion: ""}, ListMeta: { SelfLink: "", ResourceVersion: "", Continue: "", RemainingItemCount: nil, }, Status: "Failure", Message: "CustomResourceDefinition.apiextensions.k8s.io \"kubeadmcontrolplanes.cluster.x-k8s.io\" is invalid: spec.validation.openAPIV3Schema.properties[spec].properties[replicas].default: Forbidden: must not be set", Reason: "Invalid", Details: { Name: "kubeadmcontrolplanes.cluster.x-k8s.io", Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition", UID: "", Causes: [ { Type: "FieldValueForbidden", Message: "Forbidden: must not be set", Field: "spec.validation.openAPIV3Schema.properties[spec].properties[replicas].default", }, ], RetryAfterSeconds: 0, }, Code: 422, }, } CustomResourceDefinition.apiextensions.k8s.io "kubeadmcontrolplanes.cluster.x-k8s.io" is invalid: spec.validation.openAPIV3Schema.properties[spec].properties[replicas].default: Forbidden: must not be set occurred /home/prow/go/src/sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2/suite_test.go:66 ------------------------------ Ran 1 of 0 Specs in 9.949 seconds FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped --- FAIL: TestAPIs (9.95s) FAIL FAIL sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2 10.011s
1 parent ba6c20a commit b96f452

5 files changed

+59
-2
lines changed

api/v1alpha3/kubeadm_control_plane_types.go

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type KubeadmControlPlaneSpec struct {
2828
// odd numbers are permitted, as per [etcd best practice](https://etcd.io/docs/v3.3.12/faq/#why-an-odd-number-of-cluster-members).
2929
// This is a pointer to distinguish between explicit zero and not specified.
3030
// +optional
31-
// +kubebuilder:default:=1
3231
Replicas *int32 `json:"replicas,omitempty"`
3332

3433
// Version defines the desired Kubernetes version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2019 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 v1alpha3
18+
19+
import (
20+
"sigs.k8s.io/controller-runtime/pkg/webhook"
21+
)
22+
23+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1alpha3-kubeadmcontrolplane,mutating=true,failurePolicy=fail,groups=cluster.x-k8s.io,resources=kubeadmcontrolplane,versions=v1alpha3,name=default.kubeadmcontrolplane.cluster.x-k8s.io
24+
25+
var _ webhook.Defaulter = &KubeadmControlPlane{}
26+
27+
// Default implements webhook.Defaulter so a webhook will be registered for the type
28+
func (r *KubeadmControlPlane) Default() {
29+
if r.Spec.Replicas == nil {
30+
replicas := int32(1)
31+
r.Spec.Replicas = &replicas
32+
}
33+
}

config/crd/bases/cluster.x-k8s.io_kubeadmcontrolplanes.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,6 @@ spec:
861861
type: array
862862
type: object
863863
replicas:
864-
default: 1
865864
description: Number of desired machines. Defaults to 1. When stacked
866865
etcd is used only odd numbers are permitted, as per [etcd best practice](https://etcd.io/docs/v3.3.12/faq/#why-an-odd-number-of-cluster-members).
867866
This is a pointer to distinguish between explicit zero and not specified.

config/webhook/manifests.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11

2+
---
3+
apiVersion: admissionregistration.k8s.io/v1beta1
4+
kind: MutatingWebhookConfiguration
5+
metadata:
6+
creationTimestamp: null
7+
name: mutating-webhook-configuration
8+
webhooks:
9+
- clientConfig:
10+
caBundle: Cg==
11+
service:
12+
name: webhook-service
13+
namespace: system
14+
path: /mutate-cluster-x-k8s-io-v1alpha3-kubeadmcontrolplane
15+
failurePolicy: Fail
16+
name: default.kubeadmcontrolplane.cluster.x-k8s.io
17+
rules:
18+
- apiGroups:
19+
- cluster.x-k8s.io
20+
apiVersions:
21+
- v1alpha3
22+
operations:
23+
- CREATE
24+
- UPDATE
25+
resources:
26+
- kubeadmcontrolplane
27+
228
---
329
apiVersion: admissionregistration.k8s.io/v1beta1
430
kind: ValidatingWebhookConfiguration

0 commit comments

Comments
 (0)