create thirdcomponent with static endpoints

This commit is contained in:
GLYASAI 2021-08-04 20:24:55 +08:00
parent c0bb3cedaa
commit b1fbc171ae
8 changed files with 92 additions and 16 deletions

View File

@ -18,6 +18,8 @@
package model
import "fmt"
// Endpoint is a persistent object for table 3rd_party_svc_endpoints.
type Endpoint struct {
Model
@ -34,6 +36,14 @@ func (Endpoint) TableName() string {
return "tenant_service_3rd_party_endpoints"
}
// Address -
func (e *Endpoint) Address() string {
if e.Port == 0 {
return e.IP
}
return fmt.Sprintf("%s:%d", e.IP, e.Port)
}
// DiscorveryType type of service discovery center.
type DiscorveryType string

View File

@ -35,7 +35,7 @@ func init() {
// +genclient
// +kubebuilder:object:root=true
// HelmApp -
// ThirdComponent -
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=thirdcomponents,scope=Namespaced
type ThirdComponent struct {
@ -55,6 +55,7 @@ type ThirdComponentList struct {
Items []ThirdComponent `json:"items"`
}
// ThirdComponentSpec -
type ThirdComponentSpec struct {
// health check probe
// +optional
@ -65,6 +66,7 @@ type ThirdComponentSpec struct {
EndpointSource ThirdComponentEndpointSource `json:"endpointSource"`
}
// ThirdComponentEndpointSource -
type ThirdComponentEndpointSource struct {
StaticEndpoints []*ThirdComponentEndpoint `json:"endpoints,omitempty"`
KubernetesService *KubernetesServiceSource `json:"kubernetesService,omitempty"`
@ -75,6 +77,7 @@ type ThirdComponentEndpointSource struct {
// CustomAPISource
}
// ThirdComponentEndpoint -
type ThirdComponentEndpoint struct {
// The address including the port number.
Address string `json:"address"`
@ -83,9 +86,10 @@ type ThirdComponentEndpoint struct {
Protocol string `json:"protocol,omitempty"`
// Specify a private certificate when the protocol is HTTPS
// +optional
ClentSecret string `json:"clientSecret,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
}
// KubernetesServiceSource -
type KubernetesServiceSource struct {
// If not specified, the namespace is the namespace of the current resource
// +optional
@ -93,6 +97,7 @@ type KubernetesServiceSource struct {
Name string `json:"name"`
}
// HealthProbe -
type HealthProbe struct {
// HTTPGet specifies the http request to perform.
// +optional
@ -134,6 +139,7 @@ type HTTPHeader struct {
Value string `json:"value"`
}
// ComponentPhase -
type ComponentPhase string
// These are the valid statuses of pods.
@ -147,12 +153,14 @@ const (
ComponentFailed ComponentPhase = "Failed"
)
// ThirdComponentStatus -
type ThirdComponentStatus struct {
Phase ComponentPhase `json:"phase"`
Reason string `json:"reason,omitempty"`
Endpoints []*ThirdComponentEndpointStatus `json:"endpoints"`
}
// EndpointStatus -
type EndpointStatus string
const (
@ -162,8 +170,10 @@ const (
EndpointNotReady EndpointStatus = "NotReady"
)
// EndpointAddress -
type EndpointAddress string
// GetIP -
func (e EndpointAddress) GetIP() string {
info := strings.Split(string(e), ":")
if len(info) == 2 {
@ -172,6 +182,7 @@ func (e EndpointAddress) GetIP() string {
return ""
}
// GetPort -
func (e EndpointAddress) GetPort() int {
info := strings.Split(string(e), ":")
if len(info) == 2 {
@ -181,6 +192,7 @@ func (e EndpointAddress) GetPort() int {
return 0
}
// NewEndpointAddress -
func NewEndpointAddress(host string, port int) *EndpointAddress {
if net.ParseIP(host) == nil {
return nil

View File

@ -527,6 +527,7 @@ func Zip(source, target string) error {
return err
}
// UnTar -
func UnTar(archive, target string, zip bool) error {
parameter := "-x"
if zip {

View File

@ -20,10 +20,12 @@ package componentdefinition
//ThirdComponentProperties third component properties
type ThirdComponentProperties struct {
Kubernetes ThirdComponentKubernetes `json:"kubernetes"`
Port []*ThirdComponentPort `json:"port"`
Kubernetes *ThirdComponentKubernetes `json:"kubernetes,omitempty"`
Endpoints []*ThirdComponentEndpoint `json:"endpoints,omitempty"`
Port []*ThirdComponentPort `json:"port"`
}
// ThirdComponentPort -
type ThirdComponentPort struct {
Name string `json:"name"`
Port int `json:"port"`
@ -31,7 +33,15 @@ type ThirdComponentPort struct {
OpenOuter bool `json:"openOuter"`
}
// ThirdComponentKubernetes -
type ThirdComponentKubernetes struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// ThirdComponentEndpoint -
type ThirdComponentEndpoint struct {
Address string `json:"address"`
Protocol string `json:"protocol,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
}

View File

@ -33,10 +33,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ErrNotSupport -
var ErrNotSupport = fmt.Errorf("not support component definition")
// ErrOnlyCUESupport -
var ErrOnlyCUESupport = fmt.Errorf("component definition only support cue template")
// ComponentDefinitionBuilder -
type ComponentDefinitionBuilder struct {
logger *logrus.Entry
definitions map[string]*v1alpha1.ComponentDefinition
namespace string
lock sync.Mutex
@ -44,18 +48,22 @@ type ComponentDefinitionBuilder struct {
var componentDefinitionBuilder *ComponentDefinitionBuilder
// NewComponentDefinitionBuilder -
func NewComponentDefinitionBuilder(namespace string) *ComponentDefinitionBuilder {
componentDefinitionBuilder = &ComponentDefinitionBuilder{
logger: logrus.WithField("WHO", "ComponentDefinitionBuilder"),
definitions: make(map[string]*v1alpha1.ComponentDefinition),
namespace: namespace,
}
return componentDefinitionBuilder
}
// GetComponentDefinitionBuilder -
func GetComponentDefinitionBuilder() *ComponentDefinitionBuilder {
return componentDefinitionBuilder
}
// OnAdd -
func (c *ComponentDefinitionBuilder) OnAdd(obj interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
@ -65,6 +73,8 @@ func (c *ComponentDefinitionBuilder) OnAdd(obj interface{}) {
c.definitions[cd.Name] = cd
}
}
// OnUpdate -
func (c *ComponentDefinitionBuilder) OnUpdate(oldObj, newObj interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
@ -74,6 +84,8 @@ func (c *ComponentDefinitionBuilder) OnUpdate(oldObj, newObj interface{}) {
c.definitions[cd.Name] = cd
}
}
// OnDelete -
func (c *ComponentDefinitionBuilder) OnDelete(obj interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
@ -84,16 +96,18 @@ func (c *ComponentDefinitionBuilder) OnDelete(obj interface{}) {
}
}
// GetComponentDefinition -
func (c *ComponentDefinitionBuilder) GetComponentDefinition(name string) *v1alpha1.ComponentDefinition {
c.lock.Lock()
defer c.lock.Unlock()
return c.definitions[name]
}
// GetComponentProperties -
func (c *ComponentDefinitionBuilder) GetComponentProperties(as *v1.AppService, dbm db.Manager, cd *v1alpha1.ComponentDefinition) interface{} {
//TODO: support custom component properties
switch cd.Name {
case thirdComponetDefineName:
case thirdComponentDefineName:
properties := &ThirdComponentProperties{}
tpsd, err := dbm.ThirdPartySvcDiscoveryCfgDao().GetByServiceID(as.ServiceID)
if err != nil {
@ -102,17 +116,24 @@ func (c *ComponentDefinitionBuilder) GetComponentProperties(as *v1.AppService, d
if tpsd != nil {
// support other source type
if tpsd.Type == dbmodel.DiscorveryTypeKubernetes.String() {
properties.Kubernetes = ThirdComponentKubernetes{
properties.Kubernetes = &ThirdComponentKubernetes{
Name: tpsd.ServiceName,
Namespace: tpsd.Namespace,
}
}
}
// static endpoints
endpoints, err := c.listStaticEndpoints(as.ServiceID)
if err != nil {
c.logger.Errorf("component id: %s; list static endpoints: %v", as.ServiceID, err)
}
properties.Endpoints = endpoints
ports, err := dbm.TenantServicesPortDao().GetPortsByServiceID(as.ServiceID)
if err != nil {
logrus.Errorf("query component %s ports failure %s", as.ServiceID, err.Error())
}
for _, port := range ports {
properties.Port = append(properties.Port, &ThirdComponentPort{
Port: port.ContainerPort,
@ -124,12 +145,29 @@ func (c *ComponentDefinitionBuilder) GetComponentProperties(as *v1.AppService, d
if properties.Port == nil {
properties.Port = []*ThirdComponentPort{}
}
return properties
default:
return nil
}
}
func (c *ComponentDefinitionBuilder) listStaticEndpoints(componentID string) ([]*ThirdComponentEndpoint, error) {
endpoints, err := db.GetManager().EndpointsDao().List(componentID)
if err != nil {
return nil, err
}
var res []*ThirdComponentEndpoint
for _, ep := range endpoints {
res = append(res, &ThirdComponentEndpoint{
Address: ep.Address(),
})
}
return res, nil
}
// BuildWorkloadResource -
func (c *ComponentDefinitionBuilder) BuildWorkloadResource(as *v1.AppService, dbm db.Manager) error {
cd := c.GetComponentDefinition(as.GetComponentDefinitionName())
if cd == nil {
@ -154,7 +192,7 @@ func (c *ComponentDefinitionBuilder) BuildWorkloadResource(as *v1.AppService, db
//InitCoreComponentDefinition init the built-in component type definition.
//Should be called after the store is initialized.
func (c *ComponentDefinitionBuilder) InitCoreComponentDefinition(rainbondClient rainbondversioned.Interface) {
coreComponentDefinition := []*v1alpha1.ComponentDefinition{&thirdComponetDefine}
coreComponentDefinition := []*v1alpha1.ComponentDefinition{&thirdComponentDefine}
for _, ccd := range coreComponentDefinition {
if c.GetComponentDefinition(ccd.Name) == nil {
logrus.Infof("create core componentdefinition %s", ccd.Name)

View File

@ -40,6 +40,9 @@ output: {
name: parameter["kubernetes"]["name"]
}
}
if parameter["endpoints"] != _|_ {
endpoints: parameter["endpoints"]
}
}
if parameter["port"] != _|_ {
ports: parameter["port"]
@ -52,6 +55,11 @@ parameter: {
namespace?: string
name: string
}
endpoints?: [...{
address: string
protocol?: string
clientSecret?: string
}]
port?: [...{
name: string
port: >0 & <=65533
@ -60,14 +68,14 @@ parameter: {
}]
}
`
var thirdComponetDefineName = "core-thirdcomponent"
var thirdComponetDefine = v1alpha1.ComponentDefinition{
var thirdComponentDefineName = "core-thirdcomponent"
var thirdComponentDefine = v1alpha1.ComponentDefinition{
TypeMeta: v1.TypeMeta{
Kind: "ComponentDefinition",
APIVersion: "rainbond.io/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: thirdComponetDefineName,
Name: thirdComponentDefineName,
Annotations: map[string]string{
"definition.oam.dev/description": "Rainbond built-in component type that defines third-party service components.",
},

View File

@ -101,9 +101,6 @@ func InitAppService(dbmanager db.Manager, serviceID string, configs map[string]s
}
return appService, nil
}
if appService.IsThirdComponent() {
return appService, nil
}
for _, c := range conversionList {
if len(enableConversionList) == 0 || util.StringArrayContains(enableConversionList, c.Name) {
if err := c.Conversion(appService, dbmanager); err != nil {

View File

@ -111,7 +111,7 @@ func (a AppServiceBase) GetComponentDefinitionName() string {
if strings.HasPrefix(a.ServiceKind.String(), dbmodel.ServiceKindCustom.String()) {
return strings.Replace(a.ServiceKind.String(), dbmodel.ServiceKindCustom.String(), "", 1)
}
if a.discoveryCfg != nil && a.discoveryCfg.Type == dbmodel.DiscorveryTypeKubernetes.String() {
if a.ServiceKind == model.ServiceKindThirdParty {
return "core-thirdcomponent"
}
return ""
@ -122,7 +122,7 @@ func (a AppServiceBase) IsCustomComponent() bool {
if strings.HasPrefix(a.ServiceKind.String(), dbmodel.ServiceKindCustom.String()) {
return true
}
if a.discoveryCfg != nil && a.discoveryCfg.Type == dbmodel.DiscorveryTypeKubernetes.String() {
if a.ServiceKind == model.ServiceKindThirdParty {
return true
}
return false