-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (110 loc) · 3.06 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"flag"
"log"
"math/rand"
"time"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
var (
minWatchTimeout = 5 * time.Minute
)
func main() {
masterURL := flag.String("master", "", "kubernetes api server url")
kubeconfigPath := flag.String("kubeconfig", "", "path to kubeconfig file")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags(*masterURL, *kubeconfigPath)
if err != nil {
log.Fatalln(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalln(err)
}
nsCh := make(chan *v1.Namespace, 128)
go nsWatcher(clientset, nsCh)
for ns := range nsCh {
err := handleNs(clientset, ns)
if err != nil {
log.Println(err)
}
}
}
func nsWatcher(clientset *kubernetes.Clientset, c chan *v1.Namespace) {
for {
err := internalNsWatcher(clientset, c)
if statusErr, ok := err.(*apierrs.StatusError); ok {
if statusErr.ErrStatus.Reason == metav1.StatusReasonExpired {
log.Println("nsWatcher:", err, "Restarting watch")
continue
}
}
log.Fatalln(err)
}
}
func internalNsWatcher(clientset *kubernetes.Clientset, c chan *v1.Namespace) error {
list, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
resourceVersion := list.ResourceVersion
for {
log.Println("nsWatcher: watching since", resourceVersion)
timeoutSeconds := int64(minWatchTimeout.Seconds() * (rand.Float64() + 1.0))
watcher, err := clientset.CoreV1().Namespaces().Watch(context.TODO(), metav1.ListOptions{
ResourceVersion: resourceVersion,
TimeoutSeconds: &timeoutSeconds,
})
if err != nil {
return err
}
for watchEvent := range watcher.ResultChan() {
if watchEvent.Type == watch.Error {
return apierrs.FromObject(watchEvent.Object)
}
ns, ok := watchEvent.Object.(*v1.Namespace)
if !ok {
log.Println("nsWatcher: unexpected kind:", watchEvent.Object.GetObjectKind().GroupVersionKind())
continue
}
resourceVersion = ns.ResourceVersion
if watchEvent.Type != watch.Added {
continue
}
c <- ns
}
}
}
func handleNs(clientset *kubernetes.Clientset, ns *v1.Namespace) error {
log.Println("Creating default network policy in namespace", ns.Name)
policy := &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
Spec: networkingv1.NetworkPolicySpec{
Ingress: []networkingv1.NetworkPolicyIngressRule{
networkingv1.NetworkPolicyIngressRule{
From: []networkingv1.NetworkPolicyPeer{
networkingv1.NetworkPolicyPeer{
PodSelector: &metav1.LabelSelector{},
},
},
},
},
PodSelector: metav1.LabelSelector{},
PolicyTypes: []networkingv1.PolicyType{
networkingv1.PolicyTypeIngress,
},
},
}
c := clientset.NetworkingV1().NetworkPolicies(ns.Name)
_, err := c.Create(context.TODO(), policy, metav1.CreateOptions{})
return err
}