From 3e7d545bb0df5e9f5862a4fc1a86cba729e6ae7a Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Tue, 29 Nov 2022 13:52:19 +0530 Subject: [PATCH 1/7] setting reload while create new acd token --- util/argo/ArgoUserService.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/util/argo/ArgoUserService.go b/util/argo/ArgoUserService.go index 8abeada797..614c00abcc 100644 --- a/util/argo/ArgoUserService.go +++ b/util/argo/ArgoUserService.go @@ -51,13 +51,14 @@ type ArgoUserServiceImpl struct { devtronSecretConfig *util2.DevtronSecretConfig runTimeConfig *client.RuntimeConfig gitOpsRepository repository.GitOpsConfigRepository + settingsManager *settings.SettingsManager } func NewArgoUserServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.ClusterService, acdSettings *settings.ArgoCDSettings, devtronSecretConfig *util2.DevtronSecretConfig, - runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository) (*ArgoUserServiceImpl, error) { + runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository, settingsManager *settings.SettingsManager) (*ArgoUserServiceImpl, error) { argoUserServiceImpl := &ArgoUserServiceImpl{ logger: Logger, clusterService: clusterService, @@ -65,6 +66,7 @@ func NewArgoUserServiceImpl(Logger *zap.SugaredLogger, devtronSecretConfig: devtronSecretConfig, runTimeConfig: runTimeConfig, gitOpsRepository: gitOpsRepository, + settingsManager: settingsManager, } if !runTimeConfig.LocalDevMode { go argoUserServiceImpl.ValidateGitOpsAndGetOrUpdateArgoCdUserDetail() @@ -143,6 +145,13 @@ func (impl *ArgoUserServiceImpl) createNewArgoCdUserForDevtron(k8sClient *v1.Cor } func (impl *ArgoUserServiceImpl) createNewArgoCdTokenForDevtron(username, password string, tokenNo int, k8sClient *v1.CoreV1Client) (string, error) { + + //load argo-cd credential from configmap and secret + _, err := impl.settingsManager.GetSettings() + if err != nil { + impl.logger.Errorw("error in getting settings", "err", err) + return "", err + } //create new user at argo cd side token, err := impl.createTokenForArgoCdUser(username, password) if err != nil { @@ -307,6 +316,7 @@ func (impl *ArgoUserServiceImpl) createTokenForArgoCdUser(username, password str } ctx := context.Background() ctx = context.WithValue(ctx, "token", token) + acdConnection := argocdServer.GetConnection(token, impl.acdSettings) accountServiceClient := account.NewAccountServiceClient(acdConnection) acdToken, err := accountServiceClient.CreateToken(ctx, &account.CreateTokenRequest{ From 0e698648a9770b6ecde6b7055dfc826277519c57 Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Wed, 30 Nov 2022 08:07:14 +0530 Subject: [PATCH 2/7] acd setting manager policies load only when acd installed --- App.go | 2 - Wire.go | 4 +- client/argocdServer/Connection.go | 44 ++++++++++++++--- client/argocdServer/Version.go | 13 +++-- .../argocdServer/application/Application.go | 48 +++++++++---------- .../application/Application_test.go | 6 --- client/argocdServer/cluster/Cluster.go | 30 +++++------- client/argocdServer/repository/Repository.go | 30 +++++------- client/argocdServer/session/Session.go | 5 +- cmd/external-app/wire_gen.go | 5 +- util/argo/ArgoUserService.go | 25 +++------- util/session/Manager.go | 11 +---- wire_gen.go | 19 ++++---- 13 files changed, 118 insertions(+), 124 deletions(-) diff --git a/App.go b/App.go index 9fe2ce99e6..cee7c3241c 100644 --- a/App.go +++ b/App.go @@ -31,7 +31,6 @@ import ( authMiddleware "github.com/devtron-labs/authenticator/middleware" "github.com/devtron-labs/devtron/api/router" "github.com/devtron-labs/devtron/api/sse" - "github.com/devtron-labs/devtron/client/argocdServer" "github.com/devtron-labs/devtron/client/pubsub" "github.com/devtron-labs/devtron/internal/middleware" "github.com/devtron-labs/devtron/pkg/user" @@ -57,7 +56,6 @@ type App struct { func NewApp(router *router.MuxRouter, Logger *zap.SugaredLogger, sse *sse.SSE, - versionService argocdServer.VersionService, enforcer *casbin.SyncedEnforcer, db *pg.DB, pubsubClient *pubsub.PubSubClient, diff --git a/Wire.go b/Wire.go index 95491a7f98..3691f18bb1 100644 --- a/Wire.go +++ b/Wire.go @@ -149,7 +149,7 @@ func InitializeApp() (*App, error) { wire.Value(chart.DefaultChart("reference-app-rolling")), wire.Value(util.ChartWorkingDir("/tmp/charts/")), session.SettingsManager, - session.CDSettingsManager, + //session.CDSettingsManager, //auth.GetConfig, argocdServer.GetConfig, @@ -738,6 +738,8 @@ func InitializeApp() (*App, error) { wire.Bind(new(pipeline.PipelineStageService), new(*pipeline.PipelineStageServiceImpl)), //plugin ends + argocdServer.NewArgoCdConnectionImpl, + wire.Bind(new(argocdServer.ArgoCdConnection), new(*argocdServer.ArgoCdConnectionImpl)), argo.NewArgoUserServiceImpl, wire.Bind(new(argo.ArgoUserService), new(*argo.ArgoUserServiceImpl)), util2.GetDevtronSecretName, diff --git a/client/argocdServer/Connection.go b/client/argocdServer/Connection.go index 542601cac2..ef861ac4c7 100644 --- a/client/argocdServer/Connection.go +++ b/client/argocdServer/Connection.go @@ -19,6 +19,9 @@ package argocdServer import ( "fmt" + moduleRepo "github.com/devtron-labs/devtron/pkg/module/repo" + "github.com/go-pg/pg" + "go.uber.org/zap" "log" "github.com/argoproj/argo-cd/v2/util/settings" @@ -27,13 +30,46 @@ import ( ) func init() { - grpc_prometheus.EnableClientHandlingTimeHistogram() } -func GetConnection(token string, settings *settings.ArgoCDSettings) *grpc.ClientConn { +type ArgoCdConnection interface { + GetConnection(token string) *grpc.ClientConn +} +type ArgoCdConnectionImpl struct { + logger *zap.SugaredLogger + settingsManager *settings.SettingsManager + moduleRepository moduleRepo.ModuleRepository +} + +func NewArgoCdConnectionImpl(Logger *zap.SugaredLogger, settingsManager *settings.SettingsManager, + moduleRepository moduleRepo.ModuleRepository) (*ArgoCdConnectionImpl, error) { + argoUserServiceImpl := &ArgoCdConnectionImpl{ + logger: Logger, + settingsManager: settingsManager, + moduleRepository: moduleRepository, + } + return argoUserServiceImpl, nil +} + +func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { conf, err := GetConfig() if err != nil { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + module, err := impl.moduleRepository.FindOne("acd") + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + if module == nil || module.Status != "installed" { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + settings, err := impl.settingsManager.GetSettings() + if err != nil { + impl.logger.Errorw("error on get acd connection", "err", err) log.Fatal(err) } var option []grpc.DialOption @@ -42,10 +78,6 @@ func GetConnection(token string, settings *settings.ArgoCDSettings) *grpc.Client option = append(option, grpc.WithPerRPCCredentials(TokenAuth{token: token})) } option = append(option, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor)) - - //if conf.Environment=="DEV"{ - // option=append(option,grpc.WithInsecure()) - //} conn, err := grpc.Dial(fmt.Sprintf("%s:%s", conf.Host, conf.Port), option...) if err != nil { log.Fatal(err) diff --git a/client/argocdServer/Version.go b/client/argocdServer/Version.go index 5f79ba60d2..0b83a090df 100644 --- a/client/argocdServer/Version.go +++ b/client/argocdServer/Version.go @@ -20,7 +20,6 @@ package argocdServer import ( "context" "github.com/argoproj/argo-cd/v2/pkg/apiclient/version" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/golang/protobuf/ptypes/empty" "go.uber.org/zap" ) @@ -31,16 +30,16 @@ type VersionService interface { } type VersionServiceImpl struct { - settings *settings.ArgoCDSettings - logger *zap.SugaredLogger + logger *zap.SugaredLogger + argoCdConnection ArgoCdConnection } -func NewVersionServiceImpl(settings *settings.ArgoCDSettings, logger *zap.SugaredLogger) *VersionServiceImpl { - return &VersionServiceImpl{settings: settings, logger: logger} +func NewVersionServiceImpl(logger *zap.SugaredLogger, argoCdConnection ArgoCdConnection) *VersionServiceImpl { + return &VersionServiceImpl{logger: logger, argoCdConnection: argoCdConnection} } func (service VersionServiceImpl) CheckVersion() (err error) { - conn := GetConnection("", service.settings) + conn := service.argoCdConnection.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) if err != nil { return err @@ -50,7 +49,7 @@ func (service VersionServiceImpl) CheckVersion() (err error) { } func (service VersionServiceImpl) GetVersion() (apiVersion string, err error) { - conn := GetConnection("", service.settings) + conn := service.argoCdConnection.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) if err != nil { return "", err diff --git a/client/argocdServer/application/Application.go b/client/argocdServer/application/Application.go index cf865a7606..a0b52e3523 100644 --- a/client/argocdServer/application/Application.go +++ b/client/argocdServer/application/Application.go @@ -28,7 +28,6 @@ import ( "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/argoproj/argo-cd/v2/reposerver/apiclient" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/devtron/client/argocdServer" "github.com/devtron-labs/devtron/util" "go.uber.org/zap" @@ -114,17 +113,16 @@ type Manifests struct { } type ServiceClientImpl struct { - settings *settings.ArgoCDSettings - logger *zap.SugaredLogger + logger *zap.SugaredLogger + acdConnection argocdServer.ArgoCdConnection } func NewApplicationClientImpl( - settings *settings.ArgoCDSettings, - logger *zap.SugaredLogger, + logger *zap.SugaredLogger, acdConnection argocdServer.ArgoCdConnection, ) *ServiceClientImpl { return &ServiceClientImpl{ - settings: settings, - logger: logger, + logger: logger, + acdConnection: acdConnection, } } @@ -135,7 +133,7 @@ func (c ServiceClientImpl) ManagedResources(ctxt context.Context, query *applica if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.ManagedResources(ctx, query) @@ -149,7 +147,7 @@ func (c ServiceClientImpl) Rollback(ctxt context.Context, query *application.App if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Rollback(ctx, query) @@ -163,7 +161,7 @@ func (c ServiceClientImpl) Patch(ctxt context.Context, query *application.Applic if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Patch(ctx, query) @@ -177,7 +175,7 @@ func (c ServiceClientImpl) GetManifests(ctxt context.Context, query *application if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.GetManifests(ctx, query) @@ -191,7 +189,7 @@ func (c ServiceClientImpl) Get(ctxt context.Context, query *application.Applicat if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Get(ctx, query) @@ -205,7 +203,7 @@ func (c ServiceClientImpl) Update(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Update(ctx, query) @@ -232,7 +230,7 @@ func (c ServiceClientImpl) Sync(ctxt context.Context, query *application.Applica if !ok { return nil, NewErrUnauthorized("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Sync(ctx, query) @@ -246,7 +244,7 @@ func (c ServiceClientImpl) TerminateOperation(ctxt context.Context, query *appli if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.TerminateOperation(ctx, query) @@ -260,7 +258,7 @@ func (c ServiceClientImpl) PatchResource(ctxt context.Context, query *applicatio if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.PatchResource(ctx, query) @@ -274,7 +272,7 @@ func (c ServiceClientImpl) DeleteResource(ctxt context.Context, query *applicati if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.DeleteResource(ctx, query) @@ -288,7 +286,7 @@ func (c ServiceClientImpl) List(ctxt context.Context, query *application.Applica if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.List(ctx, query) @@ -297,7 +295,7 @@ func (c ServiceClientImpl) List(ctxt context.Context, query *application.Applica func (c ServiceClientImpl) PodLogs(ctx context.Context, query *application.ApplicationPodLogsQuery) (application.ApplicationService_PodLogsClient, *grpc.ClientConn, error) { token, _ := ctx.Value("token").(string) - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) //defer conn.Close() asc := application.NewApplicationServiceClient(conn) logs, err := asc.PodLogs(ctx, query) @@ -306,7 +304,7 @@ func (c ServiceClientImpl) PodLogs(ctx context.Context, query *application.Appli func (c ServiceClientImpl) Watch(ctx context.Context, query *application.ApplicationQuery) (application.ApplicationService_WatchClient, *grpc.ClientConn, error) { token, _ := ctx.Value("token").(string) - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) //defer conn.Close() asc := application.NewApplicationServiceClient(conn) logs, err := asc.Watch(ctx, query) @@ -320,7 +318,7 @@ func (c ServiceClientImpl) GetResource(ctxt context.Context, query *application. if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) return asc.GetResource(ctx, query) @@ -333,7 +331,7 @@ func (c ServiceClientImpl) Delete(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) return asc.Delete(ctx, query) @@ -346,7 +344,7 @@ func (c ServiceClientImpl) ListResourceEvents(ctxt context.Context, query *appli if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.ListResourceEvents(ctx, query) @@ -360,7 +358,7 @@ func (c ServiceClientImpl) Create(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Create(ctx, query) @@ -374,7 +372,7 @@ func (c ServiceClientImpl) ResourceTree(ctxt context.Context, query *application if !ok { return nil, errors.New("unauthorized") } - conn := argocdServer.GetConnection(token, c.settings) + conn := c.acdConnection.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) c.logger.Debugw("GRPC_GET_RESOURCETREE", "req", query) diff --git a/client/argocdServer/application/Application_test.go b/client/argocdServer/application/Application_test.go index 16b8b2c3d1..5cab0e0001 100644 --- a/client/argocdServer/application/Application_test.go +++ b/client/argocdServer/application/Application_test.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - "github.com/argoproj/argo-cd/v2/util/settings" "testing" "go.uber.org/zap" @@ -29,7 +28,6 @@ import ( func TestServiceClientImpl_getRolloutStatus(t *testing.T) { type fields struct { - asc *settings.ArgoCDSettings logger *zap.SugaredLogger } type args struct { @@ -53,7 +51,6 @@ func TestServiceClientImpl_getRolloutStatus(t *testing.T) { { name: "test1", fields: fields{ - asc: nil, logger: logger.Sugar(), }, args: args{ @@ -68,7 +65,6 @@ func TestServiceClientImpl_getRolloutStatus(t *testing.T) { { name: "test2", fields: fields{ - asc: nil, logger: logger.Sugar(), }, args: args{ @@ -83,7 +79,6 @@ func TestServiceClientImpl_getRolloutStatus(t *testing.T) { { name: "test3", fields: fields{ - asc: nil, logger: logger.Sugar(), }, args: args{ @@ -98,7 +93,6 @@ func TestServiceClientImpl_getRolloutStatus(t *testing.T) { { name: "test4", fields: fields{ - asc: nil, logger: logger.Sugar(), }, args: args{ diff --git a/client/argocdServer/cluster/Cluster.go b/client/argocdServer/cluster/Cluster.go index bbe41dc4c3..1a0b00a096 100644 --- a/client/argocdServer/cluster/Cluster.go +++ b/client/argocdServer/cluster/Cluster.go @@ -22,7 +22,6 @@ import ( "errors" "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/devtron/client/argocdServer" "go.uber.org/zap" "time" @@ -44,26 +43,23 @@ type ServiceClient interface { } type ServiceClientImpl struct { - settings *settings.ArgoCDSettings - logger *zap.SugaredLogger + logger *zap.SugaredLogger + argoCdConnection argocdServer.ArgoCdConnection } -func NewServiceClientImpl( - settings *settings.ArgoCDSettings, - logger *zap.SugaredLogger, -) *ServiceClientImpl { +func NewServiceClientImpl(logger *zap.SugaredLogger, argoCdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { return &ServiceClientImpl{ - settings: settings, - logger: logger, + logger: logger, + argoCdConnection: argoCdConnection, } } -func getService(ctx context.Context, settings *settings.ArgoCDSettings) (cluster.ClusterServiceClient, error) { +func (c ServiceClientImpl) getService(ctx context.Context) (cluster.ClusterServiceClient, error) { token, ok := ctx.Value("token").(string) if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, settings) + conn := c.argoCdConnection.GetConnection(token) //defer conn.Close() return cluster.NewClusterServiceClient(conn), nil } @@ -71,7 +67,7 @@ func getService(ctx context.Context, settings *settings.ArgoCDSettings) (cluster func (c ServiceClientImpl) List(ctx context.Context, query *cluster.ClusterQuery) (*v1alpha1.ClusterList, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } @@ -81,7 +77,7 @@ func (c ServiceClientImpl) List(ctx context.Context, query *cluster.ClusterQuery func (c ServiceClientImpl) Create(ctx context.Context, query *cluster.ClusterCreateRequest) (*v1alpha1.Cluster, error) { ctx, cancel := context.WithTimeout(ctx, 60*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } @@ -91,7 +87,7 @@ func (c ServiceClientImpl) Create(ctx context.Context, query *cluster.ClusterCre func (c ServiceClientImpl) CreateFromKubeConfig(ctx context.Context, query *cluster.ClusterCreateRequest) (*v1alpha1.Cluster, error) { ctx, cancel := context.WithTimeout(ctx, 60*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } @@ -101,7 +97,7 @@ func (c ServiceClientImpl) CreateFromKubeConfig(ctx context.Context, query *clus func (c ServiceClientImpl) Get(ctx context.Context, query *cluster.ClusterQuery) (*v1alpha1.Cluster, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } @@ -111,7 +107,7 @@ func (c ServiceClientImpl) Get(ctx context.Context, query *cluster.ClusterQuery) func (c ServiceClientImpl) Update(ctx context.Context, query *cluster.ClusterUpdateRequest) (*v1alpha1.Cluster, error) { ctx, cancel := context.WithTimeout(ctx, 60*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } @@ -121,7 +117,7 @@ func (c ServiceClientImpl) Update(ctx context.Context, query *cluster.ClusterUpd func (c ServiceClientImpl) Delete(ctx context.Context, query *cluster.ClusterQuery) (*cluster.ClusterResponse, error) { ctx, cancel := context.WithTimeout(ctx, 60*time.Second) defer cancel() - client, err := getService(ctx, c.settings) + client, err := c.getService(ctx) if err != nil { return nil, err } diff --git a/client/argocdServer/repository/Repository.go b/client/argocdServer/repository/Repository.go index 27d72500a1..d5d9eaf971 100644 --- a/client/argocdServer/repository/Repository.go +++ b/client/argocdServer/repository/Repository.go @@ -23,7 +23,6 @@ import ( repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository" "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" "github.com/argoproj/argo-cd/v2/reposerver/apiclient" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/devtron/client/argocdServer" "github.com/devtron-labs/devtron/client/argocdServer/application" "go.uber.org/zap" @@ -45,26 +44,23 @@ type ServiceClient interface { } type ServiceClientImpl struct { - settings *settings.ArgoCDSettings - logger *zap.SugaredLogger + logger *zap.SugaredLogger + acdConnection argocdServer.ArgoCdConnection } -func NewServiceClientImpl( - settings *settings.ArgoCDSettings, - logger *zap.SugaredLogger, -) *ServiceClientImpl { +func NewServiceClientImpl(logger *zap.SugaredLogger, acdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { return &ServiceClientImpl{ - settings: settings, - logger: logger, + logger: logger, + acdConnection: acdConnection, } } -func getService(ctx context.Context, settings *settings.ArgoCDSettings) (repository2.RepositoryServiceClient, error) { +func (r ServiceClientImpl) getService(ctx context.Context) (repository2.RepositoryServiceClient, error) { token, ok := ctx.Value("token").(string) if !ok { return nil, errors.New("Unauthorized") } - conn := argocdServer.GetConnection(token, settings) + conn := r.acdConnection.GetConnection(token) //defer conn.Close() return repository2.NewRepositoryServiceClient(conn), nil } @@ -72,7 +68,7 @@ func getService(ctx context.Context, settings *settings.ArgoCDSettings) (reposit func (r ServiceClientImpl) List(ctx context.Context, query *repository2.RepoQuery) (*v1alpha1.RepositoryList, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutFast) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } @@ -82,7 +78,7 @@ func (r ServiceClientImpl) List(ctx context.Context, query *repository2.RepoQuer func (r ServiceClientImpl) ListApps(ctx context.Context, query *repository2.RepoAppsQuery) (*repository2.RepoAppsResponse, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutFast) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } @@ -92,7 +88,7 @@ func (r ServiceClientImpl) ListApps(ctx context.Context, query *repository2.Repo func (r ServiceClientImpl) GetAppDetails(ctx context.Context, query *repository2.RepoAppDetailsQuery) (*apiclient.RepoAppDetailsResponse, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutFast) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } @@ -102,7 +98,7 @@ func (r ServiceClientImpl) GetAppDetails(ctx context.Context, query *repository2 func (r ServiceClientImpl) Create(ctx context.Context, query *repository2.RepoCreateRequest) (*v1alpha1.Repository, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutSlow) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } @@ -112,7 +108,7 @@ func (r ServiceClientImpl) Create(ctx context.Context, query *repository2.RepoCr func (r ServiceClientImpl) Update(ctx context.Context, query *repository2.RepoUpdateRequest) (*v1alpha1.Repository, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutSlow) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } @@ -122,7 +118,7 @@ func (r ServiceClientImpl) Update(ctx context.Context, query *repository2.RepoUp func (r ServiceClientImpl) Delete(ctx context.Context, query *repository2.RepoQuery) (*repository2.RepoResponse, error) { ctx, cancel := context.WithTimeout(ctx, application.TimeoutSlow) defer cancel() - client, err := getService(ctx, r.settings) + client, err := r.getService(ctx) if err != nil { return nil, err } diff --git a/client/argocdServer/session/Session.go b/client/argocdServer/session/Session.go index 660ffad2bf..00bd9eefd3 100644 --- a/client/argocdServer/session/Session.go +++ b/client/argocdServer/session/Session.go @@ -20,7 +20,6 @@ package session import ( "context" "github.com/argoproj/argo-cd/v2/pkg/apiclient/session" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/devtron/client/argocdServer" "time" ) @@ -33,8 +32,8 @@ type ServiceClientImpl struct { ssc session.SessionServiceClient } -func NewSessionServiceClient(settings *settings.ArgoCDSettings) *ServiceClientImpl { - conn := argocdServer.GetConnection("", settings) +func NewSessionServiceClient(acdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { + conn := acdConnection.GetConnection("") ssc := session.NewSessionServiceClient(conn) return &ServiceClientImpl{ssc: ssc} } diff --git a/cmd/external-app/wire_gen.go b/cmd/external-app/wire_gen.go index 199a26f6e6..03c5a402d8 100644 --- a/cmd/external-app/wire_gen.go +++ b/cmd/external-app/wire_gen.go @@ -1,8 +1,7 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire -//go:build !wireinject -// +build !wireinject +//go:generate wire +//+build !wireinject package main diff --git a/util/argo/ArgoUserService.go b/util/argo/ArgoUserService.go index 614c00abcc..08f7680a22 100644 --- a/util/argo/ArgoUserService.go +++ b/util/argo/ArgoUserService.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "github.com/argoproj/argo-cd/v2/pkg/apiclient/account" - "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/authenticator/client" "github.com/devtron-labs/devtron/client/argocdServer" "github.com/devtron-labs/devtron/client/argocdServer/session" @@ -47,26 +46,23 @@ type ArgoUserService interface { type ArgoUserServiceImpl struct { logger *zap.SugaredLogger clusterService cluster.ClusterService - acdSettings *settings.ArgoCDSettings devtronSecretConfig *util2.DevtronSecretConfig runTimeConfig *client.RuntimeConfig gitOpsRepository repository.GitOpsConfigRepository - settingsManager *settings.SettingsManager + acdConnection argocdServer.ArgoCdConnection } func NewArgoUserServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.ClusterService, - acdSettings *settings.ArgoCDSettings, devtronSecretConfig *util2.DevtronSecretConfig, - runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository, settingsManager *settings.SettingsManager) (*ArgoUserServiceImpl, error) { + runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository, acdConnection argocdServer.ArgoCdConnection) (*ArgoUserServiceImpl, error) { argoUserServiceImpl := &ArgoUserServiceImpl{ logger: Logger, clusterService: clusterService, - acdSettings: acdSettings, devtronSecretConfig: devtronSecretConfig, runTimeConfig: runTimeConfig, gitOpsRepository: gitOpsRepository, - settingsManager: settingsManager, + acdConnection: acdConnection, } if !runTimeConfig.LocalDevMode { go argoUserServiceImpl.ValidateGitOpsAndGetOrUpdateArgoCdUserDetail() @@ -145,13 +141,6 @@ func (impl *ArgoUserServiceImpl) createNewArgoCdUserForDevtron(k8sClient *v1.Cor } func (impl *ArgoUserServiceImpl) createNewArgoCdTokenForDevtron(username, password string, tokenNo int, k8sClient *v1.CoreV1Client) (string, error) { - - //load argo-cd credential from configmap and secret - _, err := impl.settingsManager.GetSettings() - if err != nil { - impl.logger.Errorw("error in getting settings", "err", err) - return "", err - } //create new user at argo cd side token, err := impl.createTokenForArgoCdUser(username, password) if err != nil { @@ -309,7 +298,7 @@ func (impl *ArgoUserServiceImpl) createNewArgoCdUser(username, password string, } func (impl *ArgoUserServiceImpl) createTokenForArgoCdUser(username, password string) (string, error) { - token, err := passwordLogin(impl.acdSettings, username, password) + token, err := impl.passwordLogin(username, password) if err != nil { impl.logger.Errorw("error in getting jwt token with username & password", "err", err) return "", err @@ -317,7 +306,7 @@ func (impl *ArgoUserServiceImpl) createTokenForArgoCdUser(username, password str ctx := context.Background() ctx = context.WithValue(ctx, "token", token) - acdConnection := argocdServer.GetConnection(token, impl.acdSettings) + acdConnection := impl.acdConnection.GetConnection(token) accountServiceClient := account.NewAccountServiceClient(acdConnection) acdToken, err := accountServiceClient.CreateToken(ctx, &account.CreateTokenRequest{ Name: username, @@ -329,8 +318,8 @@ func (impl *ArgoUserServiceImpl) createTokenForArgoCdUser(username, password str return acdToken.Token, nil } -func passwordLogin(acdSettings *settings.ArgoCDSettings, username, password string) (string, error) { - serviceClient := session.NewSessionServiceClient(acdSettings) +func (impl *ArgoUserServiceImpl) passwordLogin(username, password string) (string, error) { + serviceClient := session.NewSessionServiceClient(impl.acdConnection) token, err := serviceClient.Create(context.Background(), username, password) return token, err } diff --git a/util/session/Manager.go b/util/session/Manager.go index b9e64e5a22..5c6cb379ab 100644 --- a/util/session/Manager.go +++ b/util/session/Manager.go @@ -19,7 +19,6 @@ package session import ( "context" - "fmt" "github.com/argoproj/argo-cd/v2/util/settings" "github.com/devtron-labs/devtron/client/argocdServer" "k8s.io/client-go/kubernetes" @@ -27,7 +26,7 @@ import ( "log" ) -func CDSettingsManager(settings *settings.SettingsManager) (*settings.ArgoCDSettings, error) { +/*func CDSettingsManager(settings *settings.SettingsManager) (*settings.ArgoCDSettings, error) { at, err := settings.GetSettings() if err != nil { //return nil, err @@ -36,7 +35,7 @@ func CDSettingsManager(settings *settings.SettingsManager) (*settings.ArgoCDSett } return at, nil -} +}*/ func SettingsManager(cfg *argocdServer.Config) (*settings.SettingsManager, error) { clientset, kubeconfig := GetK8sclient() @@ -63,9 +62,3 @@ func GetK8sclient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.Client clientset := kubernetes.NewForConfigOrDie(config) return clientset, kubeconfig } - -//workaround for wire single value return -func NewK8sClient() *kubernetes.Clientset { - client, _ := GetK8sclient() - return client -} diff --git a/wire_gen.go b/wire_gen.go index 60de200d47..8418bf530a 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -1,8 +1,7 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire -//go:build !wireinject -// +build !wireinject +//go:generate wire +//+build !wireinject package main @@ -188,11 +187,11 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - argoCDSettings, err := session.CDSettingsManager(settingsManager) + argoCdConnectionImpl, err := argocdServer.NewArgoCdConnectionImpl(sugaredLogger, settingsManager, moduleRepositoryImpl) if err != nil { return nil, err } - serviceClientImpl := cluster.NewServiceClientImpl(argoCDSettings, sugaredLogger) + serviceClientImpl := cluster.NewServiceClientImpl(sugaredLogger, argoCdConnectionImpl) v := informer.NewGlobalMapClusterNamespace() k8sInformerFactoryImpl := informer.NewK8sInformerFactoryImpl(sugaredLogger, v, runtimeConfig) gitOpsConfigRepositoryImpl := repository.NewGitOpsConfigRepositoryImpl(sugaredLogger, db) @@ -251,7 +250,7 @@ func InitializeApp() (*App, error) { ciWorkflowRepositoryImpl := pipelineConfig.NewCiWorkflowRepositoryImpl(db, sugaredLogger) ciPipelineMaterialRepositoryImpl := pipelineConfig.NewCiPipelineMaterialRepositoryImpl(db, sugaredLogger) eventSimpleFactoryImpl := client.NewEventSimpleFactoryImpl(sugaredLogger, cdWorkflowRepositoryImpl, pipelineOverrideRepositoryImpl, ciWorkflowRepositoryImpl, ciPipelineMaterialRepositoryImpl, ciPipelineRepositoryImpl, pipelineRepositoryImpl, userRepositoryImpl) - applicationServiceClientImpl := application.NewApplicationClientImpl(argoCDSettings, sugaredLogger) + applicationServiceClientImpl := application.NewApplicationClientImpl(sugaredLogger, argoCdConnectionImpl) acdAuthConfig, err := util2.GetACDAuthConfig() if err != nil { return nil, err @@ -291,7 +290,7 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - repositoryServiceClientImpl := repository7.NewServiceClientImpl(argoCDSettings, sugaredLogger) + repositoryServiceClientImpl := repository7.NewServiceClientImpl(sugaredLogger, argoCdConnectionImpl) chartTemplateServiceImpl := util.NewChartTemplateServiceImpl(sugaredLogger, chartWorkingDir, httpClient, gitFactory, globalEnvVariables, gitOpsConfigRepositoryImpl, userRepositoryImpl, repositoryServiceClientImpl) refChartDir := _wireRefChartDirValue chartRepoRepositoryImpl := chartRepoRepository.NewChartRepoRepositoryImpl(db) @@ -304,7 +303,7 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, argoCDSettings, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl) + argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl, argoCdConnectionImpl) if err != nil { return nil, err } @@ -548,7 +547,7 @@ func InitializeApp() (*App, error) { imageScanRouterImpl := router.NewImageScanRouterImpl(imageScanRestHandlerImpl) policyRestHandlerImpl := restHandler.NewPolicyRestHandlerImpl(sugaredLogger, policyServiceImpl, userServiceImpl, userAuthServiceImpl, enforcerImpl, enforcerUtilImpl, environmentServiceImpl) policyRouterImpl := router.NewPolicyRouterImpl(policyRestHandlerImpl) - versionServiceImpl := argocdServer.NewVersionServiceImpl(argoCDSettings, sugaredLogger) + versionServiceImpl := argocdServer.NewVersionServiceImpl(sugaredLogger, argoCdConnectionImpl) gitOpsConfigServiceImpl := gitops.NewGitOpsConfigServiceImpl(sugaredLogger, gitOpsConfigRepositoryImpl, k8sUtil, acdAuthConfig, clusterServiceImplExtended, environmentServiceImpl, versionServiceImpl, gitFactory, chartTemplateServiceImpl, argoUserServiceImpl, serviceClientImpl) gitOpsConfigRestHandlerImpl := restHandler.NewGitOpsConfigRestHandlerImpl(sugaredLogger, gitOpsConfigServiceImpl, userServiceImpl, validate, enforcerImpl, teamServiceImpl, gitOpsConfigRepositoryImpl) gitOpsConfigRouterImpl := router.NewGitOpsConfigRouterImpl(gitOpsConfigRestHandlerImpl) @@ -643,7 +642,7 @@ func InitializeApp() (*App, error) { globalCMCSRestHandlerImpl := restHandler.NewGlobalCMCSRestHandlerImpl(sugaredLogger, userServiceImpl, validate, enforcerImpl, globalCMCSServiceImpl) globalCMCSRouterImpl := router.NewGlobalCMCSRouterImpl(globalCMCSRestHandlerImpl) muxRouter := router.NewMuxRouter(sugaredLogger, pipelineTriggerRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, gitHostRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, chartRepositoryRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, userAttributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl, telemetryRouterImpl, telemetryEventClientImplExtended, bulkUpdateRouterImpl, webhookListenerRouterImpl, appRouterImpl, coreAppRouterImpl, helmAppRouterImpl, k8sApplicationRouterImpl, pProfRouterImpl, deploymentConfigRouterImpl, dashboardTelemetryRouterImpl, commonDeploymentRouterImpl, externalLinkRouterImpl, globalPluginRouterImpl, moduleRouterImpl, serverRouterImpl, apiTokenRouterImpl, cdApplicationStatusUpdateHandlerImpl, k8sCapacityRouterImpl, webhookHelmRouterImpl, globalCMCSRouterImpl) - mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, versionServiceImpl, syncedEnforcer, db, pubSubClient, sessionManager, posthogClient) + mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, syncedEnforcer, db, pubSubClient, sessionManager, posthogClient) return mainApp, nil } From 3da21dea975218db2d38a7c552303cf8a4034b7d Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Wed, 30 Nov 2022 13:44:40 +0530 Subject: [PATCH 3/7] acd cd setting manager modification --- Wire.go | 3 +- client/argocdServer/Connection.go | 67 +++++++++++++++++++++++++------ util/session/Manager.go | 12 +----- wire_gen.go | 3 +- 4 files changed, 58 insertions(+), 27 deletions(-) diff --git a/Wire.go b/Wire.go index 3691f18bb1..64de80e326 100644 --- a/Wire.go +++ b/Wire.go @@ -105,7 +105,6 @@ import ( "github.com/devtron-labs/devtron/util/argo" "github.com/devtron-labs/devtron/util/k8s" "github.com/devtron-labs/devtron/util/rbac" - "github.com/devtron-labs/devtron/util/session" "github.com/google/wire" ) @@ -148,7 +147,7 @@ func InitializeApp() (*App, error) { wire.Value(appStoreBean.RefChartProxyDir("scripts/devtron-reference-helm-charts")), wire.Value(chart.DefaultChart("reference-app-rolling")), wire.Value(util.ChartWorkingDir("/tmp/charts/")), - session.SettingsManager, + argocdServer.SettingsManager, //session.CDSettingsManager, //auth.GetConfig, diff --git a/client/argocdServer/Connection.go b/client/argocdServer/Connection.go index ef861ac4c7..34fbe3cd97 100644 --- a/client/argocdServer/Connection.go +++ b/client/argocdServer/Connection.go @@ -18,10 +18,13 @@ package argocdServer import ( + "context" "fmt" moduleRepo "github.com/devtron-labs/devtron/pkg/module/repo" "github.com/go-pg/pg" "go.uber.org/zap" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" "log" "github.com/argoproj/argo-cd/v2/util/settings" @@ -40,6 +43,7 @@ type ArgoCdConnectionImpl struct { logger *zap.SugaredLogger settingsManager *settings.SettingsManager moduleRepository moduleRepo.ModuleRepository + argoCDSettings *settings.ArgoCDSettings } func NewArgoCdConnectionImpl(Logger *zap.SugaredLogger, settingsManager *settings.SettingsManager, @@ -48,29 +52,40 @@ func NewArgoCdConnectionImpl(Logger *zap.SugaredLogger, settingsManager *setting logger: Logger, settingsManager: settingsManager, moduleRepository: moduleRepository, + argoCDSettings: nil, } return argoUserServiceImpl, nil } +const ( + ModuleNameArgoCd string = "argo-cd" + ModuleStatusInstalled string = "installed" +) + +// GetConnection - this function will call only for acd connection func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { conf, err := GetConfig() if err != nil { impl.logger.Errorw("error on get acd connection", "err", err) log.Fatal(err) } - module, err := impl.moduleRepository.FindOne("acd") - if err != nil && err != pg.ErrNoRows { - impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) - } - if module == nil || module.Status != "installed" { - impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) - } - settings, err := impl.settingsManager.GetSettings() - if err != nil { - impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) + settings := impl.argoCDSettings + if settings == nil { + module, err := impl.moduleRepository.FindOne(ModuleNameArgoCd) + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + if module == nil || module.Status != ModuleStatusInstalled { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + settings, err = impl.settingsManager.GetSettings() + if err != nil { + impl.logger.Errorw("error on get acd connection", "err", err) + log.Fatal(err) + } + impl.argoCDSettings = settings } var option []grpc.DialOption option = append(option, grpc.WithTransportCredentials(GetTLS(settings.Certificate))) @@ -84,3 +99,29 @@ func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { } return conn } + +func SettingsManager(cfg *Config) (*settings.SettingsManager, error) { + clientset, kubeconfig := GetK8sclient() + namespace, _, err := kubeconfig.Namespace() + if err != nil { + return nil, err + } + //TODO: remove this hardcoding + if len(cfg.Namespace) >= 0 { + namespace = cfg.Namespace + } + return settings.NewSettingsManager(context.Background(), clientset, namespace), nil +} + +func GetK8sclient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.ClientConfig) { + kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + clientcmd.NewDefaultClientConfigLoadingRules(), + &clientcmd.ConfigOverrides{}, + ) + config, err := kubeconfig.ClientConfig() + if err != nil { + log.Fatal(err) + } + clientset := kubernetes.NewForConfigOrDie(config) + return clientset, kubeconfig +} diff --git a/util/session/Manager.go b/util/session/Manager.go index 5c6cb379ab..e0f5e4516e 100644 --- a/util/session/Manager.go +++ b/util/session/Manager.go @@ -17,15 +17,6 @@ package session -import ( - "context" - "github.com/argoproj/argo-cd/v2/util/settings" - "github.com/devtron-labs/devtron/client/argocdServer" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" - "log" -) - /*func CDSettingsManager(settings *settings.SettingsManager) (*settings.ArgoCDSettings, error) { at, err := settings.GetSettings() if err != nil { @@ -37,7 +28,7 @@ import ( return at, nil }*/ -func SettingsManager(cfg *argocdServer.Config) (*settings.SettingsManager, error) { +/*func SettingsManager(cfg *argocdServer.Config) (*settings.SettingsManager, error) { clientset, kubeconfig := GetK8sclient() namespace, _, err := kubeconfig.Namespace() if err != nil { @@ -62,3 +53,4 @@ func GetK8sclient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.Client clientset := kubernetes.NewForConfigOrDie(config) return clientset, kubeconfig } +*/ \ No newline at end of file diff --git a/wire_gen.go b/wire_gen.go index 8418bf530a..629fff171f 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -116,7 +116,6 @@ import ( "github.com/devtron-labs/devtron/util/argo" "github.com/devtron-labs/devtron/util/k8s" "github.com/devtron-labs/devtron/util/rbac" - "github.com/devtron-labs/devtron/util/session" ) import ( @@ -183,7 +182,7 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - settingsManager, err := session.SettingsManager(argocdServerConfig) + settingsManager, err := argocdServer.SettingsManager(argocdServerConfig) if err != nil { return nil, err } From a437ef76b78bdbfd61707aac24104082e85a9295 Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Wed, 30 Nov 2022 20:07:43 +0530 Subject: [PATCH 4/7] fixes --- client/argocdServer/Connection.go | 6 +++-- client/argocdServer/Version.go | 2 ++ util/session/Manager.go | 38 ------------------------------- 3 files changed, 6 insertions(+), 40 deletions(-) diff --git a/client/argocdServer/Connection.go b/client/argocdServer/Connection.go index 34fbe3cd97..c2f2c13796 100644 --- a/client/argocdServer/Connection.go +++ b/client/argocdServer/Connection.go @@ -74,11 +74,13 @@ func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { module, err := impl.moduleRepository.FindOne(ModuleNameArgoCd) if err != nil && err != pg.ErrNoRows { impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) + //log.Fatal(err) + return nil } if module == nil || module.Status != ModuleStatusInstalled { impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) + //log.Fatal(err) + return nil } settings, err = impl.settingsManager.GetSettings() if err != nil { diff --git a/client/argocdServer/Version.go b/client/argocdServer/Version.go index 0b83a090df..7c9a0b58ea 100644 --- a/client/argocdServer/Version.go +++ b/client/argocdServer/Version.go @@ -38,6 +38,7 @@ func NewVersionServiceImpl(logger *zap.SugaredLogger, argoCdConnection ArgoCdCon return &VersionServiceImpl{logger: logger, argoCdConnection: argoCdConnection} } +// CheckVersion deprecated func (service VersionServiceImpl) CheckVersion() (err error) { conn := service.argoCdConnection.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) @@ -48,6 +49,7 @@ func (service VersionServiceImpl) CheckVersion() (err error) { return nil } +// GetVersion deprecated func (service VersionServiceImpl) GetVersion() (apiVersion string, err error) { conn := service.argoCdConnection.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) diff --git a/util/session/Manager.go b/util/session/Manager.go index e0f5e4516e..d9ed6b27c8 100644 --- a/util/session/Manager.go +++ b/util/session/Manager.go @@ -16,41 +16,3 @@ */ package session - -/*func CDSettingsManager(settings *settings.SettingsManager) (*settings.ArgoCDSettings, error) { - at, err := settings.GetSettings() - if err != nil { - //return nil, err - //skip this error for no git-ops , as it will be set auto when acd configured for acd case - fmt.Printf("skiping this error, error on getting acd setting, err=%s", err.Error()) - - } - return at, nil -}*/ - -/*func SettingsManager(cfg *argocdServer.Config) (*settings.SettingsManager, error) { - clientset, kubeconfig := GetK8sclient() - namespace, _, err := kubeconfig.Namespace() - if err != nil { - return nil, err - } - //TODO: remove this hardcoding - if len(cfg.Namespace) >= 0 { - namespace = cfg.Namespace - } - return settings.NewSettingsManager(context.Background(), clientset, namespace), nil -} - -func GetK8sclient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.ClientConfig) { - kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - clientcmd.NewDefaultClientConfigLoadingRules(), - &clientcmd.ConfigOverrides{}, - ) - config, err := kubeconfig.ClientConfig() - if err != nil { - log.Fatal(err) - } - clientset := kubernetes.NewForConfigOrDie(config) - return clientset, kubeconfig -} -*/ \ No newline at end of file From ac8d3675b26e5088de9fe67e7b5adcd8520688ff Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Thu, 1 Dec 2022 19:24:53 +0530 Subject: [PATCH 5/7] review changes and code refactor --- Wire.go | 5 +- client/argocdServer/Connection.go | 87 ++++++++++--------- client/argocdServer/Version.go | 13 ++- .../argocdServer/application/Application.go | 42 ++++----- client/argocdServer/cluster/Cluster.go | 4 +- client/argocdServer/repository/Repository.go | 8 +- client/argocdServer/session/Session.go | 5 +- util/argo/ArgoUserService.go | 49 ++++++----- util/session/Manager.go | 18 ---- wire_gen.go | 12 +-- 10 files changed, 117 insertions(+), 126 deletions(-) delete mode 100644 util/session/Manager.go diff --git a/Wire.go b/Wire.go index 64de80e326..6ea6b57fc9 100644 --- a/Wire.go +++ b/Wire.go @@ -148,7 +148,6 @@ func InitializeApp() (*App, error) { wire.Value(chart.DefaultChart("reference-app-rolling")), wire.Value(util.ChartWorkingDir("/tmp/charts/")), argocdServer.SettingsManager, - //session.CDSettingsManager, //auth.GetConfig, argocdServer.GetConfig, @@ -737,8 +736,8 @@ func InitializeApp() (*App, error) { wire.Bind(new(pipeline.PipelineStageService), new(*pipeline.PipelineStageServiceImpl)), //plugin ends - argocdServer.NewArgoCdConnectionImpl, - wire.Bind(new(argocdServer.ArgoCdConnection), new(*argocdServer.ArgoCdConnectionImpl)), + argocdServer.NewArgoCDConnectionManagerImpl, + wire.Bind(new(argocdServer.ArgoCDConnectionManager), new(*argocdServer.ArgoCDConnectionManagerImpl)), argo.NewArgoUserServiceImpl, wire.Bind(new(argo.ArgoUserService), new(*argo.ArgoUserServiceImpl)), util2.GetDevtronSecretName, diff --git a/client/argocdServer/Connection.go b/client/argocdServer/Connection.go index c2f2c13796..b38617ea44 100644 --- a/client/argocdServer/Connection.go +++ b/client/argocdServer/Connection.go @@ -20,35 +20,33 @@ package argocdServer import ( "context" "fmt" + "github.com/argoproj/argo-cd/v2/util/settings" moduleRepo "github.com/devtron-labs/devtron/pkg/module/repo" "github.com/go-pg/pg" + grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "go.uber.org/zap" + "google.golang.org/grpc" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - "log" - - "github.com/argoproj/argo-cd/v2/util/settings" - grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" - "google.golang.org/grpc" ) func init() { grpc_prometheus.EnableClientHandlingTimeHistogram() } -type ArgoCdConnection interface { +type ArgoCDConnectionManager interface { GetConnection(token string) *grpc.ClientConn } -type ArgoCdConnectionImpl struct { +type ArgoCDConnectionManagerImpl struct { logger *zap.SugaredLogger settingsManager *settings.SettingsManager moduleRepository moduleRepo.ModuleRepository argoCDSettings *settings.ArgoCDSettings } -func NewArgoCdConnectionImpl(Logger *zap.SugaredLogger, settingsManager *settings.SettingsManager, - moduleRepository moduleRepo.ModuleRepository) (*ArgoCdConnectionImpl, error) { - argoUserServiceImpl := &ArgoCdConnectionImpl{ +func NewArgoCDConnectionManagerImpl(Logger *zap.SugaredLogger, settingsManager *settings.SettingsManager, + moduleRepository moduleRepo.ModuleRepository) (*ArgoCDConnectionManagerImpl, error) { + argoUserServiceImpl := &ArgoCDConnectionManagerImpl{ logger: Logger, settingsManager: settingsManager, moduleRepository: moduleRepository, @@ -63,32 +61,13 @@ const ( ) // GetConnection - this function will call only for acd connection -func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { +func (impl *ArgoCDConnectionManagerImpl) GetConnection(token string) *grpc.ClientConn { conf, err := GetConfig() if err != nil { - impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) - } - settings := impl.argoCDSettings - if settings == nil { - module, err := impl.moduleRepository.FindOne(ModuleNameArgoCd) - if err != nil && err != pg.ErrNoRows { - impl.logger.Errorw("error on get acd connection", "err", err) - //log.Fatal(err) - return nil - } - if module == nil || module.Status != ModuleStatusInstalled { - impl.logger.Errorw("error on get acd connection", "err", err) - //log.Fatal(err) - return nil - } - settings, err = impl.settingsManager.GetSettings() - if err != nil { - impl.logger.Errorw("error on get acd connection", "err", err) - log.Fatal(err) - } - impl.argoCDSettings = settings + impl.logger.Errorw("error on get acd config while creating connection", "err", err) + panic(err) } + settings := impl.getArgoCdSettings() var option []grpc.DialOption option = append(option, grpc.WithTransportCredentials(GetTLS(settings.Certificate))) if len(token) > 0 { @@ -97,14 +76,14 @@ func (impl *ArgoCdConnectionImpl) GetConnection(token string) *grpc.ClientConn { option = append(option, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor)) conn, err := grpc.Dial(fmt.Sprintf("%s:%s", conf.Host, conf.Port), option...) if err != nil { - log.Fatal(err) + panic(err) } return conn } func SettingsManager(cfg *Config) (*settings.SettingsManager, error) { - clientset, kubeconfig := GetK8sclient() - namespace, _, err := kubeconfig.Namespace() + clientSet, kubeConfig := getK8sClient() + namespace, _, err := kubeConfig.Namespace() if err != nil { return nil, err } @@ -112,18 +91,40 @@ func SettingsManager(cfg *Config) (*settings.SettingsManager, error) { if len(cfg.Namespace) >= 0 { namespace = cfg.Namespace } - return settings.NewSettingsManager(context.Background(), clientset, namespace), nil + return settings.NewSettingsManager(context.Background(), clientSet, namespace), nil } -func GetK8sclient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.ClientConfig) { - kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( +func getK8sClient() (k8sClient *kubernetes.Clientset, k8sConfig clientcmd.ClientConfig) { + kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}, ) - config, err := kubeconfig.ClientConfig() + config, err := kubeConfig.ClientConfig() if err != nil { - log.Fatal(err) + panic(err) + } + clientSet := kubernetes.NewForConfigOrDie(config) + return clientSet, kubeConfig +} + +func (impl *ArgoCDConnectionManagerImpl) getArgoCdSettings() *settings.ArgoCDSettings { + settings := impl.argoCDSettings + if settings == nil { + module, err := impl.moduleRepository.FindOne(ModuleNameArgoCd) + if err != nil && err != pg.ErrNoRows { + impl.logger.Errorw("error on get acd connection", "err", err) + return nil + } + if module == nil || module.Status != ModuleStatusInstalled { + impl.logger.Errorw("error on get acd connection", "err", err) + return nil + } + settings, err = impl.settingsManager.GetSettings() + if err != nil { + impl.logger.Errorw("error on get acd connection", "err", err) + panic(err) + } + impl.argoCDSettings = settings } - clientset := kubernetes.NewForConfigOrDie(config) - return clientset, kubeconfig + return impl.argoCDSettings } diff --git a/client/argocdServer/Version.go b/client/argocdServer/Version.go index 7c9a0b58ea..ac1c4f3828 100644 --- a/client/argocdServer/Version.go +++ b/client/argocdServer/Version.go @@ -30,17 +30,16 @@ type VersionService interface { } type VersionServiceImpl struct { - logger *zap.SugaredLogger - argoCdConnection ArgoCdConnection + logger *zap.SugaredLogger + argoCDConnectionManager ArgoCDConnectionManager } -func NewVersionServiceImpl(logger *zap.SugaredLogger, argoCdConnection ArgoCdConnection) *VersionServiceImpl { - return &VersionServiceImpl{logger: logger, argoCdConnection: argoCdConnection} +func NewVersionServiceImpl(logger *zap.SugaredLogger, argoCDConnectionManager ArgoCDConnectionManager) *VersionServiceImpl { + return &VersionServiceImpl{logger: logger, argoCDConnectionManager: argoCDConnectionManager} } -// CheckVersion deprecated func (service VersionServiceImpl) CheckVersion() (err error) { - conn := service.argoCdConnection.GetConnection("") + conn := service.argoCDConnectionManager.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) if err != nil { return err @@ -51,7 +50,7 @@ func (service VersionServiceImpl) CheckVersion() (err error) { // GetVersion deprecated func (service VersionServiceImpl) GetVersion() (apiVersion string, err error) { - conn := service.argoCdConnection.GetConnection("") + conn := service.argoCDConnectionManager.GetConnection("") version, err := version.NewVersionServiceClient(conn).Version(context.Background(), &empty.Empty{}) if err != nil { return "", err diff --git a/client/argocdServer/application/Application.go b/client/argocdServer/application/Application.go index a0b52e3523..967f8d48c0 100644 --- a/client/argocdServer/application/Application.go +++ b/client/argocdServer/application/Application.go @@ -114,15 +114,15 @@ type Manifests struct { type ServiceClientImpl struct { logger *zap.SugaredLogger - acdConnection argocdServer.ArgoCdConnection + argoCDConnectionManager argocdServer.ArgoCDConnectionManager } func NewApplicationClientImpl( - logger *zap.SugaredLogger, acdConnection argocdServer.ArgoCdConnection, + logger *zap.SugaredLogger, argoCDConnectionManager argocdServer.ArgoCDConnectionManager, ) *ServiceClientImpl { return &ServiceClientImpl{ logger: logger, - acdConnection: acdConnection, + argoCDConnectionManager: argoCDConnectionManager, } } @@ -133,7 +133,7 @@ func (c ServiceClientImpl) ManagedResources(ctxt context.Context, query *applica if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.ManagedResources(ctx, query) @@ -147,7 +147,7 @@ func (c ServiceClientImpl) Rollback(ctxt context.Context, query *application.App if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Rollback(ctx, query) @@ -161,7 +161,7 @@ func (c ServiceClientImpl) Patch(ctxt context.Context, query *application.Applic if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Patch(ctx, query) @@ -175,7 +175,7 @@ func (c ServiceClientImpl) GetManifests(ctxt context.Context, query *application if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.GetManifests(ctx, query) @@ -189,7 +189,7 @@ func (c ServiceClientImpl) Get(ctxt context.Context, query *application.Applicat if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Get(ctx, query) @@ -203,7 +203,7 @@ func (c ServiceClientImpl) Update(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Update(ctx, query) @@ -230,7 +230,7 @@ func (c ServiceClientImpl) Sync(ctxt context.Context, query *application.Applica if !ok { return nil, NewErrUnauthorized("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Sync(ctx, query) @@ -244,7 +244,7 @@ func (c ServiceClientImpl) TerminateOperation(ctxt context.Context, query *appli if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.TerminateOperation(ctx, query) @@ -258,7 +258,7 @@ func (c ServiceClientImpl) PatchResource(ctxt context.Context, query *applicatio if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.PatchResource(ctx, query) @@ -272,7 +272,7 @@ func (c ServiceClientImpl) DeleteResource(ctxt context.Context, query *applicati if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.DeleteResource(ctx, query) @@ -286,7 +286,7 @@ func (c ServiceClientImpl) List(ctxt context.Context, query *application.Applica if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.List(ctx, query) @@ -295,7 +295,7 @@ func (c ServiceClientImpl) List(ctxt context.Context, query *application.Applica func (c ServiceClientImpl) PodLogs(ctx context.Context, query *application.ApplicationPodLogsQuery) (application.ApplicationService_PodLogsClient, *grpc.ClientConn, error) { token, _ := ctx.Value("token").(string) - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) //defer conn.Close() asc := application.NewApplicationServiceClient(conn) logs, err := asc.PodLogs(ctx, query) @@ -304,7 +304,7 @@ func (c ServiceClientImpl) PodLogs(ctx context.Context, query *application.Appli func (c ServiceClientImpl) Watch(ctx context.Context, query *application.ApplicationQuery) (application.ApplicationService_WatchClient, *grpc.ClientConn, error) { token, _ := ctx.Value("token").(string) - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) //defer conn.Close() asc := application.NewApplicationServiceClient(conn) logs, err := asc.Watch(ctx, query) @@ -318,7 +318,7 @@ func (c ServiceClientImpl) GetResource(ctxt context.Context, query *application. if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) return asc.GetResource(ctx, query) @@ -331,7 +331,7 @@ func (c ServiceClientImpl) Delete(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) return asc.Delete(ctx, query) @@ -344,7 +344,7 @@ func (c ServiceClientImpl) ListResourceEvents(ctxt context.Context, query *appli if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.ListResourceEvents(ctx, query) @@ -358,7 +358,7 @@ func (c ServiceClientImpl) Create(ctxt context.Context, query *application.Appli if !ok { return nil, errors.New("Unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) resp, err := asc.Create(ctx, query) @@ -372,7 +372,7 @@ func (c ServiceClientImpl) ResourceTree(ctxt context.Context, query *application if !ok { return nil, errors.New("unauthorized") } - conn := c.acdConnection.GetConnection(token) + conn := c.argoCDConnectionManager.GetConnection(token) defer util.Close(conn, c.logger) asc := application.NewApplicationServiceClient(conn) c.logger.Debugw("GRPC_GET_RESOURCETREE", "req", query) diff --git a/client/argocdServer/cluster/Cluster.go b/client/argocdServer/cluster/Cluster.go index 1a0b00a096..5658f4760c 100644 --- a/client/argocdServer/cluster/Cluster.go +++ b/client/argocdServer/cluster/Cluster.go @@ -44,10 +44,10 @@ type ServiceClient interface { type ServiceClientImpl struct { logger *zap.SugaredLogger - argoCdConnection argocdServer.ArgoCdConnection + argoCdConnection argocdServer.ArgoCDConnectionManager } -func NewServiceClientImpl(logger *zap.SugaredLogger, argoCdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { +func NewServiceClientImpl(logger *zap.SugaredLogger, argoCdConnection argocdServer.ArgoCDConnectionManager) *ServiceClientImpl { return &ServiceClientImpl{ logger: logger, argoCdConnection: argoCdConnection, diff --git a/client/argocdServer/repository/Repository.go b/client/argocdServer/repository/Repository.go index d5d9eaf971..6b3d697572 100644 --- a/client/argocdServer/repository/Repository.go +++ b/client/argocdServer/repository/Repository.go @@ -45,13 +45,13 @@ type ServiceClient interface { type ServiceClientImpl struct { logger *zap.SugaredLogger - acdConnection argocdServer.ArgoCdConnection + argoCDConnectionManager argocdServer.ArgoCDConnectionManager } -func NewServiceClientImpl(logger *zap.SugaredLogger, acdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { +func NewServiceClientImpl(logger *zap.SugaredLogger, argoCDConnectionManager argocdServer.ArgoCDConnectionManager) *ServiceClientImpl { return &ServiceClientImpl{ logger: logger, - acdConnection: acdConnection, + argoCDConnectionManager: argoCDConnectionManager, } } @@ -60,7 +60,7 @@ func (r ServiceClientImpl) getService(ctx context.Context) (repository2.Reposito if !ok { return nil, errors.New("Unauthorized") } - conn := r.acdConnection.GetConnection(token) + conn := r.argoCDConnectionManager.GetConnection(token) //defer conn.Close() return repository2.NewRepositoryServiceClient(conn), nil } diff --git a/client/argocdServer/session/Session.go b/client/argocdServer/session/Session.go index 00bd9eefd3..f4b041e0d7 100644 --- a/client/argocdServer/session/Session.go +++ b/client/argocdServer/session/Session.go @@ -32,8 +32,9 @@ type ServiceClientImpl struct { ssc session.SessionServiceClient } -func NewSessionServiceClient(acdConnection argocdServer.ArgoCdConnection) *ServiceClientImpl { - conn := acdConnection.GetConnection("") +func NewSessionServiceClient(argoCDConnectionManager argocdServer.ArgoCDConnectionManager) *ServiceClientImpl { + // this function only called when gitops configured and user ask for creating acd token + conn := argoCDConnectionManager.GetConnection("") ssc := session.NewSessionServiceClient(conn) return &ServiceClientImpl{ssc: ssc} } diff --git a/util/argo/ArgoUserService.go b/util/argo/ArgoUserService.go index 08f7680a22..5ab8135c8b 100644 --- a/util/argo/ArgoUserService.go +++ b/util/argo/ArgoUserService.go @@ -44,25 +44,28 @@ type ArgoUserService interface { } type ArgoUserServiceImpl struct { - logger *zap.SugaredLogger - clusterService cluster.ClusterService - devtronSecretConfig *util2.DevtronSecretConfig - runTimeConfig *client.RuntimeConfig - gitOpsRepository repository.GitOpsConfigRepository - acdConnection argocdServer.ArgoCdConnection + logger *zap.SugaredLogger + clusterService cluster.ClusterService + devtronSecretConfig *util2.DevtronSecretConfig + runTimeConfig *client.RuntimeConfig + gitOpsRepository repository.GitOpsConfigRepository + argoCDConnectionManager argocdServer.ArgoCDConnectionManager + versionService argocdServer.VersionService } func NewArgoUserServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.ClusterService, devtronSecretConfig *util2.DevtronSecretConfig, - runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository, acdConnection argocdServer.ArgoCdConnection) (*ArgoUserServiceImpl, error) { + runTimeConfig *client.RuntimeConfig, gitOpsRepository repository.GitOpsConfigRepository, + argoCDConnectionManager argocdServer.ArgoCDConnectionManager, versionService argocdServer.VersionService) (*ArgoUserServiceImpl, error) { argoUserServiceImpl := &ArgoUserServiceImpl{ - logger: Logger, - clusterService: clusterService, - devtronSecretConfig: devtronSecretConfig, - runTimeConfig: runTimeConfig, - gitOpsRepository: gitOpsRepository, - acdConnection: acdConnection, + logger: Logger, + clusterService: clusterService, + devtronSecretConfig: devtronSecretConfig, + runTimeConfig: runTimeConfig, + gitOpsRepository: gitOpsRepository, + argoCDConnectionManager: argoCDConnectionManager, + versionService: versionService, } if !runTimeConfig.LocalDevMode { go argoUserServiceImpl.ValidateGitOpsAndGetOrUpdateArgoCdUserDetail() @@ -305,23 +308,29 @@ func (impl *ArgoUserServiceImpl) createTokenForArgoCdUser(username, password str } ctx := context.Background() ctx = context.WithValue(ctx, "token", token) - - acdConnection := impl.acdConnection.GetConnection(token) - accountServiceClient := account.NewAccountServiceClient(acdConnection) + clientConn := impl.argoCDConnectionManager.GetConnection(token) + accountServiceClient := account.NewAccountServiceClient(clientConn) acdToken, err := accountServiceClient.CreateToken(ctx, &account.CreateTokenRequest{ Name: username, }) if err != nil { - impl.logger.Errorw("error in creating token at argocd side", "err", err) + impl.logger.Errorw("error in creating acdToken in ArgoCd", "err", err) + return "", err + } + + // just checking and logging the ArgoCd version + err = impl.versionService.CheckVersion() + if err != nil { + impl.logger.Errorw("error found while checking ArgoCd Version", "err", err) return "", err } return acdToken.Token, nil } func (impl *ArgoUserServiceImpl) passwordLogin(username, password string) (string, error) { - serviceClient := session.NewSessionServiceClient(impl.acdConnection) - token, err := serviceClient.Create(context.Background(), username, password) - return token, err + serviceClient := session.NewSessionServiceClient(impl.argoCDConnectionManager) + jwtToken, err := serviceClient.Create(context.Background(), username, password) + return jwtToken, err } func getNewPassword() string { diff --git a/util/session/Manager.go b/util/session/Manager.go deleted file mode 100644 index d9ed6b27c8..0000000000 --- a/util/session/Manager.go +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2020 Devtron Labs - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package session diff --git a/wire_gen.go b/wire_gen.go index 629fff171f..affe3be0fb 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -186,11 +186,11 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - argoCdConnectionImpl, err := argocdServer.NewArgoCdConnectionImpl(sugaredLogger, settingsManager, moduleRepositoryImpl) + argoCDConnectionManagerImpl, err := argocdServer.NewArgoCDConnectionManagerImpl(sugaredLogger, settingsManager, moduleRepositoryImpl) if err != nil { return nil, err } - serviceClientImpl := cluster.NewServiceClientImpl(sugaredLogger, argoCdConnectionImpl) + serviceClientImpl := cluster.NewServiceClientImpl(sugaredLogger, argoCDConnectionManagerImpl) v := informer.NewGlobalMapClusterNamespace() k8sInformerFactoryImpl := informer.NewK8sInformerFactoryImpl(sugaredLogger, v, runtimeConfig) gitOpsConfigRepositoryImpl := repository.NewGitOpsConfigRepositoryImpl(sugaredLogger, db) @@ -249,7 +249,7 @@ func InitializeApp() (*App, error) { ciWorkflowRepositoryImpl := pipelineConfig.NewCiWorkflowRepositoryImpl(db, sugaredLogger) ciPipelineMaterialRepositoryImpl := pipelineConfig.NewCiPipelineMaterialRepositoryImpl(db, sugaredLogger) eventSimpleFactoryImpl := client.NewEventSimpleFactoryImpl(sugaredLogger, cdWorkflowRepositoryImpl, pipelineOverrideRepositoryImpl, ciWorkflowRepositoryImpl, ciPipelineMaterialRepositoryImpl, ciPipelineRepositoryImpl, pipelineRepositoryImpl, userRepositoryImpl) - applicationServiceClientImpl := application.NewApplicationClientImpl(sugaredLogger, argoCdConnectionImpl) + applicationServiceClientImpl := application.NewApplicationClientImpl(sugaredLogger, argoCDConnectionManagerImpl) acdAuthConfig, err := util2.GetACDAuthConfig() if err != nil { return nil, err @@ -289,7 +289,7 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - repositoryServiceClientImpl := repository7.NewServiceClientImpl(sugaredLogger, argoCdConnectionImpl) + repositoryServiceClientImpl := repository7.NewServiceClientImpl(sugaredLogger, argoCDConnectionManagerImpl) chartTemplateServiceImpl := util.NewChartTemplateServiceImpl(sugaredLogger, chartWorkingDir, httpClient, gitFactory, globalEnvVariables, gitOpsConfigRepositoryImpl, userRepositoryImpl, repositoryServiceClientImpl) refChartDir := _wireRefChartDirValue chartRepoRepositoryImpl := chartRepoRepository.NewChartRepoRepositoryImpl(db) @@ -302,7 +302,8 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl, argoCdConnectionImpl) + versionServiceImpl := argocdServer.NewVersionServiceImpl(sugaredLogger, argoCDConnectionManagerImpl) + argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl, argoCDConnectionManagerImpl, versionServiceImpl) if err != nil { return nil, err } @@ -546,7 +547,6 @@ func InitializeApp() (*App, error) { imageScanRouterImpl := router.NewImageScanRouterImpl(imageScanRestHandlerImpl) policyRestHandlerImpl := restHandler.NewPolicyRestHandlerImpl(sugaredLogger, policyServiceImpl, userServiceImpl, userAuthServiceImpl, enforcerImpl, enforcerUtilImpl, environmentServiceImpl) policyRouterImpl := router.NewPolicyRouterImpl(policyRestHandlerImpl) - versionServiceImpl := argocdServer.NewVersionServiceImpl(sugaredLogger, argoCdConnectionImpl) gitOpsConfigServiceImpl := gitops.NewGitOpsConfigServiceImpl(sugaredLogger, gitOpsConfigRepositoryImpl, k8sUtil, acdAuthConfig, clusterServiceImplExtended, environmentServiceImpl, versionServiceImpl, gitFactory, chartTemplateServiceImpl, argoUserServiceImpl, serviceClientImpl) gitOpsConfigRestHandlerImpl := restHandler.NewGitOpsConfigRestHandlerImpl(sugaredLogger, gitOpsConfigServiceImpl, userServiceImpl, validate, enforcerImpl, teamServiceImpl, gitOpsConfigRepositoryImpl) gitOpsConfigRouterImpl := router.NewGitOpsConfigRouterImpl(gitOpsConfigRestHandlerImpl) From ed2ae7bf0335d6aabf118317deaac007839db899 Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Fri, 2 Dec 2022 11:32:10 +0530 Subject: [PATCH 6/7] removed panic from acd connection fetch --- client/argocdServer/Connection.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/argocdServer/Connection.go b/client/argocdServer/Connection.go index b38617ea44..474cd8a04e 100644 --- a/client/argocdServer/Connection.go +++ b/client/argocdServer/Connection.go @@ -65,7 +65,7 @@ func (impl *ArgoCDConnectionManagerImpl) GetConnection(token string) *grpc.Clien conf, err := GetConfig() if err != nil { impl.logger.Errorw("error on get acd config while creating connection", "err", err) - panic(err) + return nil } settings := impl.getArgoCdSettings() var option []grpc.DialOption @@ -76,7 +76,7 @@ func (impl *ArgoCDConnectionManagerImpl) GetConnection(token string) *grpc.Clien option = append(option, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor)) conn, err := grpc.Dial(fmt.Sprintf("%s:%s", conf.Host, conf.Port), option...) if err != nil { - panic(err) + return nil } return conn } @@ -122,7 +122,7 @@ func (impl *ArgoCDConnectionManagerImpl) getArgoCdSettings() *settings.ArgoCDSet settings, err = impl.settingsManager.GetSettings() if err != nil { impl.logger.Errorw("error on get acd connection", "err", err) - panic(err) + return nil } impl.argoCDSettings = settings } From d24e112693b4846d70ea86fc08ee493e21bece72 Mon Sep 17 00:00:00 2001 From: vikramdevtron Date: Fri, 16 Dec 2022 19:08:21 +0530 Subject: [PATCH 7/7] wire --- cmd/external-app/wire_gen.go | 2 +- wire_gen.go | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cmd/external-app/wire_gen.go b/cmd/external-app/wire_gen.go index 7be1d46f3e..c62b54f6f6 100644 --- a/cmd/external-app/wire_gen.go +++ b/cmd/external-app/wire_gen.go @@ -1,6 +1,6 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire +//go:generate wire //+build !wireinject package main diff --git a/wire_gen.go b/wire_gen.go index 636f00bd97..66981265b6 100644 --- a/wire_gen.go +++ b/wire_gen.go @@ -1,6 +1,6 @@ // Code generated by Wire. DO NOT EDIT. -//go:generate go run github.com/google/wire/cmd/wire +//go:generate wire //+build !wireinject package main @@ -120,7 +120,6 @@ import ( "github.com/devtron-labs/devtron/util/argo" "github.com/devtron-labs/devtron/util/k8s" "github.com/devtron-labs/devtron/util/rbac" - "github.com/devtron-labs/devtron/util/session" ) import ( @@ -187,15 +186,15 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - settingsManager, err := session.SettingsManager(argocdServerConfig) + settingsManager, err := argocdServer.SettingsManager(argocdServerConfig) if err != nil { return nil, err } - argoCDSettings, err := session.CDSettingsManager(settingsManager) + argoCDConnectionManagerImpl, err := argocdServer.NewArgoCDConnectionManagerImpl(sugaredLogger, settingsManager, moduleRepositoryImpl) if err != nil { return nil, err } - serviceClientImpl := cluster.NewServiceClientImpl(argoCDSettings, sugaredLogger) + serviceClientImpl := cluster.NewServiceClientImpl(sugaredLogger, argoCDConnectionManagerImpl) v := informer.NewGlobalMapClusterNamespace() k8sInformerFactoryImpl := informer.NewK8sInformerFactoryImpl(sugaredLogger, v, runtimeConfig) gitOpsConfigRepositoryImpl := repository.NewGitOpsConfigRepositoryImpl(sugaredLogger, db) @@ -257,7 +256,7 @@ func InitializeApp() (*App, error) { ciWorkflowRepositoryImpl := pipelineConfig.NewCiWorkflowRepositoryImpl(db, sugaredLogger) ciPipelineMaterialRepositoryImpl := pipelineConfig.NewCiPipelineMaterialRepositoryImpl(db, sugaredLogger) eventSimpleFactoryImpl := client.NewEventSimpleFactoryImpl(sugaredLogger, cdWorkflowRepositoryImpl, pipelineOverrideRepositoryImpl, ciWorkflowRepositoryImpl, ciPipelineMaterialRepositoryImpl, ciPipelineRepositoryImpl, pipelineRepositoryImpl, userRepositoryImpl) - applicationServiceClientImpl := application.NewApplicationClientImpl(argoCDSettings, sugaredLogger) + applicationServiceClientImpl := application.NewApplicationClientImpl(sugaredLogger, argoCDConnectionManagerImpl) acdAuthConfig, err := util2.GetACDAuthConfig() if err != nil { return nil, err @@ -297,7 +296,7 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - repositoryServiceClientImpl := repository7.NewServiceClientImpl(argoCDSettings, sugaredLogger) + repositoryServiceClientImpl := repository7.NewServiceClientImpl(sugaredLogger, argoCDConnectionManagerImpl) chartTemplateServiceImpl := util.NewChartTemplateServiceImpl(sugaredLogger, chartWorkingDir, httpClient, gitFactory, globalEnvVariables, gitOpsConfigRepositoryImpl, userRepositoryImpl, repositoryServiceClientImpl) refChartDir := _wireRefChartDirValue chartRepoRepositoryImpl := chartRepoRepository.NewChartRepoRepositoryImpl(db) @@ -310,7 +309,8 @@ func InitializeApp() (*App, error) { if err != nil { return nil, err } - argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, argoCDSettings, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl) + versionServiceImpl := argocdServer.NewVersionServiceImpl(sugaredLogger, argoCDConnectionManagerImpl) + argoUserServiceImpl, err := argo.NewArgoUserServiceImpl(sugaredLogger, clusterServiceImplExtended, devtronSecretConfig, runtimeConfig, gitOpsConfigRepositoryImpl, argoCDConnectionManagerImpl, versionServiceImpl) if err != nil { return nil, err } @@ -565,7 +565,6 @@ func InitializeApp() (*App, error) { imageScanRouterImpl := router.NewImageScanRouterImpl(imageScanRestHandlerImpl) policyRestHandlerImpl := restHandler.NewPolicyRestHandlerImpl(sugaredLogger, policyServiceImpl, userServiceImpl, userAuthServiceImpl, enforcerImpl, enforcerUtilImpl, environmentServiceImpl) policyRouterImpl := router.NewPolicyRouterImpl(policyRestHandlerImpl) - versionServiceImpl := argocdServer.NewVersionServiceImpl(argoCDSettings, sugaredLogger) gitOpsConfigServiceImpl := gitops.NewGitOpsConfigServiceImpl(sugaredLogger, gitOpsConfigRepositoryImpl, k8sUtil, acdAuthConfig, clusterServiceImplExtended, environmentServiceImpl, versionServiceImpl, gitFactory, chartTemplateServiceImpl, argoUserServiceImpl, serviceClientImpl) gitOpsConfigRestHandlerImpl := restHandler.NewGitOpsConfigRestHandlerImpl(sugaredLogger, gitOpsConfigServiceImpl, userServiceImpl, validate, enforcerImpl, teamServiceImpl, gitOpsConfigRepositoryImpl) gitOpsConfigRouterImpl := router.NewGitOpsConfigRouterImpl(gitOpsConfigRestHandlerImpl) @@ -676,7 +675,7 @@ func InitializeApp() (*App, error) { } ciStatusUpdateCronImpl := cron.NewCiStatusUpdateCronImpl(sugaredLogger, appServiceImpl, ciWorkflowStatusUpdateConfig, ciPipelineRepositoryImpl, ciHandlerImpl) muxRouter := router.NewMuxRouter(sugaredLogger, pipelineTriggerRouterImpl, pipelineConfigRouterImpl, migrateDbRouterImpl, appListingRouterImpl, environmentRouterImpl, clusterRouterImpl, webhookRouterImpl, userAuthRouterImpl, applicationRouterImpl, cdRouterImpl, projectManagementRouterImpl, gitProviderRouterImpl, gitHostRouterImpl, dockerRegRouterImpl, notificationRouterImpl, teamRouterImpl, gitWebhookHandlerImpl, workflowStatusUpdateHandlerImpl, applicationStatusUpdateHandlerImpl, ciEventHandlerImpl, pubSubClient, userRouterImpl, chartRefRouterImpl, configMapRouterImpl, appStoreRouterImpl, chartRepositoryRouterImpl, releaseMetricsRouterImpl, deploymentGroupRouterImpl, batchOperationRouterImpl, chartGroupRouterImpl, testSuitRouterImpl, imageScanRouterImpl, policyRouterImpl, gitOpsConfigRouterImpl, dashboardRouterImpl, attributesRouterImpl, userAttributesRouterImpl, commonRouterImpl, grafanaRouterImpl, ssoLoginRouterImpl, telemetryRouterImpl, telemetryEventClientImplExtended, bulkUpdateRouterImpl, webhookListenerRouterImpl, appRouterImpl, coreAppRouterImpl, helmAppRouterImpl, k8sApplicationRouterImpl, pProfRouterImpl, deploymentConfigRouterImpl, dashboardTelemetryRouterImpl, commonDeploymentRouterImpl, externalLinkRouterImpl, globalPluginRouterImpl, moduleRouterImpl, serverRouterImpl, apiTokenRouterImpl, cdApplicationStatusUpdateHandlerImpl, k8sCapacityRouterImpl, webhookHelmRouterImpl, globalCMCSRouterImpl, userTerminalAccessRouterImpl, ciStatusUpdateCronImpl) - mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, versionServiceImpl, syncedEnforcer, db, pubSubClient, sessionManager, posthogClient) + mainApp := NewApp(muxRouter, sugaredLogger, sseSSE, syncedEnforcer, db, pubSubClient, sessionManager, posthogClient) return mainApp, nil }