Skip to content

Commit

Permalink
validation for not having both local and external etcd in kcp
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed Mar 9, 2020
1 parent 5f5e57f commit f1b2729
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
27 changes: 27 additions & 0 deletions controlplane/kubeadm/api/v1alpha3/kubeadm_control_plane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (in *KubeadmControlPlane) ValidateUpdate(old runtime.Object) error {
allErrs := in.validateCommon()

prev := old.(*KubeadmControlPlane)

allErrs = append(allErrs, in.validateEtcd(prev)...)

originalJSON, err := json.Marshal(prev)
if err != nil {
return apierrors.NewInternalError(err)
Expand Down Expand Up @@ -229,6 +232,30 @@ func (in *KubeadmControlPlane) validateCommon() (allErrs field.ErrorList) {
return allErrs
}

func (in *KubeadmControlPlane) validateEtcd(prev *KubeadmControlPlane) (allErrs field.ErrorList) {
if in.Spec.KubeadmConfigSpec.InitConfiguration.Etcd.External != nil && prev.Spec.KubeadmConfigSpec.InitConfiguration.Etcd.Local != nil {
allErrs = append(
allErrs,
field.Forbidden(
field.NewPath("spec", "kubeadmConfigSpec", "initConfiguration", "etcd", "local"),
"cannot have both local and external etcd at the same time",
),
)
}

if in.Spec.KubeadmConfigSpec.InitConfiguration.Etcd.Local != nil && prev.Spec.KubeadmConfigSpec.InitConfiguration.Etcd.External != nil {
allErrs = append(
allErrs,
field.Forbidden(
field.NewPath("spec", "kubeadmConfigSpec", "initConfiguration", "etcd", "local"),
"cannot have both local and external etcd at the same time",
),
)
}

return allErrs
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *KubeadmControlPlane) ValidateDelete() error {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"time"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
kubeadmv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
)
Expand Down Expand Up @@ -293,6 +291,23 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
scaleToEvenExternalEtcdCluster := beforeExternalEtcdCluster.DeepCopy()
scaleToEvenExternalEtcdCluster.Spec.Replicas = pointer.Int32Ptr(2)

beforeInvalidEtcdCluster := before.DeepCopy()
beforeInvalidEtcdCluster.Spec.KubeadmConfigSpec.InitConfiguration.ClusterConfiguration.Etcd = kubeadmv1beta1.Etcd{
Local: &kubeadmv1beta1.LocalEtcd{
ImageMeta: kubeadmv1beta1.ImageMeta{
ImageRepository: "image-repository",
ImageTag: "latest",
},
},
}

afterInvalidEtcdCluster := beforeInvalidEtcdCluster.DeepCopy()
afterInvalidEtcdCluster.Spec.KubeadmConfigSpec.InitConfiguration.ClusterConfiguration.Etcd = kubeadmv1beta1.Etcd{
External: &kubeadmv1beta1.ExternalEtcd{
Endpoints: []string{"127.0.0.1"},
},
}

tests := []struct {
name string
expectErr bool
Expand Down Expand Up @@ -473,6 +488,12 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
before: localDataDir,
kcp: modifyLocalDataDir,
},
{
name: "should fail if both local and external etcd are set",
expectErr: true,
before: beforeInvalidEtcdCluster,
kcp: afterInvalidEtcdCluster,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f1b2729

Please sign in to comment.