Skip to content

Commit cad6a0c

Browse files
added the ingress
1 parent 3009d32 commit cad6a0c

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

controller/controller.go

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package controller
22

3+
// https://danielms.site/zet/2024/client-go-kubernetes-deploymentservice-and-ingress/
34
import (
45
"context"
56
"fmt"
67
appsv1 "k8s.io/api/apps/v1"
78
corev1 "k8s.io/api/core/v1"
9+
networkingv1 "k8s.io/api/networking/v1"
810
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
"k8s.io/apimachinery/pkg/util/wait"
1012
appsInformer "k8s.io/client-go/informers/apps/v1"
@@ -106,18 +108,93 @@ func (c *controller) syncDeployment(ns, name string) error {
106108
fmt.Printf("sync deployment, %s\n", err.Error())
107109
}
108110

111+
err = c.createIngress(ns, name)
112+
if err != nil {
113+
fmt.Printf("sync deployment, %s\n", err.Error())
114+
}
109115
return nil
110116
}
111117

118+
func (c *controller) createIngress(ns, name string) error {
119+
ctx := context.Background()
120+
pathType := "Prefix"
121+
ingress := networkingv1.Ingress{
122+
ObjectMeta: metav1.ObjectMeta{
123+
Name: name,
124+
Namespace: ns,
125+
Annotations: map[string]string{
126+
"nginx.ingress.kubernetes.io/rewrite-target": "/",
127+
},
128+
},
129+
Spec: networkingv1.IngressSpec{
130+
Rules: []networkingv1.IngressRule{
131+
networkingv1.IngressRule{
132+
Host: "demo.local",
133+
IngressRuleValue: networkingv1.IngressRuleValue{
134+
HTTP: &networkingv1.HTTPIngressRuleValue{
135+
Paths: []networkingv1.HTTPIngressPath{
136+
networkingv1.HTTPIngressPath{
137+
Path: fmt.Sprintf("/%s", name),
138+
PathType: (*networkingv1.PathType)(&pathType),
139+
Backend: networkingv1.IngressBackend{
140+
Service: &networkingv1.IngressServiceBackend{
141+
Name: name,
142+
Port: networkingv1.ServiceBackendPort{
143+
Number: 80,
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
},
151+
},
152+
},
153+
},
154+
}
155+
_, err := c.clientset.NetworkingV1().Ingresses(ns).Create(ctx, &ingress, metav1.CreateOptions{})
156+
if err != nil {
157+
return err
158+
}
159+
return nil
160+
161+
}
162+
112163
func depLabels(dep appsv1.Deployment) map[string]string {
113164
return dep.Spec.Template.Labels
114165
}
166+
167+
// Almost working
115168
func (c *controller) handleAdd(obj interface{}) {
116-
fmt.Println("hello add is called")
169+
//fmt.Println("hello add is called")
170+
item, ok := obj.(*appsv1.Deployment)
171+
if !ok {
172+
fmt.Println("\n Not a Deployemt")
173+
return
174+
}
175+
fmt.Printf("Deployment \n")
176+
fmt.Printf(item.Name)
177+
fmt.Printf(item.Kind)
178+
179+
fmt.Printf("ADDED: %s", "Kind=%s, Name=%s, Namespace=%s, UID=%s", item.CreationTimestamp,
180+
item.Kind, item.Name, item.Namespace, item.UID)
181+
117182
c.queue.Add(obj)
118183
}
119184

185+
// Not tested
120186
func (c *controller) handleDel(obj interface{}) {
121-
fmt.Println("hello del is called")
187+
item, ok := obj.(*appsv1.Deployment)
188+
if !ok {
189+
fmt.Println("\n Not a Deployemt")
190+
return
191+
}
192+
fmt.Printf("Deployment \n")
193+
fmt.Printf(item.Name)
194+
fmt.Printf(item.Kind)
195+
196+
fmt.Printf("DELETED: %s", "Kind=%s, Name=%s, Namespace=%s, UID=%s", item.CreationTimestamp,
197+
item.Kind, item.Name, item.Namespace, item.UID)
198+
122199
c.queue.Add(obj)
123200
}

ingress.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: demo-ingress
5+
annotations:
6+
nginx.ingress.kubernetes.io/rewrite-target: /
7+
spec:
8+
ingressClassName: nginx
9+
rules:
10+
- host: demo.local
11+
http:
12+
paths:
13+
- path: /
14+
pathType: Prefix
15+
backend:
16+
service:
17+
name: demo
18+
port:
19+
number: 80
20+

0 commit comments

Comments
 (0)