-
Notifications
You must be signed in to change notification settings - Fork 15
/
controller.go
194 lines (171 loc) · 4.82 KB
/
controller.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"context"
"fmt"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
appsinformers "k8s.io/client-go/informers/apps/v1"
"k8s.io/client-go/kubernetes"
appslisters "k8s.io/client-go/listers/apps/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
type controller struct {
clientset kubernetes.Interface
depLister appslisters.DeploymentLister
depCacheSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
}
func newController(clientset kubernetes.Interface, depInformer appsinformers.DeploymentInformer) *controller {
c := &controller{
clientset: clientset,
depLister: depInformer.Lister(),
depCacheSynced: depInformer.Informer().HasSynced,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ekspose"),
}
depInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: c.handleAdd,
DeleteFunc: c.handleDel,
},
)
return c
}
func (c *controller) run(ch <-chan struct{}) {
fmt.Println("starting controller")
if !cache.WaitForCacheSync(ch, c.depCacheSynced) {
fmt.Print("waiting for cache to be synced\n")
}
go wait.Until(c.worker, 1*time.Second, ch)
<-ch
}
func (c *controller) worker() {
for c.processItem() {
}
}
func (c *controller) processItem() bool {
item, shutdown := c.queue.Get()
if shutdown {
return false
}
defer c.queue.Forget(item)
key, err := cache.MetaNamespaceKeyFunc(item)
if err != nil {
fmt.Printf("getting key from cahce %s\n", err.Error())
}
ns, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
fmt.Printf("splitting key into namespace and name %s\n", err.Error())
return false
}
// check if the object has been deleted from k8s cluster
ctx := context.Background()
_, err = c.clientset.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
fmt.Printf("handle delete event for dep %s\n", name)
// delete service
err := c.clientset.CoreV1().Services(ns).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
fmt.Printf("deleting service %s, error %s\n", name, err.Error())
return false
}
err = c.clientset.NetworkingV1().Ingresses(ns).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
fmt.Printf("deleting ingrss %s, error %s\n", name, err.Error())
return false
}
return true
}
err = c.syncDeployment(ns, name)
if err != nil {
// re-try
fmt.Printf("syncing deployment %s\n", err.Error())
return false
}
return true
}
func (c *controller) syncDeployment(ns, name string) error {
ctx := context.Background()
dep, err := c.depLister.Deployments(ns).Get(name)
if err != nil {
fmt.Printf("getting deployment from lister %s\n", err.Error())
}
// create service
// we have to modify this, to figure out the port
// our deployment's container is listening on
svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: dep.Name,
Namespace: ns,
},
Spec: corev1.ServiceSpec{
Selector: depLabels(*dep),
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
},
},
},
}
s, err := c.clientset.CoreV1().Services(ns).Create(ctx, &svc, metav1.CreateOptions{})
if err != nil {
fmt.Printf("creating service %s\n", err.Error())
}
// create ingress
return createIngress(ctx, c.clientset, s)
}
func createIngress(ctx context.Context, client kubernetes.Interface, svc *corev1.Service) error {
pathType := "Prefix"
ingress := netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: svc.Name,
Namespace: svc.Namespace,
Annotations: map[string]string{
"nginx.ingress.kubernetes.io/rewrite-target": "/",
},
},
Spec: netv1.IngressSpec{
Rules: []netv1.IngressRule{
netv1.IngressRule{
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
netv1.HTTPIngressPath{
Path: fmt.Sprintf("/%s", svc.Name),
PathType: (*netv1.PathType)(&pathType),
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: svc.Name,
Port: netv1.ServiceBackendPort{
Number: 80,
},
},
},
},
},
},
},
},
},
},
}
_, err := client.NetworkingV1().Ingresses(svc.Namespace).Create(ctx, &ingress, metav1.CreateOptions{})
return err
}
func depLabels(dep appsv1.Deployment) map[string]string {
return dep.Spec.Template.Labels
}
func (c *controller) handleAdd(obj interface{}) {
fmt.Println("add was called")
c.queue.Add(obj)
}
func (c *controller) handleDel(obj interface{}) {
fmt.Println("del was called")
c.queue.Add(obj)
}