Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: populating HelmListClusterMap with appName-namespace as key instead of only appName to handle multiple app with same name in diff namespace #126

Merged
merged 7 commits into from
May 20, 2024
18 changes: 11 additions & 7 deletions pkg/k8sInformer/K8sInformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type K8sInformer interface {
stopInformer(clusterName string, clusterId int)
startInformerAndPopulateCache(clusterId int) error
GetAllReleaseByClusterId(clusterId int) []*client.DeployedAppDetail
CheckReleaseExists(clusterId int32, releaseName string) bool
CheckReleaseExists(clusterId int32, releaseIdentifier string) bool
GetClusterClientSet(clusterInfo bean.ClusterInfo) (*kubernetes.Clientset, error)
RegisterListener(listener ClusterSecretUpdateListener)
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func (impl *K8sInformerImpl) startInformerAndPopulateCache(clusterId int) error
}
impl.mutex.Lock()
defer impl.mutex.Unlock()
impl.HelmListClusterMap[clusterId][releaseDTO.Name+string(rune(clusterModel.Id))] = appDetail
impl.HelmListClusterMap[clusterId][impl.getUniqueReleaseKey(&ReleaseDto{releaseDTO}, clusterModel.Id)] = appDetail
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
Expand All @@ -445,8 +445,8 @@ func (impl *K8sInformerImpl) startInformerAndPopulateCache(clusterId int) error
}
impl.mutex.Lock()
defer impl.mutex.Unlock()
// adding cluster id with release name because there can be case when two cluster have release with same name
impl.HelmListClusterMap[clusterId][releaseDTO.Name+string(rune(clusterModel.Id))] = appDetail
// adding cluster id with release name and namespace because there can be case when two cluster or two namespaces have release with same name
impl.HelmListClusterMap[clusterId][impl.getUniqueReleaseKey(&ReleaseDto{releaseDTO}, clusterModel.Id)] = appDetail
}
},
DeleteFunc: func(obj interface{}) {
Expand All @@ -461,7 +461,7 @@ func (impl *K8sInformerImpl) startInformerAndPopulateCache(clusterId int) error
}
impl.mutex.Lock()
defer impl.mutex.Unlock()
delete(impl.HelmListClusterMap[clusterId], releaseDTO.Name+string(rune(clusterModel.Id)))
delete(impl.HelmListClusterMap[clusterId], impl.getUniqueReleaseKey(&ReleaseDto{releaseDTO}, clusterModel.Id))
}
},
})
Expand All @@ -471,6 +471,10 @@ func (impl *K8sInformerImpl) startInformerAndPopulateCache(clusterId int) error
return nil
}

func (impl *K8sInformerImpl) getUniqueReleaseKey(release *ReleaseDto, clusterId int) string {
return release.getUniqueReleaseIdentifier() + "_" + strconv.Itoa(clusterId)
}

func (impl *K8sInformerImpl) GetAllReleaseByClusterId(clusterId int) []*client.DeployedAppDetail {

var deployedAppDetailList []*client.DeployedAppDetail
Expand All @@ -481,9 +485,9 @@ func (impl *K8sInformerImpl) GetAllReleaseByClusterId(clusterId int) []*client.D
return deployedAppDetailList
}

func (impl *K8sInformerImpl) CheckReleaseExists(clusterId int32, releaseName string) bool {
func (impl *K8sInformerImpl) CheckReleaseExists(clusterId int32, releaseIdentifier string) bool {
releaseMap := impl.HelmListClusterMap[int(clusterId)]
_, ok := releaseMap[releaseName]
_, ok := releaseMap[releaseIdentifier]
if ok {
return true
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/k8sInformer/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package k8sInformer

import "helm.sh/helm/v3/pkg/release"

type ReleaseDto struct {
*release.Release
}

func (r *ReleaseDto) getUniqueReleaseIdentifier() string {
return r.Namespace + "_" + r.Name
}
2 changes: 1 addition & 1 deletion pkg/service/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ func (impl HelmAppServiceImpl) installRelease(ctx context.Context, request *clie
InstallAppVersionHistoryId: int(request.InstallAppVersionHistoryId),
}
// Checking release exist because there can be case when release already exist with same name
releaseExist := impl.K8sInformer.CheckReleaseExists(releaseIdentifier.ClusterConfig.ClusterId, releaseIdentifier.ReleaseName)
releaseExist := impl.K8sInformer.CheckReleaseExists(releaseIdentifier.ClusterConfig.ClusterId, getUniqueReleaseIdentifierName(releaseIdentifier))
if releaseExist {
// release with name already exist, will not continue with release
helmInstallMessage.ErrorInInstallation = true
Expand Down
10 changes: 10 additions & 0 deletions pkg/service/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import (
client "github.com/devtron-labs/kubelink/grpc"
"strconv"
)

func getUniqueReleaseIdentifierName(releaseIdentifier *client.ReleaseIdentifier) string {
return releaseIdentifier.ReleaseNamespace + "_" + releaseIdentifier.ReleaseName + "_" + strconv.Itoa(int(releaseIdentifier.ClusterConfig.ClusterId))
}