forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpa.go
161 lines (145 loc) · 4.27 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
// 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"
)
const commandTimeout = 1 * time.Minute
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 returns the HPA definition specified in a given namespace
func Get(name, namespace string) (*HPA, error) {
cmd := exec.Command("k", "get", "hpa", "-o", "json", "-n", namespace, name)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to run 'kubectl get hpa':%s\n", string(out))
return nil, err
}
h := HPA{}
err = json.Unmarshal(out, &h)
if err != nil {
log.Printf("Error unmarshalling service json:%s\n", err)
return nil, err
}
return &h, nil
}
// 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
}
// 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
}
// Delete will delete a HPA in a given namespace
func (h *HPA) Delete(retries int) error {
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, commandTimeout)
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, duration time.Duration) (bool, error) {
succeededCh := make(chan bool, 1)
errCh := make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
go func() {
for {
select {
case <-ctx.Done():
errCh <- errors.Errorf("Timeout exceeded (%s) while waiting for Pods (%s) to be deleted in namespace (%s)", duration.String(), hpaPrefix, namespace)
default:
p, err := GetAllByPrefix(hpaPrefix, namespace)
if err != nil {
errCh <- errors.Errorf("Got error while getting Pods with prefix \"%s\" in namespace \"%s\"", hpaPrefix, namespace)
}
if len(p) == 0 {
succeededCh <- true
}
time.Sleep(sleep)
}
}
}()
for {
select {
case err := <-errCh:
return false, err
case deleted := <-succeededCh:
return deleted, nil
}
}
}