Skip to content

Commit

Permalink
Changes to check label in deployment if ingress is required before in…
Browse files Browse the repository at this point in the history
…g creation. Added error handling if svc or ing already exists
  • Loading branch information
abhilashshetty004 committed Feb 4, 2022
1 parent f2606f3 commit 4cae449
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
43 changes: 37 additions & 6 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ func (c *controller) syncDeployment(ns, name string) error {
return err
}

/*if dep.Name == "nginxd" {
fmt.Println(dep.Name)
var ingReq string
for key, val := range dep.ObjectMeta.Labels {
if key == "ingReq" {
ingReq = val
}
}
fmt.Printf("Ingress resource for the deployment object is: %s\n", ingReq)
}*/

svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: dep.Name,
Expand All @@ -134,17 +145,32 @@ func (c *controller) syncDeployment(ns, name string) error {
},
},
}

service, err := c.clientset.CoreV1().Services(ns).Create(ctx, &svc, metav1.CreateOptions{})
if apierror.IsAlreadyExists(err) {
fmt.Printf("Service %s already exists\n", dep.Name)
return nil
}
if err != nil {
fmt.Printf("Service creation failed %s\n", err.Error())
fmt.Printf("Service creation failed: %s\n", err.Error())
return err

}
fmt.Printf("Created service %s in %s ns\n", service.Name, service.Namespace)
//create ingress
ingerror := createIngress(ctx, c.clientset, service)
if ingerror != nil {
return ingerror

ingReq := "notNeeded"
for key, val := range dep.ObjectMeta.Labels {
if key == "ingReq" {
ingReq = val
}
}
fmt.Printf("Ingress resource for the deployment object is: %s\n", ingReq)
if ingReq == "needed" {
//create ingress
ingerror := createIngress(ctx, c.clientset, service)
if ingerror != nil {
return ingerror
}
return nil
}
return nil
}
Expand Down Expand Up @@ -183,7 +209,12 @@ func createIngress(ctx context.Context, client kubernetes.Interface, svc *corev1
},
}
ing, err := client.NetworkingV1().Ingresses(svc.Namespace).Create(ctx, &ingress, metav1.CreateOptions{})
if apierror.IsAlreadyExists(err) {
fmt.Printf("Ingress %s already exists\n", ing.Name)
return nil
}
if err != nil {
fmt.Printf("Ingress creation failed: %s\n", err.Error())
return err
}
fmt.Printf("Created ingress %s in %s ns\n", ing.Name, ing.Namespace)
Expand Down
Binary file modified ekspose
Binary file not shown.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
fmt.Printf("Error %s building config from flag\n", err.Error())
config, err = rest.InClusterConfig()
if err != nil {
fmt.Printf("Error in getting incluster config", err.Error())
fmt.Printf("Error in getting incluster config %s", err.Error())
}
}
clientset, err := kubernetes.NewForConfig(config)
Expand Down
25 changes: 25 additions & 0 deletions manifests/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginxd
ingReq: needed
name: nginxd
spec:
replicas: 1
selector:
matchLabels:
app: nginxd
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginxd
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
2 changes: 1 addition & 1 deletion manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spec:
app: ekspose
spec:
containers:
- image: abhilashshetty04/ekspose:0.1.0
- image: abhilashshetty04/ekspose:0.1.1
name: ekspose
resources: {}
serviceAccountName: ekspose-sa
Expand Down

0 comments on commit 4cae449

Please sign in to comment.