-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
main.go
108 lines (95 loc) · 2.93 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
// This code builds a minimal binary of the lightweight kubernetes
// client and exposes it for manual testing.
// The intention is that the binary can be built and dropped into
// a Kube environment like this:
// https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/
// Then, commands can be run to test its API calls.
// The above commands are intended to be run inside an instance of
// minikube that has been started.
// After building this binary, place it in the container like this:
// $ kubectl cp kubeclient /shell-demo:/
// At first you may get 403's, which can be resolved using this:
// https://github.com/fabric8io/fabric8/issues/6840#issuecomment-307560275
//
// Example calls:
// ./kubeclient -call='get-pod' -namespace='default' -pod-name='shell-demo'
// ./kubeclient -call='patch-pod' -namespace='default' -pod-name='shell-demo' -patches='/metadata/labels/fizz:buzz,/metadata/labels/foo:bar'
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/serviceregistration/kubernetes/client"
)
var callToMake string
var patchesToAdd string
var namespace string
var podName string
func init() {
flag.StringVar(&callToMake, "call", "", `the call to make: 'get-pod' or 'patch-pod'`)
flag.StringVar(&patchesToAdd, "patches", "", `if call is "patch-pod", the patches to do like so: "/metadata/labels/fizz:buzz,/metadata/labels/foo:bar"`)
flag.StringVar(&namespace, "namespace", "", "the namespace to use")
flag.StringVar(&podName, "pod-name", "", "the pod name to use")
}
func main() {
flag.Parse()
c, err := client.New(hclog.Default())
if err != nil {
panic(err)
}
reqCh := make(chan struct{})
shutdownCh := makeShutdownCh()
go func() {
defer close(reqCh)
switch callToMake {
case "get-pod":
pod, err := c.GetPod(namespace, podName)
if err != nil {
panic(err)
}
b, _ := json.Marshal(pod)
fmt.Printf("pod: %s\n", b)
return
case "patch-pod":
patchPairs := strings.Split(patchesToAdd, ",")
var patches []*client.Patch
for _, patchPair := range patchPairs {
fields := strings.Split(patchPair, ":")
if len(fields) != 2 {
panic(fmt.Errorf("unable to split %s from selectors provided of %s", fields, patchesToAdd))
}
patches = append(patches, &client.Patch{
Operation: client.Replace,
Path: fields[0],
Value: fields[1],
})
}
if err := c.PatchPod(namespace, podName, patches...); err != nil {
panic(err)
}
return
default:
panic(fmt.Errorf(`unsupported call provided: %q`, callToMake))
}
}()
select {
case <-shutdownCh:
fmt.Println("Interrupt received, exiting...")
case <-reqCh:
}
}
func makeShutdownCh() chan struct{} {
resultCh := make(chan struct{})
shutdownCh := make(chan os.Signal, 4)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-shutdownCh
close(resultCh)
}()
return resultCh
}