Rainbond/worker/appm/conversion/plugin.go

479 lines
16 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 conversion
import (
2019-02-25 20:04:48 +08:00
"encoding/json"
"fmt"
"os"
2019-12-11 21:37:43 +08:00
"strconv"
"strings"
2019-12-11 21:37:43 +08:00
"github.com/jinzhu/gorm"
2020-10-24 17:07:54 +08:00
"github.com/sirupsen/logrus"
2019-12-11 21:37:43 +08:00
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-02-25 20:04:48 +08:00
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
typesv1 "github.com/goodrain/rainbond/worker/appm/types/v1"
)
//TenantServicePlugin conv service all plugin
func TenantServicePlugin(as *typesv1.AppService, dbmanager db.Manager) error {
initContainers, preContainers, postContainers, err := conversionServicePlugin(as, dbmanager)
if err != nil {
logrus.Errorf("create plugin containers for component %s failure: %s", as.ServiceID, err.Error())
return err
}
podtemplate := as.GetPodTemplate()
2018-12-04 23:15:02 +08:00
if podtemplate != nil {
if len(preContainers) > 0 {
podtemplate.Spec.Containers = append(preContainers, podtemplate.Spec.Containers...)
}
if len(postContainers) > 0 {
podtemplate.Spec.Containers = append(podtemplate.Spec.Containers, postContainers...)
}
2018-12-04 23:15:02 +08:00
podtemplate.Spec.InitContainers = initContainers
return nil
}
return fmt.Errorf("pod templete is nil before define plugin")
}
func conversionServicePlugin(as *typesv1.AppService, dbmanager db.Manager) ([]v1.Container, []v1.Container, []v1.Container, error) {
var precontainers, postcontainers []v1.Container
var initContainers []v1.Container
appPlugins, err := dbmanager.TenantServicePluginRelationDao().GetALLRelationByServiceID(as.ServiceID)
if err != nil && err.Error() != gorm.ErrRecordNotFound.Error() {
2020-05-19 18:59:21 +08:00
return nil, nil, nil, fmt.Errorf("find plugins error. %v", err.Error())
}
if len(appPlugins) == 0 && !as.NeedProxy {
2020-05-19 18:59:21 +08:00
return nil, nil, nil, nil
}
netPlugin := false
2019-02-28 14:52:39 +08:00
var meshPluginID string
var mainContainer v1.Container
if as.GetPodTemplate() != nil && len(as.GetPodTemplate().Spec.Containers) > 0 {
mainContainer = as.GetPodTemplate().Spec.Containers[0]
}
2019-06-14 15:19:53 +08:00
var inBoundPlugin *model.TenantServicePluginRelation
for _, pluginR := range appPlugins {
//if plugin not enable,ignore it
if !pluginR.Switch {
logrus.Debugf("plugin %s is disable in component %s", pluginR.PluginID, as.ServiceID)
continue
}
versionInfo, err := dbmanager.TenantPluginBuildVersionDao().GetLastBuildVersionByVersionID(pluginR.PluginID, pluginR.VersionID)
if err != nil {
logrus.Errorf("do not found available plugin versions %s", pluginR.PluginID)
continue
}
2018-12-05 17:54:54 +08:00
podTmpl := as.GetPodTemplate()
if podTmpl == nil {
logrus.Warnf("Can't not get pod for plugin(plugin_id=%s)", pluginR.PluginID)
continue
}
2019-02-28 14:52:39 +08:00
envs, err := createPluginEnvs(pluginR.PluginID, as.TenantID, as.ServiceAlias, mainContainer.Env, pluginR.VersionID, as.ServiceID, dbmanager)
if err != nil {
2020-05-19 18:59:21 +08:00
return nil, nil, nil, err
}
2020-10-24 17:07:54 +08:00
var envFromSecrets []corev1.EnvFromSource
envVarSecrets := as.GetEnvVarSecrets(true)
for _, secret := range envVarSecrets {
envFromSecrets = append(envFromSecrets, corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: secret.Name,
},
},
})
}
args, err := createPluginArgs(versionInfo.ContainerCMD, *envs)
if err != nil {
2020-05-19 18:59:21 +08:00
return nil, nil, nil, err
}
pc := v1.Container{
Name: "plugin-" + pluginR.PluginID,
Image: versionInfo.BuildLocalImage,
Env: *envs,
2020-10-24 17:07:54 +08:00
EnvFrom: envFromSecrets,
Resources: createPluginResources(pluginR.ContainerMemory, pluginR.ContainerCPU),
TerminationMessagePath: "",
Args: args,
2019-02-28 14:52:39 +08:00
VolumeMounts: mainContainer.VolumeMounts,
}
pluginModel, err := getPluginModel(pluginR.PluginID, as.TenantID, dbmanager)
if err != nil {
2020-05-19 18:59:21 +08:00
return nil, nil, nil, fmt.Errorf("get plugin model info failure %s", err.Error())
}
var preconatiner = false
2019-06-14 15:19:53 +08:00
if pluginModel == model.InBoundAndOutBoundNetPlugin || pluginModel == model.InBoundNetPlugin {
inBoundPlugin = pluginR
preconatiner = true
2019-06-14 15:19:53 +08:00
}
if pluginModel == model.OutBoundNetPlugin || pluginModel == model.InBoundAndOutBoundNetPlugin {
netPlugin = true
2019-02-28 14:52:39 +08:00
meshPluginID = pluginR.PluginID
preconatiner = true
}
if netPlugin {
config, err := dbmanager.TenantPluginVersionConfigDao().GetPluginConfig(as.ServiceID, pluginR.PluginID)
if err != nil && err != gorm.ErrRecordNotFound {
logrus.Errorf("get service plugin config from db failure %s", err.Error())
}
if config != nil {
var resourceConfig api_model.ResourceSpec
if err := json.Unmarshal([]byte(config.ConfigStr), &resourceConfig); err != nil {
logrus.Warningf("load mesh plugin %s config of componet %s failure %s")
}
if len(resourceConfig.BaseServices) > 0 {
setSidecarContainerLifecycle(as, &pc, &resourceConfig)
}
}
2019-02-28 14:52:39 +08:00
}
if pluginModel == model.InitPlugin {
if strings.ToLower(os.Getenv("DISABLE_INIT_CONTAINER_ENABLE_SECURITY")) != "true" {
//init container default open security
pc.SecurityContext = &corev1.SecurityContext{Privileged: util.Bool(true)}
}
2019-02-28 14:52:39 +08:00
initContainers = append(initContainers, pc)
} else if preconatiner {
precontainers = append(precontainers, pc)
2019-02-28 14:52:39 +08:00
} else {
postcontainers = append(postcontainers, pc)
}
}
2019-06-14 15:19:53 +08:00
var inboundPluginConfig *api_model.ResourceSpec
//apply plugin dynamic config
if inBoundPlugin != nil {
config, err := dbmanager.TenantPluginVersionConfigDao().GetPluginConfig(inBoundPlugin.ServiceID,
inBoundPlugin.PluginID)
if err != nil && err != gorm.ErrRecordNotFound {
logrus.Errorf("get service plugin config from db failure %s", err.Error())
}
if config != nil {
var resourceConfig api_model.ResourceSpec
if err := json.Unmarshal([]byte(config.ConfigStr), &resourceConfig); err == nil {
inboundPluginConfig = &resourceConfig
}
2019-06-14 15:19:53 +08:00
}
}
//create plugin config to configmap
for i := range appPlugins {
ApplyPluginConfig(as, appPlugins[i], dbmanager, inboundPluginConfig)
}
//if need proxy but not install net plugin
if as.NeedProxy && !netPlugin {
pluginID, pluginConfig, err := applyDefaultMeshPluginConfig(as, dbmanager)
if err != nil {
logrus.Errorf("apply default mesh plugin config failure %s", err.Error())
}
defaultSidecarContainer := createTCPDefaultPluginContainer(as, pluginID, mainContainer.Env, pluginConfig)
precontainers = append(precontainers, defaultSidecarContainer)
meshPluginID = pluginID
}
2019-12-06 13:30:17 +08:00
2020-05-19 18:59:21 +08:00
bootSequence := createProbeMeshInitContainer(as, meshPluginID, as.ServiceAlias, mainContainer.Env)
if bootSeqDepServiceIds := as.ExtensionSet["boot_seq_dep_service_ids"]; as.NeedProxy && bootSeqDepServiceIds != "" {
2020-05-19 18:59:21 +08:00
initContainers = append(initContainers, bootSequence)
2019-02-28 14:52:39 +08:00
}
as.BootSeqContainer = &bootSequence
return initContainers, precontainers, postcontainers, nil
}
func createTCPDefaultPluginContainer(as *typesv1.AppService, pluginID string, envs []v1.EnvVar, pluginConfig *api_model.ResourceSpec) v1.Container {
2019-02-25 20:04:48 +08:00
envs = append(envs, v1.EnvVar{Name: "PLUGIN_ID", Value: pluginID})
2020-07-24 09:50:22 +08:00
xdsHost, xdsHostPort, apiHostPort := getXDSHostIPAndPort()
envs = append(envs, xdsHostIPEnv(xdsHost))
2019-06-24 21:19:12 +08:00
envs = append(envs, v1.EnvVar{Name: "API_HOST_PORT", Value: apiHostPort})
2019-02-28 14:52:39 +08:00
envs = append(envs, v1.EnvVar{Name: "XDS_HOST_PORT", Value: xdsHostPort})
2019-12-11 21:37:43 +08:00
container := v1.Container{
2019-12-26 21:33:30 +08:00
Name: "default-tcpmesh-" + as.ServiceID[len(as.ServiceID)-20:],
2019-02-25 20:04:48 +08:00
Env: envs,
2019-03-20 14:15:32 +08:00
Image: typesv1.GetTCPMeshImageName(),
2019-12-26 21:33:30 +08:00
Resources: createTCPUDPMeshRecources(as),
2019-02-28 14:52:39 +08:00
}
setSidecarContainerLifecycle(as, &container, pluginConfig)
return container
}
func setSidecarContainerLifecycle(as *typesv1.AppService, con *corev1.Container, pluginConfig *api_model.ResourceSpec) {
if strings.ToLower(as.ExtensionSet["disable_sidecar_check"]) != "true" {
var port int
if as.ExtensionSet["sidecar_check_port"] != "" {
cport, _ := strconv.Atoi(as.ExtensionSet["sidecar_check_port"])
2021-06-21 19:03:31 +08:00
if cport != 0 {
port = cport
}
}
if port == 0 {
for _, dep := range pluginConfig.BaseServices {
if strings.ToLower(dep.Protocol) != "udp" {
port = dep.Port
break
}
}
if port == 0 {
2021-06-21 19:03:31 +08:00
for _, bport := range pluginConfig.BasePorts {
if strings.ToLower(bport.Protocol) != "udp" {
port = bport.Port
break
}
}
}
}
con.Lifecycle = &corev1.Lifecycle{
PostStart: &corev1.Handler{
Exec: &v1.ExecAction{
Command: []string{"/root/rainbond-mesh-data-panel", "wait", strconv.Itoa(port)},
},
},
}
}
2019-02-28 14:52:39 +08:00
}
2019-12-26 21:33:30 +08:00
func createProbeMeshInitContainer(as *typesv1.AppService, pluginID, serviceAlias string, envs []v1.EnvVar) v1.Container {
2019-02-28 14:52:39 +08:00
envs = append(envs, v1.EnvVar{Name: "PLUGIN_ID", Value: pluginID})
2020-07-24 09:50:22 +08:00
xdsHost, xdsHostPort, apiHostPort := getXDSHostIPAndPort()
envs = append(envs, xdsHostIPEnv(xdsHost))
2019-06-24 21:19:12 +08:00
envs = append(envs, v1.EnvVar{Name: "API_HOST_PORT", Value: apiHostPort})
2019-02-28 14:52:39 +08:00
envs = append(envs, v1.EnvVar{Name: "XDS_HOST_PORT", Value: xdsHostPort})
return v1.Container{
2019-12-26 21:33:30 +08:00
Name: "probe-mesh-" + as.ServiceID[len(as.ServiceID)-20:],
2019-02-28 14:52:39 +08:00
Env: envs,
2019-03-20 14:15:32 +08:00
Image: typesv1.GetProbeMeshImageName(),
2019-12-26 21:33:30 +08:00
Resources: createTCPUDPMeshRecources(as),
2019-02-25 20:04:48 +08:00
}
}
//ApplyPluginConfig applyPluginConfig
2019-06-14 15:19:53 +08:00
func ApplyPluginConfig(as *typesv1.AppService, servicePluginRelation *model.TenantServicePluginRelation,
dbmanager db.Manager, inboundPluginConfig *api_model.ResourceSpec) {
config, err := dbmanager.TenantPluginVersionConfigDao().GetPluginConfig(servicePluginRelation.ServiceID,
servicePluginRelation.PluginID)
if err != nil && err != gorm.ErrRecordNotFound {
logrus.Errorf("get service plugin config from db failure %s", err.Error())
}
if config != nil {
2019-06-14 15:19:53 +08:00
configStr := config.ConfigStr
//if have inbound plugin,will Propagate its listner port to other plug-ins
if inboundPluginConfig != nil {
var oldConfig api_model.ResourceSpec
if err := json.Unmarshal([]byte(configStr), &oldConfig); err == nil {
for i := range oldConfig.BasePorts {
for j := range inboundPluginConfig.BasePorts {
if oldConfig.BasePorts[i].Port == inboundPluginConfig.BasePorts[j].Port {
oldConfig.BasePorts[i].ListenPort = inboundPluginConfig.BasePorts[j].ListenPort
}
}
}
if newConfig, err := json.Marshal(&oldConfig); err == nil {
configStr = string(newConfig)
}
}
}
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("config-%s-%s", config.ServiceID, config.PluginID),
Labels: as.GetCommonLabels(map[string]string{
"plugin_id": servicePluginRelation.PluginID,
"service_alias": as.ServiceAlias,
}),
},
Data: map[string]string{
2019-06-14 15:19:53 +08:00
"plugin-config": configStr,
2019-02-15 18:46:11 +08:00
"plugin-model": servicePluginRelation.PluginModel,
},
}
as.SetConfigMap(cm)
}
}
2019-02-25 20:04:48 +08:00
//applyDefaultMeshPluginConfig applyDefaultMeshPluginConfig
func applyDefaultMeshPluginConfig(as *typesv1.AppService, dbmanager db.Manager) (string, *api_model.ResourceSpec, error) {
2019-02-25 20:04:48 +08:00
var baseServices []*api_model.BaseService
deps, err := dbmanager.TenantServiceRelationDao().GetTenantServiceRelations(as.ServiceID)
if err != nil {
logrus.Errorf("get service depend service info failure %s", err.Error())
}
for _, dep := range deps {
ports, err := dbmanager.TenantServicesPortDao().GetPortsByServiceID(dep.DependServiceID)
if err != nil {
logrus.Errorf("get service depend service port info failure %s", err.Error())
}
2019-02-28 14:52:39 +08:00
depService, err := dbmanager.TenantServiceDao().GetServiceByID(dep.DependServiceID)
2019-02-25 20:04:48 +08:00
if err != nil {
logrus.Errorf("get service depend service info failure %s", err.Error())
}
for _, port := range ports {
2019-12-26 21:33:30 +08:00
if *port.IsInnerService {
depService := &api_model.BaseService{
ServiceAlias: as.ServiceAlias,
ServiceID: as.ServiceID,
DependServiceAlias: depService.ServiceAlias,
DependServiceID: depService.ServiceID,
Port: port.ContainerPort,
Protocol: port.Protocol,
2019-12-26 21:33:30 +08:00
}
baseServices = append(baseServices, depService)
2019-02-25 20:04:48 +08:00
}
}
}
var res = &api_model.ResourceSpec{
BaseServices: baseServices,
}
resJSON, err := json.Marshal(res)
if err != nil {
return "", nil, err
2019-02-25 20:04:48 +08:00
}
2020-03-26 17:54:24 +08:00
pluginID := "def-mesh" + as.ServiceID
2019-02-25 20:04:48 +08:00
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("config-%s-%s", as.ServiceID, pluginID),
Labels: as.GetCommonLabels(map[string]string{
"plugin_id": pluginID,
"service_alias": as.ServiceAlias,
}),
},
Data: map[string]string{
"plugin-config": string(resJSON),
2019-06-14 15:19:53 +08:00
"plugin-model": model.OutBoundNetPlugin,
2019-02-25 20:04:48 +08:00
},
}
as.SetConfigMap(cm)
return pluginID, res, nil
2019-02-25 20:04:48 +08:00
}
func getPluginModel(pluginID, tenantID string, dbmanager db.Manager) (string, error) {
plugin, err := dbmanager.TenantPluginDao().GetPluginByID(pluginID, tenantID)
if err != nil {
return "", err
}
return plugin.PluginModel, nil
}
func createPluginArgs(cmd string, envs []v1.EnvVar) ([]string, error) {
if cmd == "" {
return nil, nil
}
configs := make(map[string]string, len(envs))
for _, env := range envs {
configs[env.Name] = env.Value
}
return strings.Split(util.ParseVariable(cmd, configs), " "), nil
}
2019-06-24 21:19:12 +08:00
func getXDSHostIPAndPort() (string, string, string) {
2020-07-19 12:13:17 +08:00
xdsHost := ""
2019-02-28 14:52:39 +08:00
xdsHostPort := "6101"
2019-06-24 21:19:12 +08:00
apiHostPort := "6100"
2019-02-28 14:52:39 +08:00
if os.Getenv("XDS_HOST_IP") != "" {
2020-07-19 12:13:17 +08:00
xdsHost = os.Getenv("XDS_HOST_IP")
2019-02-28 14:52:39 +08:00
}
if os.Getenv("XDS_HOST_PORT") != "" {
xdsHostPort = os.Getenv("XDS_HOST_PORT")
}
2019-06-24 21:19:12 +08:00
if os.Getenv("API_HOST_PORT") != "" {
apiHostPort = os.Getenv("API_HOST_PORT")
}
2020-07-19 12:13:17 +08:00
return xdsHost, xdsHostPort, apiHostPort
2019-02-28 14:52:39 +08:00
}
//container envs
func createPluginEnvs(pluginID, tenantID, serviceAlias string, mainEnvs []v1.EnvVar, versionID, serviceID string, dbmanager db.Manager) (*[]v1.EnvVar, error) {
versionEnvs, err := dbmanager.TenantPluginVersionENVDao().GetVersionEnvByServiceID(serviceID, pluginID)
2018-12-06 14:33:54 +08:00
if err != nil && err.Error() != gorm.ErrRecordNotFound.Error() {
return nil, err
}
var envs []v1.EnvVar
//first set main service env
for _, e := range mainEnvs {
envs = append(envs, e)
}
for _, e := range versionEnvs {
envs = append(envs, v1.EnvVar{Name: e.EnvName, Value: e.EnvValue})
}
2020-07-19 12:13:17 +08:00
xdsHost, xdsHostPort, apiHostPort := getXDSHostIPAndPort()
2020-07-24 09:50:22 +08:00
envs = append(envs, xdsHostIPEnv(xdsHost))
2019-06-24 21:19:12 +08:00
envs = append(envs, v1.EnvVar{Name: "API_HOST_PORT", Value: apiHostPort})
2019-02-28 14:52:39 +08:00
envs = append(envs, v1.EnvVar{Name: "XDS_HOST_PORT", Value: xdsHostPort})
discoverURL := fmt.Sprintf(
"http://%s:6100/v1/resources/%s/%s/%s",
2020-07-19 12:13:17 +08:00
"${XDS_HOST_IP}",
tenantID,
serviceAlias,
pluginID)
envs = append(envs, v1.EnvVar{Name: "DISCOVER_URL", Value: discoverURL})
envs = append(envs, v1.EnvVar{Name: "DISCOVER_URL_NOHOST", Value: fmt.Sprintf(
"/v1/resources/%s/%s/%s",
tenantID,
serviceAlias,
pluginID)})
envs = append(envs, v1.EnvVar{Name: "PLUGIN_ID", Value: pluginID})
var config = make(map[string]string, len(envs))
for _, env := range envs {
config[env.Name] = env.Value
}
for i, env := range envs {
envs[i].Value = util.ParseVariable(env.Value, config)
}
return &envs, nil
}
2019-12-26 21:33:30 +08:00
func createPluginResources(memory int, cpu int) v1.ResourceRequirements {
return createResourcesByDefaultCPU(memory, int64(cpu), int64(cpu))
}
2019-12-11 21:37:43 +08:00
2019-12-26 21:33:30 +08:00
func createTCPUDPMeshRecources(as *typesv1.AppService) v1.ResourceRequirements {
var memory = 128
var cpu int64
if limit, ok := as.ExtensionSet["tcpudp_mesh_cpu"]; ok {
limitint, _ := strconv.Atoi(limit)
if limitint > 0 {
cpu = int64(limitint)
2019-12-11 21:37:43 +08:00
}
}
2019-12-26 21:33:30 +08:00
if request, ok := as.ExtensionSet["tcpudp_mesh_memory"]; ok {
requestint, _ := strconv.Atoi(request)
if requestint > 0 {
memory = requestint
}
2019-12-11 21:37:43 +08:00
}
2019-12-26 21:33:30 +08:00
return createResourcesByDefaultCPU(memory, cpu, func() int64 {
if cpu < 120 {
return 120
}
return cpu
}())
2019-12-11 21:37:43 +08:00
}
2020-07-24 09:50:22 +08:00
func xdsHostIPEnv(xdsHost string) corev1.EnvVar {
if xdsHost == "" {
return v1.EnvVar{Name: "XDS_HOST_IP", ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "status.hostIP",
},
}}
}
return v1.EnvVar{Name: "XDS_HOST_IP", Value: xdsHost}
}