Skip to content

Commit

Permalink
chore: switch exposed kvrock client to interface (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayinzhang-mint authored Nov 19, 2023
1 parent 0f74878 commit 55299c6
Show file tree
Hide file tree
Showing 29 changed files with 174 additions and 128 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/helm-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: helm-lint
on:
push:
branches:
- unstable
- unstable
pull_request:
branches: [ unstable ]
branches: [unstable]

jobs:
lint-test-helm:
Expand All @@ -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
run: ct lint --all --chart-dirs deploy/ --validate-maintainers=false
21 changes: 12 additions & 9 deletions pkg/client/k8s/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package k8s

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -70,6 +71,7 @@ func TestGetConfigMap(t *testing.T) {
})
}
}

func TestUpdateConfigMap(t *testing.T) {
ns, updatedKey, updatedValue := "unit-test", "key1", "value2"
testConfigMap := &corev1.ConfigMap{
Expand Down Expand Up @@ -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)
}
Expand All @@ -133,8 +135,8 @@ func TestUpdateConfigMap(t *testing.T) {
}
})
}

}

func TestCreateOrUpdateConfigMap(t *testing.T) {
ns := "unit-test"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions pkg/client/k8s/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -198,6 +198,7 @@ func TestUpdateDeployment(t *testing.T) {
})
}
}

func TestListDeploymentPods(t *testing.T) {
ns := "unit-test"
testDeployment := &appsv1.Deployment{
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/k8s/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/k8s/kvrocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions pkg/client/k8s/kvrocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package k8s

import (
"context"
"testing"

kvrocksv1alpha1 "github.com/RocksLabs/kvrocks-operator/api/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/errors"
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 TestGetKVRocks(t *testing.T) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions pkg/client/k8s/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package k8s

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/k8s/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 55299c6

Please sign in to comment.