Skip to content

Commit

Permalink
fix: add suffix name redis for cache cr
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 27, 2020
1 parent 4b9230e commit 0394939
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
10 changes: 5 additions & 5 deletions pkg/cluster/controllers/cache/readiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (rc *RedisController) generateRedisSpec() *v1alpha2.ExternalRedisSpec {
return &v1alpha2.ExternalRedisSpec{
RedisHostSpec: harbormetav1.RedisHostSpec{
Host: rc.ResourceManager.GetServiceName(),
Port: 6379,
Port: RedisRedisConnPort,
},
RedisCredentials: harbormetav1.RedisCredentials{
PasswordRef: rc.ResourceManager.GetSecretName(),
Expand Down Expand Up @@ -132,13 +132,13 @@ func (rc *RedisController) CheckInClusterRedisHealth(ctx context.Context, cluste
return nil, err
}

_, sentinelPodList, err := rc.GetDeploymentPods(cluster.Name, cluster.Namespace)
_, sentinelPodList, err := rc.GetDeploymentPods(rc.ResourceManager.GetCacheCRName(), cluster.Namespace)
if err != nil {
rc.Log.Error(err, "Fail to get deployment pods.")
return nil, err
}

_, redisPodList, err := rc.GetStatefulSetPods(cluster.Name, cluster.Namespace)
_, redisPodList, err := rc.GetStatefulSetPods(rc.ResourceManager.GetCacheCRName(), cluster.Namespace)
if err != nil {
rc.Log.Error(err, "Fail to get deployment pods.")
return nil, err
Expand All @@ -157,7 +157,7 @@ func (rc *RedisController) CheckInClusterRedisHealth(ctx context.Context, cluste
if len(currentSentinelPods) == 0 {
return nil, errors.New("need to requeue")
}
endpoint := rc.GetSentinelServiceUrl(cluster.Name, cluster.Namespace, currentSentinelPods)
endpoint := rc.GetSentinelServiceUrl(rc.ResourceManager.GetCacheCRName(), cluster.Namespace, currentSentinelPods)
config := &lcm.ServiceConfig{
Endpoint: &lcm.Endpoint{
Host: endpoint,
Expand All @@ -175,7 +175,7 @@ func (rc *RedisController) CheckInClusterRedisHealth(ctx context.Context, cluste
if len(currentRedisPods) == 0 {
return nil, errors.New("need to requeue")
}
endpoint := rc.GetRedisServiceUrl(cluster.Name, cluster.Namespace, currentRedisPods)
endpoint := rc.GetRedisServiceUrl(rc.ResourceManager.GetCacheCRName(), cluster.Namespace, currentRedisPods)
config := &lcm.ServiceConfig{
Endpoint: &lcm.Endpoint{
Host: endpoint,
Expand Down
6 changes: 1 addition & 5 deletions pkg/cluster/controllers/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (rc *RedisController) Apply(ctx context.Context, cluster *v1alpha2.HarborCl

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

actualCR, err := crdClient.Get(cluster.Name, metav1.GetOptions{})
actualCR, err := crdClient.Get(rc.ResourceManager.GetCacheCRName(), metav1.GetOptions{})
if errors.IsNotFound(err) {
return rc.Deploy(cluster)
} else if err != nil {
Expand All @@ -75,10 +75,6 @@ func (rc *RedisController) Apply(ctx context.Context, cluster *v1alpha2.HarborCl
return cacheNotReadyStatus(ErrorSetOwnerReference, err.Error()), err
}

if err = runtime.DefaultUnstructuredConverter.FromUnstructured(actualCR.UnstructuredContent(), rc.actualCR); err != nil {
return cacheNotReadyStatus(ErrorDefaultUnstructuredConverter, err.Error()), err
}

rc.expectCR = expectCR
crStatus, err := rc.Update(cluster)
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions pkg/cluster/controllers/cache/resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ResourceManager interface {
// ResourceGetter gets resources.
type ResourceGetter interface {
GetCacheCR() runtime.Object
GetCacheCRName() string
GetResourceList() corev1.ResourceList
GetServiceName() string
GetService() *corev1.Service
Expand All @@ -30,6 +31,8 @@ type ResourceGetter interface {
GetStorageSize() string
}

var _ ResourceManager = &RedisResourceManager{}

type RedisResourceManager struct {
cluster *v1alpha2.HarborCluster
}
Expand All @@ -55,7 +58,7 @@ func (rm *RedisResourceManager) GetCacheCR() runtime.Object {
APIVersion: "databases.spotahome.com/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: rm.cluster.Name,
Name: rm.GetCacheCRName(),
Namespace: rm.cluster.Namespace,
Labels: rm.GetLabels(),
},
Expand All @@ -82,9 +85,14 @@ func (rm *RedisResourceManager) GetCacheCR() runtime.Object {
}
}

// GetCacheCRName gets cache cr name.
func (rm *RedisResourceManager) GetCacheCRName() string {
return fmt.Sprintf("%s-%s", rm.cluster.Name, "redis")
}

// GetServiceName gets service name.
func (rm *RedisResourceManager) GetServiceName() string {
return fmt.Sprintf("%s-%s", "redis", rm.cluster.Name)
return rm.GetCacheCRName()
}

// GetService gets service.
Expand Down Expand Up @@ -121,7 +129,7 @@ func (rm *RedisResourceManager) GetService() *corev1.Service {

// GetSecretName gets secret name.
func (rm *RedisResourceManager) GetSecretName() string {
return fmt.Sprintf("%s-%s", "redis", rm.cluster.Name)
return rm.GetCacheCRName()
}

// GetSecret gets redis secret.
Expand Down
11 changes: 7 additions & 4 deletions pkg/cluster/controllers/cache/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import (
// - update RedisFailovers CR resource
func (rc *RedisController) RollingUpgrades(cluster *v1alpha2.HarborCluster) (*lcm.CRStatus, error) {
crdClient := rc.DClient.WithResource(redisFailoversGVR).WithNamespace(cluster.Namespace)
if rc.expectCR == nil {
if rc.expectCR == nil || rc.actualCR == nil {
return cacheUnknownStatus(), nil
}

var (
actualCR, expectCR redisOp.RedisFailover
)
expectCR := rc.expectCR.(*redisOp.RedisFailover)
unstructuredActualCR := rc.actualCR.(*unstructured.Unstructured)
actualCR := &redisOp.RedisFailover{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstructuredActualCR.UnstructuredContent(), actualCR); err != nil {
return cacheNotReadyStatus(ErrorDefaultUnstructuredConverter, err.Error()), err
}

if !IsEqual(actualCR.DeepCopy().Spec, expectCR.DeepCopy().Spec) {
//msg := fmt.Sprintf(UpdateMessageRedisCluster, cluster.Name)
Expand Down

0 comments on commit 0394939

Please sign in to comment.