-
Notifications
You must be signed in to change notification settings - Fork 75
/
mesh_helpers.go
152 lines (134 loc) · 4.14 KB
/
mesh_helpers.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
package mesh
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
import (
mesh_proto "github.com/apache/dubbo-kubernetes/api/mesh/v1alpha1"
)
func (m *MeshResource) HasPrometheusMetricsEnabled() bool {
return m != nil && m.GetEnabledMetricsBackend().GetType() == mesh_proto.MetricsPrometheusType
}
func (m *MeshResource) GetEnabledMetricsBackend() *mesh_proto.MetricsBackend {
// TODO: support this!
return nil
}
func (m *MeshResource) GetMetricsBackend(name string) *mesh_proto.MetricsBackend {
// TODO: support this!
return nil
}
func (m *MeshResource) MTLSEnabled() bool {
return m != nil && m.Spec.GetMtls().GetEnabledBackend() != ""
}
// ZoneEgress works only when mTLS is enabled.
// Configuration of mTLS is validated on Mesh configuration
// change and when zoneEgress is enabled.
func (m *MeshResource) ZoneEgressEnabled() bool {
return m != nil && m.Spec.GetRouting().GetZoneEgress()
}
func (m *MeshResource) LocalityAwareLbEnabled() bool {
return m != nil && m.Spec.GetRouting().GetLocalityAwareLoadBalancing()
}
func (m *MeshResource) GetLoggingBackend(name string) *mesh_proto.LoggingBackend {
backends := map[string]*mesh_proto.LoggingBackend{}
for _, backend := range m.Spec.GetLogging().GetBackends() {
backends[backend.Name] = backend
}
if name == "" {
return backends[m.Spec.GetLogging().GetDefaultBackend()]
}
return backends[name]
}
func (m *MeshResource) GetTracingBackend(name string) *mesh_proto.TracingBackend {
backends := map[string]*mesh_proto.TracingBackend{}
for _, backend := range m.Spec.GetTracing().GetBackends() {
backends[backend.Name] = backend
}
if name == "" {
return backends[m.Spec.GetTracing().GetDefaultBackend()]
}
return backends[name]
}
// GetLoggingBackends will return logging backends as comma separated strings
// if empty return empty string
func (m *MeshResource) GetLoggingBackends() string {
var backends []string
for _, backend := range m.Spec.GetLogging().GetBackends() {
backend := fmt.Sprintf("%s/%s", backend.GetType(), backend.GetName())
backends = append(backends, backend)
}
return strings.Join(backends, ", ")
}
// GetTracingBackends will return tracing backends as comma separated strings
// if empty return empty string
func (m *MeshResource) GetTracingBackends() string {
var backends []string
for _, backend := range m.Spec.GetTracing().GetBackends() {
backend := fmt.Sprintf("%s/%s", backend.GetType(), backend.GetName())
backends = append(backends, backend)
}
return strings.Join(backends, ", ")
}
func (m *MeshResource) GetEnabledCertificateAuthorityBackend() *mesh_proto.CertificateAuthorityBackend {
return m.GetCertificateAuthorityBackend(m.Spec.GetMtls().GetEnabledBackend())
}
func (m *MeshResource) GetCertificateAuthorityBackend(name string) *mesh_proto.CertificateAuthorityBackend {
for _, backend := range m.Spec.GetMtls().GetBackends() {
if backend.Name == name {
return backend
}
}
return nil
}
var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$")
// ParseDuration parses a string into a time.Duration
func ParseDuration(durationStr string) (time.Duration, error) {
// Allow 0 without a unit.
if durationStr == "0" {
return 0, nil
}
matches := durationRE.FindStringSubmatch(durationStr)
if len(matches) != 3 {
return 0, fmt.Errorf("not a valid duration string: %q", durationStr)
}
var (
n, _ = strconv.Atoi(matches[1])
dur = time.Duration(n) * time.Millisecond
)
switch unit := matches[2]; unit {
case "y":
dur *= 1000 * 60 * 60 * 24 * 365
case "w":
dur *= 1000 * 60 * 60 * 24 * 7
case "d":
dur *= 1000 * 60 * 60 * 24
case "h":
dur *= 1000 * 60 * 60
case "m":
dur *= 1000 * 60
case "s":
dur *= 1000
case "ms":
// Value already correct
default:
return 0, fmt.Errorf("invalid time unit in duration string: %q", unit)
}
return dur, nil
}
func (ml *MeshResourceList) MarshalLog() interface{} {
maskedList := make([]*MeshResource, 0, len(ml.Items))
for _, mesh := range ml.Items {
maskedList = append(maskedList, mesh.MarshalLog().(*MeshResource))
}
return MeshResourceList{
Items: maskedList,
Pagination: ml.Pagination,
}
}
func (m *MeshResource) MarshalLog() interface{} {
// TODO: support this!
return nil
}