forked from kmesh-net/kmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
164 lines (146 loc) · 5.58 KB
/
api.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
/*
* Copyright The Kmesh Authors.
*
* 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 status
import (
"net"
"kmesh.net/kmesh/api/v2/workloadapi"
)
type Workload struct {
Uid string `json:"uid,omitempty"`
Addresses []string `json:"addresses"`
Waypoint string `json:"waypoint,omitempty"`
Protocol string `json:"protocol"`
Name string `json:"name"`
Namespace string `json:"namespace"`
ServiceAccount string `json:"serviceAccount"`
WorkloadName string `json:"workloadName"`
WorkloadType string `json:"workloadType"`
CanonicalName string `json:"canonicalName"`
CanonicalRevision string `json:"canonicalRevision"`
ClusterID string `json:"clusterId"`
TrustDomain string `json:"trustDomain,omitempty"`
Locality Locality `json:"locality,omitempty"`
Node string `json:"node"`
Network string `json:"network,omitempty"`
Status string `json:"status"`
ApplicationTunnel ApplicationTunnel `json:"applicationTunnel,omitempty"`
Services []string `json:"services,omitempty"`
AuthorizationPolicies []string `json:"authorizationPolicies,omitempty"`
}
type Locality struct {
Region string `json:"region,omitempty"`
Zone string `json:"zone,omitempty"`
Subzone string `json:"subzone,omitempty"`
}
type ApplicationTunnel struct {
Protocol string `json:"protocol"`
Port uint32 `json:"port,omitempty"`
}
type Waypoint struct {
Destination string `json:"destination"`
}
type LoadBalancer struct {
Mode string `json:"mode"`
RoutingPreferences []string `json:"routingPreferences"`
}
type Service struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Hostname string `json:"hostname"`
Addresses []string `json:"vips"`
Ports []*workloadapi.Port `json:"ports"`
LoadBalancer *LoadBalancer `json:"loadBalancer"`
Waypoint *Waypoint `json:"waypoint"`
}
type NetworkAddress struct {
// Network represents the network this address is on.
Network string
// Address presents the IP (v4 or v6).
Address net.IP
}
func ConvertWorkload(w *workloadapi.Workload) *Workload {
ips := make([]string, 0, len(w.Addresses))
for _, addr := range w.Addresses {
ips = append(ips, net.IP(addr).String())
}
var waypoint string
if waypointAddress := w.Waypoint.GetAddress(); waypointAddress != nil {
waypoint = waypointAddress.Network + "/" + net.IP(waypointAddress.Address).String()
} else if host := w.Waypoint.GetHostname(); host != nil {
waypoint = host.Namespace + "/" + host.Hostname
}
out := &Workload{
Uid: w.Uid,
Addresses: ips,
Waypoint: waypoint,
Protocol: w.TunnelProtocol.String(),
Name: w.Name,
Namespace: w.Namespace,
ServiceAccount: w.ServiceAccount,
WorkloadName: w.WorkloadName,
WorkloadType: w.WorkloadType.String(),
CanonicalName: w.CanonicalName,
CanonicalRevision: w.CanonicalRevision,
ClusterID: w.ClusterId,
TrustDomain: w.TrustDomain,
Node: w.Node,
Network: w.Network,
Status: w.Status.String(),
AuthorizationPolicies: w.AuthorizationPolicies,
}
if w.Locality != nil {
out.Locality = Locality{Region: w.Locality.Region, Zone: w.Locality.Zone, Subzone: w.Locality.Subzone}
}
if w.ApplicationTunnel != nil {
out.ApplicationTunnel = ApplicationTunnel{Protocol: w.ApplicationTunnel.Protocol.String(), Port: w.ApplicationTunnel.Port}
}
if len(w.Services) > 0 {
services := make([]string, 0, len(w.Services))
for svc := range w.Services {
services = append(services, svc)
}
out.Services = services
}
return out
}
func ConvertService(s *workloadapi.Service) *Service {
vips := make([]string, 0, len(s.Addresses))
for _, addr := range s.Addresses {
vips = append(vips, addr.Network+"/"+net.IP(addr.Address).String())
}
var waypoint string
if waypointAddress := s.Waypoint.GetAddress(); waypointAddress != nil {
waypoint = waypointAddress.Network + "/" + net.IP(waypointAddress.Address).String()
} else if host := s.Waypoint.GetHostname(); host != nil {
waypoint = host.Namespace + "/" + host.Hostname
}
out := &Service{
Name: s.Name,
Namespace: s.Namespace,
Hostname: s.Hostname,
Addresses: vips,
Ports: s.Ports,
Waypoint: &Waypoint{Destination: waypoint},
}
if s.LoadBalancing != nil {
routingPreferences := make([]string, 0, len(s.LoadBalancing.RoutingPreference))
for _, p := range s.LoadBalancing.RoutingPreference {
routingPreferences = append(routingPreferences, p.String())
}
out.LoadBalancer = &LoadBalancer{Mode: s.LoadBalancing.Mode.String(), RoutingPreferences: routingPreferences}
}
return out
}