forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpa.go
83 lines (71 loc) · 2.31 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package hpa
import (
"encoding/json"
"log"
"os/exec"
"time"
"github.com/Azure/aks-engine/test/e2e/kubernetes/util"
)
const commandTimeout = 1 * time.Minute
// 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
}
// 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
}