Skip to content

Commit

Permalink
Update Team ingress on challenge deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Jul 30, 2023
1 parent 1ed4e2c commit 883ac3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 58 deletions.
47 changes: 0 additions & 47 deletions lib/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/sdslabs/katana/types"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -422,49 +421,3 @@ func CreateService(clientset *kubernetes.Clientset, serviceName string, namespac

return nil
}

func CreateIngress(clientset *kubernetes.Clientset, ingressName string, namespace string, serviceName string, servicePort int32, host string) error {
ingressClassName := "nginx"

ingress := &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: ingressName,
},
Spec: networkingv1.IngressSpec{
IngressClassName: &ingressClassName,
Rules: []networkingv1.IngressRule{
{
Host: host,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
PathType: func() *networkingv1.PathType {
pathType := networkingv1.PathTypePrefix
return &pathType
}(),
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: serviceName,
Port: networkingv1.ServiceBackendPort{
Number: servicePort,
},
},
},
},
},
},
},
},
},
},
}

_, err := clientset.NetworkingV1().Ingresses(namespace).Create(context.Background(), ingress, metav1.CreateOptions{})
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion services/challengedeployerservice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func DeployChallenge(c *fiber.Ctx) error {
log.Println("-----------Deploying challenge for team: " + strconv.Itoa(i) + " --------")
teamName := "katana-team-" + strconv.Itoa(i)
utils.DeployChallenge(folderName, teamName, patch, replicas)
url, err := createServiceAndIngressForChallenge(folderName, teamName, 3000)
url, err := createServiceAndIngressRuleForChallenge(folderName, teamName, 3000)
if err != nil {
res = append(res, []string{teamName, err.Error()})
} else {
Expand Down
56 changes: 46 additions & 10 deletions services/challengedeployerservice/helper.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package challengedeployerservice

import (
"context"
"fmt"
"log"
"os"
"regexp"

git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
configs "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/configs"
g "github.com/sdslabs/katana/configs"
"github.com/sdslabs/katana/lib/utils"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func copyChallengeIntoTsuka(dirPath string, challengeName string, challengeType string) error {
Expand Down Expand Up @@ -68,12 +71,11 @@ func copyChallengeIntoTsuka(dirPath string, challengeName string, challengeType
return nil
}

func createServiceAndIngressForChallenge(challengeName, teamName string, targetPort int32) (string, error) {

func createServiceAndIngressRuleForChallenge(challengeName, teamName string, targetPort int32) (string, error) {
kubeclient, _ := utils.GetKubeClient()
serviceName := challengeName + "-svc"
teamNamespace := teamName + "-ns"
port := int32(80)
port := int32(3000)
selector := map[string]string{
"app": challengeName,
}
Expand All @@ -82,14 +84,48 @@ func createServiceAndIngressForChallenge(challengeName, teamName string, targetP

log.Printf("Created service %s for challenge %s in namespace %s", serviceName, challengeName, teamNamespace)

// Create ingress
ingressName := challengeName + "-ingress"
ingressHost := fmt.Sprintf("%s.%s.%s", challengeName, teamName, configs.KatanaConfig.IngressHost)
utils.CreateIngress(kubeclient, ingressName, teamNamespace, serviceName, port, ingressHost)
// Get team ingress
ingressName := "team-ingress"
teamIngress, err := kubeclient.NetworkingV1().Ingresses(teamNamespace).Get(context.TODO(), ingressName, metav1.GetOptions{})
if err != nil {
return "", err
}

additionalRules := networkingv1.IngressRule{
Host: fmt.Sprintf("%s.%s.%s", challengeName, teamName, configs.KatanaConfig.IngressHost),
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
PathType: func() *networkingv1.PathType {
pt := networkingv1.PathTypePrefix
return &pt
}(),
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: serviceName,
Port: networkingv1.ServiceBackendPort{
Number: port,
},
},
},
},
},
},
},
}

teamIngress.Spec.Rules = append(teamIngress.Spec.Rules, additionalRules)

_, err = kubeclient.NetworkingV1().Ingresses(teamNamespace).Update(context.Background(), teamIngress, metav1.UpdateOptions{})
if err != nil {
return "", err
}

log.Printf("Created ingress %s for challenge %s in namespace %s", ingressName, challengeName, teamNamespace)
log.Printf("Added ingress rule for challenge %s in namespace %s", challengeName, teamNamespace)

return ingressHost, nil
return fmt.Sprintf("%s.%s.%s", challengeName, teamName, configs.KatanaConfig.IngressHost), nil
}

func createFolder(challengeName string) (message int, challengePath string) {
Expand Down

0 comments on commit 883ac3e

Please sign in to comment.