-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (57 loc) · 1.7 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"k8s-patch-app/k8s"
"os"
"os/signal"
"path/filepath"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Error occured: %v\n", err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
// Has to be done here as the defer statement wouldn't get executed in the
// main function.
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()
var (
kubeconfigPath *string
name *string
patch *string
)
// https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go#L44
if home := homedir.HomeDir(); home != "" {
kubeconfigPath = flag.String("k", filepath.Join(home, ".kube", "config"), "(optional) Absolute path to the kubeconfig file")
} else {
kubeconfigPath = flag.String("k", "", "Absolute path to the kubeconfig file")
}
name = flag.String("n", "", "Deployment name to patch")
patch = flag.String("p", "", "JSON patch")
flag.Parse()
if *name == "" {
return errors.New("no deployment name provided")
}
if *patch == "" {
return errors.New("no JSON patch to apply")
}
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfigPath)
if err != nil {
return fmt.Errorf("failed to build configs from %s: %w", *kubeconfigPath, err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create a new Clientset for the kube config: %w", err)
}
jsonPatcher := k8s.NewJSONPatcher(clientset)
return jsonPatcher.PatchDeployment(ctx, "default", *name, []byte(*patch))
}