-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (64 loc) · 1.81 KB
/
main.go
File metadata and controls
77 lines (64 loc) · 1.81 KB
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
package main
import (
"context"
"flag"
"path/filepath"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
)
const (
Service = "ingress-nginx"
NameSpace = "kube-system"
defaultConfig = ".kube/config"
)
func main() {
klog.InitFlags(nil)
flag.Parse()
if len(externalIP) == 0 {
klog.Fatal("empty external ip")
}
// Build the kubeconfig from kubeConfig
if len(kubeconfig) == 0 {
kubeconfig = defaultConfig
}
config, err := clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), defaultConfig))
if err != nil {
klog.Fatal(err)
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
klog.Fatal(err)
}
service, err := clientSet.CoreV1().Services(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
klog.Fatal(err)
}
for _, extIp := range service.Spec.ExternalIPs {
if externalIP == extIp {
klog.Infof("externalIp %q already binded on service %s/%s", externalIP, namespace, name)
return
}
}
service.Spec.ExternalIPs = []string{externalIP}
service.Spec.Type = v1.ServiceTypeLoadBalancer
if _, err = clientSet.CoreV1().Services(namespace).Update(context.TODO(), service, metav1.UpdateOptions{}); err != nil {
klog.Fatal(err)
}
klog.Infof("externalIp %q binded on service %s/%s", externalIP, namespace, name)
}
var (
kubeconfig string
name string
namespace string
externalIP string
)
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&name, "name", Service, "service name")
flag.StringVar(&namespace, "namespace", NameSpace, "service namespace")
flag.StringVar(&externalIP, "externalip", "", "service external ip")
}