diff --git a/cmd/worker/option/option.go b/cmd/worker/option/option.go index 2d591b2476..850c146ad0 100644 --- a/cmd/worker/option/option.go +++ b/cmd/worker/option/option.go @@ -50,6 +50,7 @@ type Config struct { Listen string HostIP string ServerPort int + SharedStorageClass string KubeClient kubernetes.Interface LeaderElectionNamespace string LeaderElectionIdentity string @@ -106,6 +107,7 @@ func (a *Worker) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.RBDNamespace, "rbd-system-namespace", "rbd-system", "rbd components kubernetes namespace") fs.StringVar(&a.GrdataPVCName, "grdata-pvc-name", "rbd-cpt-grdata", "The name of grdata persistent volume claim") fs.StringVar(&a.Helm.DataDir, "/grdata/helm", "/grdata/helm", "The data directory of Helm.") + fs.StringVar(&a.SharedStorageClass, "shared-storageclass", "", "custom shared storage class.use the specified storageclass to create shared storage, if this parameter is not specified, it will use rainbondsssc by default") 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") diff --git a/cmd/worker/server/server.go b/cmd/worker/server/server.go index fabb39a215..fafb2c9f23 100644 --- a/cmd/worker/server/server.go +++ b/cmd/worker/server/server.go @@ -112,7 +112,7 @@ func Run(s *option.Worker) error { } //step 5: create controller manager - controllerManager := controller.NewManager(cachestore, clientset, runtimeClient) + controllerManager := controller.NewManager(s.Config, cachestore, clientset, runtimeClient) defer controllerManager.Stop() //step 6 : start runtime master diff --git a/worker/appm/controller/controller.go b/worker/appm/controller/controller.go index 001a8fb331..f7febba02d 100644 --- a/worker/appm/controller/controller.go +++ b/worker/appm/controller/controller.go @@ -21,6 +21,7 @@ package controller import ( "context" "fmt" + "github.com/goodrain/rainbond/cmd/worker/option" "sync" "github.com/goodrain/rainbond/util" @@ -74,10 +75,11 @@ type Manager struct { controllers map[string]Controller store store.Storer lock sync.Mutex + config option.Config } //NewManager new manager -func NewManager(store store.Storer, client kubernetes.Interface, runtimeClient client.Client) *Manager { +func NewManager(config option.Config, store store.Storer, client kubernetes.Interface, runtimeClient client.Client) *Manager { ctx, cancel := context.WithCancel(context.Background()) return &Manager{ ctx: ctx, @@ -87,6 +89,7 @@ func NewManager(store store.Storer, client kubernetes.Interface, runtimeClient c runtimeClient: runtimeClient, controllers: make(map[string]Controller), store: store, + config: config, } } @@ -165,6 +168,7 @@ func (m *Manager) StartController(controllerType TypeController, apps ...v1.AppS manager: m, stopChan: make(chan struct{}), ctx: context.Background(), + cfg: m.config, } case TypeApplyRuleController: controller = &applyRuleController{ diff --git a/worker/appm/controller/restart.go b/worker/appm/controller/restart.go index f2e9eb0fe6..b4327cf02a 100644 --- a/worker/appm/controller/restart.go +++ b/worker/appm/controller/restart.go @@ -21,19 +21,20 @@ package controller import ( "context" "fmt" - "sync" - "time" - + "github.com/goodrain/rainbond/cmd/worker/option" "github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/event" "github.com/goodrain/rainbond/util" "github.com/goodrain/rainbond/worker/appm/conversion" v1 "github.com/goodrain/rainbond/worker/appm/types/v1" "github.com/sirupsen/logrus" + "sync" + "time" ) type restartController struct { stopChan chan struct{} + cfg option.Config controllerID string appService []v1.AppService manager *Manager @@ -86,7 +87,7 @@ func (s *restartController) restartOne(app v1.AppService) error { ctx: s.ctx, controllerID: s.controllerID, } - newAppService, err := conversion.InitAppService(false, db.GetManager(), app.ServiceID, app.ExtensionSet) + newAppService, err := conversion.InitAppService(s.cfg.SharedStorageClass, false, db.GetManager(), app.ServiceID, app.ExtensionSet) if err != nil { logrus.Errorf("Application model init create failure:%s", err.Error()) app.Logger.Error(util.Translation("(restart)Application model init create failure"), event.GetCallbackLoggerOption()) diff --git a/worker/appm/conversion/conversion.go b/worker/appm/conversion/conversion.go index e23f1f8abe..80e71b27a6 100644 --- a/worker/appm/conversion/conversion.go +++ b/worker/appm/conversion/conversion.go @@ -63,17 +63,18 @@ func RegistConversion(name string, fun Conversion) { } //InitAppService init a app service -func InitAppService(dryRun bool, dbmanager db.Manager, serviceID string, configs map[string]string, enableConversionList ...string) (*v1.AppService, error) { +func InitAppService(sharedStorageClass string, dryRun bool, dbmanager db.Manager, serviceID string, configs map[string]string, enableConversionList ...string) (*v1.AppService, error) { if configs == nil { configs = make(map[string]string) } appService := &v1.AppService{ AppServiceBase: v1.AppServiceBase{ - ServiceID: serviceID, - ExtensionSet: configs, - GovernanceMode: model.GovernanceModeBuildInServiceMesh, - DryRun: dryRun, + ServiceID: serviceID, + ExtensionSet: configs, + GovernanceMode: model.GovernanceModeBuildInServiceMesh, + DryRun: dryRun, + SharedStorageClass: sharedStorageClass, }, UpgradePatch: make(map[string][]byte, 2), } diff --git a/worker/appm/types/v1/v1.go b/worker/appm/types/v1/v1.go index 1b5ec00d82..243760cf5e 100644 --- a/worker/appm/types/v1/v1.go +++ b/worker/appm/types/v1/v1.go @@ -114,12 +114,13 @@ type AppServiceBase struct { IsWindowsService bool CreaterID string //depend all service id - Dependces []string - ExtensionSet map[string]string - GovernanceMode string - K8sApp string - K8sComponentName string - DryRun bool + Dependces []string + ExtensionSet map[string]string + GovernanceMode string + K8sApp string + K8sComponentName string + DryRun bool + SharedStorageClass string } //GetComponentDefinitionName get component definition name by component kind diff --git a/worker/appm/volume/other.go b/worker/appm/volume/other.go index 7580a7ad82..598a6642dc 100644 --- a/worker/appm/volume/other.go +++ b/worker/appm/volume/other.go @@ -20,8 +20,8 @@ package volume import ( "fmt" - "github.com/goodrain/rainbond/db" + dbmodel "github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/node/nodem/client" workerutil "github.com/goodrain/rainbond/worker/util" "github.com/sirupsen/logrus" @@ -35,6 +35,11 @@ type OtherVolume struct { // CreateVolume ceph rbd volume create volume func (v *OtherVolume) CreateVolume(define *Define) error { + var shareFile bool + if v.svm.VolumeType == dbmodel.ShareFileVolumeType.String() { + v.svm.VolumeType = v.as.SharedStorageClass + shareFile = true + } volumeType, err := db.GetManager().VolumeTypeDao().GetVolumeTypeByType(v.svm.VolumeType) if err != nil { logrus.Errorf("get volume type by type error: %s", err.Error()) @@ -44,6 +49,7 @@ func (v *OtherVolume) CreateVolume(define *Define) error { logrus.Errorf("validate volume capacity[%v] error: %s", v.svm.VolumeCapacity, err.Error()) return err } + v.svm.VolumeProviderName = volumeType.Provisioner volumeMountName := fmt.Sprintf("manual%d", v.svm.ID) volumeMountPath := v.svm.VolumePath volumeReadOnly := v.svm.IsReadOnly @@ -59,14 +65,18 @@ func (v *OtherVolume) CreateVolume(define *Define) error { return "linux" }(), } - v.as.SetClaim(claim) // store claim to appService - statefulset := v.as.GetStatefulSet() //有状态组件 + v.as.SetClaim(claim) // store claim to appService vo := corev1.Volume{Name: volumeMountName} vo.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{ClaimName: claim.GetName(), ReadOnly: volumeReadOnly} define.volumes = append(define.volumes, vo) - if statefulset != nil { - statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, *claim) - logrus.Debugf("stateset.Spec.VolumeClaimTemplates: %+v", statefulset.Spec.VolumeClaimTemplates) + if shareFile { + v.as.SetClaimManually(claim) + } else { + statefulset := v.as.GetStatefulSet() //有状态组件 + if statefulset != nil { + statefulset.Spec.VolumeClaimTemplates = append(statefulset.Spec.VolumeClaimTemplates, *claim) + logrus.Debugf("stateset.Spec.VolumeClaimTemplates: %+v", statefulset.Spec.VolumeClaimTemplates) + } } vm := corev1.VolumeMount{ diff --git a/worker/appm/volume/volume.go b/worker/appm/volume/volume.go index c756c8d24d..97490ada37 100644 --- a/worker/appm/volume/volume.go +++ b/worker/appm/volume/volume.go @@ -58,6 +58,7 @@ func NewVolumeManager(as *v1.AppService, } if serviceMountR != nil { volumeType = serviceMountR.VolumeType + as.SharedStorageClass = "" } if volumeType == "" { logrus.Warn("unknown volume Type, can't create volume") @@ -66,6 +67,10 @@ func NewVolumeManager(as *v1.AppService, switch volumeType { case dbmodel.ShareFileVolumeType.String(): v = new(ShareFileVolume) + css := as.SharedStorageClass + if css != "" { + v = new(OtherVolume) + } case dbmodel.ConfigFileVolumeType.String(): v = &ConfigFileVolume{envs: envs, envVarSecrets: envVarSecrets} case dbmodel.MemoryFSVolumeType.String(): diff --git a/worker/handle/manager.go b/worker/handle/manager.go index d7d9f0a9fb..264d9ddeb0 100644 --- a/worker/handle/manager.go +++ b/worker/handle/manager.go @@ -167,7 +167,7 @@ func (m *Manager) startExec(task *model.Task) error { event.GetManager().ReleaseLogger(logger) return nil } - newAppService, err := conversion.InitAppService(false, m.dbmanager, body.ServiceID, body.Configs) + newAppService, err := conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, body.Configs) if err != nil { logrus.Errorf("component init create failure:%s", err.Error()) logger.Error("component init create failure", event.GetCallbackLoggerOption()) @@ -330,7 +330,7 @@ func (m *Manager) verticalScalingExec(task *model.Task) error { appService.ContainerMemory = service.ContainerMemory appService.ContainerGPU = service.ContainerGPU appService.Logger = logger - newAppService, err := conversion.InitAppService(false, m.dbmanager, body.ServiceID, nil) + newAppService, err := conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, nil) if err != nil { logrus.Errorf("component init create failure:%s", err.Error()) logger.Error("component init create failure", event.GetCallbackLoggerOption()) @@ -357,7 +357,7 @@ func (m *Manager) rollingUpgradeExec(task *model.Task) error { return fmt.Errorf("rolling_upgrade body convert to taskbody error") } logger := event.GetManager().GetLogger(body.EventID) - newAppService, err := conversion.InitAppService(body.DryRun, m.dbmanager, body.ServiceID, body.Configs) + newAppService, err := conversion.InitAppService(m.cfg.SharedStorageClass, body.DryRun, m.dbmanager, body.ServiceID, body.Configs) if err != nil { logrus.Errorf("component init create failure:%s", err.Error()) logger.Error("component init create failure", event.GetCallbackLoggerOption()) @@ -429,10 +429,10 @@ func (m *Manager) applyRuleExec(task *model.Task) error { } var newAppService *v1.AppService if svc.Kind == dbmodel.ServiceKindThirdParty.String() { - newAppService, err = conversion.InitAppService(false, m.dbmanager, body.ServiceID, nil, + newAppService, err = conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, nil, "ServiceSource", "TenantServiceBase", "TenantServiceRegist") } else { - newAppService, err = conversion.InitAppService(false, m.dbmanager, body.ServiceID, nil) + newAppService, err = conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, nil) } if err != nil { logrus.Errorf("component init create failure:%s", err.Error()) @@ -465,7 +465,7 @@ func (m *Manager) applyPluginConfig(task *model.Task) error { logrus.Debugf("service is closed,no need handle") return nil } - newApp, err := conversion.InitAppService(false, m.dbmanager, body.ServiceID, nil, "ServiceSource", "TenantServiceBase", "TenantServicePlugin") + newApp, err := conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, nil, "ServiceSource", "TenantServiceBase", "TenantServicePlugin") if err != nil { logrus.Errorf("component apply plugin config controller failure:%s", err.Error()) return err @@ -558,7 +558,7 @@ func (m *Manager) ExecRefreshHPATask(task *model.Task) error { return nil } - newAppService, err := conversion.InitAppService(false, m.dbmanager, body.ServiceID, nil) + newAppService, err := conversion.InitAppService(m.cfg.SharedStorageClass, false, m.dbmanager, body.ServiceID, nil) if err != nil { logrus.Errorf("component init create failure:%s", err.Error()) logger.Error("component init create failure", event.GetCallbackLoggerOption())