generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
/
helpers.go
121 lines (106 loc) · 2.87 KB
/
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
package controllers
import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"net/http"
"net/url"
"github.com/layer5io/meshery-operator/api/v1alpha1"
"github.com/layer5io/meshkit/utils"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
)
const BrokerPingEndpoint = "/connz"
type Connections struct {
Connections []connection `json:"connections"`
}
type connection struct {
Name string `json:"name"`
}
func GetBrokerEndpoint(kclient *mesherykube.Client, broker *v1alpha1.Broker) string {
endpoint := broker.Status.Endpoint.Internal
if len(strings.Split(broker.Status.Endpoint.Internal, ":")) > 1 {
port, _ := strconv.Atoi(strings.Split(broker.Status.Endpoint.Internal, ":")[1])
if !utils.TcpCheck(&utils.HostPort{
Address: strings.Split(broker.Status.Endpoint.Internal, ":")[0],
Port: int32(port),
}, nil) {
endpoint = broker.Status.Endpoint.External
port, _ = strconv.Atoi(strings.Split(broker.Status.Endpoint.External, ":")[1])
if !utils.TcpCheck(&utils.HostPort{
Address: strings.Split(broker.Status.Endpoint.External, ":")[0],
Port: int32(port),
}, nil) {
if !utils.TcpCheck(&utils.HostPort{
Address: "host.docker.internal",
Port: int32(port),
}, nil) {
u, _ := url.Parse(kclient.RestConfig.Host)
if utils.TcpCheck(&utils.HostPort{
Address: u.Hostname(),
Port: int32(port),
}, nil) {
endpoint = fmt.Sprintf("%s:%d", u.Hostname(), int32(port))
}
} else {
endpoint = fmt.Sprintf("host.docker.internal:%d", int32(port))
}
}
}
}
return endpoint
}
func applyOperatorHelmChart(chartRepo string, client mesherykube.Client, mesheryReleaseVersion string, delete bool, overrides map[string]interface{}) error {
var (
act = mesherykube.INSTALL
chart = "meshery-operator"
)
if delete {
act = mesherykube.UNINSTALL
}
err := client.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
Namespace: "meshery",
ReleaseName: "meshery-operator",
ChartLocation: mesherykube.HelmChartLocation{
Repository: chartRepo,
Chart: chart,
Version: mesheryReleaseVersion,
},
// CreateNamespace doesn't have any effect when the action is UNINSTALL
CreateNamespace: true,
Action: act,
// Setting override values
OverrideValues: overrides,
UpgradeIfInstalled: true,
})
if err != nil {
return err
}
return nil
}
func ConnectivityTest(clientName, hostPort string) bool {
endpoint, err := url.Parse("http://" + hostPort + BrokerPingEndpoint)
if err != nil {
return false
}
resp, err := http.Get(endpoint.String())
if err != nil {
return false
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
var natsResponse Connections
err = json.Unmarshal(body, &natsResponse)
if err != nil {
return false
}
for _, client := range natsResponse.Connections {
if client.Name == clientName {
return true
}
}
return false
}