Skip to content

Commit

Permalink
Update HA k0smotron cluster test
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <[email protected]>
  • Loading branch information
makhov committed Dec 18, 2024
1 parent 0a411d1 commit 8a73648
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/controller/k0smotron.io/k0smotroncluster_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ set -eu
export ETCDCTL_ENDPOINTS=https://${SVC_NAME}:2379
if [[ ! -f /var/lib/k0s/etcd/snap/db ]]; then
if [[ ! -f /var/lib/k0s/etcd/member/snap/db ]]; then
echo "Checking if cluster is functional"
if etcdctl member list; then
echo "Cluster is functional"
Expand Down
47 changes: 46 additions & 1 deletion inttest/ha-controller-etcd/ha_controller_etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ package hacontrolleretcd

import (
"context"
"k8s.io/apimachinery/pkg/util/wait"
"testing"
"time"

"github.com/k0sproject/k0s/inttest/common"
"github.com/k0sproject/k0smotron/inttest/util"
km "github.com/k0sproject/k0smotron/api/k0smotron.io/v1beta1"
"github.com/stretchr/testify/suite"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"

"github.com/k0sproject/k0smotron/inttest/util"
)

type HAControllerEtcdSuite struct {
Expand Down Expand Up @@ -77,6 +85,22 @@ func (s *HAControllerEtcdSuite) TestK0sGetsUp() {
s.Require().NoError(err)
s.Require().NoError(s.WaitForNodeReady(s.K0smotronNode(0), kmcKC))

s.T().Log("update cluster")
s.updateK0smotronCluster(s.Context(), rc)

err = wait.PollUntilContextCancel(s.Context(), 5*time.Second, true, func(ctx context.Context) (bool, error) {
sts, err := kc.AppsV1().StatefulSets("kmc-test").Get(s.Context(), "kmc-kmc-test", metav1.GetOptions{})
if err != nil {
return false, nil
}

return sts.Spec.Template.Spec.Containers[0].Image == "k0sproject/k0s:v1.28.3-k0s.0", nil
})
s.Require().NoError(err)

s.Require().NoError(common.WaitForStatefulSet(s.Context(), kc, "kmc-kmc-test", "kmc-test"))
s.Require().NoError(common.WaitForStatefulSet(s.Context(), kc, "kmc-kmc-test-etcd", "kmc-test"))
s.Require().NoError(s.WaitForNodeReady(s.K0smotronNode(0), kmcKC))
}

func TestHAControllerEtcdSuite(t *testing.T) {
Expand Down Expand Up @@ -110,6 +134,7 @@ func (s *HAControllerEtcdSuite) createK0smotronCluster(ctx context.Context, kc *
},
"spec": {
"replicas": 3,
"version": "v1.27.2-k0s.0",
"service":{
"type": "NodePort"
},
Expand All @@ -128,6 +153,26 @@ func (s *HAControllerEtcdSuite) createK0smotronCluster(ctx context.Context, kc *
s.Require().NoError(res.Error())
}

func (s *HAControllerEtcdSuite) updateK0smotronCluster(ctx context.Context, rc *rest.Config) {
crdConfig := *rc
crdConfig.ContentConfig.GroupVersion = &km.GroupVersion
crdConfig.APIPath = "/apis"
crdConfig.NegotiatedSerializer = serializer.NewCodecFactory(scheme.Scheme)
crdConfig.UserAgent = rest.DefaultKubernetesUserAgent()
crdRestClient, err := rest.UnversionedRESTClientFor(&crdConfig)
s.Require().NoError(err)

patch := `[{"op": "replace", "path": "/spec/version", "value": "v1.28.3-k0s.0"}]`
res := crdRestClient.
Patch(types.JSONPatchType).
Resource("clusters").
Name("kmc-test").
Namespace("kmc-test").
Body([]byte(patch)).
Do(ctx)
s.Require().NoError(res.Error())
}

func (s *HAControllerEtcdSuite) getPod(ctx context.Context, kc *kubernetes.Clientset) corev1.Pod {
pods, err := kc.CoreV1().Pods("kmc-test").List(
ctx,
Expand Down

0 comments on commit 8a73648

Please sign in to comment.