Skip to content

Commit fd90dfb

Browse files
authored
fix: unimplemented cluster cron service (#5781)
1 parent 47843d9 commit fd90dfb

File tree

10 files changed

+77
-123
lines changed

10 files changed

+77
-123
lines changed

api/cluster/wire_cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
var ClusterWireSet = wire.NewSet(
3030
repository.NewClusterRepositoryImpl,
3131
wire.Bind(new(repository.ClusterRepository), new(*repository.ClusterRepositoryImpl)),
32+
cluster.NewClusterServiceImpl,
3233
cluster.NewClusterServiceImplExtended,
3334
wire.Bind(new(cluster.ClusterService), new(*cluster.ClusterServiceImplExtended)),
3435

api/k8s/application/k8sApplicationRestHandler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ func (handler *K8sApplicationRestHandlerImpl) GetPodLogs(w http.ResponseWriter,
559559
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
560560
return
561561
}
562+
handler.logger.Infow("get pod logs request", "request", request)
562563
handler.requestValidationAndRBAC(w, r, token, request)
563564
lastEventId := r.Header.Get(bean2.LastEventID)
564565
isReconnect := false

api/k8s/wire_k8sApp.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,4 @@ var K8sApplicationWireSet = wire.NewSet(
5353
informer.NewGlobalMapClusterNamespace,
5454
informer.NewK8sInformerFactoryImpl,
5555
wire.Bind(new(informer.K8sInformerFactory), new(*informer.K8sInformerFactoryImpl)),
56-
57-
cluster.NewClusterCronServiceImpl,
58-
wire.Bind(new(cluster.ClusterCronService), new(*cluster.ClusterCronServiceImpl)),
5956
)

cmd/external-app/wire_gen.go

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

pkg/cluster/ClusterCronService.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

pkg/cluster/ClusterService.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
cronUtil "github.com/devtron-labs/devtron/util/cron"
24+
"github.com/robfig/cron/v3"
2325
"log"
2426
"net/http"
2527
"net/url"
@@ -43,7 +45,7 @@ import (
4345
"github.com/devtron-labs/devtron/internal/constants"
4446
"github.com/devtron-labs/devtron/internal/util"
4547
"github.com/devtron-labs/devtron/pkg/cluster/repository"
46-
util2 "github.com/devtron-labs/devtron/util"
48+
globalUtil "github.com/devtron-labs/devtron/util"
4749
"github.com/go-pg/pg"
4850
"go.uber.org/zap"
4951
)
@@ -201,7 +203,9 @@ type ClusterServiceImpl struct {
201203
func NewClusterServiceImpl(repository repository.ClusterRepository, logger *zap.SugaredLogger,
202204
K8sUtil *k8s.K8sServiceImpl, K8sInformerFactory informer.K8sInformerFactory,
203205
userAuthRepository repository3.UserAuthRepository, userRepository repository3.UserRepository,
204-
roleGroupRepository repository3.RoleGroupRepository) *ClusterServiceImpl {
206+
roleGroupRepository repository3.RoleGroupRepository,
207+
envVariables *globalUtil.EnvironmentVariables,
208+
cronLogger *cronUtil.CronLoggerImpl) (*ClusterServiceImpl, error) {
205209
clusterService := &ClusterServiceImpl{
206210
clusterRepository: repository,
207211
logger: logger,
@@ -211,8 +215,19 @@ func NewClusterServiceImpl(repository repository.ClusterRepository, logger *zap.
211215
userRepository: userRepository,
212216
roleGroupRepository: roleGroupRepository,
213217
}
218+
// initialise cron
219+
newCron := cron.New(cron.WithChain(cron.Recover(cronLogger)))
220+
newCron.Start()
221+
cfg := envVariables.GlobalClusterConfig
222+
// add function into cron
223+
_, err := newCron.AddFunc(fmt.Sprintf("@every %dm", cfg.ClusterStatusCronTime), clusterService.getAndUpdateClusterConnectionStatus)
224+
if err != nil {
225+
fmt.Println("error in adding cron function into cluster cron service")
226+
return clusterService, err
227+
}
228+
logger.Infow("cluster cron service started successfully!", "cronTime", cfg.ClusterStatusCronTime)
214229
go clusterService.buildInformer()
215-
return clusterService
230+
return clusterService, nil
216231
}
217232

218233
func (impl *ClusterServiceImpl) ConvertClusterBeanToCluster(clusterBean *ClusterBean, userId int32) *repository.Cluster {
@@ -242,6 +257,20 @@ func (impl *ClusterServiceImpl) ConvertClusterBeanToCluster(clusterBean *Cluster
242257
return model
243258
}
244259

260+
// getAndUpdateClusterConnectionStatus is a cron function to update the connection status of all clusters
261+
func (impl *ClusterServiceImpl) getAndUpdateClusterConnectionStatus() {
262+
impl.logger.Debug("starting cluster connection status fetch thread")
263+
defer impl.logger.Debug("stopped cluster connection status fetch thread")
264+
265+
//getting all clusters
266+
clusters, err := impl.FindAllExceptVirtual()
267+
if err != nil {
268+
impl.logger.Errorw("error in getting all clusters", "err", err)
269+
return
270+
}
271+
impl.ConnectClustersInBatch(clusters, true)
272+
}
273+
245274
func (impl *ClusterServiceImpl) Save(parent context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error) {
246275
//validating config
247276

@@ -289,7 +318,7 @@ func (impl *ClusterServiceImpl) Save(parent context.Context, bean *ClusterBean,
289318

290319
//on successful creation of new cluster, update informer cache for namespace group by cluster
291320
//here sync for ea mode only
292-
if util2.IsBaseStack() {
321+
if globalUtil.IsBaseStack() {
293322
impl.SyncNsInformer(bean)
294323
}
295324
impl.logger.Info("saving secret for cluster informer")
@@ -530,7 +559,7 @@ func (impl *ClusterServiceImpl) Update(ctx context.Context, bean *ClusterBean, u
530559
bean.Id = model.Id
531560

532561
//here sync for ea mode only
533-
if bean.HasConfigOrUrlChanged && util2.IsBaseStack() {
562+
if bean.HasConfigOrUrlChanged && globalUtil.IsBaseStack() {
534563
impl.SyncNsInformer(bean)
535564
}
536565
impl.logger.Infow("saving secret for cluster informer")
@@ -643,7 +672,7 @@ func (impl *ClusterServiceImpl) buildInformer() {
643672
impl.K8sInformerFactory.BuildInformer(clusterInfo)
644673
}
645674

646-
func (impl ClusterServiceImpl) DeleteFromDb(bean *ClusterBean, userId int32) error {
675+
func (impl *ClusterServiceImpl) DeleteFromDb(bean *ClusterBean, userId int32) error {
647676
existingCluster, err := impl.clusterRepository.FindById(bean.Id)
648677
if err != nil {
649678
impl.logger.Errorw("No matching entry found for delete.", "id", bean.Id)
@@ -668,7 +697,7 @@ func (impl ClusterServiceImpl) DeleteFromDb(bean *ClusterBean, userId int32) err
668697
return nil
669698
}
670699

671-
func (impl ClusterServiceImpl) CheckIfConfigIsValid(cluster *ClusterBean) error {
700+
func (impl *ClusterServiceImpl) CheckIfConfigIsValid(cluster *ClusterBean) error {
672701
clusterConfig := cluster.GetClusterConfig()
673702
response, err := impl.K8sUtil.DiscoveryClientGetLiveZCall(clusterConfig)
674703
if err != nil {
@@ -1068,7 +1097,7 @@ func (impl *ClusterServiceImpl) GetAndUpdateConnectionStatusForOneCluster(k8sCli
10681097
mutex.Unlock()
10691098
}
10701099

1071-
func (impl ClusterServiceImpl) ConvertClusterBeanObjectToCluster(bean *ClusterBean) *v1alpha1.Cluster {
1100+
func (impl *ClusterServiceImpl) ConvertClusterBeanObjectToCluster(bean *ClusterBean) *v1alpha1.Cluster {
10721101
configMap := bean.Config
10731102
serverUrl := bean.ServerUrl
10741103
bearerToken := ""
@@ -1097,7 +1126,7 @@ func (impl ClusterServiceImpl) ConvertClusterBeanObjectToCluster(bean *ClusterBe
10971126
return cl
10981127
}
10991128

1100-
func (impl ClusterServiceImpl) GetClusterConfigByClusterId(clusterId int) (*k8s.ClusterConfig, error) {
1129+
func (impl *ClusterServiceImpl) GetClusterConfigByClusterId(clusterId int) (*k8s.ClusterConfig, error) {
11011130
clusterBean, err := impl.FindById(clusterId)
11021131
if err != nil {
11031132
impl.logger.Errorw("error in getting clusterBean by cluster id", "err", err, "clusterId", clusterId)
@@ -1108,7 +1137,7 @@ func (impl ClusterServiceImpl) GetClusterConfigByClusterId(clusterId int) (*k8s.
11081137
return clusterConfig, nil
11091138
}
11101139

1111-
func (impl ClusterServiceImpl) IsClusterReachable(clusterId int) (bool, error) {
1140+
func (impl *ClusterServiceImpl) IsClusterReachable(clusterId int) (bool, error) {
11121141
cluster, err := impl.clusterRepository.FindById(clusterId)
11131142
if err != nil {
11141143
impl.logger.Errorw("error in finding cluster from clusterId", "envId", clusterId)

pkg/cluster/ClusterServiceExtended.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ import (
2727
cluster3 "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
2828
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2929
"github.com/devtron-labs/common-lib/utils/k8s"
30-
repository5 "github.com/devtron-labs/devtron/pkg/auth/user/repository"
31-
"github.com/devtron-labs/devtron/pkg/k8s/informer"
32-
3330
cluster2 "github.com/devtron-labs/devtron/client/argocdServer/cluster"
3431
"github.com/devtron-labs/devtron/client/grafana"
3532
"github.com/devtron-labs/devtron/internal/constants"
3633
"github.com/devtron-labs/devtron/internal/util"
3734
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
3835
repository2 "github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
3936
"github.com/devtron-labs/devtron/pkg/cluster/repository"
40-
"go.uber.org/zap"
4137
)
4238

4339
// extends ClusterServiceImpl and enhances method of ClusterService with full mode specific errors
@@ -50,30 +46,19 @@ type ClusterServiceImplExtended struct {
5046
*ClusterServiceImpl
5147
}
5248

53-
func NewClusterServiceImplExtended(repository repository.ClusterRepository, environmentRepository repository.EnvironmentRepository,
54-
grafanaClient grafana.GrafanaClient, logger *zap.SugaredLogger, installedAppRepository repository2.InstalledAppRepository,
55-
K8sUtil *k8s.K8sServiceImpl,
56-
clusterServiceCD cluster2.ServiceClient, K8sInformerFactory informer.K8sInformerFactory,
57-
userAuthRepository repository5.UserAuthRepository,
58-
userRepository repository5.UserRepository, roleGroupRepository repository5.RoleGroupRepository,
59-
gitOpsConfigReadService config.GitOpsConfigReadService) *ClusterServiceImplExtended {
49+
func NewClusterServiceImplExtended(environmentRepository repository.EnvironmentRepository,
50+
grafanaClient grafana.GrafanaClient, installedAppRepository repository2.InstalledAppRepository,
51+
clusterServiceCD cluster2.ServiceClient,
52+
gitOpsConfigReadService config.GitOpsConfigReadService,
53+
clusterServiceImpl *ClusterServiceImpl) *ClusterServiceImplExtended {
6054
clusterServiceExt := &ClusterServiceImplExtended{
6155
environmentRepository: environmentRepository,
6256
grafanaClient: grafanaClient,
6357
installedAppRepository: installedAppRepository,
6458
clusterServiceCD: clusterServiceCD,
6559
gitOpsConfigReadService: gitOpsConfigReadService,
66-
ClusterServiceImpl: &ClusterServiceImpl{
67-
clusterRepository: repository,
68-
logger: logger,
69-
K8sUtil: K8sUtil,
70-
K8sInformerFactory: K8sInformerFactory,
71-
userAuthRepository: userAuthRepository,
72-
userRepository: userRepository,
73-
roleGroupRepository: roleGroupRepository,
74-
},
60+
ClusterServiceImpl: clusterServiceImpl,
7561
}
76-
go clusterServiceExt.buildInformer()
7762
return clusterServiceExt
7863
}
7964

pkg/k8s/capacity/k8sCapacityService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (impl *K8sCapacityServiceImpl) GetClusterCapacityDetail(ctx context.Context
113113
if err != nil {
114114
if client.IsClusterUnReachableError(err) {
115115
impl.logger.Errorw("k8s cluster unreachable", "err", err)
116-
return nil, &util.ApiError{HttpStatusCode: http.StatusBadRequest, UserMessage: err.Error()}
116+
return nil, &util.ApiError{HttpStatusCode: http.StatusBadRequest, UserMessage: err.Error(), InternalMessage: err.Error()}
117117
}
118118
impl.logger.Errorw("error in getting node list", "err", err, "clusterId", cluster.Id)
119119
return nil, err

util/GlobalConfig.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type EnvironmentVariables struct {
2525
DevtronSecretConfig *DevtronSecretConfig
2626
DeploymentServiceTypeConfig *DeploymentServiceTypeConfig
2727
TerminalEnvVariables *TerminalEnvVariables
28+
GlobalClusterConfig *GlobalClusterConfig
2829
}
2930

3031
type DeploymentServiceTypeConfig struct {
@@ -43,6 +44,10 @@ type GlobalEnvVariables struct {
4344
ExecuteWireNilChecker bool `env:"EXECUTE_WIRE_NIL_CHECKER" envDefault:"false"`
4445
}
4546

47+
type GlobalClusterConfig struct {
48+
ClusterStatusCronTime int `env:"CLUSTER_STATUS_CRON_TIME" envDefault:"15"`
49+
}
50+
4651
type DevtronSecretConfig struct {
4752
DevtronSecretName string `env:"DEVTRON_SECRET_NAME" envDefault:"devtron-secret"`
4853
DevtronDexSecretNamespace string `env:"DEVTRON_DEX_SECRET_NAMESPACE" envDefault:"devtroncd"`
@@ -58,6 +63,7 @@ func GetEnvironmentVariables() (*EnvironmentVariables, error) {
5863
DevtronSecretConfig: &DevtronSecretConfig{},
5964
DeploymentServiceTypeConfig: &DeploymentServiceTypeConfig{},
6065
TerminalEnvVariables: &TerminalEnvVariables{},
66+
GlobalClusterConfig: &GlobalClusterConfig{},
6167
}
6268
err := env.Parse(cfg)
6369
if err != nil {

0 commit comments

Comments
 (0)