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
/
k8sapi_ops.go
109 lines (100 loc) · 2.86 KB
/
k8sapi_ops.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package operations
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/Azure/aks-engine/pkg/armhelpers"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
)
type getNodesResult struct {
nodes []v1.Node
err error
}
// GetNodes is a thin wrapper around the k8s api list nodes interface
// Pass in a pool string to filter only node objects in that AKS Engine-deployed node pool
// Pass in a waitForNumNodes int to wait for an explicit target node count before returning
func GetNodes(az armhelpers.AKSEngineClient, logger *log.Entry, apiserverURL, kubeConfig string, timeout time.Duration, pool string, waitForNumNodes int) ([]v1.Node, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ch := make(chan getNodesResult)
var mostRecentGetNodesErr error
go func() {
for {
select {
case <-ctx.Done():
return
case ch <- listNodes(az, logger, apiserverURL, kubeConfig, timeout):
time.Sleep(3 * time.Second)
}
}
}()
for {
select {
case result := <-ch:
mostRecentGetNodesErr = result.err
if result.err == nil {
var ret []v1.Node
for _, node := range result.nodes {
if strings.Contains(node.Name, pool) {
ret = append(ret, node)
}
}
if waitForNumNodes >= 0 {
if len(ret) == waitForNumNodes {
return ret, nil
}
} else {
return ret, nil
}
}
case <-ctx.Done():
return nil, errors.Errorf("GetAllNodes timed out: %s\n", mostRecentGetNodesErr)
}
}
}
func listNodes(az armhelpers.AKSEngineClient, logger *log.Entry, apiserverURL, kubeConfig string, timeout time.Duration) getNodesResult {
logger.Debugf("Instantiating a Kubernetes client object at apiserver %s", apiserverURL)
client, err := az.GetKubernetesClient(apiserverURL, kubeConfig, interval, timeout)
if err != nil {
return getNodesResult{
err: err,
}
}
logger.Debugf("Listing Nodes at apiserver %s", apiserverURL)
nodes, err := client.ListNodes()
if err != nil {
return getNodesResult{
err: err,
}
}
return getNodesResult{
nodes: nodes.Items,
err: nil,
}
}
// PrintNodes outputs nodes to stdout
func PrintNodes(nodes []v1.Node) {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 4, ' ', tabwriter.FilterHTML)
fmt.Fprintln(w, "NODE\tSTATUS\tVERSION\tOS\tKERNEL")
for _, node := range nodes {
nodeStatus := "NotReady"
for _, condition := range node.Status.Conditions {
if condition.Type == "Ready" && condition.Status == "True" {
nodeStatus = "Ready"
}
}
fmt.Fprintf(w, "%s\t", node.Name)
fmt.Fprintf(w, "%s\t", nodeStatus)
fmt.Fprintf(w, "%s\t", node.Status.NodeInfo.KubeletVersion)
fmt.Fprintf(w, "%s\t", node.Status.NodeInfo.OSImage)
fmt.Fprintf(w, "%s\n", node.Status.NodeInfo.KernelVersion)
}
w.Flush()
}