/
types.go
245 lines (205 loc) · 5.77 KB
/
types.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 envoy
import (
"context"
"fmt"
"sort"
)
import (
envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
import (
core_xds "github.com/apache/dubbo-kubernetes/pkg/core/xds"
"github.com/apache/dubbo-kubernetes/pkg/xds/envoy/tags"
)
type Cluster interface {
Service() string
Name() string
Mesh() string
Tags() tags.Tags
Hash() string
IsExternalService() bool
}
type Split interface {
ClusterName() string
Weight() uint32
LBMetadata() tags.Tags
HasExternalService() bool
}
// Deprecated: for new policies use pkg/plugins/policies/xds/cluster.go
type ClusterImpl struct {
service string
name string
weight uint32
tags tags.Tags
mesh string
isExternalService bool
}
func (c *ClusterImpl) Service() string { return c.service }
func (c *ClusterImpl) Name() string { return c.name }
func (c *ClusterImpl) Weight() uint32 { return c.weight }
func (c *ClusterImpl) Tags() tags.Tags { return c.tags }
// Mesh returns a non-empty string only if the cluster is in a different mesh
// from the context.
func (c *ClusterImpl) Mesh() string { return c.mesh }
func (c *ClusterImpl) IsExternalService() bool { return c.isExternalService }
func (c *ClusterImpl) Hash() string { return fmt.Sprintf("%s-%s", c.name, c.tags.String()) }
func (c *ClusterImpl) SetName(name string) {
c.name = name
}
func (c *ClusterImpl) SetMesh(mesh string) {
c.mesh = mesh
}
type NewClusterOpt interface {
apply(cluster *ClusterImpl)
}
type newClusterOptFunc func(cluster *ClusterImpl)
func (f newClusterOptFunc) apply(cluster *ClusterImpl) {
f(cluster)
}
// Deprecated: for new policies use pkg/plugins/policies/xds/cluster.go
func NewCluster(opts ...NewClusterOpt) *ClusterImpl {
c := &ClusterImpl{}
for _, opt := range opts {
opt.apply(c)
}
if err := c.validate(); err != nil {
panic(err)
}
return c
}
func (c *ClusterImpl) validate() error {
if c.service == "" || c.name == "" {
return errors.New("either WithService() or WithName() should be called")
}
return nil
}
func WithService(service string) NewClusterOpt {
return newClusterOptFunc(func(cluster *ClusterImpl) {
cluster.service = service
if len(cluster.name) == 0 {
cluster.name = service
}
})
}
func WithName(name string) NewClusterOpt {
return newClusterOptFunc(func(cluster *ClusterImpl) {
cluster.name = name
if len(cluster.service) == 0 {
cluster.service = name
}
})
}
func WithWeight(weight uint32) NewClusterOpt {
return newClusterOptFunc(func(cluster *ClusterImpl) {
cluster.weight = weight
})
}
func WithTags(tags tags.Tags) NewClusterOpt {
return newClusterOptFunc(func(cluster *ClusterImpl) {
cluster.tags = tags
})
}
func WithExternalService(isExternalService bool) NewClusterOpt {
return newClusterOptFunc(func(cluster *ClusterImpl) {
cluster.isExternalService = isExternalService
})
}
type Service struct {
name string
clusters []Cluster
hasExternalService bool
tlsReady bool
}
func (c *Service) Add(cluster Cluster) {
c.clusters = append(c.clusters, cluster)
if cluster.IsExternalService() {
c.hasExternalService = true
}
}
func (c *Service) Tags() []tags.Tags {
var result []tags.Tags
for _, cluster := range c.clusters {
result = append(result, cluster.Tags())
}
return result
}
func (c *Service) HasExternalService() bool {
return c.hasExternalService
}
func (c *Service) Clusters() []Cluster {
return c.clusters
}
func (c *Service) TLSReady() bool {
return c.tlsReady
}
type Services map[string]*Service
func (c Services) Sorted() []string {
var keys []string
for key := range c {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
type ServicesAccumulator struct {
tlsReadiness map[string]bool
services map[string]*Service
}
func NewServicesAccumulator(tlsReadiness map[string]bool) ServicesAccumulator {
return ServicesAccumulator{
tlsReadiness: tlsReadiness,
services: map[string]*Service{},
}
}
func (sa ServicesAccumulator) Services() Services {
return sa.services
}
func (sa ServicesAccumulator) Add(clusters ...Cluster) {
for _, c := range clusters {
if sa.services[c.Service()] == nil {
sa.services[c.Service()] = &Service{
tlsReady: sa.tlsReadiness[c.Service()],
name: c.Service(),
}
}
sa.services[c.Service()].Add(c)
}
}
type CLACache interface {
GetCLA(ctx context.Context, meshName, meshHash string, cluster Cluster, apiVersion core_xds.APIVersion, endpointMap core_xds.EndpointMap) (proto.Message, error)
}
type NamedResource interface {
envoy_types.Resource
GetName() string
}
type TrafficDirection string
const (
TrafficDirectionOutbound TrafficDirection = "OUTBOUND"
TrafficDirectionInbound TrafficDirection = "INBOUND"
TrafficDirectionUnspecified TrafficDirection = "UNSPECIFIED"
)
type StaticEndpointPath struct {
Path string
ClusterName string
RewritePath string
Header string
HeaderExactMatch string
}