Skip to content

App store chore refactoring v4 sync oss #4778

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 17 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func InitializeApp() (*App, error) {
team.TeamsWireSet,
AuthWireSet,
util4.NewK8sUtil,
wire.Bind(new(util4.K8sService), new(*util4.K8sServiceImpl)),
user.UserWireSet,
sso.SsoConfigWireSet,
cluster.ClusterWireSet,
Expand Down Expand Up @@ -963,6 +964,8 @@ func InitializeApp() (*App, error) {

imageDigestPolicy.NewImageDigestPolicyServiceImpl,
wire.Bind(new(imageDigestPolicy.ImageDigestPolicyService), new(*imageDigestPolicy.ImageDigestPolicyServiceImpl)),

appStoreRestHandler.AppStoreWireSet,
)
return &App{}, nil
}
14 changes: 9 additions & 5 deletions api/appStore/InstalledAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"time"

bean2 "github.com/devtron-labs/devtron/api/bean"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/client/cron"
Expand Down Expand Up @@ -98,7 +97,8 @@ type InstalledAppRestHandlerImpl struct {

func NewInstalledAppRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService user.UserService,
enforcer casbin.Enforcer, enforcerUtil rbac.EnforcerUtil, enforcerUtilHelm rbac.EnforcerUtilHelm,
installedAppService FullMode.InstalledAppDBExtendedService, installedAppResourceService resource.InstalledAppResourceService,
installedAppService FullMode.InstalledAppDBExtendedService,
installedAppResourceService resource.InstalledAppResourceService,
chartGroupService chartGroup.ChartGroupService, validator *validator.Validate, clusterService cluster.ClusterService,
acdServiceClient application.ServiceClient, appStoreDeploymentService service.AppStoreDeploymentService,
appStoreDeploymentDBService service.AppStoreDeploymentDBService,
Expand Down Expand Up @@ -268,7 +268,7 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri
return
}

appIdToAppMap := make(map[string]openapi.HelmApp)
appIdToAppMap := make(map[string]appStoreBean.HelmAppDetails)

//the value of this map is array of strings because the GetHelmObjectByAppNameAndEnvId method may return "//" for error cases
//so different apps may contain same object, to handle that we are using (map[string] []string)
Expand Down Expand Up @@ -324,7 +324,7 @@ func (handler InstalledAppRestHandlerImpl) GetAllInstalledApp(w http.ResponseWri
}
}

