This commit is contained in:
yangk 2021-12-09 23:15:09 +08:00
parent 4366acc00a
commit 7b6cf11b0b
5 changed files with 48 additions and 23 deletions

View File

@ -1,9 +1,9 @@
package app_governance_mode package appgovernancemode
import ( import (
"github.com/goodrain/rainbond/api/handler/app_governance_mode/build_in" "github.com/goodrain/rainbond/api/handler/app_governance_mode/build_in"
"github.com/goodrain/rainbond/api/handler/app_governance_mode/istio" "github.com/goodrain/rainbond/api/handler/app_governance_mode/istio"
"github.com/goodrain/rainbond/api/handler/app_governance_mode/kubernetes_native" kubernetesnative "github.com/goodrain/rainbond/api/handler/app_governance_mode/kubernetes_native"
"github.com/goodrain/rainbond/api/util/bcode" "github.com/goodrain/rainbond/api/util/bcode"
"github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/db/model"
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
@ -19,13 +19,11 @@ type AppGoveranceModeHandler interface {
func NewAppGoveranceModeHandler(governanceMode string, kubeClient clientset.Interface) (AppGoveranceModeHandler, error) { func NewAppGoveranceModeHandler(governanceMode string, kubeClient clientset.Interface) (AppGoveranceModeHandler, error) {
switch governanceMode { switch governanceMode {
case model.GovernanceModeIstioServiceMesh: case model.GovernanceModeIstioServiceMesh:
return &istio.IstioServiceMeshMode{ return istio.New(kubeClient), nil
KubeClient: kubeClient,
}, nil
case model.GovernanceModeBuildInServiceMesh: case model.GovernanceModeBuildInServiceMesh:
return &build_in.BuildInServiceMeshMode{}, nil return buildin.New(), nil
case model.GovernanceModeKubernetesNativeService: case model.GovernanceModeKubernetesNativeService:
return &kubernetes_native.KubernetesNativeMode{}, nil return kubernetesnative.New(), nil
default: default:
return nil, bcode.ErrInvalidGovernanceMode return nil, bcode.ErrInvalidGovernanceMode
} }

View File

@ -1,12 +1,20 @@
package build_in package buildin
type BuildInServiceMeshMode struct { import appgovernancemode "github.com/goodrain/rainbond/api/handler/app_governance_mode"
type buildInServiceMeshMode struct{}
// New Build In ServiceMeshMode Handler
func New() appgovernancemode.AppGoveranceModeHandler {
return &buildInServiceMeshMode{}
} }
func (b *BuildInServiceMeshMode) IsInstalledControlPlane() bool { // IsInstalledControlPlane -
func (b *buildInServiceMeshMode) IsInstalledControlPlane() bool {
return true return true
} }
func (b *BuildInServiceMeshMode) GetInjectLabels() map[string]string { // GetInjectLabels -
func (b *buildInServiceMeshMode) GetInjectLabels() map[string]string {
return nil return nil
} }

View File

@ -2,30 +2,40 @@ package istio
import ( import (
"context" "context"
appgovernancemode "github.com/goodrain/rainbond/api/handler/app_governance_mode"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
"os" "os"
"time" "time"
) )
type IstioServiceMeshMode struct { type istioServiceMeshMode struct {
KubeClient clientset.Interface kubeClient clientset.Interface
} }
func (i *IstioServiceMeshMode) IsInstalledControlPlane() bool { // New Istio Goverance Mode Handler
func New(kubeClient clientset.Interface) appgovernancemode.AppGoveranceModeHandler{
return &istioServiceMeshMode{
kubeClient: kubeClient,
}
}
// IsInstalledControlPlane -
func (i *istioServiceMeshMode) IsInstalledControlPlane() bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
cmName := os.Getenv("ISTIO_CM") cmName := os.Getenv("ISTIO_CM")
if cmName == "" { if cmName == "" {
cmName = "istio-ca-root-cert" cmName = "istio-ca-root-cert"
} }
_, err := i.KubeClient.CoreV1().ConfigMaps("default").Get(ctx, cmName, metav1.GetOptions{}) _, err := i.kubeClient.CoreV1().ConfigMaps("default").Get(ctx, cmName, metav1.GetOptions{})
if err != nil { if err != nil {
return false return false
} }
return true return true
} }
func (i *IstioServiceMeshMode) GetInjectLabels() map[string]string { // GetInjectLabels -
func (i *istioServiceMeshMode) GetInjectLabels() map[string]string {
return map[string]string{"sidecar.istio.io/inject": "true"} return map[string]string{"sidecar.istio.io/inject": "true"}
} }

View File

@ -1,12 +1,21 @@
package kubernetes_native package kubernetesnative
type KubernetesNativeMode struct { import appgovernancemode "github.com/goodrain/rainbond/api/handler/app_governance_mode"
type kubernetesNativeMode struct {
} }
func (k *KubernetesNativeMode) IsInstalledControlPlane() bool { // New Kubernetes Native Mode Handler
func New() appgovernancemode.AppGoveranceModeHandler {
return &kubernetesNativeMode{}
}
// IsInstalledControlPlane -
func (k *kubernetesNativeMode) IsInstalledControlPlane() bool {
return true return true
} }
func (k *KubernetesNativeMode) GetInjectLabels() map[string]string { // GetInjectLabels-
func (k *kubernetesNativeMode) GetInjectLabels() map[string]string {
return nil return nil
} }

View File

@ -902,11 +902,11 @@ func (s *ServiceAction) ServiceUpdate(sc map[string]interface{}) error {
if appID, ok := sc["app_id"].(string); ok && appID != "" { if appID, ok := sc["app_id"].(string); ok && appID != "" {
ts.AppID = appID ts.AppID = appID
} }
if k8s_component_name, ok := sc["k8s_component_name"].(string); ok && k8s_component_name != "" { if k8sComponentName, ok := sc["k8s_component_name"].(string); ok && k8sComponentName != "" {
if db.GetManager().TenantServiceDao().IsK8sComponentNameDuplicate(ts.AppID, ts.ServiceID, k8s_component_name) { if db.GetManager().TenantServiceDao().IsK8sComponentNameDuplicate(ts.AppID, ts.ServiceID, k8sComponentName) {
return bcode.ErrK8sComponentNameExists return bcode.ErrK8sComponentNameExists
} }
ts.K8sComponentName = k8s_component_name ts.K8sComponentName = k8sComponentName
} }
if sc["extend_method"] != nil { if sc["extend_method"] != nil {
extendMethod := sc["extend_method"].(string) extendMethod := sc["extend_method"].(string)