Rainbond/worker/master/master.go

302 lines
10 KiB
Go
Raw Normal View History

// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package master
import (
"context"
"fmt"
"strings"
"time"
2019-11-25 16:14:32 +08:00
2019-08-29 14:15:46 +08:00
"github.com/goodrain/rainbond/cmd/worker/option"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/pkg/common"
2021-05-07 21:41:09 +08:00
"github.com/goodrain/rainbond/pkg/generated/clientset/versioned"
"github.com/goodrain/rainbond/util/leader"
2019-08-29 14:15:46 +08:00
"github.com/goodrain/rainbond/worker/appm/store"
"github.com/goodrain/rainbond/worker/master/controller/helmapp"
"github.com/goodrain/rainbond/worker/master/controller/thirdcomponent"
2019-09-01 15:44:35 +08:00
"github.com/goodrain/rainbond/worker/master/podevent"
2019-08-29 14:15:46 +08:00
"github.com/goodrain/rainbond/worker/master/volumes/provider"
2018-11-30 22:20:48 +08:00
"github.com/goodrain/rainbond/worker/master/volumes/provider/lib/controller"
2019-08-29 14:15:46 +08:00
"github.com/goodrain/rainbond/worker/master/volumes/statistical"
2020-04-16 16:20:40 +08:00
"github.com/goodrain/rainbond/worker/master/volumes/sync"
2021-05-07 21:41:09 +08:00
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
2021-05-19 15:13:42 +08:00
"k8s.io/apimachinery/pkg/version"
2021-05-07 21:41:09 +08:00
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
)
2018-11-20 17:40:43 +08:00
//Controller app runtime master controller
type Controller struct {
ctx context.Context
cancel context.CancelFunc
conf option.Config
store store.Storer
dbmanager db.Manager
memoryUse *prometheus.GaugeVec
cpuUse *prometheus.GaugeVec
fsUse *prometheus.GaugeVec
diskCache *statistical.DiskCache
namespaceMemRequest *prometheus.GaugeVec
namespaceMemLimit *prometheus.GaugeVec
namespaceCPURequest *prometheus.GaugeVec
namespaceCPULimit *prometheus.GaugeVec
pc *controller.ProvisionController
2021-05-07 21:41:09 +08:00
helmAppController *helmapp.Controller
isLeader bool
2019-08-29 14:15:46 +08:00
2021-05-18 14:53:21 +08:00
kubeClient kubernetes.Interface
2020-04-16 16:20:40 +08:00
stopCh chan struct{}
podEvent *podevent.PodEvent
volumeTypeEvent *sync.VolumeTypeEvent
2021-05-19 15:13:42 +08:00
version *version.Info
rainbondsssc controller.Provisioner
rainbondsslc controller.Provisioner
mgr ctrl.Manager
2018-11-20 17:40:43 +08:00
}
//NewMasterController new master controller
func NewMasterController(conf option.Config, store store.Storer, kubeClient kubernetes.Interface, rainbondClient versioned.Interface, restConfig *rest.Config) (*Controller, error) {
ctx, cancel := context.WithCancel(context.Background())
2021-05-18 14:53:21 +08:00
2018-11-29 18:22:17 +08:00
// The controller needs to know what the server version is because out-of-tree
// provisioners aren't officially supported until 1.5
2021-05-18 14:53:21 +08:00
serverVersion, err := kubeClient.Discovery().ServerVersion()
2018-11-29 18:22:17 +08:00
if err != nil {
logrus.Errorf("Error getting server version: %v", err)
cancel()
return nil, err
}
// Create the provisioner: it implements the Provisioner interface expected by
// the controller
2018-11-30 22:20:48 +08:00
//statefulset share controller
rainbondssscProvisioner := provider.NewRainbondssscProvisioner()
//statefulset local controller
2021-05-18 14:53:21 +08:00
rainbondsslcProvisioner := provider.NewRainbondsslcProvisioner(kubeClient, store)
2018-11-29 18:22:17 +08:00
// Start the provision controller which will dynamically provision hostPath
// PVs
2021-05-18 14:53:21 +08:00
pc := controller.NewProvisionController(kubeClient, &conf, map[string]controller.Provisioner{
2018-11-30 22:20:48 +08:00
rainbondssscProvisioner.Name(): rainbondssscProvisioner,
rainbondsslcProvisioner.Name(): rainbondsslcProvisioner,
}, serverVersion.GitVersion)
2019-09-01 15:44:35 +08:00
stopCh := make(chan struct{})
2019-11-15 16:12:41 +08:00
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: common.Scheme,
LeaderElection: false,
LeaderElectionID: "controllers.rainbond.io",
})
if err != nil {
cancel()
return nil, err
}
thirdcomponent.Setup(ctx, mgr)
2021-06-23 17:23:46 +08:00
helmAppController := helmapp.NewController(ctx, stopCh, kubeClient, rainbondClient,
store.Informer().HelmApp, store.Lister().HelmApp, conf.Helm.RepoFile, conf.Helm.RepoCache, conf.Helm.RepoCache)
2021-05-07 21:41:09 +08:00
2018-11-20 17:40:43 +08:00
return &Controller{
2021-05-07 21:41:09 +08:00
conf: conf,
pc: pc,
helmAppController: helmAppController,
store: store,
stopCh: stopCh,
cancel: cancel,
ctx: ctx,
dbmanager: db.GetManager(),
mgr: mgr,
memoryUse: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app_resource",
Name: "appmemory",
Help: "tenant service memory request.",
2020-09-24 21:09:00 +08:00
}, []string{"tenant_id", "app_id", "service_id", "service_status"}),
cpuUse: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app_resource",
Name: "appcpu",
Help: "tenant service cpu request.",
2020-09-24 21:09:00 +08:00
}, []string{"tenant_id", "app_id", "service_id", "service_status"}),
fsUse: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app_resource",
Name: "appfs",
Help: "tenant service fs used.",
2020-09-24 21:09:00 +08:00
}, []string{"tenant_id", "app_id", "service_id", "volume_type"}),
namespaceMemRequest: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "namespace_resource",
Name: "memory_request",
Help: "total memory request in namespace",
}, []string{"namespace"}),
namespaceMemLimit: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "namespace_resource",
Name: "memory_limit",
Help: "total memory limit in namespace",
}, []string{"namespace"}),
namespaceCPURequest: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "namespace_resource",
Name: "cpu_request",
Help: "total cpu request in namespace",
}, []string{"namespace"}),
namespaceCPULimit: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "namespace_resource",
Name: "cpu_limit",
Help: "total cpu limit in namespace",
}, []string{"namespace"}),
2020-04-16 16:20:40 +08:00
diskCache: statistical.CreatDiskCache(ctx),
podEvent: podevent.New(conf.KubeClient, stopCh),
volumeTypeEvent: sync.New(stopCh),
2021-05-18 14:53:21 +08:00
kubeClient: kubeClient,
2021-05-19 15:13:42 +08:00
rainbondsssc: rainbondssscProvisioner,
rainbondsslc: rainbondsslcProvisioner,
version: serverVersion,
2018-11-29 18:22:17 +08:00
}, nil
}
//IsLeader is leader
func (m *Controller) IsLeader() bool {
return m.isLeader
}
//Start start
2018-11-20 17:40:43 +08:00
func (m *Controller) Start() error {
logrus.Debug("master controller starting")
2020-02-06 11:45:44 +08:00
start := func(ctx context.Context) {
2021-05-19 15:13:42 +08:00
pc := controller.NewProvisionController(m.kubeClient, &m.conf, map[string]controller.Provisioner{
m.rainbondsslc.Name(): m.rainbondsslc,
m.rainbondsssc.Name(): m.rainbondsssc,
}, m.version.GitVersion)
m.isLeader = true
defer func() {
m.isLeader = false
}()
2018-11-29 21:48:36 +08:00
go m.diskCache.Start()
defer m.diskCache.Stop()
2021-05-19 15:13:42 +08:00
go pc.Run(ctx)
2019-09-04 14:50:34 +08:00
m.store.RegistPodUpdateListener("podEvent", m.podEvent.GetChan())
defer m.store.UnRegistPodUpdateListener("podEvent")
2019-09-01 15:44:35 +08:00
go m.podEvent.Handle()
2020-04-16 16:20:40 +08:00
m.store.RegisterVolumeTypeListener("volumeTypeEvent", m.volumeTypeEvent.GetChan())
defer m.store.UnRegisterVolumeTypeListener("volumeTypeEvent")
go m.volumeTypeEvent.Handle()
2021-05-07 21:41:09 +08:00
// helm app controller
go m.helmAppController.Start()
2021-05-12 10:49:26 +08:00
defer m.helmAppController.Stop()
// start controller
stopchan := make(chan struct{})
go m.mgr.Start(stopchan)
defer func() { stopchan <- struct{}{} }()
2021-05-07 21:41:09 +08:00
2019-11-25 16:14:32 +08:00
select {
case <-ctx.Done():
case <-m.ctx.Done():
}
}
// Leader election was requested.
if m.conf.LeaderElectionNamespace == "" {
return fmt.Errorf("-leader-election-namespace must not be empty")
}
if m.conf.LeaderElectionIdentity == "" {
m.conf.LeaderElectionIdentity = m.conf.NodeName
}
if m.conf.LeaderElectionIdentity == "" {
return fmt.Errorf("-leader-election-identity must not be empty")
}
// Name of config map with leader election lock
lockName := "rainbond-appruntime-worker-leader"
2021-05-19 15:13:42 +08:00
// Become leader again on stop leading.
leaderCh := make(chan struct{}, 1)
go func() {
for {
select {
case <-m.ctx.Done():
return
case <-leaderCh:
logrus.Info("run as leader")
ctx, cancel := context.WithCancel(m.ctx)
defer cancel()
leader.RunAsLeader(ctx, m.kubeClient, m.conf.LeaderElectionNamespace, m.conf.LeaderElectionIdentity, lockName, start, func() {
leaderCh <- struct{}{}
logrus.Info("restart leader")
})
}
}
}()
leaderCh <- struct{}{}
2019-08-29 14:15:46 +08:00
return nil
}
//Stop stop
func (m *Controller) Stop() {
2019-08-29 14:15:46 +08:00
close(m.stopCh)
}
//Scrape scrape app runtime
func (m *Controller) Scrape(ch chan<- prometheus.Metric, scrapeDurationDesc *prometheus.Desc) {
if !m.isLeader {
return
}
scrapeTime := time.Now()
services := m.store.GetAllAppServices()
status := m.store.GetNeedBillingStatus(nil)
//获取内存使用情况
for _, service := range services {
if _, ok := status[service.ServiceID]; ok {
2020-09-24 21:09:00 +08:00
m.memoryUse.WithLabelValues(service.TenantID, service.AppID, service.ServiceID, "running").Set(float64(service.GetMemoryRequest()))
m.cpuUse.WithLabelValues(service.TenantID, service.AppID, service.ServiceID, "running").Set(float64(service.GetMemoryRequest()))
}
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), "collect.memory")
scrapeTime = time.Now()
diskcache := m.diskCache.Get()
for k, v := range diskcache {
key := strings.Split(k, "_")
2020-09-24 21:09:00 +08:00
if len(key) == 3 {
m.fsUse.WithLabelValues(key[2], key[1], key[0], string(model.ShareFileVolumeType)).Set(v)
}
}
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(scrapeTime).Seconds(), "collect.fs")
resources := m.store.GetTenantResourceList()
for _, re := range resources {
m.namespaceMemLimit.WithLabelValues(re.Namespace).Set(float64(re.MemoryLimit / 1024 / 1024))
m.namespaceCPULimit.WithLabelValues(re.Namespace).Set(float64(re.CPULimit))
m.namespaceMemRequest.WithLabelValues(re.Namespace).Set(float64(re.MemoryRequest / 1024 / 1024))
m.namespaceCPURequest.WithLabelValues(re.Namespace).Set(float64(re.CPURequest))
}
m.fsUse.Collect(ch)
m.memoryUse.Collect(ch)
m.cpuUse.Collect(ch)
m.namespaceMemLimit.Collect(ch)
m.namespaceCPULimit.Collect(ch)
m.namespaceMemRequest.Collect(ch)
m.namespaceCPURequest.Collect(ch)
logrus.Infof("success collect worker master metric")
}