Skip to content

Commit 2014a14

Browse files
final commit
1 parent 74d03d0 commit 2014a14

File tree

22 files changed

+997
-101
lines changed

22 files changed

+997
-101
lines changed

client/telemetry/TelemetryEventClient.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
cloudProviderIdentifier "github.com/devtron-labs/common-lib/cloud-provider-identifier"
2525
"github.com/devtron-labs/devtron/api/helm-app/gRPC"
26+
util2 "github.com/devtron-labs/devtron/internal/util"
2627
bean2 "github.com/devtron-labs/devtron/pkg/attributes/bean"
2728
cron3 "github.com/devtron-labs/devtron/util/cron"
2829
"net/http"
@@ -90,8 +91,8 @@ type TelemetryEventClient interface {
9091
SendSummaryEvent(eventType string) error
9192
}
9293

93-
func NewK8sAppTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService, PosthogClient *PosthogClient) (*TelemetryEventClientImpl, error) {
94-
return NewTelemetryEventClientImpl(logger, client, clusterService, nil, nil, nil, nil, nil, PosthogClient, nil, nil, nil, nil, nil)
94+
func NewK8sAppTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService, PosthogClient *PosthogClient, cloudProviderIdentifierService cloudProviderIdentifier.ProviderIdentifierService, cronLogger *cron3.CronLoggerImpl) (*TelemetryEventClientImpl, error) {
95+
return NewTelemetryEventClientImpl(logger, client, clusterService, nil, nil, nil, nil, nil, PosthogClient, nil, nil, nil, nil, nil, cloudProviderIdentifierService, cronLogger)
9596
}
9697

