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

Configure k8s version explicitly #80

Closed
Closed
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
23 changes: 3 additions & 20 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
run: |
export CLUSTER_NAME=vcluster-k3s
export CLUSTER_NAMESPACE=vcluster-k3s
export CHART_VERSION=0.20.0-beta.12
export CHART_VERSION=0.20.0-beta.15
export HELM_VALUES=$(cat ./test/e2e/k3s-values.yaml | sed -z 's/\n/\\n/g')
kubectl create namespace ${CLUSTER_NAMESPACE}
cat templates/cluster-template.yaml | ./bin/envsubst | kubectl apply -n ${CLUSTER_NAMESPACE} -f -
Expand All @@ -129,7 +129,7 @@ jobs:
run: |
export CLUSTER_NAME=vcluster-k0s
export CLUSTER_NAMESPACE=vcluster-k0s
export CHART_VERSION=0.20.0-beta.12
export CHART_VERSION=0.20.0-beta.15
export CHART_NAME=vcluster
export HELM_VALUES=$(cat ./test/e2e/k0s-values.yaml | sed -z 's/\n/\\n/g')
kubectl create namespace ${CLUSTER_NAMESPACE}
Expand All @@ -146,7 +146,7 @@ jobs:
run: |
export CLUSTER_NAME=vcluster-k8s
export CLUSTER_NAMESPACE=vcluster-k8s
export CHART_VERSION=0.20.0-beta.12
export CHART_VERSION=0.20.0-beta.15
export CHART_NAME=vcluster
export HELM_VALUES=$(cat ./test/e2e/k8s-values.yaml | sed -z 's/\n/\\n/g')
kubectl create namespace ${CLUSTER_NAMESPACE}
Expand All @@ -158,20 +158,3 @@ jobs:

- name: Run E2E Tests K8S
run: CLUSTER_NAME=vcluster-k8s NAMESPACE=vcluster-k8s LOCAL_PORT=14552 go test -mod=vendor ./test/e2e -v

- name: Create Vcluster Custom Resource EKS
run: |
export CLUSTER_NAME=vcluster-eks
export CLUSTER_NAMESPACE=vcluster-eks
export CHART_VERSION=0.20.0-beta.12
export CHART_NAME=vcluster
export HELM_VALUES=$(cat ./test/e2e/eks-values.yaml | sed -z 's/\n/\\n/g')
kubectl create namespace ${CLUSTER_NAMESPACE}
cat templates/cluster-template.yaml | ./bin/envsubst | kubectl apply -n ${CLUSTER_NAMESPACE} -f -

- name: Validate Resource Ready EKS
run: |
kubectl wait --for=condition=ready vcluster -n vcluster-eks vcluster-eks --timeout=100s

