Skip to content
This repository was archived by the owner on Mar 9, 2025. It is now read-only.

Commit aee19d2

Browse files
committed
Generate ingress draft
1 parent 2f94a2e commit aee19d2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

pkg/gitops/sync/sync.go

+79
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
3232
notifv1 "github.com/fluxcd/notification-controller/api/v1"
3333
notifv1beta3 "github.com/fluxcd/notification-controller/api/v1beta3"
34+
networkingv1 "k8s.io/api/networking/v1"
3435

3536
"github.com/fluxcd/pkg/apis/meta"
3637
sourcev1 "github.com/fluxcd/source-controller/api/v1"
@@ -262,6 +263,84 @@ func GenerateKustomizationForApp(
262263
}, nil
263264
}
264265

266+
func GenerateIngress(
267+
app string,
268+
port int32,
269+
namespace string,
270+
host string,
271+
targetPath string,
272+
httpPath string,
273+
) (*manifestgen.Manifest, error) {
274+
nginx := "nginx"
275+
var pathType networkingv1.PathType
276+
pathType = "Prefix"
277+
278+
ingress := networkingv1.Ingress{
279+
TypeMeta: metav1.TypeMeta{
280+
APIVersion: "networking.k8s.io/v1",
281+
Kind: "Ingress",
282+
},
283+
ObjectMeta: metav1.ObjectMeta{
284+
Name: fmt.Sprintf("%s-ingress", app),
285+
Namespace: namespace,
286+
Annotations: map[string]string{
287+
"kubernetes.io/ingress.class": "nginx",
288+
"nginx.ingress.kubernetes.io/configuration-snippet": `sub_filter '</body>' '
289+
<div class="bg-transparent bottom-0 md:px-0 fixed z-[2147483647] left-0 md:left-[calc(50%-390px)]">
290+
<iframe class="h-48 min-h-[initial] max-h-[initial] translate-[initial] bg-transparent border-0 block w-screen md:w-[780px]"
291+
id="github-iframe"
292+
title="Gimlet Drawer"
293+
src=""
294+
>
295+
</iframe>
296+
<script src="https://cdn.tailwindcss.com"></script>
297+
<script>
298+
fetch("https://api.github.com/repos/dzsak/deploying-a-static-site-with-netlify-sample/contents/gimlet-preview.html?ref=v0.0.1-rc.19").then(function(t){return t.json()})
299+
.then(function(t){(iframe=document.getElementById("github-iframe")).src="data:text/html;base64,"+encodeURIComponent(t.content)});
300+
</script>
301+
</body>';
302+
proxy_set_header Accept-Encoding "";`,
303+
},
304+
},
305+
Spec: networkingv1.IngressSpec{
306+
IngressClassName: &nginx,
307+
Rules: []networkingv1.IngressRule{
308+
{
309+
Host: host,
310+
IngressRuleValue: networkingv1.IngressRuleValue{
311+
HTTP: &networkingv1.HTTPIngressRuleValue{
312+
Paths: []networkingv1.HTTPIngressPath{
313+
{
314+
Backend: networkingv1.IngressBackend{
315+
Service: &networkingv1.IngressServiceBackend{
316+
Name: app,
317+
Port: networkingv1.ServiceBackendPort{
318+
Number: port,
319+
},
320+
},
321+
},
322+
Path: httpPath,
323+
PathType: &pathType,
324+
},
325+
},
326+
},
327+
},
328+
},
329+
},
330+
},
331+
}
332+
333+
ingressData, err := yaml.Marshal(ingress)
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
return &manifestgen.Manifest{
339+
Path: path.Join(targetPath, namespace, fmt.Sprintf("ingress-%s.yaml", app)),
340+
Content: fmt.Sprintf("---\n%s", resourceToString(ingressData)),
341+
}, nil
342+
}
343+
265344
func GenerateConfigMap(
266345
configMapName string,
267346
namespace string,

pkg/gitops/sync/sync_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ func TestGenerateKustomizationForApp(t *testing.T) {
9595

9696
fmt.Println(output.Content)
9797
}
98+
99+
func TestGenerateIngress(t *testing.T) {
100+
app := "gimlet"
101+
var port int32 = 9000
102+
namespace := "default"
103+
host := "demo.localdev.me"
104+
targetPath := ""
105+
httpPath := "/"
106+
107+
output, err := GenerateIngress(app, port, namespace, host, targetPath, httpPath)
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
if !strings.Contains(output.Content, "networking.k8s.io/v1") {
113+
t.Errorf("apiVersion 'networking.k8s.io/v1' not found")
114+
}
115+
116+
fmt.Println(output.Content)
117+
}

0 commit comments

Comments
 (0)