Merge branch 'V5.2' of https://github.com/goodrain/rainbond into V5.2

This commit is contained in:
barnettZQG 2020-09-17 15:06:59 +08:00
commit 57a2803f12
7 changed files with 71 additions and 7 deletions

View File

@ -141,7 +141,7 @@ func upgradeImages(ctx *cli.Context) error {
if util.StringArrayContains(ctx.StringSlice("s"), service.Name) &&
service.Start != "" && !service.OnlyHealthCheck {
par := parser.CreateDockerRunOrImageParse("", "", service.Start, nil, event.GetTestLogger())
par.ParseDockerun(strings.Split(service.Start, " "))
par.ParseDockerun(service.Start)
image := par.GetImage()
if image.Name == "" {
continue

View File

@ -119,7 +119,8 @@ func convert(in map[string]string) []*Rewrite {
}
rewrite := m[sli[0]]
if rewrite == nil {
m[k] = &Rewrite{}
rewrite = &Rewrite{}
m[sli[0]] = rewrite
}
switch sli[1] {
case "regex":

View File

@ -27,6 +27,7 @@ import (
"github.com/goodrain/rainbond/discover/config"
"github.com/goodrain/rainbond/monitor/prometheus"
"github.com/goodrain/rainbond/monitor/utils"
"github.com/goodrain/rainbond/util"
"github.com/goodrain/rainbond/util/watch"
"github.com/prometheus/common/model"
"github.com/tidwall/gjson"
@ -68,6 +69,9 @@ func (c *Cadvisor) Name() string {
}
func (c *Cadvisor) toScrape() *prometheus.ScrapeConfig {
apiServerHost := util.Getenv("KUBERNETES_SERVICE_HOST", "kubernetes.default.svc")
apiServerPort := util.Getenv("KUBERNETES_SERVICE_PORT", "443")
return &prometheus.ScrapeConfig{
JobName: c.Name(),
ScrapeInterval: model.Duration(15 * time.Second),
@ -90,7 +94,7 @@ func (c *Cadvisor) toScrape() *prometheus.ScrapeConfig {
RelabelConfigs: []*prometheus.RelabelConfig{
{
TargetLabel: "__address__",
Replacement: "kubernetes.default.svc:443",
Replacement: apiServerHost + ":" + apiServerPort,
},
{
SourceLabels: []model.LabelName{

View File

@ -529,7 +529,7 @@ func (m *ManagerService) ListServiceImages() []string {
}
par := parser.CreateDockerRunOrImageParse("", "", svc.Start, nil, event.GetTestLogger())
par.ParseDockerun(strings.Split(svc.Start, " "))
par.ParseDockerun(svc.Start)
logrus.Debugf("detect image: %s", par.GetImage().String())
if par.GetImage().String() == "" {
continue

View File

@ -790,3 +790,12 @@ func getVariableKey(source string) (key, value string) {
}
return k, ""
}
// Getenv returns env by key or default value.
func Getenv(key, def string) string {
value := os.Getenv(key)
if value == "" {
return def
}
return value
}

View File

@ -27,6 +27,7 @@ import (
eventutil "github.com/goodrain/rainbond/eventlog/util"
"github.com/goodrain/rainbond/worker/discover/model"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
@ -79,9 +80,7 @@ func (g *GarbageCollector) DelVolumeData(serviceGCReq model.ServiceGCTaskBody) {
func (g *GarbageCollector) DelPvPvcByServiceID(serviceGCReq model.ServiceGCTaskBody) {
logrus.Infof("service_id: %s", serviceGCReq.ServiceID)
deleteOpts := &metav1.DeleteOptions{}
listOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("service_id=%s", serviceGCReq.ServiceID),
}
listOpts := g.listOptionsServiceID(serviceGCReq.ServiceID)
if err := g.clientset.CoreV1().PersistentVolumes().DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("service id: %s; delete a collection fo PV: %v", serviceGCReq.ServiceID, err)
}
@ -90,3 +89,53 @@ func (g *GarbageCollector) DelPvPvcByServiceID(serviceGCReq model.ServiceGCTaskB
logrus.Warningf("service id: %s; delete a collection fo PVC: %v", serviceGCReq.ServiceID, err)
}
}
// DelKubernetesObjects deletes all kubernetes objects.
func (g *GarbageCollector) DelKubernetesObjects(serviceGCReq model.ServiceGCTaskBody) {
deleteOpts := &metav1.DeleteOptions{}
listOpts := g.listOptionsServiceID(serviceGCReq.ServiceID)
if err := g.clientset.AppsV1().Deployments(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete deployments(%s): %v", serviceGCReq.ServiceID, err)
}
if err := g.clientset.AppsV1().StatefulSets(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete statefulsets(%s): %v", serviceGCReq.ServiceID, err)
}
if err := g.clientset.ExtensionsV1beta1().Ingresses(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete ingresses(%s): %v", serviceGCReq.ServiceID, err)
}
if err := g.clientset.CoreV1().Secrets(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete secrets(%s): %v", serviceGCReq.ServiceID, err)
}
if err := g.clientset.CoreV1().ConfigMaps(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete configmaps(%s): %v", serviceGCReq.ServiceID, err)
}
if err := g.clientset.AutoscalingV2beta2().HorizontalPodAutoscalers(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete hpas(%s): %v", serviceGCReq.ServiceID, err)
}
// kubernetes does not support api for deleting collection of service
// read: https://github.com/kubernetes/kubernetes/issues/68468#issuecomment-419981870
serviceList, err := g.clientset.CoreV1().Services(serviceGCReq.TenantID).List(listOpts)
if err != nil {
logrus.Warningf("[DelKubernetesObjects] list services(%s): %v", serviceGCReq.ServiceID, err)
} else {
for _, svc := range serviceList.Items {
if err := g.clientset.CoreV1().Services(serviceGCReq.TenantID).Delete(svc.Name, deleteOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete service(%s): %v", svc.GetName(), err)
}
}
}
// delete endpoints after deleting services
if err := g.clientset.CoreV1().Endpoints(serviceGCReq.TenantID).DeleteCollection(deleteOpts, listOpts); err != nil {
logrus.Warningf("[DelKubernetesObjects] delete endpoints(%s): %v", serviceGCReq.ServiceID, err)
}
}
func (g *GarbageCollector) listOptionsServiceID(serviceID string) metav1.ListOptions {
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{
"creator": "Rainbond",
"service_id": serviceID,
}}
return metav1.ListOptions{
LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
}
}

View File

@ -492,6 +492,7 @@ func (m *Manager) ExecServiceGCTask(task *model.Task) error {
m.garbageCollector.DelLogFile(serviceGCReq)
m.garbageCollector.DelPvPvcByServiceID(serviceGCReq)
m.garbageCollector.DelVolumeData(serviceGCReq)
m.garbageCollector.DelKubernetesObjects(serviceGCReq)
return nil
}