9798
func NewTelemetryEventClientImpl(logger *zap.SugaredLogger, client *http.Client, clusterService cluster.ClusterService,
@@ -846,7 +847,7 @@ func (impl *TelemetryEventClientImpl) buildIntegrationsList() ([]string, []strin
846847

847848
func (impl *TelemetryEventClientImpl) GetOrSetDesktopAppUcid() string {
848849
ucid := ""
849-
err, devtronDirPath := util.CheckOrCreateDevtronDir()
850+
err, devtronDirPath := util2.CheckOrCreateDevtronDir()
850851
if err != nil {
851852
impl.logger.Warnw("error occurred while creating dir", "err", err)
852853
}

cmd/k8s-client-app/k8sClientApp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"github.com/devtron-labs/devtron/api/restHandler/common"
77
"github.com/devtron-labs/devtron/internal/middleware"
8-
"github.com/devtron-labs/devtron/util"
8+
util2 "github.com/devtron-labs/devtron/internal/util"
99
"github.com/go-pg/pg"
1010
"go.uber.org/zap"
1111
"io/fs"
@@ -102,7 +102,7 @@ func (app *App) Stop() {
102102
}
103103

104104
func (app *App) writePortToFile(port int) {
105-
err, devtronDirPath := util.CheckOrCreateDevtronDir()
105+
err, devtronDirPath := util2.CheckOrCreateDevtronDir()
106106
if err != nil {
107107
app.Logger.Fatal("error occurred while creating dir", "err", err)
108108
}

cmd/k8s-client-app/wire.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
cloudProviderIdentifier "github.com/devtron-labs/common-lib/cloud-provider-identifier"
78
k8s2 "github.com/devtron-labs/common-lib/utils/k8s"
89
"github.com/devtron-labs/devtron/api/argoApplication"
910
"github.com/devtron-labs/devtron/api/cluster"
@@ -67,6 +68,9 @@ func InitializeApp() (*App, error) {
6768
connector.NewPumpImpl,
6869
wire.Bind(new(connector.Pump), new(*connector.PumpImpl)),
6970

71+
cloudProviderIdentifier.NewProviderIdentifierServiceImpl,
72+
wire.Bind(new(cloudProviderIdentifier.ProviderIdentifierService), new(*cloudProviderIdentifier.ProviderIdentifierServiceImpl)),
73+
7074
telemetry.NewK8sAppTelemetryEventClientImpl,
7175
wire.Bind(new(telemetry.TelemetryEventClient), new(*telemetry.TelemetryEventClientImpl)),
7276

cmd/k8s-client-app/wire_gen.go

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ require (
8383
google.golang.org/protobuf v1.33.0
8484
gopkg.in/go-playground/validator.v9 v9.30.0
8585
gopkg.in/igm/sockjs-go.v3 v3.0.0
86+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
8687
gopkg.in/yaml.v2 v2.4.0
8788
gorm.io/gorm v1.24.2
8889
helm.sh/helm/v3 v3.14.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,8 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
11971197
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
11981198
gopkg.in/mgo.v2 v2.0.0-20160818015218-f2b6f6c918c4/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
11991199
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
1200+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
1201+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
12001202
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
12011203
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
12021204
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637/go.mod h1:BHsqpu/nsuzkT5BpiH1EMZPLyqSMM8JbIavyFACoFNk=

internal/sql/repository/terminal/TerminalAccessFileBasedRepository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package terminal
33
import (
44
"errors"
55
"github.com/devtron-labs/devtron/internal/sql/models"
6+
util2 "github.com/devtron-labs/devtron/internal/util"
67
"github.com/devtron-labs/devtron/pkg/sql"
7-
"github.com/devtron-labs/devtron/util"
88
"github.com/glebarez/sqlite"
99
"go.uber.org/zap"
1010
"gorm.io/gorm"
@@ -45,7 +45,7 @@ func NewTerminalAccessFileBasedRepository(logger *zap.SugaredLogger) *TerminalAc
4545
}
4646

4747
func createOrCheckClusterDbPath(logger *zap.SugaredLogger) (error, string) {
48-
err, devtronDirPath := util.CheckOrCreateDevtronDir()
48+
err, devtronDirPath := util2.CheckOrCreateDevtronDir()
4949
if err != nil {
5050
logger.Errorw("error occurred while creating devtron dir ", "err", err)
5151
return err, ""

internal/util/BasicProviders.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import (
2121
"github.com/caarlos0/env"
2222
"go.uber.org/zap"
2323
"go.uber.org/zap/zapcore"
24+
"gopkg.in/natefinch/lumberjack.v2"
25+
"log"
2426
"net/http"
27+
"os"
28+
"path"
2529
)
2630

2731
var (
@@ -92,7 +96,7 @@ func InitFileBasedLogger() (*zap.SugaredLogger, error) {
9296
func getLogWriter(cfg *LogConfig) zapcore.WriteSyncer {
9397
// lumberjack.Logger is already safe for concurrent use, so we don't need to
9498
// lock it.
95-
err, devtronDirPath := util.CheckOrCreateDevtronDir()
99+
err, devtronDirPath := CheckOrCreateDevtronDir()
96100
if err != nil {
97101
devtronDirPath = "/tmp/"
98102
}
@@ -118,3 +122,18 @@ func NewFileBaseSugaredLogger() (*zap.SugaredLogger, error) {
118122
func NewHttpClient() *http.Client {
119123
return http.DefaultClient
120124
}
125+
126+
func CheckOrCreateDevtronDir() (err error, devtronDirPath string) {
127+
userHomeDir, err := os.UserHomeDir()
128+
if err != nil {
129+
log.Fatalln("error occurred while finding home dir", "err", err)
130+
return err, ""
131+
}
132+
devtronDirPath = path.Join(userHomeDir, "./.devtron")
133+
err = os.MkdirAll(devtronDirPath, os.ModePerm)
134+
if err != nil {
135+
log.Fatalln("error occurred while creating folder", "path", devtronDirPath, "err", err)
136+
return err, ""
137+
}
138+
return err, devtronDirPath
139+
}
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,54 @@
11
package repository
22

3+
import (
4+
"fmt"
5+
"github.com/glebarez/sqlite"
6+
"github.com/go-pg/pg"
7+
"github.com/pkg/errors"
8+
"go.uber.org/zap"
9+
"gorm.io/gorm"
10+
)
11+
312
type ClusterDescriptionFileBasedRepositoryImpl struct {
13+
logger *zap.SugaredLogger
14+
dbConnection *gorm.DB
415
}
516

6-
func NewClusterDescriptionFileBasedRepository() *ClusterDescriptionFileBasedRepositoryImpl {
7-
return &ClusterDescriptionFileBasedRepositoryImpl{}
17+
func NewClusterDescriptionFileBasedRepository(logger *zap.SugaredLogger) *ClusterDescriptionFileBasedRepositoryImpl {
18+
err, dbPath := createOrCheckClusterDbPath(logger)
19+
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
20+
//db, err := sql.Open("sqlite3", "./cluster.db")
21+
if err != nil {
22+
logger.Fatal("error occurred while opening db connection", "error", err)
23+
}
24+
migrator := db.Migrator()
25+
clusterDescription := &ClusterDescription{}
26+
hasTable := migrator.HasTable(clusterDescription)
27+
if !hasTable {
28+
err = migrator.CreateTable(clusterDescription)
29+
if err != nil {
30+
logger.Fatal("error occurred while creating cluster description table", "error", err)
31+
}
32+
}
33+
logger.Debugw("cluster description repository file based initialized")
34+
return &ClusterDescriptionFileBasedRepositoryImpl{logger, db}
835
}
936

1037
func (impl ClusterDescriptionFileBasedRepositoryImpl) FindByClusterIdWithClusterDetails(clusterId int) (*ClusterDescription, error) {
11-
//TODO implement me
12-
panic("implement me")
38+
clusterDescription := &ClusterDescription{}
39+
query := "SELECT cl.id AS cluster_id, cl.cluster_name AS cluster_name, cl.description AS cluster_description, cl.server_url, cl.created_on AS cluster_created_on, cl.created_by AS cluster_created_by, gn.id AS note_id, gn.description AS note, gn.created_by, gn.created_on, gn.updated_by, gn.updated_on FROM" +
40+
" cluster_entities cl LEFT JOIN" +
41+
" generic_notes gn " +
42+
" ON cl.id=gn.identifier AND (gn.identifier_type = %d OR gn.identifier_type IS NULL)" +
43+
" WHERE cl.id=%d AND cl.active=true " +
44+
" LIMIT 1 OFFSET 0;"
45+
query = fmt.Sprintf(query, 0, clusterId) //0 is for cluster type description
46+
//_, err := impl.dbConnection.Query(clusterDescription, query)
47+
result := impl.dbConnection.Raw(query).Find(clusterDescription)
48+
err := result.Error
49+
if errors.Is(err, gorm.ErrRecordNotFound) {
50+
err = pg.ErrNoRows
51+
}
52+
return clusterDescription, err
1353
}
1454

pkg/cluster/repository/ClusterFileBasedRepository.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package repository
33
import (
44
"encoding/json"
55
"errors"
6+
util2 "github.com/devtron-labs/devtron/internal/util"
67
"github.com/devtron-labs/devtron/pkg/sql"
7-
"github.com/devtron-labs/devtron/util"
88
"path"
99
"time"
1010

@@ -31,6 +31,15 @@ type ClusterEntity struct {
3131
K8sVersion string
3232
ErrorInConnecting string
3333
Description string
34+
PrometheusEndpoint string
35+
CdArgoSetup bool
36+
PUserName string
37+
PPassword string
38+
PTlsClientCert string
39+
PTlsClientKey string
40+
AgentInstallationStage int
41+
IsVirtualCluster bool
42+
InsecureSkipTlsVerify bool
3443
sql.AuditLog
3544
}
3645

@@ -43,20 +52,16 @@ func NewClusterRepositoryFileBased(logger *zap.SugaredLogger) *ClusterFileBasedR
4352
}
4453
migrator := db.Migrator()
4554
clusterEntity := &ClusterEntity{}
46-
//TODO KB: Need to handle table upgrade migration
47-
hasTable := migrator.HasTable(clusterEntity)
48-
if !hasTable {
49-
err = migrator.CreateTable(clusterEntity)
50-
if err != nil {
51-
logger.Fatal("error occurred while creating cluster table", "error", err)
52-
}
55+
err = migrator.AutoMigrate(clusterEntity)
56+
if err != nil {
57+
logger.Fatal("error occurred while migrating cluster table", "error", err)
5358
}
5459
logger.Debugw("cluster repository file based initialized")
5560
return &ClusterFileBasedRepository{logger, db}
5661
}
5762

5863
func createOrCheckClusterDbPath(logger *zap.SugaredLogger) (error, string) {
59-
err, devtronDirPath := util.CheckOrCreateDevtronDir()
64+
err, devtronDirPath := util2.CheckOrCreateDevtronDir()
6065
if err != nil {
6166
logger.Errorw("error occurred while creating devtron dir ", "err", err)
6267
return err, ""
@@ -72,6 +77,9 @@ func (impl *ClusterFileBasedRepository) FindAllActiveExceptVirtual() ([]Cluster,
7277
Where("is_virtual_cluster=? OR is_virtual_cluster IS NULL", false).
7378
Find(&clusterEntities)
7479
err := result.Error
80+
if errors.Is(err, gorm.ErrRecordNotFound) {
81+
err = pg.ErrNoRows
82+
}
7583
if err != nil {
7684
impl.logger.Errorw("error occurred while finding all cluster data", "err", err)
7785
return nil, err
@@ -93,7 +101,7 @@ func (impl *ClusterFileBasedRepository) SetDescription(id int, description strin
93101
clusterEntity.Description = description
94102
clusterEntity.UpdatedBy = userId
95103
clusterEntity.UpdatedOn = time.Now()
96-
result := impl.dbConnection.Model(&ClusterEntity{}).Updates(clusterEntity)
104+
result := impl.dbConnection.Model(clusterEntity).Updates(clusterEntity)
97105
err = result.Error
98106
if err != nil {
99107
impl.logger.Errorw("error occurred while updating cluster description", "clusterId", id, "err", err)
@@ -277,6 +285,9 @@ func (impl *ClusterFileBasedRepository) FindById(id int) (*Cluster, error) {
277285
Find(clusterEntity).
278286
Limit(1)
279287
err := result.Error
288+
if errors.Is(err, gorm.ErrRecordNotFound) {
289+
err = pg.ErrNoRows
290+
}
280291
if err != nil {
281292
impl.logger.Errorw("error occurred while finding cluster data ", "id", id, "err", err)
282293
return nil, err
@@ -295,22 +306,19 @@ func (impl *ClusterFileBasedRepository) FindByIds(id []int) ([]Cluster, error) {
295306

296307
var clusterEntities []ClusterEntity
297308
result := impl.dbConnection.
298-
Where("id in(?)", pg.In(id)).
309+
Where("id in ?", id).
299310
Where("active = ?", true).
300311
Find(&clusterEntities)
301312
err := result.Error
313+
if errors.Is(err, gorm.ErrRecordNotFound) {
314+
err = pg.ErrNoRows
315+
}
302316
if err != nil {
303317
impl.logger.Errorw("error occurred while finding all cluster data", "err", err)
304318
return nil, err
305319
}
306320
clusters := impl.ConvertEntitiesToModel(clusterEntities)
307321
return clusters, nil
308-
//var cluster []Cluster
309-
//result := impl.dbConnection.
310-
// Find(&cluster).
311-
// Where("id in(?)", pg.In(id)).
312-
// Where("active =?", true)
313-
//return cluster, result.Error
314322
}
315323

316324
func (impl *ClusterFileBasedRepository) Update(model *Cluster) error {

0 commit comments

Comments
 (0)