This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
/
cluster_wrapper.go
112 lines (97 loc) · 2.43 KB
/
cluster_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wrapper
import (
"fmt"
"strings"
)
type Cluster interface {
ClusterName() string
HostName() string
}
type K8sCluster struct {
ServiceName string
Namespace string
Port int64
Version string
Host string
}
func (c K8sCluster) ClusterName() string {
namespace := "default"
if c.Namespace != "" {
namespace = c.Namespace
}
return fmt.Sprintf("outbound|%d|%s|%s.%s.svc.cluster.local",
c.Port, c.Version, c.ServiceName, namespace)
}
func (c K8sCluster) HostName() string {
if c.Host != "" {
return c.Host
}
return fmt.Sprintf("%s.%s.svc.cluster.local", c.ServiceName, c.Namespace)
}
type NacosCluster struct {
ServiceName string
// use DEFAULT-GROUP by default
Group string
NamespaceID string
Port int64
// set true if use edas/sae registry
IsExtRegistry bool
Version string
Host string
}
func (c NacosCluster) ClusterName() string {
group := "DEFAULT-GROUP"
if c.Group != "" {
group = strings.ReplaceAll(c.Group, "_", "-")
}
tail := "nacos"
if c.IsExtRegistry {
tail += "-ext"
}
return fmt.Sprintf("outbound|%d|%s|%s.%s.%s.%s",
c.Port, c.Version, c.ServiceName, group, c.NamespaceID, tail)
}
func (c NacosCluster) HostName() string {
if c.Host != "" {
return c.Host
}
return c.ServiceName
}
type StaticIpCluster struct {
ServiceName string
Port int64
Host string
}
func (c StaticIpCluster) ClusterName() string {
return fmt.Sprintf("outbound|%d||%s.static", c.Port, c.ServiceName)
}
func (c StaticIpCluster) HostName() string {
if c.Host != "" {
return c.Host
}
return c.ServiceName
}
type DnsCluster struct {
ServiceName string
Domain string
Port int64
}
func (c DnsCluster) ClusterName() string {
return fmt.Sprintf("outbound|%d||%s.dns", c.Port, c.ServiceName)
}
func (c DnsCluster) HostName() string {
return c.Domain
}