Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏃[KCP] Add KCP spec.version semver check #2647

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/clusterctl/test/e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bifurcation/mint v0.0.0-20180715133206-93c51c6ce115/go.mod h1:zVt7zX3K/aDCk9Tj+VM7YymsX66ERvzCJzw8rFCX2JU=
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/caddyserver/caddy v1.0.3/go.mod h1:G+ouvOY32gENkJC+jhgl62TyhvqEsFaDiZ4uw0RzP1E=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha3
import (
"encoding/json"

"github.com/blang/semver"
jsonpatch "github.com/evanphx/json-patch"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -228,6 +229,11 @@ func (in *KubeadmControlPlane) validateCommon() (allErrs field.ErrorList) {
)
}

_, err := semver.ParseTolerant(in.Spec.Version)
if err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "version"), in.Spec.Version, "must be a valid semantic version"))
}

return allErrs
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
Name: "infraTemplate",
},
Replicas: pointer.Int32Ptr(1),
Version: "v1.16.6",
},
}
invalidNamespace := valid.DeepCopy()
Expand All @@ -79,6 +80,15 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
},
}

validVersion1 := valid.DeepCopy()
validVersion1.Spec.Version = "v1.16.6"

validVersion2 := valid.DeepCopy()
validVersion2.Spec.Version = "1.16.6"

invalidVersion := valid.DeepCopy()
invalidVersion.Spec.Version = "vv1.16.6"

tests := []struct {
name string
expectErr bool
Expand Down Expand Up @@ -114,6 +124,21 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
expectErr: false,
kcp: evenReplicasExternalEtcd,
},
{
name: "should succeed when given a valid semantic version with prepended 'v'",
expectErr: false,
kcp: validVersion1,
},
{
name: "should succeed when given a valid semantic version without 'v'",
expectErr: false,
kcp: validVersion2,
},
{
name: "should return error when given an invalid semantic version",
expectErr: true,
kcp: invalidVersion,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -157,6 +182,7 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
},
},
},
Version: "v1.16.6",
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) {
Name: "foo",
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := client.ObjectKey{Namespace: "test", Name: "foo"}

Expand Down Expand Up @@ -191,6 +194,9 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) {
Name: "foo",
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := client.ObjectKey{Namespace: "test", Name: "foo"}
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}
Expand Down Expand Up @@ -227,6 +233,9 @@ func TestReconcileKubeconfigSecretAlreadyExists(t *testing.T) {
Name: "foo",
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := util.ObjectKey(cluster)
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}
Expand Down Expand Up @@ -273,6 +282,9 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) {
Name: "foo",
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := util.ObjectKey(cluster)
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}
Expand Down Expand Up @@ -348,6 +360,9 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) {
Namespace: "test",
Name: "foo",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -385,6 +400,9 @@ func TestReconcileNoCluster(t *testing.T) {
},
},
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -431,6 +449,9 @@ func TestReconcileClusterNoEndpoints(t *testing.T) {
},
},
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -517,7 +538,7 @@ func TestReconcileInitializeControlPlane(t *testing.T) {
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: nil,
Version: "",
Version: "v1.16.6",
InfrastructureTemplate: corev1.ObjectReference{
Kind: genericMachineTemplate.GetKind(),
APIVersion: genericMachineTemplate.GetAPIVersion(),
Expand Down Expand Up @@ -657,7 +678,7 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) {
Namespace: cluster.Namespace,
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "my-version",
Version: "v1.16.6",
},
}

Expand Down Expand Up @@ -767,6 +788,9 @@ func TestKubeadmControlPlaneReconciler_updateStatusNoMachines(t *testing.T) {
Namespace: cluster.Namespace,
Name: "foo",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -850,6 +874,9 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesNotReady(t *testin
Namespace: cluster.Namespace,
Name: "foo",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -912,6 +939,9 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesReady(t *testing.T
Namespace: cluster.Namespace,
Name: "foo",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -971,6 +1001,9 @@ func TestKubeadmControlPlaneReconciler_updateStatusMachinesReadyMixed(t *testing
Namespace: cluster.Namespace,
Name: "foo",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
kcp.Default()
g.Expect(kcp.ValidateCreate()).To(Succeed())
Expand Down Expand Up @@ -1032,7 +1065,7 @@ func TestKubeadmControlPlaneReconciler_updateCoreDNS(t *testing.T) {
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: nil,
Version: "",
Version: "v1.16.6",
KubeadmConfigSpec: bootstrapv1.KubeadmConfigSpec{
ClusterConfiguration: &kubeadmv1.ClusterConfiguration{
DNS: kubeadmv1.DNS{
Expand Down Expand Up @@ -1300,6 +1333,7 @@ func TestCloneConfigsAndGenerateMachine(t *testing.T) {
Name: genericMachineTemplate.GetName(),
Namespace: cluster.Namespace,
},
Version: "v1.16.6",
},
}

Expand Down Expand Up @@ -1366,6 +1400,7 @@ func createClusterWithControlPlane() (*clusterv1.Cluster, *controlplanev1.Kubead
Name: "infra-foo",
APIVersion: "generic.io/v1",
},
Version: "v1.16.6",
},
}

Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/internal/workload_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestUpdateKubeProxyImageInfo(t *testing.T) {
c := &Workload{
Client: fakeClient,
}
err := c.UpdateKubeProxyImageInfo(ctx, &v1alpha3.KubeadmControlPlane{Spec: v1alpha3.KubeadmControlPlaneSpec{Version: "1.16.3"}})
err := c.UpdateKubeProxyImageInfo(ctx, &v1alpha3.KubeadmControlPlane{Spec: v1alpha3.KubeadmControlPlaneSpec{Version: "v1.16.3"}})
if err != nil && !tt.expectErr {
t.Fatalf("expected no error, got %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions test/infrastructure/docker/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bifurcation/mint v0.0.0-20180715133206-93c51c6ce115/go.mod h1:zVt7zX3K/aDCk9Tj+VM7YymsX66ERvzCJzw8rFCX2JU=
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/caddyserver/caddy v1.0.3/go.mod h1:G+ouvOY32gENkJC+jhgl62TyhvqEsFaDiZ4uw0RzP1E=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
Expand Down