From 253f8b96a3468e254739350d63481347149a8b71 Mon Sep 17 00:00:00 2001 From: GLYASAI Date: Mon, 21 Jun 2021 16:48:25 +0800 Subject: [PATCH] chart cache --- api/controller/application.go | 2 +- api/handler/service.go | 14 +------------- api/handler/service_handler.go | 2 -- cmd/worker/option/option.go | 8 +++++--- db/model/tenant.go | 7 +++++++ go.mod | 2 +- localcheck.sh | 10 ++++++++++ worker/master/helmapp/app.go | 4 ++-- worker/master/helmapp/controller.go | 6 +++--- worker/master/helmapp/controlloop.go | 5 ++++- worker/master/helmapp/finilizer.go | 5 ++++- worker/master/helmapp/suite_test.go | 2 +- worker/master/master.go | 2 +- 13 files changed, 40 insertions(+), 29 deletions(-) create mode 100755 localcheck.sh diff --git a/api/controller/application.go b/api/controller/application.go index e6e039a40..54ede4136 100644 --- a/api/controller/application.go +++ b/api/controller/application.go @@ -32,7 +32,7 @@ func (a *ApplicationController) CreateApp(w http.ResponseWriter, r *http.Request httputil.ReturnBcodeError(r, w, bcode.NewBadRequest("the field 'app_template_name' is required")) return } - if tenantReq.AppTemplateName == "" { + if tenantReq.AppName == "" { httputil.ReturnBcodeError(r, w, bcode.NewBadRequest("the field 'helm_app_name' is required")) return } diff --git a/api/handler/service.go b/api/handler/service.go index d13173a20..db1782908 100644 --- a/api/handler/service.go +++ b/api/handler/service.go @@ -40,7 +40,6 @@ import ( "github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/event" - "github.com/goodrain/rainbond/util/commonutil" "github.com/goodrain/rainbond/worker/client" "github.com/goodrain/rainbond/worker/discover/model" "github.com/goodrain/rainbond/worker/server" @@ -762,7 +761,7 @@ func (s *ServiceAction) ServiceCreate(sc *api_model.ServiceStruct) error { func (s *ServiceAction) openInnerPorts(componentID string, ports []dbmodel.TenantServicesPort) { // TODO: support open multiple ports in one task. for _, port := range ports { - if !commonutil.BoolValue(port.IsInnerService) { + if !port.IsOpen(){ continue } @@ -2062,17 +2061,6 @@ func (s *ServiceAction) isServiceClosed(serviceID string) error { return nil } -// DeleteComponentInBatch deletes components in batch. -func (s *ServiceAction) DeleteComponentInBatch(tx *gorm.DB, components []*dbmodel.TenantServices) error { - for _, cpt := range components { - err := s.deleteComponent(tx, cpt) - if err != nil { - return err - } - } - return nil -} - func (s *ServiceAction) deleteComponent(tx *gorm.DB, service *dbmodel.TenantServices) error { delService := service.ChangeDelete() delService.ID = 0 diff --git a/api/handler/service_handler.go b/api/handler/service_handler.go index bb7ffce26..716f63dfa 100644 --- a/api/handler/service_handler.go +++ b/api/handler/service_handler.go @@ -25,7 +25,6 @@ import ( dbmodel "github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/worker/discover/model" "github.com/goodrain/rainbond/worker/server/pb" - "github.com/jinzhu/gorm" ) //ServiceHandler service handler @@ -77,7 +76,6 @@ type ServiceHandler interface { GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError) GetServiceDeployInfo(tenantID, serviceID string) (*pb.DeployInfo, *util.APIHandleError) ListVersionInfo(serviceID string) (*api_model.BuildListRespVO, error) - DeleteComponentInBatch(tx *gorm.DB, components []*dbmodel.TenantServices) error AddAutoscalerRule(req *api_model.AutoscalerRuleReq) error UpdAutoscalerRule(req *api_model.AutoscalerRuleReq) error diff --git a/cmd/worker/option/option.go b/cmd/worker/option/option.go index 69957ca43..0c7fc752c 100644 --- a/cmd/worker/option/option.go +++ b/cmd/worker/option/option.go @@ -58,9 +58,10 @@ type Config struct { // Helm helm configuration. type Helm struct { - DataDir string - RepoFile string - RepoCache string + DataDir string + RepoFile string + RepoCache string + ChartCache string } //Worker worker server @@ -107,6 +108,7 @@ func (a *Worker) AddFlags(fs *pflag.FlagSet) { } a.Helm.RepoFile = path.Join(a.Helm.DataDir, "repo/repositories.yaml") a.Helm.RepoCache = path.Join(a.Helm.DataDir, "cache") + a.Helm.ChartCache = path.Join(a.Helm.DataDir, "chart") } //SetLog 设置log diff --git a/db/model/tenant.go b/db/model/tenant.go index 52b889ecf..57cc21599 100644 --- a/db/model/tenant.go +++ b/db/model/tenant.go @@ -23,6 +23,8 @@ import ( "os" "strings" "time" + + "github.com/goodrain/rainbond/util/commonutil" ) //Model 默认字段 @@ -324,6 +326,11 @@ func (t *TenantServicesPort) TableName() string { return "tenant_services_port" } +// IsOpen checks if the port is opened. +func (t *TenantServicesPort) IsOpen() bool { + return commonutil.BoolValue(t.IsOuterService) || commonutil.BoolValue(t.IsInnerService) +} + //TenantServiceLBMappingPort stream应用端口映射情况 type TenantServiceLBMappingPort struct { Model diff --git a/go.mod b/go.mod index 07e8b90d7..ca977164e 100644 --- a/go.mod +++ b/go.mod @@ -121,6 +121,7 @@ replace ( github.com/coreos/etcd => github.com/coreos/etcd v3.2.31+incompatible github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d github.com/docker/docker => github.com/docker/docker v17.12.0-ce-rc1.0.20200916142827-bd33bbf0497b+incompatible + github.com/godbus/dbus => github.com/godbus/dbus/v5 v5.0.4 google.golang.org/grpc => google.golang.org/grpc v1.29.0 k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.20.0 k8s.io/apimachinery => k8s.io/apimachinery v0.20.0 @@ -129,5 +130,4 @@ replace ( k8s.io/client-go => k8s.io/client-go v0.20.0 k8s.io/code-generator => k8s.io/code-generator v0.20.0 k8s.io/component-base => k8s.io/component-base v0.20.0 - github.com/godbus/dbus => github.com/godbus/dbus/v5 v5.0.4 ) diff --git a/localcheck.sh b/localcheck.sh new file mode 100755 index 000000000..174c62bf3 --- /dev/null +++ b/localcheck.sh @@ -0,0 +1,10 @@ +#!/bin/sh +TARGET_BRANCH=${TARGET_BRANCH:-'master'} + +CHANGED_MARKDOWN_FILES=$(git diff --name-only ${TARGET_BRANCH} | grep .go) +for file in ${CHANGED_MARKDOWN_FILES}; do + echo "golint ${file}" + golint -set_exit_status=true ${file} || exit 1 +done + +echo "code golint check success" \ No newline at end of file diff --git a/worker/master/helmapp/app.go b/worker/master/helmapp/app.go index 8525abde6..e57b732a4 100644 --- a/worker/master/helmapp/app.go +++ b/worker/master/helmapp/app.go @@ -68,7 +68,7 @@ func (a *App) Chart() string { } // NewApp creates a new app. -func NewApp(ctx context.Context, kubeClient clientset.Interface, rainbondClient versioned.Interface, helmApp *v1alpha1.HelmApp, repoFile, repoCache string) (*App, error) { +func NewApp(ctx context.Context, kubeClient clientset.Interface, rainbondClient versioned.Interface, helmApp *v1alpha1.HelmApp, repoFile, repoCache, chartCache string) (*App, error) { helmCmd, err := helm.NewHelm(helmApp.GetNamespace(), repoFile, repoCache) if err != nil { return nil, err @@ -93,7 +93,7 @@ func NewApp(ctx context.Context, kubeClient clientset.Interface, rainbondClient overrides: helmApp.Spec.Overrides, helmCmd: helmCmd, repo: repo, - chartDir: path.Join("/tmp/helm/chart", helmApp.Namespace, helmApp.Name, helmApp.Spec.Version), + chartDir: path.Join(chartCache, helmApp.Namespace, helmApp.Name, helmApp.Spec.Version), }, nil } diff --git a/worker/master/helmapp/controller.go b/worker/master/helmapp/controller.go index 1d5c123d6..09a63ea09 100644 --- a/worker/master/helmapp/controller.go +++ b/worker/master/helmapp/controller.go @@ -38,13 +38,13 @@ type Controller struct { // NewController creates a new helm app controller. func NewController(ctx context.Context, stopCh chan struct{}, kubeClient clientset.Interface, clientset versioned.Interface, resyncPeriod time.Duration, - repoFile, repoCache string) *Controller { + repoFile, repoCache, chartCache string) *Controller { workQueue := workqueue.New() finalizerQueue := workqueue.New() storer := NewStorer(clientset, resyncPeriod, workQueue, finalizerQueue) - controlLoop := NewControlLoop(ctx, kubeClient, clientset, storer, workQueue, repoFile, repoCache) - finalizer := NewFinalizer(ctx, kubeClient, clientset, finalizerQueue, repoFile, repoCache) + controlLoop := NewControlLoop(ctx, kubeClient, clientset, storer, workQueue, repoFile, repoCache, chartCache) + finalizer := NewFinalizer(ctx, kubeClient, clientset, finalizerQueue, repoFile, repoCache, chartCache) return &Controller{ storer: storer, diff --git a/worker/master/helmapp/controlloop.go b/worker/master/helmapp/controlloop.go index b28ad166f..ca843315a 100644 --- a/worker/master/helmapp/controlloop.go +++ b/worker/master/helmapp/controlloop.go @@ -52,6 +52,7 @@ type ControlLoop struct { repo *helm.Repo repoFile string repoCache string + chartCache string } // NewControlLoop - @@ -62,6 +63,7 @@ func NewControlLoop(ctx context.Context, workQueue workqueue.Interface, repoFile string, repoCache string, + chartCache string, ) *ControlLoop { repo := helm.NewRepo(repoFile, repoCache) return &ControlLoop{ @@ -74,6 +76,7 @@ func NewControlLoop(ctx context.Context, repo: repo, repoFile: repoFile, repoCache: repoCache, + chartCache: chartCache, } } @@ -124,7 +127,7 @@ func nameNamespace(key string) (string, string) { // Reconcile - func (c *ControlLoop) Reconcile(helmApp *v1alpha1.HelmApp) error { - app, err := NewApp(c.ctx, c.kubeClient, c.clientset, helmApp, c.repoFile, c.repoCache) + app, err := NewApp(c.ctx, c.kubeClient, c.clientset, helmApp, c.repoFile, c.repoCache, c.chartCache) if err != nil { return err } diff --git a/worker/master/helmapp/finilizer.go b/worker/master/helmapp/finilizer.go index b65c95c46..26396dd97 100644 --- a/worker/master/helmapp/finilizer.go +++ b/worker/master/helmapp/finilizer.go @@ -37,6 +37,7 @@ type Finalizer struct { queue workqueue.Interface repoFile string repoCache string + chartCache string } // NewFinalizer creates a new finalizer. @@ -46,6 +47,7 @@ func NewFinalizer(ctx context.Context, workQueue workqueue.Interface, repoFile string, repoCache string, + chartCache string, ) *Finalizer { return &Finalizer{ ctx: ctx, @@ -55,6 +57,7 @@ func NewFinalizer(ctx context.Context, queue: workQueue, repoFile: repoFile, repoCache: repoCache, + chartCache: chartCache, } } @@ -89,7 +92,7 @@ func (c *Finalizer) run(obj interface{}) error { logrus.Infof("start uninstall helm app: %s/%s", helmApp.Name, helmApp.Namespace) - app, err := NewApp(c.ctx, c.kubeClient, c.clientset, helmApp, c.repoFile, c.repoCache) + app, err := NewApp(c.ctx, c.kubeClient, c.clientset, helmApp, c.repoFile, c.repoCache, c.chartCache) if err != nil { return err } diff --git a/worker/master/helmapp/suite_test.go b/worker/master/helmapp/suite_test.go index 81b039c13..c33bed1fd 100644 --- a/worker/master/helmapp/suite_test.go +++ b/worker/master/helmapp/suite_test.go @@ -73,7 +73,7 @@ var _ = BeforeSuite(func() { rainbondClient = versioned.NewForConfigOrDie(restConfig) kubeClient = clientset.NewForConfigOrDie(restConfig) - ctrl := NewController(ctx, stopCh, kubeClient, rainbondClient, 5*time.Second, "/tmp/helm/repo/repositories.yaml", "/tmp/helm/cache") + ctrl := NewController(ctx, stopCh, kubeClient, rainbondClient, 5*time.Second, "/tmp/helm/repo/repositories.yaml", "/tmp/helm/cache", "/tmp/helm/chart") go ctrl.Start() // create namespace diff --git a/worker/master/master.go b/worker/master/master.go index c7a4e85e0..3ba5a564e 100644 --- a/worker/master/master.go +++ b/worker/master/master.go @@ -91,7 +91,7 @@ func NewMasterController(conf option.Config, store store.Storer, kubeClient kube }, serverVersion.GitVersion) stopCh := make(chan struct{}) - helmAppController := helmapp.NewController(ctx, stopCh, kubeClient, rainbondClient, 5*time.Second, conf.Helm.RepoFile, conf.Helm.RepoCache) + helmAppController := helmapp.NewController(ctx, stopCh, kubeClient, rainbondClient, 5*time.Second, conf.Helm.RepoFile, conf.Helm.RepoCache, conf.Helm.RepoCache) return &Controller{ conf: conf,