-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (52 loc) · 1.61 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
package main
import (
"context"
"log"
"github.com/bhechinger/k8s_nginx_ingress_whitelist/internal/cidr"
"github.com/bhechinger/k8s_nginx_ingress_whitelist/internal/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func main() {
cfg, err := config.New("0.0.2", "K8s CronJob to update NGINX Ingress whitelist")
if err != nil {
log.Fatal(err)
}
// TODO: Maybe make this so you can run outside of the cluster. I'm not sure why anyone would want to do that, however.
// k8sConfig, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
// if err != nil {
// panic(err.Error())
// }
k8sConfig, err := rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
k8sClientSet, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
log.Fatal(err)
}
configMap, err := k8sClientSet.CoreV1().ConfigMaps(cfg.ConfigMap.Namespace).Get(context.Background(), cfg.ConfigMap.Name, metav1.GetOptions{})
if err != nil {
log.Fatal(err)
}
newConfigMap := configMap.DeepCopy()
data := newConfigMap.Data
if data == nil {
data = make(map[string]string)
}
cidrs, err := cidr.GetCIDRList(cfg.SourceURIs)
if err != nil {
log.Fatal(err)
}
data[cfg.NginxConfigName] = cidrs
newConfigMap.Data = data
// if the lists are different update the ConfigMap
if configMap.Data[cfg.NginxConfigName] != newConfigMap.Data[cfg.NginxConfigName] {
log.Println("Updating ConfigMap")
_, err = k8sClientSet.CoreV1().ConfigMaps(cfg.ConfigMap.Namespace).Update(context.Background(), newConfigMap, metav1.UpdateOptions{})
if err != nil {
log.Fatal(err)
}
}
}