This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
hpa.go
195 lines (177 loc) · 5.1 KB
/
hpa.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package hpa
import (
"context"
"encoding/json"
"log"
"os/exec"
"regexp"
"time"
"github.com/Azure/aks-engine/test/e2e/kubernetes/util"
"github.com/pkg/errors"
)
type List struct {
HPAs []HPA `json:"items"`
}
// HPA represents a kubernetes HPA
type HPA struct {
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
Status Status `json:"status"`
}
// Metadata holds information like name, namespace, and labels
type Metadata struct {
CreatedAt time.Time `json:"creationTimestamp"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// Spec holds information like clusterIP and port
type Spec struct {
MinReplicas int `json:"minReplicas"`
MaxReplicas int `json:"maxReplicas"`
TargetCPUUtilizationPercentage int `json:"targetCPUUtilizationPercentage"`
}
// Status holds the load balancer definition
type Status struct {
LoadBalancer LoadBalancer `json:"loadBalancer"`
}
// LoadBalancer holds the ingress definitions
type LoadBalancer struct {
CurrentCPUUtilizationPercentage int `json:"currentCPUUtilizationPercentage"`
CurrentReplicas int `json:"currentReplicas"`
DesiredReplicas int `json:"desiredReplicas"`
}
// Get will return a pod with a given name and namespace
func Get(name, namespace string, retries int) (*HPA, error) {
h := HPA{}
var out []byte
var err error
for i := 0; i < retries; i++ {
cmd := exec.Command("k", "get", "hpa", "-o", "json", "-n", namespace, name)
out, err = cmd.CombinedOutput()
if err != nil {
util.PrintCommand(cmd)
log.Printf("Error getting hpa: %s\n", err)
} else {
jsonErr := json.Unmarshal(out, &h)
if jsonErr != nil {
log.Printf("Error unmarshalling hpa json:%s\n", jsonErr)
err = jsonErr
}
}
time.Sleep(3 * time.Second)
}
return &h, err
}
// GetAll will return all HPA resources in a given namespace
func GetAll(namespace string) (*List, error) {
cmd := exec.Command("k", "get", "hpa", "-n", namespace, "-o", "json")
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error getting hpa:\n")
util.PrintCommand(cmd)
return nil, err
}
hl := List{}
err = json.Unmarshal(out, &hl)
if err != nil {
log.Printf("Error unmarshalling pods json:%s\n", err)
return nil, err
}
return &hl, nil
}
// GetAllByPrefixResult is a return struct for GetAllByPrefixAsync
type GetAllByPrefixResult struct {
hpas []HPA
err error
}
// GetAllByPrefixAsync wraps GetAllByPrefix with a struct response for goroutine + channel usage
func GetAllByPrefixAsync(prefix, namespace string) GetAllByPrefixResult {
hpas, err := GetAllByPrefix(prefix, namespace)
return GetAllByPrefixResult{
hpas: hpas,
err: err,
}
}
// GetAllByPrefix will return all pods in a given namespace that match a prefix
func GetAllByPrefix(prefix, namespace string) ([]HPA, error) {
hl, err := GetAll(namespace)
if err != nil {
return nil, err
}
hpas := []HPA{}
for _, h := range hl.HPAs {
matched, err := regexp.MatchString(prefix+"-.*", h.Metadata.Name)
if err != nil {
log.Printf("Error trying to match pod name:%s\n", err)
return nil, err
}
if matched {
hpas = append(hpas, h)
}
}
return hpas, nil
}
// Describe will describe a HPA resource
func (h *HPA) Describe() error {
var commandTimeout time.Duration
cmd := exec.Command("k", "describe", "hpa", h.Metadata.Name, "-n", h.Metadata.Namespace)
out, err := util.RunAndLogCommand(cmd, commandTimeout)
log.Printf("\n%s\n", string(out))
return err
}
// Delete will delete a HPA in a given namespace
func (h *HPA) Delete(retries int) error {
var zeroValueDuration time.Duration
var kubectlOutput []byte
var kubectlError error
for i := 0; i < retries; i++ {
cmd := exec.Command("k", "delete", "hpa", "-n", h.Metadata.Namespace, h.Metadata.Name)
kubectlOutput, kubectlError = util.RunAndLogCommand(cmd, zeroValueDuration)
if kubectlError != nil {
log.Printf("Error while trying to delete service %s in namespace %s:%s\n", h.Metadata.Namespace, h.Metadata.Name, string(kubectlOutput))
continue
}
break
}
return kubectlError
}
// WaitOnDeleted returns when an hpa resource is successfully deleted
func WaitOnDeleted(hpaPrefix, namespace string, sleep, timeout time.Duration) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ch := make(chan GetAllByPrefixResult)
var mostRecentWaitOnDeletedError error
var hpas []HPA
go func() {
for {
select {
case <-ctx.Done():
return
case ch <- GetAllByPrefixAsync(hpaPrefix, namespace):
time.Sleep(sleep)
}
}
}()
for {
select {
case result := <-ch:
mostRecentWaitOnDeletedError = result.err
hpas = result.hpas
if mostRecentWaitOnDeletedError == nil {
if len(hpas) == 0 {
return true, nil
}
}
case <-ctx.Done():
for _, hpa := range hpas {
err := hpa.Describe()
if err != nil {
log.Printf("Unable to describe hpa %s: %s", hpa.Metadata.Name, err)
}
}
return false, errors.Errorf("WaitOnDeleted timed out: %s\n", mostRecentWaitOnDeletedError)
}
}
}