authorizedApps := make([]openapi.HelmApp, 0)
authorizedApps := make([]appStoreBean.HelmAppDetails, 0)
for appId, _ := range authorizedAppIdSet {
authorizedApp := appIdToAppMap[appId]
authorizedApps = append(authorizedApps, authorizedApp)
Expand Down Expand Up @@ -724,6 +724,10 @@ func (handler *InstalledAppRestHandlerImpl) FetchResourceTree(w http.ResponseWri
common.WriteJsonResp(w, err, "App not found in database", http.StatusBadRequest)
return
}
if installedApp.Environment.IsVirtualEnvironment {
common.WriteJsonResp(w, nil, nil, http.StatusOK)
return
}
token := r.Header.Get("token")
object, object2 := handler.enforcerUtil.GetHelmObjectByAppNameAndEnvId(installedApp.App.AppName, installedApp.EnvironmentId)
var ok bool
Expand Down Expand Up @@ -754,7 +758,7 @@ func (handler *InstalledAppRestHandlerImpl) FetchResourceTree(w http.ResponseWri
apiError, ok := err.(*util2.ApiError)
if ok && apiError != nil {
if apiError.Code == constants.AppDetailResourceTreeNotFound && installedApp.DeploymentAppDeleteRequest == true {
// TODO refactoring: should be performed in go routine
// TODO refactoring: should be performed through nats
err = handler.appStoreDeploymentService.MarkGitOpsInstalledAppsDeletedIfArgoAppIsDeleted(installedAppId, envId)
appDeleteErr, appDeleteErrOk := err.(*util2.ApiError)
if appDeleteErrOk && appDeleteErr != nil {
Expand Down
25 changes: 18 additions & 7 deletions api/appStore/deployment/AppStoreDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,24 @@ func (handler AppStoreDeploymentRestHandlerImpl) UpdateProjectHelmApp(w http.Res
handler.Logger.Errorw("service err, InstalledAppId", "err", err, "InstalledAppId", request.InstalledAppId)
common.WriteJsonResp(w, fmt.Errorf("Unable to fetch installed app details"), nil, http.StatusBadRequest)
}
rbacObjectForCurrentProject, rbacObjectForCurrentProject2 := handler.enforcerUtilHelm.GetHelmObjectByClusterIdNamespaceAndAppName(installedApp.ClusterId, installedApp.Namespace, installedApp.AppName)
ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForCurrentProject) || handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForCurrentProject2)
rbacObjectForRequestedProject := handler.enforcerUtilHelm.GetHelmObjectByTeamIdAndClusterId(request.TeamId, installedApp.ClusterId, installedApp.Namespace, installedApp.AppName)
ok = handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForRequestedProject)
if !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
if installedApp.IsVirtualEnvironment {
rbacObjectForCurrentProject, _ := handler.enforcerUtilHelm.GetAppRBACNameByInstalledAppId(request.InstalledAppId)
ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForCurrentProject)
rbacObjectForRequestedProject := handler.enforcerUtilHelm.GetAppRBACNameByInstalledAppIdAndTeamId(request.InstalledAppId, request.TeamId)
ok = handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForRequestedProject)
if !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
}
} else {
rbacObjectForCurrentProject, rbacObjectForCurrentProject2 := handler.enforcerUtilHelm.GetHelmObjectByClusterIdNamespaceAndAppName(installedApp.ClusterId, installedApp.Namespace, installedApp.AppName)
ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForCurrentProject) || handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForCurrentProject2)
rbacObjectForRequestedProject := handler.enforcerUtilHelm.GetHelmObjectByTeamIdAndClusterId(request.TeamId, installedApp.ClusterId, installedApp.Namespace, installedApp.AppName)
ok = handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionUpdate, rbacObjectForRequestedProject)
if !ok {
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
return
}
}
}
err = handler.appStoreDeploymentService.UpdateProjectHelmApp(&request)
Expand Down
1 change: 1 addition & 0 deletions api/appStore/values/AppStoreValuesRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ func (router AppStoreValuesRouterImpl) Init(configRouter *mux.Router) {

configRouter.Path("/chart/selected/metadata").
HandlerFunc(router.appStoreValuesRestHandler.GetSelectedChartMetadata).Methods("POST")

}
15 changes: 15 additions & 0 deletions api/appStore/wire_appStoreWireSet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package appStore

import (
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service"
"github.com/google/wire"
)

var AppStoreWireSet = wire.NewSet(

service.NewDeletePostProcessorImpl,
wire.Bind(new(service.DeletePostProcessor), new(*service.DeletePostProcessorImpl)),

service.NewAppAppStoreValidatorImpl,
wire.Bind(new(service.AppStoreValidator), new(*service.AppStoreValidatorImpl)),
)
1 change: 1 addition & 0 deletions api/bean/AppView.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type DeploymentDetailContainer struct {
DeploymentAppDeleteRequest bool `json:"deploymentAppDeleteRequest"`
Description string `json:"description" validate:"max=40"`
IsVirtualEnvironment bool `json:"isVirtualEnvironment"`
HelmPackageName string `json:"helmPackageName"`
HelmReleaseInstallStatus string `json:"-"`
}

Expand Down
1 change: 1 addition & 0 deletions api/helm-app/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ type InstalledAppInfo struct {
TeamId int `json:"teamId"`
TeamName string `json:"teamName"`
DeploymentType string `json:"deploymentType"`
HelmPackageName string `json:"helmPackageName"`
}
2 changes: 2 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
cloudProviderIdentifier "github.com/devtron-labs/common-lib/cloud-provider-identifier"
util4 "github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/devtron/api/apiToken"
"github.com/devtron-labs/devtron/api/appStore"
chartProvider "github.com/devtron-labs/devtron/api/appStore/chartProvider"
appStoreDeployment "github.com/devtron-labs/devtron/api/appStore/deployment"
appStoreDiscover "github.com/devtron-labs/devtron/api/appStore/discover"
Expand Down Expand Up @@ -215,6 +216,7 @@ func InitializeApp() (*App, error) {

// end: docker registry wire set injection
cron.NewCronLoggerImpl,
appStore.AppStoreWireSet,
)
return &App{}, nil
}
6 changes: 4 additions & 2 deletions cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions internal/util/ChartTemplateService.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ const (
PIPELINE_DEPLOYMENT_TYPE_ACD = "argo_cd"
PIPELINE_DEPLOYMENT_TYPE_HELM = "helm"
PIPELINE_DEPLOYMENT_TYPE_MANIFEST_DOWNLOAD = "manifest_download"
PIPELINE_DEPLOYMENT_TYPE_MANIFEST_PUSH = "manifest_push"
CHART_WORKING_DIR_PATH = "/tmp/charts/"
)

