Skip to content

Commit

Permalink
fix(redis-controller): fix npe issue and some logic error
Browse files Browse the repository at this point in the history
Signed-off-by: chlins <chlins.zhang@gmail.com>
  • Loading branch information
chlins committed Nov 25, 2020
1 parent 7acd15f commit 3c20164
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ spec:
inClusterCache:
kind: Redis
redisSpec:
schema: redis
server:
replicas: 1
resources:
Expand Down
6 changes: 4 additions & 2 deletions pkg/cluster/controllers/cache/readiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func (rc *RedisController) GetInClusterRedisInfo(cluster *v1alpha2.HarborCluster
}
rc.RedisConnect = connect
client = connect.NewRedisClient()
default:
return nil, errors.New("not supported schema")
}

return client, nil
Expand Down Expand Up @@ -235,7 +237,7 @@ func (rc *RedisController) GetSentinelServiceUrl(name, namespace string, pods []
randomPod := pods[rand.Intn(len(pods))]
url = randomPod.Status.PodIP
} else {
url = fmt.Sprintf("%s-%s.%s.svc", "rfs", name, namespace)
url = fmt.Sprintf("%s-%s.%s.svc.cluster.local", "rfs", name, namespace)
}

return url
Expand All @@ -249,7 +251,7 @@ func (rc *RedisController) GetRedisServiceUrl(name, namespace string, pods []cor
if err != nil {
url = randomPod.Status.PodIP
} else {
url = fmt.Sprintf("%s-%s.%s.svc", "cluster", name, namespace)
url = fmt.Sprintf("%s.%s.svc.cluster.local", rc.ResourceManager.GetServiceName(), namespace)
}

return url
Expand Down
7 changes: 5 additions & 2 deletions pkg/cluster/controllers/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ type RedisController struct {
Scheme *runtime.Scheme
RedisConnect *RedisConnect
ResourceManager ResourceManager
expectCR, actualCR *redisOp.RedisFailover
expectCR, actualCR runtime.Object
}

// Apply creates/updates/scales the resources, like kubernetes apply operation.
func (rc *RedisController) Apply(ctx context.Context, cluster *v1alpha2.HarborCluster) (*lcm.CRStatus, error) {
rc.Client.WithContext(ctx)
rc.DClient.WithContext(ctx)
rc.ResourceManager = &RedisResourceManager{cluster: cluster}

crdClient := rc.DClient.WithResource(redisFailoversGVR).WithNamespace(cluster.Namespace)

Expand All @@ -63,6 +64,7 @@ func (rc *RedisController) Apply(ctx context.Context, cluster *v1alpha2.HarborCl
} else if err != nil {
return cacheNotReadyStatus(ErrorGetRedisClient, err.Error()), err
}
rc.actualCR = actualCR

expectCR := rc.ResourceManager.GetCacheCR()
if err := controllerutil.SetControllerReference(cluster, expectCR.(metav1.Object), rc.Scheme); err != nil {
Expand All @@ -72,7 +74,8 @@ func (rc *RedisController) Apply(ctx context.Context, cluster *v1alpha2.HarborCl
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(actualCR.UnstructuredContent(), rc.actualCR); err != nil {
return cacheNotReadyStatus(ErrorDefaultUnstructuredConverter, err.Error()), err
}
rc.expectCR = expectCR.(*redisOp.RedisFailover)

rc.expectCR = expectCR
crStatus, err := rc.Update(cluster)
if err != nil {
return crStatus, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/cluster/controllers/cache/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (rm *RedisResourceManager) GetCacheCR() runtime.Object {
Requests: resource,
},
},
Auth: redisOp.AuthSettings{SecretPath: rm.cluster.Name},
Auth: redisOp.AuthSettings{SecretPath: rm.GetSecretName()},
},
}
}
Expand Down Expand Up @@ -136,6 +136,7 @@ func (rm *RedisResourceManager) GetSecret() *corev1.Secret {
},
StringData: map[string]string{
"redis-password": passStr,
"password": passStr,
},
}
}
Expand Down

0 comments on commit 3c20164

Please sign in to comment.