forked from Dynatrace/dynatrace-oneagent-operator
/
helper.go
205 lines (179 loc) · 5.52 KB
/
helper.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
package istio
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
istio "istio.io/api/networking/v1alpha3"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
istiov1alpha3 "github.com/Dynatrace/dynatrace-oneagent-operator/pkg/apis/networking/istio/v1alpha3"
)
var (
istioGVRName = "networking.istio.io"
// VirtualServiceGVK => definition of virtual service GVK for oneagent
VirtualServiceGVK = schema.GroupVersionKind{
Group: istioGVRName,
Version: "v1alpha3",
Kind: "VirtualService",
}
// ServiceEntryGVK => definition of virtual service GVK for oneagent
ServiceEntryGVK = schema.GroupVersionKind{
Group: istioGVRName,
Version: "v1alpha3",
Kind: "ServiceEntry",
}
)
// CheckIstioEnabled checks if Istio is installed
func CheckIstioEnabled(cfg *rest.Config) (bool, error) {
client, err := discovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
return false, err
}
apiGroupList, err := client.ServerGroups()
if err != nil {
return false, err
}
for _, apiGroup := range apiGroupList.Groups {
if apiGroup.Name == istioGVRName {
return true, nil
}
}
return false, nil
}
// BuildServiceEntry returns an Istio ServiceEntry object for the given communication endpoint.
func buildServiceEntry(name, host, protocol string, port uint32) *istiov1alpha3.ServiceEntry {
if net.ParseIP(host) != nil { // It's an IP.
return buildServiceEntryIP(name, host, port)
}
return buildServiceEntryFQDN(name, host, protocol, port)
}
// BuildVirtualService returns an Istio VirtualService object for the given communication endpoint.
func buildVirtualService(name, host, protocol string, port uint32) *istiov1alpha3.VirtualService {
if net.ParseIP(host) != nil { // It's an IP.
return nil
}
return &istiov1alpha3.VirtualService{
ObjectMeta: buildObjectMeta(name),
Spec: buildVirtualServiceSpec(host, protocol, port),
}
}
// buildServiceEntryFQDN returns an Istio ServiceEntry object for the given communication endpoint with a FQDN host.
func buildServiceEntryFQDN(name, host, protocol string, port uint32) *istiov1alpha3.ServiceEntry {
portStr := strconv.Itoa(int(port))
protocolStr := strings.ToUpper(protocol)
return &istiov1alpha3.ServiceEntry{
ObjectMeta: buildObjectMeta(name),
Spec: istiov1alpha3.ServiceEntrySpec{
ServiceEntry: istio.ServiceEntry{
Hosts: []string{host},
Ports: []*istio.Port{{
Name: protocol + "-" + portStr,
Number: port,
Protocol: protocolStr,
}},
Location: istio.ServiceEntry_MESH_EXTERNAL,
Resolution: istio.ServiceEntry_DNS,
},
},
}
}
// buildServiceEntryIP returns an Istio ServiceEntry object for the given communication endpoint with IP.
func buildServiceEntryIP(name, host string, port uint32) *istiov1alpha3.ServiceEntry {
portStr := strconv.Itoa(int(port))
return &istiov1alpha3.ServiceEntry{
ObjectMeta: buildObjectMeta(name),
Spec: istiov1alpha3.ServiceEntrySpec{
ServiceEntry: istio.ServiceEntry{
Hosts: []string{"ignored.subdomain"},
Addresses: []string{host + "/32"},
Ports: []*istio.Port{{
Name: "TCP-" + portStr,
Number: port,
Protocol: "TCP",
}},
Location: istio.ServiceEntry_MESH_EXTERNAL,
Resolution: istio.ServiceEntry_NONE,
},
},
}
}
// BuildNameForEndpoint returns a name to be used as a base to identify Istio objects.
func buildNameForEndpoint(name string, protocol string, host string, port uint32) string {
sum := sha256.Sum256([]byte(fmt.Sprintf("%s-%s-%s-%d", name, protocol, host, port)))
return hex.EncodeToString(sum[:])
}
func buildVirtualServiceSpec(host, protocol string, port uint32) istiov1alpha3.VirtualServiceSpec {
virtualServiceSpec := istiov1alpha3.VirtualServiceSpec{}
virtualServiceSpec.Hosts = []string{host}
switch protocol {
case "https":
virtualServiceSpec.Tls = buildVirtualServiceTLSRoute(host, port)
case "http":
virtualServiceSpec.Http = buildVirtualServiceHttpRoute(port, host)
}
return virtualServiceSpec
}
func buildVirtualServiceTLSRoute(host string, port uint32) []*istio.TLSRoute {
return []*istio.TLSRoute{{
Match: []*istio.TLSMatchAttributes{{
SniHosts: []string{host},
Port: port,
}},
Route: []*istio.RouteDestination{{
Destination: &istio.Destination{
Host: host,
Port: &istio.PortSelector{
Port: &istio.PortSelector_Number{Number: port},
},
},
}},
}}
}
func buildVirtualServiceHttpRoute(port uint32, host string) []*istio.HTTPRoute {
return []*istio.HTTPRoute{{
Match: []*istio.HTTPMatchRequest{{
Port: port,
}},
Route: []*istio.HTTPRouteDestination{{
Destination: &istio.Destination{
Host: host,
Port: &istio.PortSelector{
Port: &istio.PortSelector_Number{Number: port},
},
},
}},
}}
}
func buildObjectMeta(name string) v1.ObjectMeta {
return v1.ObjectMeta{
Name: name,
Namespace: os.Getenv(k8sutil.WatchNamespaceEnvVar),
}
}
func mapErrorToObjectProbeResult(err error) (probeResult, error) {
if err != nil {
if errors.IsNotFound(err) {
return probeObjectNotFound, err
} else if meta.IsNoMatchError(err) {
return probeTypeNotFound, err
}
return probeUnknown, err
}
return probeObjectFound, nil
}
func buildIstioLabels(name, role string) map[string]string {
return map[string]string{
"dynatrace": "oneagent",
"oneagent": name,
"dynatrace-istio-role": role,
}
}