type ChartCreateRequest struct {
ChartMetaData *chart.Metadata
ChartPath string
ChartMetaData *chart.Metadata
ChartPath string
IncludePackageChart bool
}

type ChartCreateResponse struct {
Expand Down Expand Up @@ -434,6 +436,10 @@ func IsManifestDownload(deploymentAppType string) bool {
return deploymentAppType == PIPELINE_DEPLOYMENT_TYPE_MANIFEST_DOWNLOAD
}

func IsManifestPush(deploymentAppType string) bool {
return deploymentAppType == PIPELINE_DEPLOYMENT_TYPE_MANIFEST_PUSH
}

func IsOCIRegistryChartProvider(ociRegistry dockerRegistryRepository.DockerArtifactStore) bool {
if ociRegistry.OCIRegistryConfig == nil ||
len(ociRegistry.OCIRegistryConfig) != 1 ||
Expand Down
36 changes: 25 additions & 11 deletions pkg/appStore/adapter/Adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
appStoreDiscoverRepository "github.com/devtron-labs/devtron/pkg/appStore/discover/repository"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
"github.com/devtron-labs/devtron/pkg/bean"
"github.com/devtron-labs/devtron/pkg/cluster/adapter"
clutserBean "github.com/devtron-labs/devtron/pkg/cluster/repository/bean"
"time"
)

// NewInstallAppModel is used to generate new repository.InstalledApps model to be saved;
Expand Down Expand Up @@ -141,20 +143,30 @@ func GenerateInstallAppVersionDTO(installedApp *repository.InstalledApps, instal
// Note: It only generates a minimal DTO and doesn't include repository.InstalledAppVersions data
func GenerateInstallAppVersionMinDTO(installedApp *repository.InstalledApps) *appStoreBean.InstallAppVersionDTO {
return &appStoreBean.InstallAppVersionDTO{
EnvironmentId: installedApp.EnvironmentId,
InstalledAppId: installedApp.Id,
AppId: installedApp.AppId,
AppOfferingMode: installedApp.App.AppOfferingMode,
ClusterId: installedApp.Environment.ClusterId,
Namespace: installedApp.Environment.Namespace,
AppName: installedApp.App.AppName,
EnvironmentName: installedApp.Environment.Name,
TeamId: installedApp.App.TeamId,
TeamName: installedApp.App.Team.Name,
DeploymentAppType: installedApp.DeploymentAppType,
EnvironmentId: installedApp.EnvironmentId,
InstalledAppId: installedApp.Id,
AppId: installedApp.AppId,
AppOfferingMode: installedApp.App.AppOfferingMode,
ClusterId: installedApp.Environment.ClusterId,
Namespace: installedApp.Environment.Namespace,
AppName: installedApp.App.AppName,
EnvironmentName: installedApp.Environment.Name,
TeamId: installedApp.App.TeamId,
TeamName: installedApp.App.Team.Name,
DeploymentAppType: installedApp.DeploymentAppType,
IsVirtualEnvironment: installedApp.Environment.IsVirtualEnvironment,
}
}

func GetGeneratedHelmPackageName(appName, envName string, updatedOn time.Time) string {
timeStampTag := updatedOn.Format(bean.LayoutDDMMYY_HHMM12hr)
return fmt.Sprintf(
"%s-%s-%s (GMT)",
appName,
envName,
timeStampTag)
}

// NewInstalledAppVersionModel will generate a new repository.InstalledAppVersions for the given appStoreBean.InstallAppVersionDTO
func NewInstalledAppVersionModel(request *appStoreBean.InstallAppVersionDTO) *repository.InstalledAppVersions {
installedAppVersion := &repository.InstalledAppVersions{
Expand Down Expand Up @@ -191,6 +203,7 @@ func UpdateAdditionalEnvDetails(request *appStoreBean.InstallAppVersionDTO, envB
request.EnvironmentName = envBean.Environment
request.ClusterId = envBean.ClusterId
request.Namespace = envBean.Namespace
request.IsVirtualEnvironment = envBean.IsVirtualEnvironment
request.UpdateACDAppName()
}

Expand Down Expand Up @@ -239,6 +252,7 @@ func UpdateInstalledAppVersionsMetaData(request *appStoreBean.InstallAppVersionD
request.Id = installedAppVersion.Id
request.InstalledAppVersionId = installedAppVersion.Id
request.AppStoreApplicationVersionId = installedAppVersion.AppStoreApplicationVersionId
request.UpdatedOn = installedAppVersion.UpdatedOn
}

func getHelmReleaseStatusConfig(helmInstallConfigDTO appStoreBean.HelmReleaseStatusConfig) (string, error) {
Expand Down
58 changes: 57 additions & 1 deletion pkg/appStore/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ package appStoreBean
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/pkg/cluster/repository/bean"
apiBean "github.com/devtron-labs/devtron/api/bean/gitOps"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
"github.com/devtron-labs/devtron/pkg/cluster/repository/bean"
"time"
)

Expand Down Expand Up @@ -98,6 +99,9 @@ type InstallAppVersionDTO struct {
DeploymentAppType string `json:"deploymentAppType"` // TODO: instead of string, use enum
AcdPartialDelete bool `json:"acdPartialDelete"`
InstalledAppDeleteResponse *InstalledAppDeleteResponseDTO `json:"deleteResponse,omitempty"`
UpdatedOn time.Time `json:"updatedOn"`
IsVirtualEnvironment bool `json:"isVirtualEnvironment"`
HelmPackageName string `json:"helmPackageName"`
GitOpsRepoURL string `json:"gitRepoURL"`
IsCustomRepository bool `json:"-"`
IsNewGitOpsRepo bool `json:"-"`
Expand Down Expand Up @@ -395,6 +399,8 @@ type ChartComponent struct {
}

const (
DEFAULT_CLUSTER_ID = 1
DEFAULT_NAMESPACE = "default"
DEFAULT_ENVIRONMENT_OR_NAMESPACE_OR_PROJECT = "devtron"
CLUSTER_COMPONENT_DIR_PATH = "/cluster/component"
HELM_RELEASE_STATUS_FAILED = "Failed"
Expand All @@ -405,3 +411,53 @@ const (
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."
)

type EnvironmentDetails struct {
EnvironmentName *string `json:"environmentName,omitempty"`
// id in which app is deployed
EnvironmentId *int32 `json:"environmentId,omitempty"`
// namespace corresponding to the environemnt
Namespace *string `json:"namespace,omitempty"`
// if given environemnt is marked as production or not, nullable
IsPrduction *bool `json:"isPrduction,omitempty"`
// cluster corresponding to the environemt where application is deployed
ClusterName *string `json:"clusterName,omitempty"`
// clusterId corresponding to the environemt where application is deployed
ClusterId *int32 `json:"clusterId,omitempty"`

IsVirtualEnvironment *bool `json:"isVirtualEnvironment"`
}

type HelmAppDetails struct {
// time when this application was last deployed/updated
LastDeployedAt *time.Time `json:"lastDeployedAt,omitempty"`
// name of the helm application/helm release name
AppName *string `json:"appName,omitempty"`
// unique identifier for app
AppId *string `json:"appId,omitempty"`
// name of the chart
ChartName *string `json:"chartName,omitempty"`
// url/location of the chart icon
ChartAvatar *string `json:"chartAvatar,omitempty"`
// unique identifier for the project, APP with no project will have id `0`
ProjectId *int32 `json:"projectId,omitempty"`
// chart version
ChartVersion *string `json:"chartVersion,omitempty"`
EnvironmentDetail *EnvironmentDetails `json:"environmentDetail,omitempty"`
AppStatus *string `json:"appStatus,omitempty"`
}

type AppListDetail struct {
// clusters to which result corresponds
ClusterIds *[]int32 `json:"clusterIds,omitempty"`
// application type inside the array
ApplicationType *string `json:"applicationType,omitempty"`
// if data fetch for that cluster produced error
Errored *bool `json:"errored,omitempty"`
// error msg if client failed to fetch
ErrorMsg *string `json:"errorMsg,omitempty"`
// all helm app list, EA+ devtronapp
HelmApps *[]HelmAppDetails `json:"helmApps,omitempty"`
// all helm app list, EA+ devtronapp
DevtronApps *[]openapi.DevtronApp `json:"devtronApps,omitempty"`
}
Loading