-
Notifications
You must be signed in to change notification settings - Fork 84
/
routegroup.go
95 lines (77 loc) · 2.16 KB
/
routegroup.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
package kubernetes
import (
"encoding/json"
"fmt"
"io"
)
type routegroupList struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Metadata routegroupListMetadata `json:"metadata"`
Items []*routegroup `json:"items"`
}
type routegroup struct {
Metadata kubeItemMetadata `json:"metadata"`
Spec routegroupSpec `json:"spec"`
Status routegroupStatus `json:"status"`
}
type routegroupListMetadata struct {
SelfLink string `json:"selfLink"`
ResourceVersion string `json:"resourceVersion"`
}
type routegroupSpec struct {
Hosts []string `json:"hosts"`
}
type routegroupStatus struct {
LoadBalancer routegroupLoadBalancerStatus `json:"loadBalancer"`
}
type routegroupLoadBalancerStatus struct {
Routegroup []routegroupLoadBalancer `json:"routegroup"`
}
type routegroupLoadBalancer struct {
Hostname string `json:"hostname"`
}
const (
routegroupListResource = "/apis/zalando.org/v1/routegroups"
routegroupNamespacedResource = "/apis/zalando.org/v1/namespaces/%s/routegroups/%s"
routegroupPatchStatusResource = "/apis/zalando.org/v1/namespaces/%s/routegroups/%s/status"
)
func listRoutegroups(c client) (*routegroupList, error) {
r, err := c.get(routegroupListResource)
if err != nil {
return nil, err
}
defer r.Close()
b, err := io.ReadAll(r)
if err != nil {
return nil, err
}
var result routegroupList
if err := json.Unmarshal(b, &result); err != nil {
return nil, err
}
return &result, nil
}
type patchRoutegroupStatus struct {
Status routegroupStatus `json:"status"`
}
func updateRoutegroupLoadBalancer(c client, ns, name, newHostName string) error {
patchStatus := patchRoutegroupStatus{
Status: routegroupStatus{
LoadBalancer: routegroupLoadBalancerStatus{
Routegroup: []routegroupLoadBalancer{{Hostname: newHostName}},
},
},
}
resource := fmt.Sprintf(routegroupPatchStatusResource, ns, name)
payload, err := json.Marshal(patchStatus)
if err != nil {
return err
}
r, err := c.patch(resource, payload)
if err != nil {
return fmt.Errorf("failed to patch routegroup %s/%s = %q: %w", ns, name, newHostName, err)
}
defer r.Close()
return nil
}