Skip to content

Commit

Permalink
Merge pull request #348 from devtron-labs/kubernetes-api-refactoring
Browse files Browse the repository at this point in the history
Enhancement: Kubernetes api refactoring
  • Loading branch information
Shivam-nagar23 committed Jul 28, 2023
2 parents a1171ba + b2da246 commit a2470a8
Show file tree
Hide file tree
Showing 106 changed files with 8,115 additions and 4,158 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
@@ -1,4 +1,4 @@
FROM golang:1.18 AS build-env
FROM golang:1.20 AS build-env

RUN echo $GOPATH
RUN apt update
Expand Down
2 changes: 1 addition & 1 deletion DockerfileEA
@@ -1,4 +1,4 @@
FROM golang:1.18 AS build-env
FROM golang:1.20 AS build-env

RUN echo $GOPATH
RUN apt update
Expand Down
5 changes: 3 additions & 2 deletions Wire.go
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/devtron-labs/devtron/api/externalLink"
"github.com/devtron-labs/devtron/api/globalPolicy"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/api/k8s"
"github.com/devtron-labs/devtron/api/module"
"github.com/devtron-labs/devtron/api/restHandler"
pipeline2 "github.com/devtron-labs/devtron/api/restHandler/app"
Expand Down Expand Up @@ -119,7 +120,7 @@ import (
util3 "github.com/devtron-labs/devtron/pkg/util"
util2 "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/argo"
"github.com/devtron-labs/devtron/util/k8s"
util4 "github.com/devtron-labs/devtron/util/k8s"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/google/wire"
)
Expand All @@ -133,6 +134,7 @@ func InitializeApp() (*App, error) {
externalLink.ExternalLinkWireSet,
team.TeamsWireSet,
AuthWireSet,
util4.NewK8sUtil,
user.UserWireSet,
sso.SsoConfigWireSet,
cluster.ClusterWireSet,
Expand Down Expand Up @@ -368,7 +370,6 @@ func InitializeApp() (*App, error) {
wire.Bind(new(repository9.ImageTaggingRepository), new(*repository9.ImageTaggingRepositoryImpl)),
pipeline.NewImageTaggingServiceImpl,
wire.Bind(new(pipeline.ImageTaggingService), new(*pipeline.ImageTaggingServiceImpl)),
util.NewK8sUtil,
argocdServer.NewVersionServiceImpl,
wire.Bind(new(argocdServer.VersionService), new(*argocdServer.VersionServiceImpl)),

Expand Down
32 changes: 13 additions & 19 deletions api/cluster/EnvironmentRestHandler.go
Expand Up @@ -20,9 +20,8 @@ package cluster
import (
"context"
"encoding/json"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/util/k8s"
"k8s.io/client-go/kubernetes"
"github.com/devtron-labs/devtron/pkg/k8s"
k8s2 "github.com/devtron-labs/devtron/util/k8s"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -62,12 +61,13 @@ type EnvironmentRestHandler interface {

type EnvironmentRestHandlerImpl struct {
environmentClusterMappingsService request.EnvironmentService
k8sApplicationService k8s.K8sApplicationService
k8sCommonService k8s.K8sCommonService
logger *zap.SugaredLogger
userService user.UserService
validator *validator.Validate
enforcer casbin.Enforcer
deleteService delete2.DeleteService
k8sUtil *k8s2.K8sUtil
cfg *bean.Config
}

Expand All @@ -76,10 +76,7 @@ type ClusterReachableResponse struct {
ClusterName string `json:"clusterName"`
}

func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, k8sApplicationService k8s.K8sApplicationService, logger *zap.SugaredLogger, userService user.UserService,
validator *validator.Validate, enforcer casbin.Enforcer,
deleteService delete2.DeleteService,
) *EnvironmentRestHandlerImpl {
func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, logger *zap.SugaredLogger, userService user.UserService, validator *validator.Validate, enforcer casbin.Enforcer, deleteService delete2.DeleteService, k8sUtil *k8s2.K8sUtil, k8sCommonService k8s.K8sCommonService) *EnvironmentRestHandlerImpl {
cfg := &bean.Config{}
err := env.Parse(cfg)
if err != nil {
Expand All @@ -89,13 +86,14 @@ func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, k8sApplicatio
logger.Infow("evironment rest handler initialized", "ignoreAuthCheckValue", cfg.IgnoreAuthCheck)
return &EnvironmentRestHandlerImpl{
environmentClusterMappingsService: svc,
k8sApplicationService: k8sApplicationService,
logger: logger,
userService: userService,
validator: validator,
enforcer: enforcer,
deleteService: deleteService,
cfg: cfg,
k8sUtil: k8sUtil,
k8sCommonService: k8sCommonService,
}
}

Expand Down Expand Up @@ -601,30 +599,26 @@ func (impl EnvironmentRestHandlerImpl) GetEnvironmentConnection(w http.ResponseW
}
//RBAC enforcer Ends
// getting restConfig and clientSet outside the goroutine because we don't want to call goroutine func with receiver function
restConfig, err := impl.k8sApplicationService.GetRestConfigByClusterId(context.Background(), clusterBean.Id)
restConfig, err, _ := impl.k8sCommonService.GetRestConfigByClusterId(context.Background(), clusterBean.Id)
if err != nil {
impl.logger.Errorw("error in getting restConfig by cluster", "err", err, "clusterId", clusterBean.Id)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
k8sHttpClient, err := util.OverrideK8sHttpClientWithTracer(restConfig)
k8sClientSet, err := impl.k8sUtil.CreateK8sClientSet(restConfig)
if err != nil {
impl.logger.Errorw("service err, OverrideK8sHttpClientWithTracer", "err", err, "restConfig", restConfig)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
k8sClientSet, err := kubernetes.NewForConfigAndClient(restConfig, k8sHttpClient)
if err != nil {
impl.logger.Errorw("error in getting client set by rest config", "err", err, "restConfig", restConfig)
impl.logger.Errorw("error in creating k8s clientSet", "err", err, "clusterId", clusterBean.Id)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}

responseObj := &ClusterReachableResponse{
ClusterReachable: true,
ClusterName: clusterBean.ClusterName,
}
err = impl.k8sApplicationService.FetchConnectionStatusForCluster(k8sClientSet, clusterBean.Id)
err = impl.k8sUtil.FetchConnectionStatusForCluster(k8sClientSet)
if err != nil {
impl.logger.Errorw("error in fetching connection status fo cluster", "err", err, "clusterId", clusterBean.Id)
responseObj.ClusterReachable = false
}
//updating the cluster connection error to db
Expand Down
27 changes: 11 additions & 16 deletions api/helm-app/HelmAppService.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/devtron-labs/devtron/util/k8s"
"net/http"
"reflect"
"strconv"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/devtron-labs/devtron/api/connector"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
openapi2 "github.com/devtron-labs/devtron/api/openapi/openapiClient"
"github.com/devtron-labs/devtron/client/k8s/application"
"github.com/devtron-labs/devtron/internal/middleware"
"github.com/devtron-labs/devtron/internal/sql/repository/app"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
Expand Down Expand Up @@ -87,7 +87,7 @@ type HelmAppServiceImpl struct {
installedAppRepository repository.InstalledAppRepository
appRepository app.AppRepository
clusterRepository clusterRepository.ClusterRepository
K8sUtil *util.K8sUtil
K8sUtil *k8s.K8sUtil
helmReleaseConfig *HelmReleaseConfig
}

Expand All @@ -97,7 +97,7 @@ func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.Clu
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
environmentService cluster.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository,
installedAppRepository repository.InstalledAppRepository, appRepository app.AppRepository,
clusterRepository clusterRepository.ClusterRepository, K8sUtil *util.K8sUtil,
clusterRepository clusterRepository.ClusterRepository, K8sUtil *k8s.K8sUtil,
helmReleaseConfig *HelmReleaseConfig) *HelmAppServiceImpl {
return &HelmAppServiceImpl{
logger: Logger,
Expand Down Expand Up @@ -130,11 +130,6 @@ func GetHelmReleaseConfig() (*HelmReleaseConfig, error) {
return cfg, err
}

type ResourceRequestBean struct {
AppId string `json:"appId"`
K8sRequest application.K8sRequestBean `json:"k8sRequest"`
}

func (impl *HelmAppServiceImpl) listApplications(ctx context.Context, clusterIds []int) (ApplicationService_ListApplicationsClient, error) {
if len(clusterIds) == 0 {
return nil, nil
Expand All @@ -150,7 +145,7 @@ func (impl *HelmAppServiceImpl) listApplications(ctx context.Context, clusterIds
for _, clusterDetail := range clusters {
config := &ClusterConfig{
ApiServerUrl: clusterDetail.ServerUrl,
Token: clusterDetail.Config[util.BearerToken],
Token: clusterDetail.Config[k8s.BearerToken],
ClusterId: int32(clusterDetail.Id),
ClusterName: clusterDetail.ClusterName,
}
Expand Down Expand Up @@ -268,25 +263,25 @@ func (impl *HelmAppServiceImpl) UnHibernateApplication(ctx context.Context, app
}

func (impl *HelmAppServiceImpl) GetClusterConf(clusterId int) (*ClusterConfig, error) {
cluster, err := impl.clusterService.FindById(clusterId)
clusterObj, err := impl.clusterService.FindById(clusterId)
if err != nil {
impl.logger.Errorw("error in fetching cluster detail", "err", err)
return nil, err
}

var config ClusterConfig
// if virtual cluster run helm template command in default cluster as no real cluster is available in this case
if cluster.IsVirtualCluster {
if clusterObj.IsVirtualCluster {
config = ClusterConfig{
ClusterName: DEFAULT_CLUSTER,
ClusterName: cluster.DEFAULT_CLUSTER,
Token: "",
}
} else {
config = ClusterConfig{
ApiServerUrl: cluster.ServerUrl,
Token: cluster.Config[util.BearerToken],
ClusterId: int32(cluster.Id),
ClusterName: cluster.ClusterName,
ApiServerUrl: clusterObj.ServerUrl,
Token: clusterObj.Config[k8s.BearerToken],
ClusterId: int32(clusterObj.Id),
ClusterName: clusterObj.ClusterName,
}
}

Expand Down
1 change: 0 additions & 1 deletion api/helm-app/bean.go
Expand Up @@ -3,7 +3,6 @@ package client
import openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"

const (
DEFAULT_CLUSTER = "default_cluster"
DEFAULT_CLUSTER_ID = 1
SOURCE_DEVTRON_APP SourceAppType = "devtron-app"
SOURCE_HELM_APP SourceAppType = "helm-app"
Expand Down

0 comments on commit a2470a8

Please sign in to comment.