Skip to content

Commit

Permalink
Added logic to create ing using path, port supplied by dep labels
Browse files Browse the repository at this point in the history
  • Loading branch information
abhilashshetty004 committed Feb 5, 2022
1 parent 4cae449 commit e50f355
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
34 changes: 31 additions & 3 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strconv"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -119,6 +120,17 @@ func (c *controller) syncDeployment(ns, name string) error {
return err
}

port := "80"
if err != nil {
fmt.Printf("Getting deployment obj failed %s\n", err.Error())
return err
}
for key, val := range dep.ObjectMeta.Labels {
if key == "port" {
port = val
}
}
portInt, _ := strconv.Atoi(port)
/*if dep.Name == "nginxd" {
fmt.Println(dep.Name)
var ingReq string
Expand All @@ -140,7 +152,7 @@ func (c *controller) syncDeployment(ns, name string) error {
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
Port: int32(portInt),
},
},
},
Expand Down Expand Up @@ -176,6 +188,22 @@ func (c *controller) syncDeployment(ns, name string) error {
}

func createIngress(ctx context.Context, client kubernetes.Interface, svc *corev1.Service) error {
path := "/api/v1/books"
port := "80"
dep, err := client.AppsV1().Deployments(svc.Namespace).Get(ctx, svc.Name, metav1.GetOptions{})
if err != nil {
fmt.Printf("Getting deployment obj failed %s\n", err.Error())
return err
}
for key, val := range dep.ObjectMeta.Labels {
/*if key == "path" {
path = val
}*/
if key == "port" {
port = val
}
}
portInt, _ := strconv.Atoi(port)
pathtype := ntw.PathTypeExact
ingress := ntw.Ingress{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -190,13 +218,13 @@ func createIngress(ctx context.Context, client kubernetes.Interface, svc *corev1
HTTP: &ntw.HTTPIngressRuleValue{
Paths: []ntw.HTTPIngressPath{
ntw.HTTPIngressPath{
Path: "/",
Path: path,
PathType: &pathtype,
Backend: ntw.IngressBackend{
Service: &ntw.IngressServiceBackend{
Name: svc.Name,
Port: ntw.ServiceBackendPort{
Number: 80,
Number: int32(portInt),
},
},
},
Expand Down
7 changes: 7 additions & 0 deletions controller_ing.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (ingc *ingCtrller) processItems() bool {
return true
}

/*required to get path for ingress resouce
deployment, err := ingc.clientset.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{})
if apierror.IsNotFound(err) {
fmt.Printf("Deployment not found for the deleted ingress : %s. Failed obj reconcilation\n", name)
return false
}*/

fmt.Printf("Service found for the deleted ingress object : %s. Hence continuing object reconcilation\n", name)
ingerror := createIngress(ctx, ingc.clientset, service)
if ingerror != nil {
Expand Down
Binary file modified ekspose
Binary file not shown.

0 comments on commit e50f355

Please sign in to comment.