Skip to content

Commit

Permalink
testing-client
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Nov 8, 2023
1 parent a2e4b79 commit fd68e31
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 42 deletions.
26 changes: 10 additions & 16 deletions k8sutils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

type K8sConfigProvider = func() (*rest.Config, error)

// generateK8sClient create client for kubernetes
func generateK8sClient() *kubernetes.Clientset {
config, err := generateK8sConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
func generateK8sClient(configProvider K8sConfigProvider) (kubernetes.Interface, error) {
config, err := configProvider()

Check warning on line 15 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L14-L15

Added lines #L14 - L15 were not covered by tests
if err != nil {
panic(err.Error())
return nil, err

Check warning on line 17 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L17

Added line #L17 was not covered by tests
}
return clientset
return kubernetes.NewForConfig(config)

Check warning on line 19 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L19

Added line #L19 was not covered by tests
}

// generateK8sClient create Dynamic client for kubernetes
func generateK8sDynamicClient() dynamic.Interface {
config, err := generateK8sConfig()
if err != nil {
panic(err.Error())
}
dynamicClientset, err := dynamic.NewForConfig(config)
func generateK8sDynamicClient(configProvider K8sConfigProvider) (dynamic.Interface, error) {
config, err := configProvider()

Check warning on line 24 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L23-L24

Added lines #L23 - L24 were not covered by tests
if err != nil {
panic(err.Error())
return nil, err

Check warning on line 26 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L26

Added line #L26 was not covered by tests
}
return dynamicClientset
return dynamic.NewForConfig(config)

Check warning on line 28 in k8sutils/client.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/client.go#L28

Added line #L28 was not covered by tests
}

// generateK8sConfig will load the kube config file
Expand Down
23 changes: 19 additions & 4 deletions k8sutils/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ func AddRedisSentinelFinalizer(cr *redisv1beta2.RedisSentinel, cl client.Client)
func finalizeRedisPVC(cr *redisv1beta2.Redis) error {
logger := finalizerLogger(cr.Namespace, RedisFinalizer)
PVCName := cr.Name + "-" + cr.Name + "-0"
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
err = client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})

Check warning on line 145 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L140-L145

Added lines #L140 - L145 were not covered by tests
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand All @@ -148,10 +153,15 @@ func finalizeRedisPVC(cr *redisv1beta2.Redis) error {
// finalizeRedisClusterPVC delete PVCs
func finalizeRedisClusterPVC(cr *redisv1beta2.RedisCluster) error {
logger := finalizerLogger(cr.Namespace, RedisClusterFinalizer)
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}

Check warning on line 160 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L156-L160

Added lines #L156 - L160 were not covered by tests
for _, role := range []string{"leader", "follower"} {
for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ {
PVCName := cr.Name + "-" + cr.Name + "-" + role + "-" + strconv.Itoa(i)
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})

Check warning on line 164 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L164

Added line #L164 was not covered by tests
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand All @@ -160,7 +170,7 @@ func finalizeRedisClusterPVC(cr *redisv1beta2.RedisCluster) error {
if cr.Spec.Storage.NodeConfVolume {
for i := 0; i < int(cr.Spec.GetReplicaCounts(role)); i++ {
PVCName := "node-conf" + cr.Name + "-" + role + "-" + strconv.Itoa(i)
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})

Check warning on line 173 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L173

Added line #L173 was not covered by tests
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand All @@ -175,9 +185,14 @@ func finalizeRedisClusterPVC(cr *redisv1beta2.RedisCluster) error {
// finalizeRedisReplicationPVC delete PVCs
func finalizeRedisReplicationPVC(cr *redisv1beta2.RedisReplication) error {
logger := finalizerLogger(cr.Namespace, RedisReplicationFinalizer)
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}

Check warning on line 192 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L188-L192

Added lines #L188 - L192 were not covered by tests
for i := 0; i < int(cr.Spec.GetReplicationCounts("replication")); i++ {
PVCName := cr.Name + "-" + cr.Name + "-" + strconv.Itoa(i)
err := generateK8sClient().CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})
err := client.CoreV1().PersistentVolumeClaims(cr.Namespace).Delete(context.TODO(), PVCName, metav1.DeleteOptions{})

Check warning on line 195 in k8sutils/finalizer.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/finalizer.go#L195

Added line #L195 was not covered by tests
if err != nil && !errors.IsNotFound(err) {
logger.Error(err, "Could not delete Persistent Volume Claim "+PVCName)
return err
Expand Down
28 changes: 24 additions & 4 deletions k8sutils/poddisruption.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ func patchPodDisruptionBudget(storedPdb *policyv1.PodDisruptionBudget, newPdb *p
// createPodDisruptionBudget is a method to create PodDisruptionBudgets in Kubernetes
func createPodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget) error {
logger := pdbLogger(namespace, pdb.Name)
_, err := generateK8sClient().PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.PolicyV1().PodDisruptionBudgets(namespace).Create(context.TODO(), pdb, metav1.CreateOptions{})

Check warning on line 185 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L180-L185

Added lines #L180 - L185 were not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruptionBudget creation failed")
return err
Expand All @@ -189,7 +194,12 @@ func createPodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudg
// updatePodDisruptionBudget is a method to update PodDisruptionBudgets in Kubernetes
func updatePodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudget) error {
logger := pdbLogger(namespace, pdb.Name)
_, err := generateK8sClient().PolicyV1().PodDisruptionBudgets(namespace).Update(context.TODO(), pdb, metav1.UpdateOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.PolicyV1().PodDisruptionBudgets(namespace).Update(context.TODO(), pdb, metav1.UpdateOptions{})

Check warning on line 202 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L197-L202

Added lines #L197 - L202 were not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruptionBudget update failed")
return err
Expand All @@ -201,7 +211,12 @@ func updatePodDisruptionBudget(namespace string, pdb *policyv1.PodDisruptionBudg
// deletePodDisruptionBudget is a method to delete PodDisruptionBudgets in Kubernetes
func deletePodDisruptionBudget(namespace string, pdbName string) error {
logger := pdbLogger(namespace, pdbName)
err := generateK8sClient().PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdbName, metav1.DeleteOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
err = client.PolicyV1().PodDisruptionBudgets(namespace).Delete(context.TODO(), pdbName, metav1.DeleteOptions{})

Check warning on line 219 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L214-L219

Added lines #L214 - L219 were not covered by tests
if err != nil {
logger.Error(err, "Redis PodDisruption deletion failed")
return err
Expand All @@ -213,10 +228,15 @@ func deletePodDisruptionBudget(namespace string, pdbName string) error {
// GetPodDisruptionBudget is a method to get PodDisruptionBudgets in Kubernetes
func GetPodDisruptionBudget(namespace string, pdb string) (*policyv1.PodDisruptionBudget, error) {
logger := pdbLogger(namespace, pdb)
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return nil, err
}

Check warning on line 235 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L231-L235

Added lines #L231 - L235 were not covered by tests
getOpts := metav1.GetOptions{
TypeMeta: generateMetaInformation("PodDisruptionBudget", "policy/v1"),
}
pdbInfo, err := generateK8sClient().PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), pdb, getOpts)
pdbInfo, err := client.PolicyV1().PodDisruptionBudgets(namespace).Get(context.TODO(), pdb, getOpts)

Check warning on line 239 in k8sutils/poddisruption.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/poddisruption.go#L239

Added line #L239 was not covered by tests
if err != nil {
logger.V(1).Info("Redis PodDisruptionBudget get action failed")
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion k8sutils/redis-sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ func getSentinelEnvVariable(ctx context.Context, cr *redisv1beta2.RedisSentinel)

func getRedisReplicationMasterIP(ctx context.Context, cr *redisv1beta2.RedisSentinel) string {
logger := generateRedisManagerLogger(cr.Namespace, cr.ObjectMeta.Name)
dClient, err := generateK8sDynamicClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Failed to generate dynamic client")
return ""
}

Check warning on line 290 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L286-L290

Added lines #L286 - L290 were not covered by tests

replicationName := cr.Spec.RedisSentinelConfig.RedisReplicationName
replicationNamespace := cr.Namespace
Expand All @@ -291,7 +296,7 @@ func getRedisReplicationMasterIP(ctx context.Context, cr *redisv1beta2.RedisSent
var realMasterPod string

// Get Request on Dynamic Client
customObject, err := generateK8sDynamicClient().Resource(schema.GroupVersionResource{
customObject, err := dClient.Resource(schema.GroupVersionResource{

Check warning on line 299 in k8sutils/redis-sentinel.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis-sentinel.go#L299

Added line #L299 was not covered by tests
Group: "redis.redis.opstreelabs.in",
Version: "v1beta2",
Resource: "redisreplications",
Expand Down
21 changes: 18 additions & 3 deletions k8sutils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ type RedisDetails struct {
// getRedisServerIP will return the IP of redis service
func getRedisServerIP(redisInfo RedisDetails) string {
logger := generateRedisManagerLogger(redisInfo.Namespace, redisInfo.PodName)
redisPod, err := generateK8sClient().CoreV1().Pods(redisInfo.Namespace).Get(context.TODO(), redisInfo.PodName, metav1.GetOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Error in getting k8s client")
return ""
}
redisPod, err := client.CoreV1().Pods(redisInfo.Namespace).Get(context.TODO(), redisInfo.PodName, metav1.GetOptions{})

Check warning on line 35 in k8sutils/redis.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis.go#L30-L35

Added lines #L30 - L35 were not covered by tests
if err != nil {
logger.Error(err, "Error in getting redis pod IP")
}
Expand Down Expand Up @@ -358,6 +363,11 @@ func executeCommand(cr *redisv1beta2.RedisCluster, cmd []string, podName string)
execErr bytes.Buffer
)
logger := generateRedisManagerLogger(cr.Namespace, cr.ObjectMeta.Name)
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return
}

Check warning on line 370 in k8sutils/redis.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis.go#L366-L370

Added lines #L366 - L370 were not covered by tests
config, err := generateK8sConfig()
if err != nil {
logger.Error(err, "Could not find pod to execute")
Expand All @@ -369,7 +379,7 @@ func executeCommand(cr *redisv1beta2.RedisCluster, cmd []string, podName string)
return
}

req := generateK8sClient().CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace(cr.Namespace).SubResource("exec")
req := client.CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace(cr.Namespace).SubResource("exec")

Check warning on line 382 in k8sutils/redis.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis.go#L382

Added line #L382 was not covered by tests
req.VersionedParams(&corev1.PodExecOptions{
Container: pod.Spec.Containers[targetContainer].Name,
Command: cmd,
Expand Down Expand Up @@ -397,7 +407,12 @@ func executeCommand(cr *redisv1beta2.RedisCluster, cmd []string, podName string)
// getContainerID will return the id of container from pod
func getContainerID(cr *redisv1beta2.RedisCluster, podName string) (int, *corev1.Pod) {
logger := generateRedisManagerLogger(cr.Namespace, cr.ObjectMeta.Name)
pod, err := generateK8sClient().CoreV1().Pods(cr.Namespace).Get(context.TODO(), podName, metav1.GetOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return -1, nil
}
pod, err := client.CoreV1().Pods(cr.Namespace).Get(context.TODO(), podName, metav1.GetOptions{})

Check warning on line 415 in k8sutils/redis.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/redis.go#L410-L415

Added lines #L410 - L415 were not covered by tests
if err != nil {
logger.Error(err, "Could not get pod info")
}
Expand Down
19 changes: 16 additions & 3 deletions k8sutils/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ var log = logf.Log.WithName("controller_redis")
// getRedisPassword method will return the redis password
func getRedisPassword(namespace, name, secretKey string) (string, error) {
logger := secretLogger(namespace, name)
secretName, err := generateK8sClient().CoreV1().Secrets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return "", err
}
secretName, err := client.CoreV1().Secrets(namespace).Get(context.TODO(), name, metav1.GetOptions{})

Check warning on line 25 in k8sutils/secrets.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/secrets.go#L20-L25

Added lines #L20 - L25 were not covered by tests
if err != nil {
logger.Error(err, "Failed in getting existing secret for redis")
return "", err
Expand All @@ -36,9 +41,13 @@ func secretLogger(namespace string, name string) logr.Logger {
}

func getRedisTLSConfig(cr *redisv1beta2.RedisCluster, redisInfo RedisDetails) *tls.Config {
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
return nil
}

Check warning on line 47 in k8sutils/secrets.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/secrets.go#L44-L47

Added lines #L44 - L47 were not covered by tests
if cr.Spec.TLS != nil {
reqLogger := log.WithValues("Request.Namespace", cr.Namespace, "Request.Name", cr.ObjectMeta.Name)
secretName, err := generateK8sClient().CoreV1().Secrets(cr.Namespace).Get(context.TODO(), cr.Spec.TLS.Secret.SecretName, metav1.GetOptions{})
secretName, err := client.CoreV1().Secrets(cr.Namespace).Get(context.TODO(), cr.Spec.TLS.Secret.SecretName, metav1.GetOptions{})

Check warning on line 50 in k8sutils/secrets.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/secrets.go#L50

Added line #L50 was not covered by tests
if err != nil {
reqLogger.Error(err, "Failed in getting TLS secret for redis")
}
Expand Down Expand Up @@ -84,9 +93,13 @@ func getRedisTLSConfig(cr *redisv1beta2.RedisCluster, redisInfo RedisDetails) *t
}

func getRedisReplicationTLSConfig(cr *redisv1beta2.RedisReplication, redisInfo RedisDetails) *tls.Config {
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
return nil
}

Check warning on line 99 in k8sutils/secrets.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/secrets.go#L96-L99

Added lines #L96 - L99 were not covered by tests
if cr.Spec.TLS != nil {
reqLogger := log.WithValues("Request.Namespace", cr.Namespace, "Request.Name", cr.ObjectMeta.Name)
secretName, err := generateK8sClient().CoreV1().Secrets(cr.Namespace).Get(context.TODO(), cr.Spec.TLS.Secret.SecretName, metav1.GetOptions{})
secretName, err := client.CoreV1().Secrets(cr.Namespace).Get(context.TODO(), cr.Spec.TLS.Secret.SecretName, metav1.GetOptions{})

Check warning on line 102 in k8sutils/secrets.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/secrets.go#L102

Added line #L102 was not covered by tests
if err != nil {
reqLogger.Error(err, "Failed in getting TLS secret for redis")
}
Expand Down
21 changes: 18 additions & 3 deletions k8sutils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func generateServiceType(k8sServiceType string) corev1.ServiceType {
// createService is a method to create service is Kubernetes
func createService(namespace string, service *corev1.Service) error {
logger := serviceLogger(namespace, service.Name)
_, err := generateK8sClient().CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})

Check warning on line 97 in k8sutils/services.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/services.go#L92-L97

Added lines #L92 - L97 were not covered by tests
if err != nil {
logger.Error(err, "Redis service creation is failed")
return err
Expand All @@ -101,7 +106,12 @@ func createService(namespace string, service *corev1.Service) error {
// updateService is a method to update service is Kubernetes
func updateService(namespace string, service *corev1.Service) error {
logger := serviceLogger(namespace, service.Name)
_, err := generateK8sClient().CoreV1().Services(namespace).Update(context.TODO(), service, metav1.UpdateOptions{})
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return err
}
_, err = client.CoreV1().Services(namespace).Update(context.TODO(), service, metav1.UpdateOptions{})

Check warning on line 114 in k8sutils/services.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/services.go#L109-L114

Added lines #L109 - L114 were not covered by tests
if err != nil {
logger.Error(err, "Redis service update failed")
return err
Expand All @@ -113,10 +123,15 @@ func updateService(namespace string, service *corev1.Service) error {
// getService is a method to get service is Kubernetes
func getService(namespace string, service string) (*corev1.Service, error) {
logger := serviceLogger(namespace, service)
client, err := generateK8sClient(generateK8sConfig)
if err != nil {
logger.Error(err, "Could not generate kubernetes client")
return nil, err
}

Check warning on line 130 in k8sutils/services.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/services.go#L126-L130

Added lines #L126 - L130 were not covered by tests
getOpts := metav1.GetOptions{
TypeMeta: generateMetaInformation("Service", "v1"),
}
serviceInfo, err := generateK8sClient().CoreV1().Services(namespace).Get(context.TODO(), service, getOpts)
serviceInfo, err := client.CoreV1().Services(namespace).Get(context.TODO(), service, getOpts)

Check warning on line 134 in k8sutils/services.go

View check run for this annotation

Codecov / codecov/patch

k8sutils/services.go#L134

Added line #L134 was not covered by tests
if err != nil {
logger.V(1).Info("Redis service get action is failed")
return nil, err
Expand Down
Loading

0 comments on commit fd68e31

Please sign in to comment.