|
1 | 1 | package controller
|
2 | 2 |
|
| 3 | +// https://danielms.site/zet/2024/client-go-kubernetes-deploymentservice-and-ingress/ |
3 | 4 | import (
|
4 | 5 | "context"
|
5 | 6 | "fmt"
|
6 | 7 | appsv1 "k8s.io/api/apps/v1"
|
7 | 8 | corev1 "k8s.io/api/core/v1"
|
| 9 | + networkingv1 "k8s.io/api/networking/v1" |
8 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
9 | 11 | "k8s.io/apimachinery/pkg/util/wait"
|
10 | 12 | appsInformer "k8s.io/client-go/informers/apps/v1"
|
@@ -106,18 +108,93 @@ func (c *controller) syncDeployment(ns, name string) error {
|
106 | 108 | fmt.Printf("sync deployment, %s\n", err.Error())
|
107 | 109 | }
|
108 | 110 |
|
| 111 | + err = c.createIngress(ns, name) |
| 112 | + if err != nil { |
| 113 | + fmt.Printf("sync deployment, %s\n", err.Error()) |
| 114 | + } |
109 | 115 | return nil
|
110 | 116 | }
|
111 | 117 |
|
| 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 | + |
112 | 163 | func depLabels(dep appsv1.Deployment) map[string]string {
|
113 | 164 | return dep.Spec.Template.Labels
|
114 | 165 | }
|
| 166 | + |
| 167 | +// Almost working |
115 | 168 | 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 | + |
117 | 182 | c.queue.Add(obj)
|
118 | 183 | }
|
119 | 184 |
|
| 185 | +// Not tested |
120 | 186 | 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 | + |
122 | 199 | c.queue.Add(obj)
|
123 | 200 | }
|
0 commit comments