Skip to content

Commit

Permalink
handled namespace case if deleted by kubectl (#5081)
Browse files Browse the repository at this point in the history
Co-authored-by: Shivam Nagar <[email protected]>
  • Loading branch information
Laeeqdev and Shivam-nagar23 authored May 10, 2024
1 parent f2df564 commit bb7960d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
46 changes: 32 additions & 14 deletions api/helm-app/service/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type HelmAppService interface {
ValidateOCIRegistry(ctx context.Context, OCIRegistryRequest *gRPC.RegistryCredential) bool
GetRevisionHistoryMaxValue(appType bean.SourceAppType) int32
GetResourceTreeForExternalResources(ctx context.Context, clusterId int, clusterConfig *gRPC.ClusterConfig, resources []*gRPC.ExternalResourceDetail) (*gRPC.ResourceTreeResponse, error)
CheckIfNsExistsForClusterIds(clusterIdToNsMap map[int]string, clusterIds []int) error
}

type HelmAppServiceImpl struct {
Expand Down Expand Up @@ -536,15 +537,15 @@ func (impl *HelmAppServiceImpl) DeleteApplication(ctx context.Context, app *AppI
//handles the case when a user deletes namespace using kubectl but created it using devtron dashboard in
//that case DeleteApplication returned with grpc error and the user was not able to delete the
//cd-pipeline after helm app is created in that namespace.
exists, err := impl.checkIfNsExists(app)
clusterIdToNsMap := map[int]string{
app.ClusterId: app.Namespace,
}
clusterId := make([]int, 0)
clusterId = append(clusterId, app.ClusterId)
err = impl.CheckIfNsExistsForClusterIds(clusterIdToNsMap, clusterId)
if err != nil {
impl.logger.Errorw("error in checking if namespace exists or not", "err", err, "clusterId", app.ClusterId)
return nil, err
}
if !exists {
return nil, models.NamespaceNotExistError{Err: fmt.Errorf("namespace %s does not exist", app.Namespace)}
}

req := &gRPC.ReleaseIdentifier{
ClusterConfig: config,
ReleaseName: app.ReleaseName,
Expand All @@ -571,23 +572,19 @@ func (impl *HelmAppServiceImpl) DeleteApplication(ctx context.Context, app *AppI
return response, nil
}

func (impl *HelmAppServiceImpl) checkIfNsExists(app *AppIdentifier) (bool, error) {
clusterBean, err := impl.clusterService.FindById(app.ClusterId)
if err != nil {
impl.logger.Errorw("error in getting cluster bean", "error", err, "clusterId", app.ClusterId)
return false, err
}
func (impl *HelmAppServiceImpl) checkIfNsExists(namespace string, clusterBean *cluster.ClusterBean) (bool, error) {

config, err := clusterBean.GetClusterConfig()
if err != nil {
impl.logger.Errorw("error in getting cluster config", "error", err, "clusterId", app.ClusterId)
impl.logger.Errorw("error in getting cluster config", "error", err, "clusterId", clusterBean.Id)
return false, err
}
v12Client, err := impl.K8sUtil.GetCoreV1Client(config)
if err != nil {
impl.logger.Errorw("error in getting k8s client", "err", err, "clusterHost", config.Host)
return false, err
}
exists, err := impl.K8sUtil.CheckIfNsExists(app.Namespace, v12Client)
exists, err := impl.K8sUtil.CheckIfNsExists(namespace, v12Client)
if err != nil {
if IsClusterUnReachableError(err) {
impl.logger.Errorw("k8s cluster unreachable", "err", err)
Expand Down Expand Up @@ -1114,3 +1111,24 @@ func (impl *HelmAppServiceImpl) GetRevisionHistoryMaxValue(appType bean.SourceAp
return 0
}
}
func (impl *HelmAppServiceImpl) CheckIfNsExistsForClusterIds(clusterIdToNsMap map[int]string, clusterIds []int) error {

clusterBeans, err := impl.clusterService.FindByIds(clusterIds)
if err != nil {
impl.logger.Errorw("error in getting cluster bean", "error", err, "clusterIds", clusterIds)
return err
}
for _, clusterBean := range clusterBeans {
if namespace, ok := clusterIdToNsMap[clusterBean.Id]; ok {
exists, err := impl.checkIfNsExists(namespace, &clusterBean)
if err != nil {
impl.logger.Errorw("error in checking if namespace exists or not", "err", err, "clusterId", clusterBean.Id)
return err
}
if !exists {
return &util.ApiError{InternalMessage: models.NamespaceNotExistError{Err: fmt.Errorf("namespace %s does not exist", namespace)}.Error(), Code: strconv.Itoa(http.StatusNotFound), HttpStatusCode: http.StatusNotFound, UserMessage: fmt.Sprintf("Namespace %s does not exist.", namespace)}
}
}
}
return nil
}
21 changes: 21 additions & 0 deletions pkg/appStore/installedApp/service/AppStoreDeploymentService.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ func (impl *AppStoreDeploymentServiceImpl) InstallApp(installAppVersionRequest *
impl.logger.Errorw(" error", "err", err)
return nil, err
}

//checking if namespace exists or not
clusterIdToNsMap := map[int]string{
installAppVersionRequest.ClusterId: installAppVersionRequest.Namespace,
}
clusterId := make([]int, 0)
clusterId = append(clusterId, installAppVersionRequest.ClusterId)
err = impl.helmAppService.CheckIfNsExistsForClusterIds(clusterIdToNsMap, clusterId)
if err != nil {
return nil, err
}
installedAppDeploymentAction := adapter.NewInstalledAppDeploymentAction(installAppVersionRequest.DeploymentAppType)

if util.IsAcdApp(installAppVersionRequest.DeploymentAppType) || util.IsManifestDownload(installAppVersionRequest.DeploymentAppType) {
Expand Down Expand Up @@ -568,6 +579,16 @@ func (impl *AppStoreDeploymentServiceImpl) UpdateInstalledApp(ctx context.Contex
if err != nil {
return nil, err
}
//checking if ns exists or not
clusterIdToNsMap := map[int]string{
installedApp.Environment.ClusterId: installedApp.Environment.Namespace,
}
clusterId := make([]int, 0)
clusterId = append(clusterId, installedApp.Environment.ClusterId)
err = impl.helmAppService.CheckIfNsExistsForClusterIds(clusterIdToNsMap, clusterId)
if err != nil {
return nil, err
}
upgradeAppRequest.UpdateDeploymentAppType(installedApp.DeploymentAppType)

installedAppDeploymentAction := adapter.NewInstalledAppDeploymentAction(installedApp.DeploymentAppType)
Expand Down
10 changes: 10 additions & 0 deletions pkg/deployment/trigger/devtronApps/TriggerService.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ func (impl *TriggerServiceImpl) ManualCdTrigger(triggerContext bean.TriggerConte
var err error
_, span := otel.Tracer("orchestrator").Start(ctx, "pipelineRepository.FindById")
cdPipeline, err := impl.pipelineRepository.FindById(overrideRequest.PipelineId)
//checking if namespace exist or not
clusterIdToNsMap := map[int]string{
cdPipeline.Environment.ClusterId: cdPipeline.Environment.Namespace,
}
clusterId := make([]int, 0)
clusterId = append(clusterId, cdPipeline.Environment.ClusterId)
err = impl.helmAppService.CheckIfNsExistsForClusterIds(clusterIdToNsMap, clusterId)
if err != nil {
return 0, err
}
span.End()
if err != nil {
impl.logger.Errorw("manual trigger request with invalid pipelineId, ManualCdTrigger", "pipelineId", overrideRequest.PipelineId, "err", err)
Expand Down
27 changes: 26 additions & 1 deletion pkg/pipeline/DeploymentPipelineConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,19 +369,25 @@ func (impl *CdPipelineConfigServiceImpl) CreateCdPipelines(pipelineCreateRequest
impl.logger.Errorw("error in checking if gitOps is configured or not", "err", err)
return nil, err
}
envIds := make([]*int, 0)
for _, pipeline := range pipelineCreateRequest.Pipelines {
// skip creation of pipeline if envId is not set
if pipeline.EnvironmentId <= 0 {
continue
}
//making environment array for fetching the clusterIds
envIds = append(envIds, &pipeline.EnvironmentId)
overrideDeploymentType, err := impl.deploymentTypeOverrideService.ValidateAndOverrideDeploymentAppType(pipeline.DeploymentAppType, gitOpsConfigurationStatus.IsGitOpsConfigured, pipeline.EnvironmentId)
if err != nil {
impl.logger.Errorw("validation error in creating pipeline", "name", pipeline.Name, "err", err)
return nil, err
}
pipeline.DeploymentAppType = overrideDeploymentType
}

err = impl.checkIfNsExistsForEnvIds(envIds)
if err != nil {
return nil, err
}
isGitOpsRequiredForCD := impl.IsGitOpsRequiredForCD(pipelineCreateRequest)
app, err := impl.appRepo.FindById(pipelineCreateRequest.AppId)
if err != nil {
Expand Down Expand Up @@ -2063,3 +2069,22 @@ func (impl *CdPipelineConfigServiceImpl) BulkDeleteCdPipelines(impactedPipelines
return respDtos

}
func (impl *CdPipelineConfigServiceImpl) checkIfNsExistsForEnvIds(envIds []*int) error {
//fetching environments for the given environment Ids
environmentList, err := impl.environmentRepository.FindByIds(envIds)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return fmt.Errorf("error in fetching environment err:", err)
}
clusterIdToNsMap := make(map[int]string, 0)
clusterIds := make([]int, 0)
for _, environment := range environmentList {
clusterIds = append(clusterIds, environment.ClusterId)
clusterIdToNsMap[environment.ClusterId] = environment.Namespace
}
err = impl.helmAppService.CheckIfNsExistsForClusterIds(clusterIdToNsMap, clusterIds)
if err != nil {
return err
}
return nil
}

0 comments on commit bb7960d

Please sign in to comment.