- name: Run E2E Tests EKS
run: CLUSTER_NAME=vcluster-eks NAMESPACE=vcluster-eks LOCAL_PORT=14553 go test -mod=vendor ./test/e2e -v
13 changes: 13 additions & 0 deletions api/v1alpha1/vcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ type VirtualClusterHelmRelease struct {
// +optional
Chart VirtualClusterHelmChart `json:"chart,omitempty"`

// KubernetesVersion specifies the version of the control plane components (kube-apiserver, kube-controller-manager & kube-scheulder).
// This only applies when the k8s distro is enabled inside the provided vcluster.yaml via `controlPlane.distro.k8s.enabled`.
// It is a shortcut for controlPlane.distro.k8s.apiServer.image.tag,
// controlPlane.distro.k8s.controllerManager.image.tag and
// controlPlane.distro.k8s.scheduler.image.tag
// If e.g. controlPlane.distro.k8s.version is set to v1.30.1 and
// controlPlane.distro.k8s.scheduler.image.tag
// (or controlPlane.distro.k8s.controllerManager.image.tag or controlPlane.distro.k8s.apiServer.image.tag)
// is set to v1.31.0,
// value from controlPlane.distro.k8s.<controlPlane-component>.image.tag will be used
// (where <controlPlane-component is apiServer, controllerManager and scheduler).
KubernetesVersion string `json:"kubernetesVersion,omitempty"`

// the values for the given chart
// +optional
Values string `json:"values,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_vclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ spec:
description: the version of the helm chart to use
type: string
type: object
kubernetesVersion:
description: |-
KubernetesVersion specifies the version of the control plane components (kube-apiserver, kube-controller-manager & kube-scheulder).
This only applies when the k8s distro is enabled inside the provided vcluster.yaml via `controlPlane.distro.k8s.enabled`.
It is a shortcut for controlPlane.distro.k8s.apiServer.image.tag,
controlPlane.distro.k8s.controllerManager.image.tag and
controlPlane.distro.k8s.scheduler.image.tag
If e.g. controlPlane.distro.k8s.version is set to v1.30.1 and
controlPlane.distro.k8s.scheduler.image.tag
(or controlPlane.distro.k8s.controllerManager.image.tag or controlPlane.distro.k8s.apiServer.image.tag)
is set to v1.31.0,
value from controlPlane.distro.k8s.<controlPlane-component>.image.tag will be used
(where <controlPlane-component is apiServer, controllerManager and scheduler).
type: string
values:
description: the values for the given chart
type: string
Expand Down
33 changes: 29 additions & 4 deletions controllers/vcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/util"
"github.com/loft-sh/vcluster/pkg/util/kubeconfig"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -294,26 +295,50 @@ func (r *VClusterReconciler) redeployIfNeeded(_ context.Context, vCluster *v1alp
values = vCluster.Spec.HelmRelease.Values
}

// update the k8s version if given
cfg, err := config.NewDefaultConfig()
if err != nil {
return err
}
if err := cfg.UnmarshalYAMLStrict([]byte(values)); err != nil {
return err
}

k8sVersion := vCluster.Spec.HelmRelease.KubernetesVersion
if k8sVersion != "" {
cfg.ControlPlane.Distro.K8S.Version = k8sVersion
}

defaultConfig, err := config.NewDefaultConfig()
if err != nil {
return err
}

finalValues, err := config.Diff(defaultConfig, cfg)
if err != nil {
return err
}

r.Log.Info("Deploy virtual cluster",
"namespace", vCluster.Namespace,
"clusterName", vCluster.Name,
"values", values,
"values", finalValues,
)
chartPath := "./" + chartName + "-" + chartVersion + ".tgz"
_, err := os.Stat(chartPath)
_, err = os.Stat(chartPath)
if err != nil {
// we have to upgrade / install the chart
err = r.HelmClient.Upgrade(vCluster.Name, vCluster.Namespace, helm.UpgradeOptions{
Chart: chartName,
Repo: chartRepo,
Version: chartVersion,
Values: values,
Values: finalValues,
})
} else {
// we have to upgrade / install the chart
err = r.HelmClient.Upgrade(vCluster.Name, vCluster.Namespace, helm.UpgradeOptions{
Path: chartPath,
Values: values,
Values: finalValues,
})
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/loft-sh/vcluster v0.20.0-beta.12
github.com/loft-sh/vcluster v0.20.0-beta.15
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac h1:Gz/7Lb7WgdgIv+KJz87
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.29 h1:P/MObccXToAZy2QoJSQDJ+OJx1qHitpFHEVj3QBSNJs=
github.com/loft-sh/utils v0.0.29/go.mod h1:9hlX9cGpWHg3mNi/oBlv3X4ePGDMK66k8MbOZGFMDTI=
github.com/loft-sh/vcluster v0.20.0-beta.12 h1:xqHLLYbcWEiKyA998CEKaI0AyQ0rf4isPk7DvUri94k=
github.com/loft-sh/vcluster v0.20.0-beta.12/go.mod h1:Xm+Y8EADJCix31Lvb9kIPO3CIoRRV6RJrL48/skB5qw=
github.com/loft-sh/vcluster v0.20.0-beta.15 h1:/E3CBS0a3sGeakKtYxwJCo9aSsQZ5oFWiGbZESytF/A=
github.com/loft-sh/vcluster v0.20.0-beta.15/go.mod h1:Xm+Y8EADJCix31Lvb9kIPO3CIoRRV6RJrL48/skB5qw=
github.com/lufeee/execinquery v1.2.1 h1:hf0Ems4SHcUGBxpGN7Jz78z1ppVkP/837ZlETPCEtOM=
github.com/lufeee/execinquery v1.2.1/go.mod h1:EC7DrEKView09ocscGHC+apXMIaorh4xqSxS/dy8SbM=
github.com/macabu/inamedparam v0.1.3 h1:2tk/phHkMlEL/1GNe/Yf6kkR/hkcUdAEY3L0hjYV1Mk=
Expand Down
3 changes: 2 additions & 1 deletion templates/cluster-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ metadata:
name: ${CLUSTER_NAME}
spec:
helmRelease:
kubernetesVersion: ${KUBERNETES_VERSION:=""}
values: "${HELM_VALUES:=}"
chart:
name: ${CHART_NAME:=vcluster}
repo: ${CHART_REPO:=https://charts.loft.sh}
version: ${CHART_VERSION:=0.20.0-beta.12}
version: ${CHART_VERSION:=0.20.0-beta.15}
controlPlaneEndpoint:
host: ${VCLUSTER_HOST:=""}
port: ${VCLUSTER_PORT:=0}
109 changes: 19 additions & 90 deletions test/controllerstest/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
}
})

ginkgo.It("reconcile successfully on k3s", func() {
values := map[string]any{
"vcluster": map[string]string{
"image": "rancher/k3s:v1.28.2-k3s1",
},
}
yamlBytes, yamlErr := yaml.Marshal(&values)
gomega.Expect(yamlErr).NotTo(gomega.HaveOccurred())
ginkgo.It("reconcile successfully on k8s", func() {
vCluster := &v1alpha1.VCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-vcluster",
Expand All @@ -91,9 +84,8 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
Spec: v1alpha1.VClusterSpec{
HelmRelease: &v1alpha1.VirtualClusterHelmRelease{
Chart: v1alpha1.VirtualClusterHelmChart{
Version: "0.19",
Version: "0.20.0-beta.15",
},
Values: string(yamlBytes),
},
},
}
Expand Down Expand Up @@ -128,75 +120,16 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
gomega.Expect(result.RequeueAfter).Should(gomega.Equal(time.Minute))
})

ginkgo.It("reconcile successfully on k0s", func() {
ginkgo.It("reconcile successfully on k3s", func() {
values := map[string]any{
"vcluster": map[string]string{
"image": "rancher/k3s:v1.28.2-k3s1",
},
}
yamlBytes, yamlErr := yaml.Marshal(&values)
gomega.Expect(yamlErr).NotTo(gomega.HaveOccurred())
vCluster := &v1alpha1.VCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-vcluster",
Namespace: "default",
},
Spec: v1alpha1.VClusterSpec{
HelmRelease: &v1alpha1.VirtualClusterHelmRelease{
Chart: v1alpha1.VirtualClusterHelmChart{
Name: "vcluster-k0s",
Version: "0.19",
"controlPlane": map[string]any{
"distro": map[string]any{
"k3s": map[string]bool{
"enabled": true,
},
Values: string(yamlBytes),
},
},
}
hemlClient.On("Upgrade").Return(nil)
f := fakeclientset.NewSimpleClientset()

_, err := f.CoreV1().ServiceAccounts("default").Create(context.Background(), &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: "default",
},
}, metav1.CreateOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())

reconciler = &controllers.VClusterReconciler{
Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(vCluster, secret).WithStatusSubresource(vCluster).Build(),
HelmClient: hemlClient,
Scheme: scheme,
ClientConfigGetter: &fakeConfigGetter{
fake: f,
},
HTTPClientGetter: &fakeHTTPClientGetter{},
}
req := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: vCluster.Name,
Namespace: vCluster.Namespace,
},
}
result, err := reconciler.Reconcile(ctx, req)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(result.RequeueAfter).Should(gomega.Equal(time.Minute))
})

ginkgo.It("reconcile successfully on k8s", func() {
values := map[string]any{
"api": map[string]string{
"image": "registry.k8s.io/kube-apiserver:v1.28.2",
},
"scheduler": map[string]string{
"image": "registry.k8s.io/kube-scheduler:v1.28.2",
},
"controller": map[string]string{
"image": "registry.k8s.io/kube-controller-manager:v1.28.2",
},
"etcd": map[string]string{
"image": "registry.k8s.io/etcd:3.5.9-0",
},
}
yamlBytes, yamlErr := yaml.Marshal(&values)
gomega.Expect(yamlErr).NotTo(gomega.HaveOccurred())
vCluster := &v1alpha1.VCluster{
Expand All @@ -207,8 +140,8 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
Spec: v1alpha1.VClusterSpec{
HelmRelease: &v1alpha1.VirtualClusterHelmRelease{
Chart: v1alpha1.VirtualClusterHelmChart{
Name: "vcluster-k8s",
Version: "0.19",
Name: "vcluster",
Version: "0.20.0-beta.15",
},
Values: string(yamlBytes),
},
Expand Down Expand Up @@ -245,19 +178,14 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
gomega.Expect(result.RequeueAfter).Should(gomega.Equal(time.Minute))
})

ginkgo.It("reconcile successfully on eks", func() {
ginkgo.It("reconcile successfully on k0s", func() {
values := map[string]any{
"api": map[string]string{
"image": "test",
},
"coredns": map[string]string{
"image": "test",
},
"controller": map[string]string{
"image": "test",
},
"etcd": map[string]string{
"image": "test",
"controlPlane": map[string]any{
"distro": map[string]any{
"k0s": map[string]bool{
"enabled": true,
},
},
},
}
yamlBytes, yamlErr := yaml.Marshal(&values)
Expand All @@ -270,8 +198,8 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
Spec: v1alpha1.VClusterSpec{
HelmRelease: &v1alpha1.VirtualClusterHelmRelease{
Chart: v1alpha1.VirtualClusterHelmChart{
Name: "vcluster-eks",
Version: "0.19",
Name: "vcluster",
Version: "0.20.0-beta.15",
},
Values: string(yamlBytes),
},
Expand Down Expand Up @@ -307,6 +235,7 @@ var _ = ginkgo.Describe("Vcluster Controller test", func() {
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(result.RequeueAfter).Should(gomega.Equal(time.Minute))
})

})

})
22 changes: 0 additions & 22 deletions test/e2e/eks-values.yaml

This file was deleted.

Loading