diff --git a/pkg/gitops/sync/sync.go b/pkg/gitops/sync/sync.go index b23c11240..2a1c366f8 100644 --- a/pkg/gitops/sync/sync.go +++ b/pkg/gitops/sync/sync.go @@ -31,6 +31,7 @@ import ( kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" notifv1 "github.com/fluxcd/notification-controller/api/v1" notifv1beta3 "github.com/fluxcd/notification-controller/api/v1beta3" + networkingv1 "k8s.io/api/networking/v1" "github.com/fluxcd/pkg/apis/meta" sourcev1 "github.com/fluxcd/source-controller/api/v1" @@ -262,6 +263,84 @@ func GenerateKustomizationForApp( }, nil } +func GenerateIngress( + app string, + port int32, + namespace string, + host string, + targetPath string, + httpPath string, +) (*manifestgen.Manifest, error) { + nginx := "nginx" + var pathType networkingv1.PathType + pathType = "Prefix" + + ingress := networkingv1.Ingress{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "networking.k8s.io/v1", + Kind: "Ingress", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: fmt.Sprintf("%s-ingress", app), + Namespace: namespace, + Annotations: map[string]string{ + "kubernetes.io/ingress.class": "nginx", + "nginx.ingress.kubernetes.io/configuration-snippet": `sub_filter '' ' +
+ + + +'; +proxy_set_header Accept-Encoding "";`, + }, + }, + Spec: networkingv1.IngressSpec{ + IngressClassName: &nginx, + Rules: []networkingv1.IngressRule{ + { + Host: host, + IngressRuleValue: networkingv1.IngressRuleValue{ + HTTP: &networkingv1.HTTPIngressRuleValue{ + Paths: []networkingv1.HTTPIngressPath{ + { + Backend: networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: app, + Port: networkingv1.ServiceBackendPort{ + Number: port, + }, + }, + }, + Path: httpPath, + PathType: &pathType, + }, + }, + }, + }, + }, + }, + }, + } + + ingressData, err := yaml.Marshal(ingress) + if err != nil { + return nil, err + } + + return &manifestgen.Manifest{ + Path: path.Join(targetPath, namespace, fmt.Sprintf("ingress-%s.yaml", app)), + Content: fmt.Sprintf("---\n%s", resourceToString(ingressData)), + }, nil +} + func GenerateConfigMap( configMapName string, namespace string, diff --git a/pkg/gitops/sync/sync_test.go b/pkg/gitops/sync/sync_test.go index 1e0f0a63b..2b408a82c 100644 --- a/pkg/gitops/sync/sync_test.go +++ b/pkg/gitops/sync/sync_test.go @@ -95,3 +95,23 @@ func TestGenerateKustomizationForApp(t *testing.T) { fmt.Println(output.Content) } + +func TestGenerateIngress(t *testing.T) { + app := "gimlet" + var port int32 = 9000 + namespace := "default" + host := "demo.localdev.me" + targetPath := "" + httpPath := "/" + + output, err := GenerateIngress(app, port, namespace, host, targetPath, httpPath) + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(output.Content, "networking.k8s.io/v1") { + t.Errorf("apiVersion 'networking.k8s.io/v1' not found") + } + + fmt.Println(output.Content) +}