/
common.go
129 lines (105 loc) · 3.44 KB
/
common.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package k8s provides a client for interacting with a Kubernetes cluster.
package k8s
import (
"fmt"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"github.com/go-logr/logr/funcr"
"k8s.io/client-go/kubernetes"
// Include the cloud auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// cannot import config.ZarfManagedByLabel due to import cycle
const zarfManagedByLabel = "app.kubernetes.io/managed-by"
// New creates a new K8s client.
func New(logger Log, defaultLabels Labels) (*K8s, error) {
klog.SetLogger(funcr.New(func(_, args string) {
logger(args)
}, funcr.Options{}))
config, clientset, err := connect()
if err != nil {
return nil, fmt.Errorf("failed to connect to k8s cluster: %w", err)
}
return &K8s{
RestConfig: config,
Clientset: clientset,
Log: logger,
Labels: defaultLabels,
}, nil
}
// NewWithWait is a convenience function that creates a new K8s client and waits for the cluster to be healthy.
func NewWithWait(logger Log, defaultLabels Labels, timeout time.Duration) (*K8s, error) {
k, err := New(logger, defaultLabels)
if err != nil {
return nil, err
}
return k, k.WaitForHealthyCluster(timeout)
}
// WaitForHealthyCluster checks for an available K8s cluster every second until timeout.
func (k *K8s) WaitForHealthyCluster(timeout time.Duration) error {
var err error
var nodes *v1.NodeList
var pods *v1.PodList
expired := time.After(timeout)
for {
select {
// on timeout abort
case <-expired:
return fmt.Errorf("timed out waiting for cluster to report healthy")
// after delay, try running
default:
if k.RestConfig == nil || k.Clientset == nil {
config, clientset, err := connect()
if err != nil {
k.Log("Cluster connection not available yet: %w", err)
continue
}
k.RestConfig = config
k.Clientset = clientset
}
// Make sure there is at least one running Node
nodes, err = k.GetNodes()
if err != nil || len(nodes.Items) < 1 {
k.Log("No nodes reporting healthy yet: %#v\n", err)
continue
}
// Get the cluster pod list
if pods, err = k.GetAllPods(); err != nil {
k.Log("Could not get the pod list: %w", err)
continue
}
// Check that at least one pod is in the 'succeeded' or 'running' state
for _, pod := range pods.Items {
// If a valid pod is found, return no error
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodRunning {
return nil
}
}
k.Log("No pods reported 'succeeded' or 'running' state yet.")
}
// delay check 1 seconds
time.Sleep(1 * time.Second)
}
}
// Use the K8s "client-go" library to get the currently active kube context, in the same way that
// "kubectl" gets it if no extra config flags like "--kubeconfig" are passed.
func connect() (config *rest.Config, clientset *kubernetes.Clientset, err error) {
// Build the config from the currently active kube context in the default way that the k8s client-go gets it, which
// is to look at the KUBECONFIG env var
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, nil, err
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, err
}
return config, clientset, nil
}