From 55299c620e98fd65e427f63f8e35cf9b967c696e Mon Sep 17 00:00:00 2001 From: Jaan Zhang Date: Sun, 19 Nov 2023 16:15:16 +0800 Subject: [PATCH] chore: switch exposed kvrock client to interface (#60) --- .github/workflows/helm-lint.yaml | 8 +++--- pkg/client/k8s/configmap_test.go | 21 ++++++++------ pkg/client/k8s/deployment.go | 4 +-- pkg/client/k8s/deployment_test.go | 11 ++++---- pkg/client/k8s/k8s_client.go | 6 ++-- pkg/client/k8s/kvrocks.go | 4 +-- pkg/client/k8s/kvrocks_test.go | 15 +++++----- pkg/client/k8s/pod.go | 4 +-- pkg/client/k8s/pod_test.go | 11 ++++---- pkg/client/k8s/pvc.go | 6 ++-- pkg/client/k8s/pvc_test.go | 13 +++++---- pkg/client/k8s/service_test.go | 7 +++-- pkg/client/k8s/statefulset.go | 6 ++-- pkg/client/k8s/statefulset_test.go | 19 +++++++------ pkg/client/kvrocks/cluster.go | 2 +- pkg/client/kvrocks/kvrocks_client.go | 40 +++++++++++++++++++++------ pkg/client/kvrocks/sentinel.go | 16 +++++++---- pkg/client/kvrocks/standard.go | 31 +++++++++++++-------- pkg/controllers/cluster/handler.go | 7 +++-- pkg/controllers/common/handler.go | 4 +-- pkg/controllers/events/event.go | 4 +-- pkg/controllers/kvrocks_controller.go | 6 ++-- pkg/controllers/sentinel/handler.go | 7 +++-- pkg/controllers/standard/handler.go | 7 +++-- pkg/controllers/suite_test.go | 11 ++++---- test/e2e/cluster/cluster_test.go | 5 +--- test/e2e/standard/standard_test.go | 13 ++++----- test/e2e/util/chaos_common.go | 6 ++-- test/e2e/util/kubernetes_env.go | 8 +++--- 29 files changed, 174 insertions(+), 128 deletions(-) diff --git a/.github/workflows/helm-lint.yaml b/.github/workflows/helm-lint.yaml index d3b5d0f..d3fad2a 100644 --- a/.github/workflows/helm-lint.yaml +++ b/.github/workflows/helm-lint.yaml @@ -3,9 +3,9 @@ name: helm-lint on: push: branches: - - unstable + - unstable pull_request: - branches: [ unstable ] + branches: [unstable] jobs: lint-test-helm: @@ -20,7 +20,7 @@ jobs: uses: azure/setup-helm@v3 - name: Set up chart-testing - uses: helm/chart-testing-action@v2.4.0 + uses: helm/chart-testing-action@v2.5.0 - name: Run chart-testing (lint) - run: ct lint --all --chart-dirs deploy/ --validate-maintainers=false \ No newline at end of file + run: ct lint --all --chart-dirs deploy/ --validate-maintainers=false diff --git a/pkg/client/k8s/configmap_test.go b/pkg/client/k8s/configmap_test.go index c41de96..7ffd6b4 100644 --- a/pkg/client/k8s/configmap_test.go +++ b/pkg/client/k8s/configmap_test.go @@ -2,6 +2,8 @@ package k8s import ( "context" + "testing" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" kubeerrors "k8s.io/apimachinery/pkg/api/errors" @@ -9,9 +11,8 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestGetConfigMap(t *testing.T) { @@ -46,7 +47,7 @@ func TestGetConfigMap(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingCM != nil { objs = append(objs, test.existingCM) } @@ -70,6 +71,7 @@ func TestGetConfigMap(t *testing.T) { }) } } + func TestUpdateConfigMap(t *testing.T) { ns, updatedKey, updatedValue := "unit-test", "key1", "value2" testConfigMap := &corev1.ConfigMap{ @@ -110,7 +112,7 @@ func TestUpdateConfigMap(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingCM != nil { objs = append(objs, test.existingCM) } @@ -133,8 +135,8 @@ func TestUpdateConfigMap(t *testing.T) { } }) } - } + func TestCreateOrUpdateConfigMap(t *testing.T) { ns := "unit-test" @@ -168,7 +170,7 @@ func TestCreateOrUpdateConfigMap(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingCM != nil { objs = append(objs, test.existingCM) } @@ -184,13 +186,14 @@ func TestCreateOrUpdateConfigMap(t *testing.T) { assert.NoError(err) cm := &corev1.ConfigMap{} - err = fakeClient.Get(context.TODO(), client.ObjectKey{Namespace: ns, Name: test.configMap.Name}, cm) + err = fakeClient.Get(context.TODO(), k8sApiClient.ObjectKey{Namespace: ns, Name: test.configMap.Name}, cm) assert.NoError(err) assert.Equal(test.configMap.ResourceVersion, cm.ResourceVersion) } }) } } + func TestCreateIfNotExistsConfigMap(t *testing.T) { ns := "unit-test" testConfigMap := &corev1.ConfigMap{ @@ -223,7 +226,7 @@ func TestCreateIfNotExistsConfigMap(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingCM != nil { objs = append(objs, test.existingCM) } @@ -238,7 +241,7 @@ func TestCreateIfNotExistsConfigMap(t *testing.T) { assert.NoError(err) cm := &corev1.ConfigMap{} - err = fakeClient.Get(context.TODO(), client.ObjectKey{Namespace: ns, Name: test.configMap.Name}, cm) + err = fakeClient.Get(context.TODO(), k8sApiClient.ObjectKey{Namespace: ns, Name: test.configMap.Name}, cm) assert.NoError(err) assert.Equal(test.configMap.Name, cm.Name) } diff --git a/pkg/client/k8s/deployment.go b/pkg/client/k8s/deployment.go index 0d75d09..548fe93 100644 --- a/pkg/client/k8s/deployment.go +++ b/pkg/client/k8s/deployment.go @@ -5,7 +5,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) func (c *Client) CreateIfNotExistsDeployment(deployment *appsv1.Deployment) error { @@ -54,7 +54,7 @@ func (c *Client) ListDeploymentPods(key types.NamespacedName) (*corev1.PodList, labels[k] = v } var pods corev1.PodList - if err := c.client.List(ctx, &pods, client.InNamespace(deployment.Namespace), client.MatchingLabels(labels)); err != nil { + if err := c.client.List(ctx, &pods, k8sApiClient.InNamespace(deployment.Namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &pods, nil diff --git a/pkg/client/k8s/deployment_test.go b/pkg/client/k8s/deployment_test.go index ba6c666..8a3d868 100644 --- a/pkg/client/k8s/deployment_test.go +++ b/pkg/client/k8s/deployment_test.go @@ -12,7 +12,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -48,7 +48,7 @@ func TestCreateIfNotExistsDeployment(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingDeploy != nil { objs = append(objs, test.existingDeploy) } @@ -105,7 +105,7 @@ func TestGetDeployment(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingDeploy != nil { objs = append(objs, test.existingDeploy) } @@ -164,7 +164,7 @@ func TestUpdateDeployment(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingDeploy != nil { objs = append(objs, test.existingDeploy) } @@ -198,6 +198,7 @@ func TestUpdateDeployment(t *testing.T) { }) } } + func TestListDeploymentPods(t *testing.T) { ns := "unit-test" testDeployment := &appsv1.Deployment{ @@ -246,7 +247,7 @@ func TestListDeploymentPods(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) objs = append(objs, test.dep) if test.pod != nil { objs = append(objs, test.pod) diff --git a/pkg/client/k8s/k8s_client.go b/pkg/client/k8s/k8s_client.go index 2716574..d7db8ae 100644 --- a/pkg/client/k8s/k8s_client.go +++ b/pkg/client/k8s/k8s_client.go @@ -4,18 +4,18 @@ import ( "context" "github.com/go-logr/logr" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) var ctx = context.TODO() // Client for k8s type Client struct { - client client.Client + client k8sApiClient.Client logger logr.Logger } -func NewK8sClient(client client.Client, logger logr.Logger) *Client { +func NewK8sClient(client k8sApiClient.Client, logger logr.Logger) *Client { return &Client{ client: client, logger: logger, diff --git a/pkg/client/k8s/kvrocks.go b/pkg/client/k8s/kvrocks.go index fb5f416..752d79f 100644 --- a/pkg/client/k8s/kvrocks.go +++ b/pkg/client/k8s/kvrocks.go @@ -3,7 +3,7 @@ package k8s import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" kvrocksv1alpha1 "github.com/RocksLabs/kvrocks-operator/api/v1alpha1" ) @@ -26,7 +26,7 @@ func (c *Client) UpdateKVRocks(instance *kvrocksv1alpha1.KVRocks) error { func (c *Client) ListKVRocks(namespace string, labels map[string]string) (*kvrocksv1alpha1.KVRocksList, error) { var kvrockses kvrocksv1alpha1.KVRocksList - if err := c.client.List(ctx, &kvrockses, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil { + if err := c.client.List(ctx, &kvrockses, k8sApiClient.InNamespace(namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &kvrockses, nil diff --git a/pkg/client/k8s/kvrocks_test.go b/pkg/client/k8s/kvrocks_test.go index df8a070..5ebc884 100644 --- a/pkg/client/k8s/kvrocks_test.go +++ b/pkg/client/k8s/kvrocks_test.go @@ -2,6 +2,8 @@ package k8s import ( "context" + "testing" + kvrocksv1alpha1 "github.com/RocksLabs/kvrocks-operator/api/v1alpha1" "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/api/errors" @@ -9,9 +11,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestGetKVRocks(t *testing.T) { @@ -46,7 +47,7 @@ func TestGetKVRocks(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingKVRocks != nil { objs = append(objs, test.existingKVRocks) } @@ -104,7 +105,7 @@ func TestUpdateKVRocks(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingKVRocks != nil { objs = append(objs, test.existingKVRocks) } @@ -195,7 +196,7 @@ func TestListKVRocks(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, len(test.kvrocks)) + objs := make([]k8sApiClient.Object, len(test.kvrocks)) for i, kvrocks := range test.kvrocks { objs[i] = kvrocks } @@ -250,7 +251,7 @@ func TestCreateIfNotExistsKVRocks(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingKVRocks != nil { objs = append(objs, test.existingKVRocks) } @@ -265,7 +266,7 @@ func TestCreateIfNotExistsKVRocks(t *testing.T) { } else { assert.NoError(err) createdKVRocks := &kvrocksv1alpha1.KVRocks{} - err := fakeClient.Get(context.Background(), client.ObjectKey{ + err := fakeClient.Get(context.Background(), k8sApiClient.ObjectKey{ Namespace: test.kvrocks.Namespace, Name: test.kvrocks.Name, }, createdKVRocks) diff --git a/pkg/client/k8s/pod.go b/pkg/client/k8s/pod.go index 9b68746..024433c 100644 --- a/pkg/client/k8s/pod.go +++ b/pkg/client/k8s/pod.go @@ -4,7 +4,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) func (c *Client) GetPod(key types.NamespacedName) (*corev1.Pod, error) { @@ -31,7 +31,7 @@ func (c *Client) DeletePodImmediately(podName, namespace string) error { if err != nil && !errors.IsNotFound(err) { return err } - if err = c.client.Delete(ctx, pod, client.GracePeriodSeconds(0)); err != nil && !errors.IsNotFound(err) { + if err = c.client.Delete(ctx, pod, k8sApiClient.GracePeriodSeconds(0)); err != nil && !errors.IsNotFound(err) { return err } c.logger.V(1).Info("delete pod successfully", "pod", pod.Name) diff --git a/pkg/client/k8s/pod_test.go b/pkg/client/k8s/pod_test.go index 55e935c..5a3c541 100644 --- a/pkg/client/k8s/pod_test.go +++ b/pkg/client/k8s/pod_test.go @@ -1,6 +1,8 @@ package k8s import ( + "testing" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" kubeerrors "k8s.io/apimachinery/pkg/api/errors" @@ -8,9 +10,8 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestGetPod(t *testing.T) { @@ -45,7 +46,7 @@ func TestGetPod(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingPod != nil { objs = append(objs, test.existingPod) } @@ -110,7 +111,7 @@ func TestUpdatePod(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingPod != nil { objs = append(objs, test.existingPod) } @@ -166,7 +167,7 @@ func TestDeletePodImmediately(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingPod != nil { objs = append(objs, test.existingPod) } diff --git a/pkg/client/k8s/pvc.go b/pkg/client/k8s/pvc.go index 5dc101a..cd3f569 100644 --- a/pkg/client/k8s/pvc.go +++ b/pkg/client/k8s/pvc.go @@ -4,7 +4,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) func (c *Client) ListStatefulSetPVC(key types.NamespacedName) (*corev1.PersistentVolumeClaimList, error) { @@ -14,7 +14,7 @@ func (c *Client) ListStatefulSetPVC(key types.NamespacedName) (*corev1.Persisten } labels := sts.Spec.Selector.MatchLabels var pvcList corev1.PersistentVolumeClaimList - if err = c.client.List(ctx, &pvcList, client.InNamespace(key.Namespace), client.MatchingLabels(labels)); err != nil { + if err = c.client.List(ctx, &pvcList, k8sApiClient.InNamespace(key.Namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &pvcList, nil @@ -30,7 +30,7 @@ func (c *Client) DeletePVC(pvc *corev1.PersistentVolumeClaim) error { func (c *Client) ListPVC(namespace string, labels map[string]string) (*corev1.PersistentVolumeClaimList, error) { var pvcList corev1.PersistentVolumeClaimList - if err := c.client.List(ctx, &pvcList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil { + if err := c.client.List(ctx, &pvcList, k8sApiClient.InNamespace(namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &pvcList, nil diff --git a/pkg/client/k8s/pvc_test.go b/pkg/client/k8s/pvc_test.go index c391c48..a72454b 100644 --- a/pkg/client/k8s/pvc_test.go +++ b/pkg/client/k8s/pvc_test.go @@ -1,6 +1,8 @@ package k8s import ( + "testing" + kruise "github.com/openkruise/kruise-api/apps/v1beta1" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -9,9 +11,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestListStatefulSetPVC(t *testing.T) { @@ -68,7 +69,7 @@ func TestListStatefulSetPVC(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingSTS != nil { objs = append(objs, test.existingSTS) } @@ -129,7 +130,7 @@ func TestDeletePVC(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - var objs []client.Object + var objs []k8sApiClient.Object if test.existingPVC != nil { objs = append(objs, test.existingPVC) } @@ -217,7 +218,7 @@ func TestListPVC(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, len(test.existingPVCs)) + objs := make([]k8sApiClient.Object, len(test.existingPVCs)) for i, pvc := range test.existingPVCs { objs[i] = pvc } @@ -270,7 +271,7 @@ func TestDeletePVCByPod(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := []client.Object{} + objs := []k8sApiClient.Object{} if test.existingPVC != nil { objs = append(objs, test.existingPVC) } diff --git a/pkg/client/k8s/service_test.go b/pkg/client/k8s/service_test.go index 05e9b70..f78860a 100644 --- a/pkg/client/k8s/service_test.go +++ b/pkg/client/k8s/service_test.go @@ -1,15 +1,16 @@ package k8s import ( + "testing" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestCreateIfNotExistsService(t *testing.T) { @@ -45,7 +46,7 @@ func TestCreateIfNotExistsService(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := []client.Object{} + objs := []k8sApiClient.Object{} if test.existingService != nil { objs = append(objs, test.existingService) } diff --git a/pkg/client/k8s/statefulset.go b/pkg/client/k8s/statefulset.go index 5cffdcb..7d86f36 100644 --- a/pkg/client/k8s/statefulset.go +++ b/pkg/client/k8s/statefulset.go @@ -6,7 +6,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) func (c *Client) CreateIfNotExistsStatefulSet(sts *kruise.StatefulSet) error { @@ -43,7 +43,7 @@ func (c *Client) ListStatefulSetPods(key types.NamespacedName) (*corev1.PodList, labels[k] = v } var pods corev1.PodList - if err := c.client.List(ctx, &pods, client.InNamespace(sts.Namespace), client.MatchingLabels(labels)); err != nil { + if err := c.client.List(ctx, &pods, k8sApiClient.InNamespace(sts.Namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &pods, nil @@ -84,7 +84,7 @@ func (c *Client) CreateStatefulSetOrUpdateImage(sts *kruise.StatefulSet) error { func (c *Client) ListStatefulSets(namespace string, labels map[string]string) (*kruise.StatefulSetList, error) { var stsList kruise.StatefulSetList - if err := c.client.List(ctx, &stsList, client.InNamespace(namespace), client.MatchingLabels(labels)); err != nil { + if err := c.client.List(ctx, &stsList, k8sApiClient.InNamespace(namespace), k8sApiClient.MatchingLabels(labels)); err != nil { return nil, err } return &stsList, nil diff --git a/pkg/client/k8s/statefulset_test.go b/pkg/client/k8s/statefulset_test.go index fd63322..2a9df33 100644 --- a/pkg/client/k8s/statefulset_test.go +++ b/pkg/client/k8s/statefulset_test.go @@ -2,6 +2,8 @@ package k8s import ( "context" + "testing" + kruise "github.com/openkruise/kruise-api/apps/v1beta1" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -10,9 +12,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "testing" ) func TestCreateIfNotExistsStatefulSet(t *testing.T) { @@ -47,7 +48,7 @@ func TestCreateIfNotExistsStatefulSet(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingSTS != nil { objs = append(objs, test.existingSTS) } @@ -105,7 +106,7 @@ func TestGetStatefulSet(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingSTS != nil { objs = append(objs, test.existingSTS) } @@ -164,7 +165,7 @@ func TestUpdateStatefulSet(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.exitingSTS != nil { objs = append(objs, test.exitingSTS) } @@ -251,7 +252,7 @@ func TestListStatefulSetPods(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) objs = append(objs, test.sts) if test.pod != nil { objs = append(objs, test.pod) @@ -313,7 +314,7 @@ func TestCreateOrUpdateStatefulSet(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.exitingSTS != nil { objs = append(objs, test.exitingSTS) } @@ -382,7 +383,7 @@ func TestListStatefulSets(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingSTS != nil { objs = append(objs, test.existingSTS) } @@ -434,7 +435,7 @@ func TestDeleteStatefulSetIfExists(t *testing.T) { t.Run(test.name, func(t *testing.T) { assert := assert.New(t) - objs := make([]client.Object, 0) + objs := make([]k8sApiClient.Object, 0) if test.existingSTS != nil { objs = append(objs, test.existingSTS) } diff --git a/pkg/client/kvrocks/cluster.go b/pkg/client/kvrocks/cluster.go index 5a2a217..43cf85c 100644 --- a/pkg/client/kvrocks/cluster.go +++ b/pkg/client/kvrocks/cluster.go @@ -4,7 +4,7 @@ import ( "strings" ) -func (s *Client) ClusterNodeInfo(ip, password string) (*Node, error) { +func (s *client) ClusterNodeInfo(ip, password string) (*Node, error) { c := kvrocksClient(ip, password) defer c.Close() info, err := c.ClusterNodes(ctx).Result() diff --git a/pkg/client/kvrocks/kvrocks_client.go b/pkg/client/kvrocks/kvrocks_client.go index d33bac9..52e9898 100644 --- a/pkg/client/kvrocks/kvrocks_client.go +++ b/pkg/client/kvrocks/kvrocks_client.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/go-logr/logr" - client "github.com/go-redis/redis/v8" + redisClient "github.com/go-redis/redis/v8" ) var ctx = context.TODO() @@ -53,23 +53,47 @@ type MigrateMsg struct { Slots []int } -type Client struct { +type client struct { logger logr.Logger } -func NewKVRocksClient(logger logr.Logger) *Client { - return &Client{logger: logger} +func (s *client) Logger() logr.Logger { + return s.logger } -func kvrocksClient(ip, password string) *client.Client { - return client.NewClient(&client.Options{ +type Client interface { + Logger() logr.Logger + + ChangeMyselfToMaster(ip string, password string) error + ChangePassword(ip string, password string, newPassword string) error + ClusterNodeInfo(ip string, password string) (*Node, error) + CreateMonitor(sentinelIP string, password string, master string, ip string, kvPass string) error + GetConfig(ip string, password string, key string) (*string, error) + GetMaster(ip string, password string) (string, error) + GetMasterFromSentinel(sentinelIP string, sentinelPassword string, master string) (string, error) + GetOffset(ip string, password string) (int, error) + NodeInfo(ip string, password string) (node Node, err error) + Ping(ip string, password string) bool + RemoveMonitor(sentinelIP string, password string, master string) error + ResetMonitor(sentinelIP string, sentinelPassword string, master string, password string) error + SetConfig(ip string, password string, key string, value string) error + SlaveOf(slaveIP string, masterIP string, password string) error + SubOdownMsg(ip string, password string) (*redisClient.PubSub, func()) +} + +func NewKVRocksClient(logger logr.Logger) Client { + return &client{logger: logger} +} + +func kvrocksClient(ip, password string) *redisClient.Client { + return redisClient.NewClient(&redisClient.Options{ Addr: net.JoinHostPort(ip, strconv.Itoa(KVRocksPort)), Password: password, }) } -func kvrocksSentinelClient(ip, password string) *client.SentinelClient { - return client.NewSentinelClient(&client.Options{ +func kvrocksSentinelClient(ip, password string) *redisClient.SentinelClient { + return redisClient.NewSentinelClient(&redisClient.Options{ Addr: net.JoinHostPort(ip, strconv.Itoa(SentinelPort)), Username: SuperUser, Password: password, diff --git a/pkg/client/kvrocks/sentinel.go b/pkg/client/kvrocks/sentinel.go index 192fd18..14f14c5 100644 --- a/pkg/client/kvrocks/sentinel.go +++ b/pkg/client/kvrocks/sentinel.go @@ -6,7 +6,8 @@ import ( "github.com/go-redis/redis/v8" ) -func (s *Client) GetMasterFromSentinel(sentinelIP, sentinelPassword, master string) (string, error) { +// GetMasterFromSentinel returns the master ip from sentinel +func (s *client) GetMasterFromSentinel(sentinelIP, sentinelPassword, master string) (string, error) { c := kvrocksSentinelClient(sentinelIP, sentinelPassword) defer c.Close() res, err := c.Master(ctx, master).Result() @@ -18,7 +19,8 @@ func (s *Client) GetMasterFromSentinel(sentinelIP, sentinelPassword, master stri return masterIP, nil } -func (s *Client) RemoveMonitor(sentinelIP, password, master string) error { +// RemoveMonitor removes the monitor from sentinel +func (s *client) RemoveMonitor(sentinelIP, password, master string) error { c := kvrocksSentinelClient(sentinelIP, password) defer c.Close() if err := c.Remove(ctx, master).Err(); err != nil { @@ -28,7 +30,8 @@ func (s *Client) RemoveMonitor(sentinelIP, password, master string) error { return nil } -func (s *Client) CreateMonitor(sentinelIP, password, master, ip, kvPass string) error { +// CreateMonitor creates the monitor in sentinel +func (s *client) CreateMonitor(sentinelIP, password, master, ip, kvPass string) error { c := kvrocksSentinelClient(sentinelIP, password) defer c.Close() var err error @@ -48,7 +51,8 @@ func (s *Client) CreateMonitor(sentinelIP, password, master, ip, kvPass string) return nil } -func (s *Client) ResetMonitor(sentinelIP, sentinelPassword, master, password string) error { +// ResetMonitor resets the monitor in sentinel +func (s *client) ResetMonitor(sentinelIP, sentinelPassword, master, password string) error { c := kvrocksSentinelClient(sentinelIP, sentinelPassword) defer c.Close() var err error @@ -62,7 +66,8 @@ func (s *Client) ResetMonitor(sentinelIP, sentinelPassword, master, password str return nil } -func (s *Client) SubOdownMsg(ip, password string) (*redis.PubSub, func()) { +// SubOdownMsg subscribes the odown message from sentinel +func (s *client) SubOdownMsg(ip, password string) (*redis.PubSub, func()) { c := kvrocksSentinelClient(ip, password) pubsub := c.Subscribe(ctx, "+odown") finalize := func() { @@ -71,5 +76,4 @@ func (s *Client) SubOdownMsg(ip, password string) (*redis.PubSub, func()) { } return pubsub, finalize - } diff --git a/pkg/client/kvrocks/standard.go b/pkg/client/kvrocks/standard.go index 66c5750..50b9a34 100644 --- a/pkg/client/kvrocks/standard.go +++ b/pkg/client/kvrocks/standard.go @@ -8,13 +8,14 @@ import ( "strings" "time" - client "github.com/go-redis/redis/v8" + redisClient "github.com/go-redis/redis/v8" ) -func (s *Client) NodeInfo(ip, password string) (node Node, err error) { +// NodeInfo returns the node info +func (s *client) NodeInfo(ip, password string) (node Node, err error) { c := kvrocksClient(ip, password) defer c.Close() - cmd := client.NewSliceCmd(ctx, "ROLE") + cmd := redisClient.NewSliceCmd(ctx, "ROLE") c.Process(ctx, cmd) resp, err := cmd.Result() if err != nil || len(resp) == 0 { @@ -26,7 +27,8 @@ func (s *Client) NodeInfo(ip, password string) (node Node, err error) { return } -func (s *Client) GetConfig(ip, password, key string) (*string, error) { +// GetConfig returns the config value +func (s *client) GetConfig(ip, password, key string) (*string, error) { c := kvrocksClient(ip, password) defer c.Close() value, err := c.ConfigGet(ctx, key).Result() @@ -40,7 +42,8 @@ func (s *Client) GetConfig(ip, password, key string) (*string, error) { return &result, nil } -func (s *Client) SetConfig(ip, password string, key, value string) error { +// SetConfig sets a single config in key value format +func (s *client) SetConfig(ip, password string, key, value string) error { c := kvrocksClient(ip, password) defer c.Close() if err := c.ConfigSet(ctx, key, value).Err(); err != nil { @@ -50,7 +53,8 @@ func (s *Client) SetConfig(ip, password string, key, value string) error { return nil } -func (s *Client) ChangePassword(ip, password, newPassword string) error { +// ChangePassword changes the password +func (s *client) ChangePassword(ip, password, newPassword string) error { c := kvrocksClient(ip, password) defer c.Close() pipe := c.Pipeline() @@ -64,7 +68,8 @@ func (s *Client) ChangePassword(ip, password, newPassword string) error { return nil } -func (s *Client) ChangeMyselfToMaster(ip, password string) error { +// ChangeMyselfToMaster changes the current node to master +func (s *client) ChangeMyselfToMaster(ip, password string) error { c := kvrocksClient(ip, password) defer c.Close() if err := c.SlaveOf(ctx, "NO", "ONE").Err(); err != nil { @@ -74,7 +79,8 @@ func (s *Client) ChangeMyselfToMaster(ip, password string) error { return nil } -func (s *Client) GetMaster(ip, password string) (string, error) { +// GetMaster returns the master ip +func (s *client) GetMaster(ip, password string) (string, error) { c := kvrocksClient(ip, password) defer c.Close() info, err := c.Info(ctx, "replication").Result() @@ -90,7 +96,8 @@ func (s *Client) GetMaster(ip, password string) (string, error) { return master, nil } -func (s *Client) SlaveOf(slaveIP, masterIP, password string) error { +// SlaveOf sets the slave of the specified master +func (s *client) SlaveOf(slaveIP, masterIP, password string) error { c := kvrocksClient(slaveIP, password) defer c.Close() if err := c.SlaveOf(ctx, masterIP, strconv.Itoa(KVRocksPort)).Err(); err != nil { @@ -100,7 +107,8 @@ func (s *Client) SlaveOf(slaveIP, masterIP, password string) error { return nil } -func (s *Client) GetOffset(ip, password string) (int, error) { +// GetOffset returns the replication offset +func (s *client) GetOffset(ip, password string) (int, error) { c := kvrocksClient(ip, password) defer c.Close() msg, err := c.Info(ctx, "replication").Result() @@ -117,7 +125,8 @@ func (s *Client) GetOffset(ip, password string) (int, error) { return -1, nil } -func (s *Client) Ping(ip, password string) bool { +// Ping checks if the node is alive +func (s *client) Ping(ip, password string) bool { c := kvrocksClient(ip, password) defer c.Close() timeout, cancel := context.WithTimeout(ctx, time.Second*1) diff --git a/pkg/controllers/cluster/handler.go b/pkg/controllers/cluster/handler.go index 2482856..a7b4508 100644 --- a/pkg/controllers/cluster/handler.go +++ b/pkg/controllers/cluster/handler.go @@ -15,7 +15,7 @@ import ( type KVRocksClusterHandler struct { instance *kvrocksv1alpha1.KVRocks k8s *k8s.Client - kvrocks *kvrocks.Client + kvrocks kvrocks.Client log logr.Logger password string requeue bool @@ -28,11 +28,12 @@ type KVRocksClusterHandler struct { func NewKVRocksClusterHandler( k8s *k8s.Client, - kvrocks *kvrocks.Client, + kvrocks kvrocks.Client, log logr.Logger, key types.NamespacedName, instance *kvrocksv1alpha1.KVRocks, - controllerClient *controller.Client) *KVRocksClusterHandler { + controllerClient *controller.Client, +) *KVRocksClusterHandler { return &KVRocksClusterHandler{ instance: instance, k8s: k8s, diff --git a/pkg/controllers/common/handler.go b/pkg/controllers/common/handler.go index 73e4d37..c6e86f0 100644 --- a/pkg/controllers/common/handler.go +++ b/pkg/controllers/common/handler.go @@ -9,11 +9,11 @@ import ( type CommandHandler struct { instance *kvrocksv1alpha1.KVRocks k8s *k8s.Client - kvrocks *kvrocks.Client + kvrocks kvrocks.Client password string } -func NewCommandHandler(instance *kvrocksv1alpha1.KVRocks, k8s *k8s.Client, kvrocks *kvrocks.Client, password string) *CommandHandler { +func NewCommandHandler(instance *kvrocksv1alpha1.KVRocks, k8s *k8s.Client, kvrocks kvrocks.Client, password string) *CommandHandler { return &CommandHandler{ instance: instance, k8s: k8s, diff --git a/pkg/controllers/events/event.go b/pkg/controllers/events/event.go index 39fec1f..89767b3 100644 --- a/pkg/controllers/events/event.go +++ b/pkg/controllers/events/event.go @@ -44,12 +44,12 @@ type event struct { messages *messageQueue producerSentinels map[string]func(msg *produceMessage) k8s *k8s.Client - kvrocks *kvrocks.Client + kvrocks kvrocks.Client controller *controller.Client log logr.Logger } -func NewEvent(k8s *k8s.Client, kvrocks *kvrocks.Client, controller *controller.Client, log logr.Logger) *event { +func NewEvent(k8s *k8s.Client, kvrocks kvrocks.Client, controller *controller.Client, log logr.Logger) *event { return &event{ k8s: k8s, kvrocks: kvrocks, diff --git a/pkg/controllers/kvrocks_controller.go b/pkg/controllers/kvrocks_controller.go index 73d9685..e34ff21 100644 --- a/pkg/controllers/kvrocks_controller.go +++ b/pkg/controllers/kvrocks_controller.go @@ -30,7 +30,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -53,7 +53,7 @@ type KVRocksHandler interface { // KVRocksReconciler reconciles a KVRocks object type KVRocksReconciler struct { - client.Client + k8sApiClient.Client Log logr.Logger Scheme *runtime.Scheme once sync.Once @@ -156,7 +156,7 @@ func (r *KVRocksReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // SetupWithManager sets up the controller with the Manager. func (r *KVRocksReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error { - mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, "spec.nodeName", func(o client.Object) []string { + mgr.GetFieldIndexer().IndexField(context.Background(), &corev1.Pod{}, "spec.nodeName", func(o k8sApiClient.Object) []string { return []string{o.(*corev1.Pod).Spec.NodeName} }) return ctrl.NewControllerManagedBy(mgr). diff --git a/pkg/controllers/sentinel/handler.go b/pkg/controllers/sentinel/handler.go index 98d4894..e93cade 100644 --- a/pkg/controllers/sentinel/handler.go +++ b/pkg/controllers/sentinel/handler.go @@ -13,7 +13,7 @@ type KVRocksSentinelHandler struct { instance *kvrocksv1alpha1.KVRocks key types.NamespacedName k8s *k8s.Client - kvrocks *kvrocks.Client + kvrocks kvrocks.Client log logr.Logger pods []string requeue bool @@ -21,10 +21,11 @@ type KVRocksSentinelHandler struct { func NewKVRocksSentinelHandler( k8s *k8s.Client, - kvrocks *kvrocks.Client, + kvrocks kvrocks.Client, logger logr.Logger, key types.NamespacedName, - instance *kvrocksv1alpha1.KVRocks) *KVRocksSentinelHandler { + instance *kvrocksv1alpha1.KVRocks, +) *KVRocksSentinelHandler { return &KVRocksSentinelHandler{ instance: instance, k8s: k8s, diff --git a/pkg/controllers/standard/handler.go b/pkg/controllers/standard/handler.go index b51cae6..9f0ca31 100644 --- a/pkg/controllers/standard/handler.go +++ b/pkg/controllers/standard/handler.go @@ -14,7 +14,7 @@ import ( type KVRocksStandardHandler struct { instance *kvrocksv1alpha1.KVRocks k8s *k8s.Client - kvrocks *kvrocks.Client + kvrocks kvrocks.Client log logr.Logger password string stsNodes []*kvrocks.Node @@ -24,10 +24,11 @@ type KVRocksStandardHandler struct { func NewKVRocksStandardHandler( k8s *k8s.Client, - kvrocks *kvrocks.Client, + kvrocks kvrocks.Client, log logr.Logger, key types.NamespacedName, - instance *kvrocksv1alpha1.KVRocks) *KVRocksStandardHandler { + instance *kvrocksv1alpha1.KVRocks, +) *KVRocksStandardHandler { return &KVRocksStandardHandler{ instance: instance, k8s: k8s, diff --git a/pkg/controllers/suite_test.go b/pkg/controllers/suite_test.go index 9adea57..0942e40 100644 --- a/pkg/controllers/suite_test.go +++ b/pkg/controllers/suite_test.go @@ -23,7 +23,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "k8s.io/client-go/kubernetes/scheme" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -35,8 +35,10 @@ import ( // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. -var k8sClient client.Client -var testEnv *envtest.Environment +var ( + k8sClient k8sApiClient.Client + testEnv *envtest.Environment +) func TestAPIs(t *testing.T) { RegisterFailHandler(Fail) @@ -65,10 +67,9 @@ var _ = BeforeSuite(func() { //+kubebuilder:scaffold:scheme - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + k8sClient, err = k8sApiClient.New(cfg, k8sApiClient.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) Expect(k8sClient).NotTo(BeNil()) - }, 60) var _ = AfterSuite(func() { diff --git a/test/e2e/cluster/cluster_test.go b/test/e2e/cluster/cluster_test.go index e7ca3f4..ff0a9bb 100644 --- a/test/e2e/cluster/cluster_test.go +++ b/test/e2e/cluster/cluster_test.go @@ -28,7 +28,7 @@ import ( var ( env *KubernetesEnv ctx context.Context - kvrocksClient *kvrocks.Client + kvrocksClient kvrocks.Client ) var _ = BeforeSuite(func() { @@ -132,7 +132,6 @@ var _ = Describe("Operator for Cluster Mode", func() { }) It("test recover when slave down", func() { - var pod corev1.Pod key := types.NamespacedName{ Namespace: kvrocksInstance.GetNamespace(), @@ -173,7 +172,6 @@ var _ = Describe("Operator for Cluster Mode", func() { }) It("test recover when master down", func() { - var pod corev1.Pod key := types.NamespacedName{ Namespace: kvrocksInstance.GetNamespace(), @@ -304,7 +302,6 @@ var _ = Describe("Operator for Cluster Mode", func() { return checkKvrocksCluster(kvrocksKey, sentinelKey) }, timeout, interval).Should(Succeed()) }) - }) func checkKvrocksCluster(kvrocksKey, sentinelKey types.NamespacedName) error { diff --git a/test/e2e/standard/standard_test.go b/test/e2e/standard/standard_test.go index 2616f2f..f386c36 100644 --- a/test/e2e/standard/standard_test.go +++ b/test/e2e/standard/standard_test.go @@ -22,14 +22,14 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) var ( env *KubernetesEnv ctx context.Context - kvrocksClient *kvrocks.Client + kvrocksClient kvrocks.Client ) var _ = BeforeSuite(func() { @@ -252,7 +252,6 @@ var _ = Describe("Operator for Standard Mode", func() { return checkKVRocks(kvrocksInstance, sentinelInstance) }, timeout, interval).Should(Succeed()) }) - }) func checkKVRocks(kvrocksInstance, sentinelInstance *kvrocksv1alpha1.KVRocks) error { @@ -323,7 +322,7 @@ func checkKVRocks(kvrocksInstance, sentinelInstance *kvrocksv1alpha1.KVRocks) er } var pvcList corev1.PersistentVolumeClaimList - if err := env.Client.List(ctx, &pvcList, client.InNamespace(kvrocksInstance.Namespace), client.MatchingLabels(kvrocksInstance.Labels)); err != nil { + if err := env.Client.List(ctx, &pvcList, k8sApiClient.InNamespace(kvrocksInstance.Namespace), k8sApiClient.MatchingLabels(kvrocksInstance.Labels)); err != nil { return err } if len(pvcList.Items) != replicas { @@ -345,9 +344,9 @@ func getSentinelPodList(sentinel *kvrocksv1alpha1.KVRocks) (*corev1.PodList, err labelSelector := labels.Set(deployment.Spec.Selector.MatchLabels).AsSelector() podList := &corev1.PodList{} - listOpts := []client.ListOption{ - client.InNamespace(sentinel.Namespace), - client.MatchingLabelsSelector{Selector: labelSelector}, + listOpts := []k8sApiClient.ListOption{ + k8sApiClient.InNamespace(sentinel.Namespace), + k8sApiClient.MatchingLabelsSelector{Selector: labelSelector}, } if err := env.Client.List(ctx, podList, listOpts...); err != nil { return nil, err diff --git a/test/e2e/util/chaos_common.go b/test/e2e/util/chaos_common.go index 16eb748..df2b0d8 100644 --- a/test/e2e/util/chaos_common.go +++ b/test/e2e/util/chaos_common.go @@ -6,7 +6,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) const ( @@ -14,7 +14,7 @@ const ( ) type Experiment struct { - chaosObject client.Object + chaosObject k8sApiClient.Object name string namespace string } @@ -23,7 +23,7 @@ func (env *KubernetesEnv) addChaosExperiment(experiment Experiment) { env.ChaosMeshExperiments = append(env.ChaosMeshExperiments, experiment) } -func (env *KubernetesEnv) CreateExperiment(chaos client.Object) *Experiment { +func (env *KubernetesEnv) CreateExperiment(chaos k8sApiClient.Object) *Experiment { fmt.Fprintf(GinkgoWriter, "CreateExperiment name=%s\n", chaos.GetName()) err := env.Client.Create(context.Background(), chaos) Expect(err).NotTo(HaveOccurred()) diff --git a/test/e2e/util/kubernetes_env.go b/test/e2e/util/kubernetes_env.go index d82e8e1..f147f62 100644 --- a/test/e2e/util/kubernetes_env.go +++ b/test/e2e/util/kubernetes_env.go @@ -19,13 +19,13 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/homedir" - "sigs.k8s.io/controller-runtime/pkg/client" + k8sApiClient "sigs.k8s.io/controller-runtime/pkg/client" ) type KubernetesEnv struct { config *Config kubernetesConfig *rest.Config - Client client.Client + Client k8sApiClient.Client ChaosMeshExperiments []Experiment Clean func() error } @@ -53,13 +53,13 @@ func Start(config *Config) *KubernetesEnv { // scheme env.registerScheme() - //config + // config checkClusterName(env.config.KubeConfig, env.config.ClusterName) cfg, err := loadKubernetesConfig(env.config.KubeConfig) Expect(err).NotTo(HaveOccurred()) env.kubernetesConfig = cfg - env.Client, err = client.New(env.kubernetesConfig, client.Options{Scheme: scheme.Scheme}) + env.Client, err = k8sApiClient.New(env.kubernetesConfig, k8sApiClient.Options{Scheme: scheme.Scheme}) Expect(err).NotTo(HaveOccurred()) env.installKruise()