Skip to content

feat: change deployment type from gitops to non-gitops and vice-versa for chart store apps #4666

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

Merged
merged 25 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
099ca48
extracting code from prev branch and pasting here with refactoring
prakash100198 Feb 13, 2024
0eedb7d
minor fix
prakash100198 Feb 13, 2024
bfc726a
bringing leftover changes
prakash100198 Feb 13, 2024
2867f6d
running wire
prakash100198 Feb 13, 2024
f45ea6a
removing unused dependencies
prakash100198 Feb 14, 2024
2cdf642
1st level code review changes incorporated
prakash100198 Feb 14, 2024
5f6bf8d
minor fixes
prakash100198 Feb 14, 2024
dd8a932
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 16, 2024
e0b3bc6
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 19, 2024
cf9059c
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 19, 2024
d825812
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 20, 2024
979231b
external-app wire fix
prakash100198 Feb 20, 2024
fcc86c5
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 22, 2024
a1cba8e
wire fix
prakash100198 Feb 22, 2024
eabd0ab
bug fixes
prakash100198 Feb 22, 2024
7ceb0ff
revert value
prakash100198 Feb 22, 2024
5020b58
bug fixes
prakash100198 Feb 22, 2024
9e1c152
refactor
prakash100198 Feb 22, 2024
eb4d624
minor fix
prakash100198 Feb 22, 2024
35ff177
logger added
prakash100198 Feb 23, 2024
55a9c02
ext app bug fix
prakash100198 Feb 23, 2024
1643944
comments added
prakash100198 Feb 23, 2024
a84d6f4
code refactor
prakash100198 Feb 23, 2024
8db1b22
Merge branch 'main' into chart-store-migrate
prakash100198 Feb 23, 2024
3fb5f56
logging some more info
prakash100198 Feb 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ func InitializeApp() (*App, error) {
wire.Bind(new(FullMode.InstalledAppDBExtendedService), new(*FullMode.InstalledAppDBExtendedServiceImpl)),
resource.NewInstalledAppResourceServiceImpl,
wire.Bind(new(resource.InstalledAppResourceService), new(*resource.InstalledAppResourceServiceImpl)),
FullMode.NewInstalledAppDeploymentTypeChangeServiceImpl,
wire.Bind(new(FullMode.InstalledAppDeploymentTypeChangeService), new(*FullMode.InstalledAppDeploymentTypeChangeServiceImpl)),

appStoreRestHandler.NewAppStoreRouterImpl,
wire.Bind(new(appStoreRestHandler.AppStoreRouter), new(*appStoreRestHandler.AppStoreRouterImpl)),
Expand Down
5 changes: 5 additions & 0 deletions api/appStore/AppStoreRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ func (router AppStoreRouterImpl) Init(configRouter *mux.Router) {
HandlerFunc(router.deployRestHandler.GetAllInstalledApp).Methods("GET")
configRouter.Path("/cluster-component/install/{clusterId}").
HandlerFunc(router.deployRestHandler.DefaultComponentInstallation).Methods("POST")

configRouter.Path("/installed-app/migrate").
HandlerFunc(router.deployRestHandler.MigrateDeploymentTypeForChartStore).Methods("POST")
configRouter.Path("/installed-app/trigger").
HandlerFunc(router.deployRestHandler.TriggerChartStoreAppAfterMigration).Methods("POST")
}
168 changes: 132 additions & 36 deletions api/appStore/InstalledAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
client "github.com/devtron-labs/devtron/api/helm-app/gRPC"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/FullMode"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/FullMode/resource"
"github.com/devtron-labs/devtron/pkg/bean"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -69,27 +70,30 @@ type InstalledAppRestHandler interface {
FetchResourceTree(w http.ResponseWriter, r *http.Request)
FetchResourceTreeForACDApp(w http.ResponseWriter, r *http.Request)
FetchNotesForArgoInstalledApp(w http.ResponseWriter, r *http.Request)
MigrateDeploymentTypeForChartStore(w http.ResponseWriter, r *http.Request)
TriggerChartStoreAppAfterMigration(w http.ResponseWriter, r *http.Request)
}

type InstalledAppRestHandlerImpl struct {
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
enforcerUtilHelm rbac.EnforcerUtilHelm
installedAppService FullMode.InstalledAppDBExtendedService
installedAppResourceService resource.InstalledAppResourceService
chartGroupService chartGroup.ChartGroupService
validator *validator.Validate
clusterService cluster.ClusterService
acdServiceClient application.ServiceClient
appStoreDeploymentService service.AppStoreDeploymentService
helmAppClient client.HelmAppClient
argoUserService argo.ArgoUserService
cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler
installedAppRepository repository.InstalledAppRepository
K8sApplicationService application2.K8sApplicationService
appCrudOperationService app2.AppCrudOperationService
Logger *zap.SugaredLogger
userAuthService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
enforcerUtilHelm rbac.EnforcerUtilHelm
installedAppService FullMode.InstalledAppDBExtendedService
installedAppResourceService resource.InstalledAppResourceService
chartGroupService chartGroup.ChartGroupService
validator *validator.Validate
clusterService cluster.ClusterService
acdServiceClient application.ServiceClient
appStoreDeploymentService service.AppStoreDeploymentService
helmAppClient client.HelmAppClient
argoUserService argo.ArgoUserService
cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler
installedAppRepository repository.InstalledAppRepository
K8sApplicationService application2.K8sApplicationService
appCrudOperationService app2.AppCrudOperationService
installedAppDeploymentTypeChangeService FullMode.InstalledAppDeploymentTypeChangeService
}

func NewInstalledAppRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService user.UserService,
Expand All @@ -100,25 +104,27 @@ func NewInstalledAppRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService u
helmAppClient client.HelmAppClient, argoUserService argo.ArgoUserService,
cdApplicationStatusUpdateHandler cron.CdApplicationStatusUpdateHandler,
installedAppRepository repository.InstalledAppRepository,
appCrudOperationService app2.AppCrudOperationService) *InstalledAppRestHandlerImpl {
appCrudOperationService app2.AppCrudOperationService,
installedAppDeploymentTypeChangeService FullMode.InstalledAppDeploymentTypeChangeService) *InstalledAppRestHandlerImpl {
return &InstalledAppRestHandlerImpl{
Logger: Logger,
userAuthService: userAuthService,
enforcer: enforcer,
enforcerUtil: enforcerUtil,
enforcerUtilHelm: enforcerUtilHelm,
installedAppService: installedAppService,
installedAppResourceService: installedAppResourceService,
chartGroupService: chartGroupService,
validator: validator,
clusterService: clusterService,
acdServiceClient: acdServiceClient,
appStoreDeploymentService: appStoreDeploymentService,
helmAppClient: helmAppClient,
argoUserService: argoUserService,
cdApplicationStatusUpdateHandler: cdApplicationStatusUpdateHandler,
installedAppRepository: installedAppRepository,
appCrudOperationService: appCrudOperationService,
Logger: Logger,
userAuthService: userAuthService,
enforcer: enforcer,
enforcerUtil: enforcerUtil,
enforcerUtilHelm: enforcerUtilHelm,
installedAppService: installedAppService,
installedAppResourceService: installedAppResourceService,
chartGroupService: chartGroupService,
validator: validator,
clusterService: clusterService,
acdServiceClient: acdServiceClient,
appStoreDeploymentService: appStoreDeploymentService,
helmAppClient: helmAppClient,
argoUserService: argoUserService,
cdApplicationStatusUpdateHandler: cdApplicationStatusUpdateHandler,
installedAppRepository: installedAppRepository,
appCrudOperationService: appCrudOperationService,
installedAppDeploymentTypeChangeService: installedAppDeploymentTypeChangeService,
}
}
func (handler *InstalledAppRestHandlerImpl) FetchAppOverview(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -843,3 +849,93 @@ func (handler *InstalledAppRestHandlerImpl) fetchResourceTreeWithHibernateForACD
cn, _ := w.(http.CloseNotifier)
handler.installedAppResourceService.FetchResourceTreeWithHibernateForACD(ctx, cn, appDetail)
}

