-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (74 loc) · 2.49 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
//
// Uncomment to load all auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
func watchDeployment(namespace, deploymentName string, client *kubernetes.Clientset, sleep int) {
// loop until break condition
for {
// get the deployments details
deployment, err := client.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
panic(fmt.Sprintf("Deployment %s not found in %s namespace", deploymentName, namespace))
} else {
panic(err.Error())
}
}
// if deployment has more than 0 replicas and desired replicas are ready, break the loop
if deployment.Status.Replicas > 0 && deployment.Status.ReadyReplicas == deployment.Status.Replicas {
break
} else {
// report status of deployment
fmt.Printf("%d of %d replicas ready\n", deployment.Status.ReadyReplicas, deployment.Status.Replicas)
}
// wait a bit before we check again
time.Sleep(time.Duration(sleep) * time.Second)
}
}
func main() {
// arg parsing
var namespace string
var deploymentName string
var sleep int
// -namespace
flag.StringVar(&namespace, "namespace", "", "Namespace of deployment")
// -deployment
flag.StringVar(&deploymentName, "deployment", "", "Deployment name")
// -sleep
flag.IntVar(&sleep, "sleep", 10, "Number of seconds between checking the deployment")
flag.Parse()
if namespace == "" || deploymentName == "" {
flag.Usage()
os.Exit(1)
}
fmt.Printf("Monitoring deployment %s in the %s namespace\n", deploymentName, namespace)
// creates the in-cluster config from service account
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the k8s api client
client, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// https://github.com/kubernetes/client-go/blob/master/examples/in-cluster-client-configuration/main.go
watchDeployment(namespace, deploymentName, client, sleep)
fmt.Println("Ready to go!")
}