func (handler *InstalledAppRestHandlerImpl) MigrateDeploymentTypeForChartStore(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}

decoder := json.NewDecoder(r.Body)
var migrateAndTriggerReq *bean.DeploymentAppTypeChangeRequest
err = decoder.Decode(&migrateAndTriggerReq)
if err != nil {
handler.Logger.Errorw("request err, MigrateDeploymentTypeForChartStore", "payload", migrateAndTriggerReq, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
migrateAndTriggerReq.UserId = userId

err = handler.validator.Struct(migrateAndTriggerReq)
if err != nil {
handler.Logger.Errorw("validation err, MigrateDeploymentTypeForChartStore", "payload", migrateAndTriggerReq, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

token := r.Header.Get("token")

if ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionDelete, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}

resp, err := handler.installedAppDeploymentTypeChangeService.MigrateDeploymentType(r.Context(), migrateAndTriggerReq)
if err != nil {
handler.Logger.Errorw(err.Error(),
"payload", migrateAndTriggerReq,
"err", err)

common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, nil, resp, http.StatusOK)
return
}

func (handler *InstalledAppRestHandlerImpl) TriggerChartStoreAppAfterMigration(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}

decoder := json.NewDecoder(r.Body)
var deploymentAppTriggerRequest *bean.DeploymentAppTypeChangeRequest
err = decoder.Decode(&deploymentAppTriggerRequest)
if err != nil {
handler.Logger.Errorw("request err, TriggerChartStoreAppAfterMigration", "payload", deploymentAppTriggerRequest, "err", err)

common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
deploymentAppTriggerRequest.UserId = userId

err = handler.validator.Struct(deploymentAppTriggerRequest)
if err != nil {
handler.Logger.Errorw("validation err, TriggerChartStoreAppAfterMigration", "payload", deploymentAppTriggerRequest, "err", err)

common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

token := r.Header.Get("token")

if ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionCreate, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}

resp, err := handler.installedAppDeploymentTypeChangeService.TriggerAfterMigration(r.Context(), deploymentAppTriggerRequest)
if err != nil {
handler.Logger.Errorw(err.Error(),
"payload", deploymentAppTriggerRequest,
"err", err)

common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, nil, resp, http.StatusOK)
return
}
12 changes: 8 additions & 4 deletions client/argocdServer/k8sClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"go.uber.org/zap"
"io/ioutil"
Expand Down Expand Up @@ -39,13 +40,16 @@ type ArgoK8sClient interface {
GetArgoApplication(namespace string, appName string, cluster *repository.Cluster) (map[string]interface{}, error)
}
type ArgoK8sClientImpl struct {
logger *zap.SugaredLogger
logger *zap.SugaredLogger
k8sUtil *k8s.K8sServiceImpl
}

func NewArgoK8sClientImpl(logger *zap.SugaredLogger,
k8sUtil *k8s.K8sServiceImpl,
) *ArgoK8sClientImpl {
return &ArgoK8sClientImpl{
logger: logger,
logger: logger,
k8sUtil: k8sUtil,
}
}

Expand Down Expand Up @@ -74,9 +78,9 @@ func (impl ArgoK8sClientImpl) CreateAcdApp(appRequest *AppTemplate, cluster *rep
return "", err
}

config, err := rest.InClusterConfig()
config, err := impl.k8sUtil.GetK8sInClusterRestConfig()
if err != nil {
impl.logger.Errorw("error in config", "err", err)
impl.logger.Errorw("error in getting in cluster rest config", "err", err)
return "", err
}
config.GroupVersion = &schema.GroupVersion{Group: "argoproj.io", Version: "v1alpha1"}
Expand Down
9 changes: 9 additions & 0 deletions internal/sql/repository/app/AppRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type AppRepository interface {
FindAppAndProjectByIdsIn(ids []int) ([]*App, error)
FetchAppIdsByDisplayNamesForJobs(names []string) (map[int]string, []int, error)
GetActiveCiCdAppsCount() (int, error)

UpdateAppOfferingModeForAppIds(successAppIds []*int, appOfferingMode string, userId int32) error
}

const DevtronApp = "DevtronApp"
Expand Down Expand Up @@ -472,3 +474,10 @@ func (repo AppRepositoryImpl) GetActiveCiCdAppsCount() (int, error) {
Where("app_type=?", helper.CustomApp).
Count()
}

func (repo AppRepositoryImpl) UpdateAppOfferingModeForAppIds(successAppIds []*int, appOfferingMode string, userId int32) error {
query := "update app set app_offering_mode = ?,updated_by = ?, updated_on = ? where id in (?);"
var app *App
_, err := repo.dbConnection.Query(app, query, appOfferingMode, userId, time.Now(), pg.In(successAppIds))
return err
}
12 changes: 3 additions & 9 deletions internal/sql/repository/pipelineConfig/PipelineRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"github.com/devtron-labs/devtron/internal/sql/repository/app"
"github.com/devtron-labs/devtron/internal/sql/repository/appWorkflow"
"github.com/devtron-labs/devtron/internal/util"
util2 "github.com/devtron-labs/devtron/pkg/appStore/util"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"go.uber.org/zap"
"k8s.io/utils/pointer"
"strconv"
"time"
)

Expand Down Expand Up @@ -332,15 +332,9 @@ func (impl PipelineRepositoryImpl) FindActiveByEnvIdAndDeploymentType(environmen
deploymentAppType string, exclusionList []int, includeApps []int) ([]*Pipeline, error) {

// NOTE: PG query throws error with slice of integer
exclusionListString := []string{}
for _, appId := range exclusionList {
exclusionListString = append(exclusionListString, strconv.Itoa(appId))
}
exclusionListString := util2.ConvertIntArrayToStringArray(exclusionList)

inclusionListString := []string{}
for _, appId := range includeApps {
inclusionListString = append(inclusionListString, strconv.Itoa(appId))
}
inclusionListString := util2.ConvertIntArrayToStringArray(includeApps)

var pipelines []*Pipeline

Expand Down
4 changes: 4 additions & 0 deletions pkg/appStore/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,8 @@ const (
HELM_RELEASE_STATUS_FAILED = "Failed"
HELM_RELEASE_STATUS_PROGRESSING = "Progressing"
HELM_RELEASE_STATUS_UNKNOWN = "Unknown"
FAILED_TO_REGISTER_IN_ACD_ERROR = "failed to register app on ACD with error: "
FAILED_TO_DELETE_APP_PREFIX_ERROR = "error deleting app with error: "
COULD_NOT_FETCH_APP_NAME_AND_ENV_NAME_ERR = "could not fetch app name or environment name"
APP_NOT_DELETED_YET_ERROR = "App Not Yet Deleted."
)
5 changes: 3 additions & 2 deletions pkg/appStore/chartGroup/ChartGroupService.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type ChartGroupService interface {

DeployBulk(chartGroupInstallRequest *ChartGroupInstallRequest) (*ChartGroupInstallAppRes, error)
DeployDefaultChartOnCluster(bean *cluster2.ClusterBean, userId int32) (bool, error)
TriggerDeploymentEvent(installAppVersions []*appStoreBean.InstallAppVersionDTO)
}

type ChartGroupList struct {
Expand Down Expand Up @@ -605,7 +606,7 @@ func (impl *ChartGroupServiceImpl) DeployBulk(chartGroupInstallRequest *ChartGro
return nil, err
}
//nats event
impl.triggerDeploymentEvent(installAppVersions)
impl.TriggerDeploymentEvent(installAppVersions)
// TODO refactoring: why empty obj ??
return &ChartGroupInstallAppRes{}, nil
}
Expand Down Expand Up @@ -670,7 +671,7 @@ func createChartGroupEntryObject(installAppVersionDTO *appStoreBean.InstallAppVe
}
}

func (impl *ChartGroupServiceImpl) triggerDeploymentEvent(installAppVersions []*appStoreBean.InstallAppVersionDTO) {
func (impl *ChartGroupServiceImpl) TriggerDeploymentEvent(installAppVersions []*appStoreBean.InstallAppVersionDTO) {
for _, versions := range installAppVersions {
var installedAppDeploymentStatus appStoreBean.AppstoreDeploymentStatus
payload := &appStoreBean.DeployPayload{InstalledAppVersionId: versions.InstalledAppVersionId, InstalledAppVersionHistoryId: versions.InstalledAppVersionHistoryId}
Expand Down
Loading