From 4dedaad731a6159c137af409e08056685a64f2af Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:06:24 +0200 Subject: [PATCH 01/10] e2e: add emojivotodemo function --- e2e/internal/kuberesource/sets.go | 311 ++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index cd576d6b29..5028e05890 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -490,3 +490,314 @@ func Emojivoto() ([]any, error) { return resources, nil } + +// EmojivotoDemo is a simplified version of Emojivoto returning resources for deploying a demo. +func EmojivotoDemo() ([]any, error) { + ns := "default" + emoji := Deployment("emoji", ns). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "emoji", + "app.kubernetes.io/part-of": "emojivoto", + "app.kubernetes.io/version": "v11", + }). + WithSpec(DeploymentSpec(). + WithReplicas(1). + WithSelector(LabelSelector(). + WithMatchLabels(map[string]string{ + "app.kubernetes.io/name": "emoji-svc", + "version": "v11", + }), + ). + WithTemplate(PodTemplateSpec(). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "emoji-svc", + "version": "v11", + }). + WithSpec(PodSpec(). + WithRuntimeClassName("kata-cc-isolation"). + WithServiceAccountName("emoji"). + WithContainers( + Container(). + WithName("emoji-svc"). + WithImage("ghcr.io/3u13r/emojivoto-emoji-svc:coco-1"). + WithPorts( + ContainerPort(). + WithName("grpc"). + WithContainerPort(8080), + ContainerPort(). + WithName("prom"). + WithContainerPort(8801), + ). + WithEnv(EnvVar().WithName("GRPC_PORT").WithValue("8080")). + WithEnv(EnvVar().WithName("PROM_PORT").WithValue("8801")). + WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). + WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). + WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(50), + ), + ), + ), + ), + ) + emoji, err := AddInitializer(emoji, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + if err != nil { + return nil, err + } + + emojiService := ServiceForDeployment(emoji). + WithName("emoji-svc"). + WithSpec(ServiceSpec(). + WithSelector( + map[string]string{"app.kubernetes.io/name": "emoji-svc"}, + ). + WithPorts( + ServicePort(). + WithName("grpc"). + WithPort(8080). + WithTargetPort(intstr.FromInt(8080)), + ServicePort(). + WithName("prom"). + WithPort(8801). + WithTargetPort(intstr.FromInt(8801)), + ), + ) + + emojiserviceAccount := ServiceAccount("emoji", ns). + WithAPIVersion("v1"). + WithKind("ServiceAccount") + + voteBot := Deployment("vote-bot", ns). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "vote-bot", + "app.kubernetes.io/part-of": "emojivoto", + "app.kubernetes.io/version": "v11", + }). + WithSpec(DeploymentSpec(). + WithReplicas(1). + WithSelector(LabelSelector(). + WithMatchLabels(map[string]string{ + "app.kubernetes.io/name": "vote-bot", + "version": "v11", + }), + ). + WithTemplate(PodTemplateSpec(). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "vote-bot", + "version": "v11", + }). + WithSpec(PodSpec(). + WithContainers( + Container(). + WithName("vote-bot"). + WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). + WithCommand("emojivoto-vote-bot"). + WithEnv(EnvVar().WithName("WEB_HOST").WithValue("web-svc:443")). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(25), + ), + ), + ), + ), + ) + + voting := Deployment("voting", ns). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "voting", + "app.kubernetes.io/part-of": "emojivoto", + "app.kubernetes.io/version": "v11", + }). + WithSpec(DeploymentSpec(). + WithReplicas(1). + WithSelector(LabelSelector(). + WithMatchLabels(map[string]string{ + "app.kubernetes.io/name": "voting-svc", + "version": "v11", + }), + ). + WithTemplate(PodTemplateSpec(). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "voting-svc", + "version": "v11", + }). + WithSpec(PodSpec(). + WithRuntimeClassName("kata-cc-isolation"). + WithServiceAccountName("voting"). + WithContainers( + Container(). + WithName("voting-svc"). + WithImage("ghcr.io/3u13r/emojivoto-voting-svc:coco-1"). + WithPorts( + ContainerPort(). + WithName("grpc"). + WithContainerPort(8080), + ContainerPort(). + WithName("prom"). + WithContainerPort(8801), + ). + WithEnv(EnvVar().WithName("GRPC_PORT").WithValue("8080")). + WithEnv(EnvVar().WithName("PROM_PORT").WithValue("8801")). + WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). + WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). + WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(50), + ), + ), + ), + ), + ) + voting, err = AddInitializer(voting, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + if err != nil { + return nil, err + } + + votingService := ServiceForDeployment(voting). + WithName("voting-svc"). + WithSpec(ServiceSpec(). + WithSelector( + map[string]string{"app.kubernetes.io/name": "voting-svc"}, + ). + WithPorts( + ServicePort(). + WithName("grpc"). + WithPort(8080). + WithTargetPort(intstr.FromInt(8080)), + ServicePort(). + WithName("prom"). + WithPort(8801). + WithTargetPort(intstr.FromInt(8801)), + ), + ) + + votingserviceAccount := ServiceAccount("voting", ns). + WithAPIVersion("v1"). + WithKind("ServiceAccount") + + web := Deployment("web", ns). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "web", + "app.kubernetes.io/part-of": "emojivoto", + "app.kubernetes.io/version": "v11", + }). + WithSpec(DeploymentSpec(). + WithReplicas(1). + WithSelector(LabelSelector(). + WithMatchLabels(map[string]string{ + "app.kubernetes.io/name": "web-svc", + "version": "v11", + }), + ). + WithTemplate(PodTemplateSpec(). + WithLabels(map[string]string{ + "app.kubernetes.io/name": "web-svc", + "version": "v11", + }). + WithSpec(PodSpec(). + WithRuntimeClassName("kata-cc-isolation"). + WithServiceAccountName("web"). + WithContainers( + Container(). + WithName("web-svc"). + WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). + WithPorts( + ContainerPort(). + WithName("https"). + WithContainerPort(8080), + ). + WithEnv(EnvVar().WithName("WEB_PORT").WithValue("8080")). + WithEnv(EnvVar().WithName("EMOJISVC_HOST").WithValue("emoji-svc:8080")). + WithEnv(EnvVar().WithName("VOTINGSVC_HOST").WithValue("voting-svc:8080")). + WithEnv(EnvVar().WithName("INDEX_BUNDLE").WithValue("dist/index_bundle.js")). + WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). + WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). + WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). + WithEnv(EnvVar().WithName("EDG_DISABLE_CLIENT_AUTH").WithValue("true")). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(50), + ), + ), + ), + ), + ) + web, err = AddInitializer(web, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + if err != nil { + return nil, err + } + + webService := ServiceForDeployment(web). + WithName("web-svc"). + WithSpec(ServiceSpec(). + WithSelector( + map[string]string{"app.kubernetes.io/name": "web-svc"}, + ). + WithType("ClusterIP"). + WithPorts( + ServicePort(). + WithName("https"). + WithPort(443). + WithTargetPort(intstr.FromInt(8080)), + ), + ) + + webserviceAccount := ServiceAccount("web", ns). + WithAPIVersion("v1"). + WithKind("ServiceAccount") + + portforwarderCoordinator := PortForwarder("coordinator", ns). + WithSpec(PodSpec(). + WithContainers( + Container(). + WithName("port-forwarder"). + WithImage("ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0"). + WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("1313")). + WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("coordinator")). + WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("1313")). + WithCommand("/bin/bash", "-c", "echo Starting port-forward with socat; exec socat -d -d TCP-LISTEN:${LISTEN_PORT},fork TCP:${FORWARD_HOST}:${FORWARD_PORT}"). + WithPorts( + ContainerPort(). + WithContainerPort(1313), + ). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(50), + ), + ), + ) + + portforwarderemojivotoWeb := PortForwarder("emojivoto-web", ns). + WithSpec(PodSpec(). + WithContainers( + Container(). + WithName("port-forwarder"). + WithImage("ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0"). + WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("8080")). + WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("web-svc")). + WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("443")). + WithCommand("/bin/bash", "-c", "echo Starting port-forward with socat; exec socat -d -d TCP-LISTEN:${LISTEN_PORT},fork TCP:${FORWARD_HOST}:${FORWARD_PORT}"). + WithPorts( + ContainerPort(). + WithContainerPort(8080), + ). + WithResources(ResourceRequirements(). + WithMemoryLimitAndRequest(50), + ), + ), + ) + + resources := []any{ + emoji, + emojiService, + emojiserviceAccount, + portforwarderCoordinator, + portforwarderemojivotoWeb, + voteBot, + voting, + votingService, + votingserviceAccount, + web, + webService, + webserviceAccount, + } + + return resources, nil +} From dab86ba026c460c831338009c281bf4c3c6462e9 Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:07:21 +0200 Subject: [PATCH 02/10] e2e: registered emojivotodemo function in resourcegen main --- e2e/internal/kuberesource/resourcegen/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/internal/kuberesource/resourcegen/main.go b/e2e/internal/kuberesource/resourcegen/main.go index e69e516578..56528244f1 100644 --- a/e2e/internal/kuberesource/resourcegen/main.go +++ b/e2e/internal/kuberesource/resourcegen/main.go @@ -26,6 +26,8 @@ func main() { resources, err = kuberesource.OpenSSL() case "emojivoto": resources, err = kuberesource.Emojivoto() + case "emojivotodemo": + resources, err = kuberesource.EmojivotoDemo() default: fmt.Printf("Error: unknown set: %s\n", set) os.Exit(1) From 896a8d544671673ed794a48628e59d4572528a6b Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:09:13 +0200 Subject: [PATCH 03/10] justfile: added emojivotodemo target --- justfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/justfile b/justfile index 73715c88ff..b08ebb0f47 100644 --- a/justfile +++ b/justfile @@ -38,7 +38,7 @@ populate target=default_deploy_target: set -euo pipefail mkdir -p ./{{ workspace_dir }} case {{ target }} in - "simple" | "openssl" | "emojivoto") + "simple" | "openssl" | "emojivoto" | "emojivotodemo") nix shell .#contrast --command resourcegen {{ target }} ./{{ workspace_dir }}/deployment/deployment.yml ;; *) @@ -67,7 +67,7 @@ generate cli=default_cli: apply target=default_deploy_target: #!/usr/bin/env bash case {{ target }} in - "simple" | "openssl" | "emojivoto") + "simple" | "openssl" | "emojivoto" | "emojivotodemo") : ;; *) @@ -157,7 +157,7 @@ wait-for-workload target=default_deploy_target: nix run .#scripts.kubectl-wait-ready -- $ns openssl-client nix run .#scripts.kubectl-wait-ready -- $ns openssl-frontend ;; - "emojivoto" | "emojivoto-sm-egress") + "emojivoto" | "emojivoto-sm-egress" | "emojivotodemo") nix run .#scripts.kubectl-wait-ready -- $ns emoji-svc nix run .#scripts.kubectl-wait-ready -- $ns vote-bot nix run .#scripts.kubectl-wait-ready -- $ns voting-svc From 7c5c5752777fb9df83141d018db8b2af7f80e4c7 Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:48:59 +0200 Subject: [PATCH 04/10] justfile: remove emojivotodemo target --- justfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index b08ebb0f47..f43e5bc6ca 100644 --- a/justfile +++ b/justfile @@ -38,7 +38,7 @@ populate target=default_deploy_target: set -euo pipefail mkdir -p ./{{ workspace_dir }} case {{ target }} in - "simple" | "openssl" | "emojivoto" | "emojivotodemo") + "simple" | "openssl" | "emojivoto") nix shell .#contrast --command resourcegen {{ target }} ./{{ workspace_dir }}/deployment/deployment.yml ;; *) @@ -67,7 +67,7 @@ generate cli=default_cli: apply target=default_deploy_target: #!/usr/bin/env bash case {{ target }} in - "simple" | "openssl" | "emojivoto" | "emojivotodemo") + "simple" | "openssl" | "emojivoto") : ;; *) From 5b0e2cea3ddad08448bf2e2b2c60925844fa4eff Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Wed, 10 Apr 2024 14:43:52 +0200 Subject: [PATCH 05/10] e2e: rewrite emojivoto resource generation for customization --- e2e/internal/kuberesource/sets.go | 375 +++++------------------------- 1 file changed, 60 insertions(+), 315 deletions(-) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index 5028e05890..9b0a978186 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -198,302 +198,26 @@ func OpenSSL() ([]any, error) { return resources, nil } -// Emojivoto returns resources for deploying EmojiVoto application. -func Emojivoto() ([]any, error) { - ns := "edg-default" - namespace := Namespace(ns) - coordinator := Coordinator(ns).DeploymentApplyConfiguration - coordinatorService := ServiceForDeployment(coordinator) - coordinatorForwarder := PortForwarder("coordinator", ns). - WithListenPort(1313). - WithForwardTarget("coordinator", 1313). - PodApplyConfiguration +// GenerateEmojivoto returns resources for deploying EmojiVoto application. +func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string, generateCoordinatorService bool) ([]any, error) { + resources := make([]any, 0) - emoji := Deployment("emoji", ns). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "emoji", - "app.kubernetes.io/part-of": "emojivoto", - "app.kubernetes.io/version": "v11", - }). - WithSpec(DeploymentSpec(). - WithReplicas(1). - WithSelector(LabelSelector(). - WithMatchLabels(map[string]string{ - "app.kubernetes.io/name": "emoji-svc", - "version": "v11", - }), - ). - WithTemplate(PodTemplateSpec(). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "emoji-svc", - "version": "v11", - }). - WithSpec(PodSpec(). - WithRuntimeClassName("kata-cc-isolation"). - WithServiceAccountName("emoji"). - WithContainers( - Container(). - WithName("emoji-svc"). - WithImage("ghcr.io/3u13r/emojivoto-emoji-svc:coco-1"). - WithPorts( - ContainerPort(). - WithName("grpc"). - WithContainerPort(8080), - ContainerPort(). - WithName("prom"). - WithContainerPort(8801), - ). - WithEnv(EnvVar().WithName("GRPC_PORT").WithValue("8080")). - WithEnv(EnvVar().WithName("PROM_PORT").WithValue("8801")). - WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). - WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). - WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(50), - ), - ), - ), - ), - ) - emoji, err := AddInitializer(emoji, Initializer()) - if err != nil { - return nil, err + if ns == "" { + ns = "edg-default" } - emojiService := ServiceForDeployment(emoji). - WithName("emoji-svc"). - WithSpec(ServiceSpec(). - WithSelector( - map[string]string{"app.kubernetes.io/name": "emoji-svc"}, - ). - WithPorts( - ServicePort(). - WithName("grpc"). - WithPort(8080). - WithTargetPort(intstr.FromInt(8080)), - ServicePort(). - WithName("prom"). - WithPort(8801). - WithTargetPort(intstr.FromInt(8801)), - ), - ) - - emojiserviceAccount := ServiceAccount("emoji", ns). - WithAPIVersion("v1"). - WithKind("ServiceAccount") - - voteBot := Deployment("vote-bot", ns). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "vote-bot", - "app.kubernetes.io/part-of": "emojivoto", - "app.kubernetes.io/version": "v11", - }). - WithSpec(DeploymentSpec(). - WithReplicas(1). - WithSelector(LabelSelector(). - WithMatchLabels(map[string]string{ - "app.kubernetes.io/name": "vote-bot", - "version": "v11", - }), - ). - WithTemplate(PodTemplateSpec(). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "vote-bot", - "version": "v11", - }). - WithSpec(PodSpec(). - WithContainers( - Container(). - WithName("vote-bot"). - WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). - WithCommand("emojivoto-vote-bot"). - WithEnv(EnvVar().WithName("WEB_HOST").WithValue("web-svc:443")). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(25), - ), - ), - ), - ), - ) - - voting := Deployment("voting", ns). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "voting", - "app.kubernetes.io/part-of": "emojivoto", - "app.kubernetes.io/version": "v11", - }). - WithSpec(DeploymentSpec(). - WithReplicas(1). - WithSelector(LabelSelector(). - WithMatchLabels(map[string]string{ - "app.kubernetes.io/name": "voting-svc", - "version": "v11", - }), - ). - WithTemplate(PodTemplateSpec(). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "voting-svc", - "version": "v11", - }). - WithSpec(PodSpec(). - WithRuntimeClassName("kata-cc-isolation"). - WithServiceAccountName("voting"). - WithContainers( - Container(). - WithName("voting-svc"). - WithImage("ghcr.io/3u13r/emojivoto-voting-svc:coco-1"). - WithPorts( - ContainerPort(). - WithName("grpc"). - WithContainerPort(8080), - ContainerPort(). - WithName("prom"). - WithContainerPort(8801), - ). - WithEnv(EnvVar().WithName("GRPC_PORT").WithValue("8080")). - WithEnv(EnvVar().WithName("PROM_PORT").WithValue("8801")). - WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). - WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). - WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(50), - ), - ), - ), - ), - ) - voting, err = AddInitializer(voting, Initializer()) - if err != nil { - return nil, err - } - - votingService := ServiceForDeployment(voting). - WithName("voting-svc"). - WithSpec(ServiceSpec(). - WithSelector( - map[string]string{"app.kubernetes.io/name": "voting-svc"}, - ). - WithPorts( - ServicePort(). - WithName("grpc"). - WithPort(8080). - WithTargetPort(intstr.FromInt(8080)), - ServicePort(). - WithName("prom"). - WithPort(8801). - WithTargetPort(intstr.FromInt(8801)), - ), - ) - - votingserviceAccount := ServiceAccount("voting", ns). - WithAPIVersion("v1"). - WithKind("ServiceAccount") - - web := Deployment("web", ns). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "web", - "app.kubernetes.io/part-of": "emojivoto", - "app.kubernetes.io/version": "v11", - }). - WithSpec(DeploymentSpec(). - WithReplicas(1). - WithSelector(LabelSelector(). - WithMatchLabels(map[string]string{ - "app.kubernetes.io/name": "web-svc", - "version": "v11", - }), - ). - WithTemplate(PodTemplateSpec(). - WithLabels(map[string]string{ - "app.kubernetes.io/name": "web-svc", - "version": "v11", - }). - WithSpec(PodSpec(). - WithRuntimeClassName("kata-cc-isolation"). - WithServiceAccountName("web"). - WithContainers( - Container(). - WithName("web-svc"). - WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). - WithPorts( - ContainerPort(). - WithName("https"). - WithContainerPort(8080), - ). - WithEnv(EnvVar().WithName("WEB_PORT").WithValue("8080")). - WithEnv(EnvVar().WithName("EMOJISVC_HOST").WithValue("emoji-svc:8080")). - WithEnv(EnvVar().WithName("VOTINGSVC_HOST").WithValue("voting-svc:8080")). - WithEnv(EnvVar().WithName("INDEX_BUNDLE").WithValue("dist/index_bundle.js")). - WithEnv(EnvVar().WithName("EDG_CERT_PATH").WithValue("/tls-config/certChain.pem")). - WithEnv(EnvVar().WithName("EDG_CA_PATH").WithValue("/tls-config/MeshCACert.pem")). - WithEnv(EnvVar().WithName("EDG_KEY_PATH").WithValue("/tls-config/key.pem")). - WithEnv(EnvVar().WithName("EDG_DISABLE_CLIENT_AUTH").WithValue("true")). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(50), - ), - ), - ), - ), - ) - web, err = AddInitializer(web, Initializer()) - if err != nil { - return nil, err - } - - webService := ServiceForDeployment(web). - WithName("web-svc"). - WithSpec(ServiceSpec(). - WithSelector( - map[string]string{"app.kubernetes.io/name": "web-svc"}, - ). - WithType("ClusterIP"). - WithPorts( - ServicePort(). - WithName("https"). - WithPort(443). - WithTargetPort(intstr.FromInt(8080)), - ), - ) - - webserviceAccount := ServiceAccount("web", ns). - WithAPIVersion("v1"). - WithKind("ServiceAccount") - - portforwarderCoordinator := PortForwarder("coordinator", ns). - WithListenPort(1313). - WithForwardTarget("coordinator", 1313). - PodApplyConfiguration - - portforwarderemojivotoWeb := PortForwarder("emojivoto-web", ns). - WithListenPort(8080). - WithForwardTarget("web-svc", 443). - PodApplyConfiguration + namespace := Namespace(ns) - resources := []any{ - namespace, - coordinator, - coordinatorService, - coordinatorForwarder, - emoji, - emojiService, - emojiserviceAccount, - portforwarderCoordinator, - portforwarderemojivotoWeb, - voteBot, - voting, - votingService, - votingserviceAccount, - web, - webService, - webserviceAccount, + if generateCoordinatorService { + coordinator := Coordinator(ns).DeploymentApplyConfiguration + coordinatorService := ServiceForDeployment(coordinator) + coordinatorForwarder := PortForwarder("coordinator", ns). + WithListenPort(1313). + WithForwardTarget("coordinator", 1313). + PodApplyConfiguration + resources = append(resources, namespace, coordinator, coordinatorService, coordinatorForwarder) } - return resources, nil -} - -// EmojivotoDemo is a simplified version of Emojivoto returning resources for deploying a demo. -func EmojivotoDemo() ([]any, error) { - ns := "default" emoji := Deployment("emoji", ns). WithLabels(map[string]string{ "app.kubernetes.io/name": "emoji", @@ -519,7 +243,7 @@ func EmojivotoDemo() ([]any, error) { WithContainers( Container(). WithName("emoji-svc"). - WithImage("ghcr.io/3u13r/emojivoto-emoji-svc:coco-1"). + WithImage(emojiImage). WithPorts( ContainerPort(). WithName("grpc"). @@ -540,10 +264,11 @@ func EmojivotoDemo() ([]any, error) { ), ), ) - emoji, err := AddInitializer(emoji, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + emoji, err := AddInitializer(emoji, Initializer().WithImage(initializerImage)) if err != nil { return nil, err } + resources = append(resources, emoji) emojiService := ServiceForDeployment(emoji). WithName("emoji-svc"). @@ -562,10 +287,12 @@ func EmojivotoDemo() ([]any, error) { WithTargetPort(intstr.FromInt(8801)), ), ) + resources = append(resources, emojiService) emojiserviceAccount := ServiceAccount("emoji", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") + resources = append(resources, emojiserviceAccount) voteBot := Deployment("vote-bot", ns). WithLabels(map[string]string{ @@ -590,7 +317,7 @@ func EmojivotoDemo() ([]any, error) { WithContainers( Container(). WithName("vote-bot"). - WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). + WithImage(webImage). WithCommand("emojivoto-vote-bot"). WithEnv(EnvVar().WithName("WEB_HOST").WithValue("web-svc:443")). WithResources(ResourceRequirements(). @@ -600,6 +327,7 @@ func EmojivotoDemo() ([]any, error) { ), ), ) + resources = append(resources, voteBot) voting := Deployment("voting", ns). WithLabels(map[string]string{ @@ -626,7 +354,7 @@ func EmojivotoDemo() ([]any, error) { WithContainers( Container(). WithName("voting-svc"). - WithImage("ghcr.io/3u13r/emojivoto-voting-svc:coco-1"). + WithImage(votingImage). WithPorts( ContainerPort(). WithName("grpc"). @@ -647,10 +375,11 @@ func EmojivotoDemo() ([]any, error) { ), ), ) - voting, err = AddInitializer(voting, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + voting, err = AddInitializer(voting, Initializer().WithImage(initializerImage)) if err != nil { return nil, err } + resources = append(resources, voting) votingService := ServiceForDeployment(voting). WithName("voting-svc"). @@ -669,10 +398,12 @@ func EmojivotoDemo() ([]any, error) { WithTargetPort(intstr.FromInt(8801)), ), ) + resources = append(resources, votingService) votingserviceAccount := ServiceAccount("voting", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") + resources = append(resources, votingserviceAccount) web := Deployment("web", ns). WithLabels(map[string]string{ @@ -699,7 +430,7 @@ func EmojivotoDemo() ([]any, error) { WithContainers( Container(). WithName("web-svc"). - WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). + WithImage(webImage). WithPorts( ContainerPort(). WithName("https"). @@ -720,10 +451,11 @@ func EmojivotoDemo() ([]any, error) { ), ), ) - web, err = AddInitializer(web, Initializer().WithImage("ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f")) + web, err = AddInitializer(web, Initializer().WithImage(initializerImage)) if err != nil { return nil, err } + resources = append(resources, web) webService := ServiceForDeployment(web). WithName("web-svc"). @@ -739,17 +471,19 @@ func EmojivotoDemo() ([]any, error) { WithTargetPort(intstr.FromInt(8080)), ), ) + resources = append(resources, webService) webserviceAccount := ServiceAccount("web", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") + resources = append(resources, webserviceAccount) portforwarderCoordinator := PortForwarder("coordinator", ns). WithSpec(PodSpec(). WithContainers( Container(). WithName("port-forwarder"). - WithImage("ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0"). + WithImage(portforwarderImage). WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("1313")). WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("coordinator")). WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("1313")). @@ -763,13 +497,14 @@ func EmojivotoDemo() ([]any, error) { ), ), ) + resources = append(resources, portforwarderCoordinator) - portforwarderemojivotoWeb := PortForwarder("emojivoto-web", ns). + portforwarderWeb := PortForwarder("emojivoto-web", ns). WithSpec(PodSpec(). WithContainers( Container(). WithName("port-forwarder"). - WithImage("ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0"). + WithImage(portforwarderImage). WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("8080")). WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("web-svc")). WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("443")). @@ -783,21 +518,31 @@ func EmojivotoDemo() ([]any, error) { ), ), ) - - resources := []any{ - emoji, - emojiService, - emojiserviceAccount, - portforwarderCoordinator, - portforwarderemojivotoWeb, - voteBot, - voting, - votingService, - votingserviceAccount, - web, - webService, - webserviceAccount, - } + resources = append(resources, portforwarderWeb) return resources, nil } + +func Emojivoto() ([]any, error) { + return GenerateEmojivoto( + "edg-default", + "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", + "ghcr.io/edgelesssys/contrast/initializer:latest", + "ghcr.io/edgelesssys/contrast/port-forwarder:latest", + "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", + "ghcr.io/3u13r/emojivoto-web:coco-1", + true, + ) +} + +func EmojivotoDemo() ([]any, error) { + return GenerateEmojivoto( + "default", + "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", + "ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f", + "ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0", + "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", + "ghcr.io/3u13r/emojivoto-web:coco-1", + false, + ) +} From c4497622f76dbd255a9363d249b6f88de6c7152b Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:34:16 +0200 Subject: [PATCH 06/10] e2e: add comments to exported functions --- e2e/internal/kuberesource/sets.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index 9b0a978186..82e3a66419 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -198,7 +198,7 @@ func OpenSSL() ([]any, error) { return resources, nil } -// GenerateEmojivoto returns resources for deploying EmojiVoto application. +// GenerateEmojivoto returns resources for deploying EmojiVoto application with custom images. func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string, generateCoordinatorService bool) ([]any, error) { resources := make([]any, 0) @@ -523,6 +523,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma return resources, nil } +// Emojivoto returns resources for deploying EmojiVoto application. func Emojivoto() ([]any, error) { return GenerateEmojivoto( "edg-default", @@ -535,6 +536,7 @@ func Emojivoto() ([]any, error) { ) } +// EmojivotoDemo returns resources for deploying a simple EmojiVoto demo. func EmojivotoDemo() ([]any, error) { return GenerateEmojivoto( "default", From 946804d154b406a46f301044f0590c7f6bce883a Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:17:42 +0200 Subject: [PATCH 07/10] e2e: add patch --- e2e/internal/kuberesource/sets.go | 67 ++++++++++++++++--------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index 82e3a66419..f8c4c084c2 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -1,6 +1,9 @@ package kuberesource -import "k8s.io/apimachinery/pkg/util/intstr" +import ( + "k8s.io/apimachinery/pkg/util/intstr" + v1 "k8s.io/client-go/applyconfigurations/apps/v1" +) // Simple returns a simple set of resources for testing. func Simple() ([]any, error) { @@ -200,7 +203,7 @@ func OpenSSL() ([]any, error) { // GenerateEmojivoto returns resources for deploying EmojiVoto application with custom images. func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string, generateCoordinatorService bool) ([]any, error) { - resources := make([]any, 0) + var resources []any if ns == "" { ns = "edg-default" @@ -479,24 +482,9 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma resources = append(resources, webserviceAccount) portforwarderCoordinator := PortForwarder("coordinator", ns). - WithSpec(PodSpec(). - WithContainers( - Container(). - WithName("port-forwarder"). - WithImage(portforwarderImage). - WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("1313")). - WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("coordinator")). - WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("1313")). - WithCommand("/bin/bash", "-c", "echo Starting port-forward with socat; exec socat -d -d TCP-LISTEN:${LISTEN_PORT},fork TCP:${FORWARD_HOST}:${FORWARD_PORT}"). - WithPorts( - ContainerPort(). - WithContainerPort(1313), - ). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(50), - ), - ), - ) + WithListenPort(1313). + WithForwardTarget("coordinator", 1313). + PodApplyConfiguration resources = append(resources, portforwarderCoordinator) portforwarderWeb := PortForwarder("emojivoto-web", ns). @@ -523,28 +511,41 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma return resources, nil } -// Emojivoto returns resources for deploying EmojiVoto application. +// Emojivoto returns resources for deploying Emojivoto application. func Emojivoto() ([]any, error) { return GenerateEmojivoto( - "edg-default", + "default", "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", "ghcr.io/edgelesssys/contrast/initializer:latest", "ghcr.io/edgelesssys/contrast/port-forwarder:latest", "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", "ghcr.io/3u13r/emojivoto-web:coco-1", - true, + false, ) } -// EmojivotoDemo returns resources for deploying a simple EmojiVoto demo. +// EmojivotoDemo returns resources for deploying a simple Emojivoto demo. func EmojivotoDemo() ([]any, error) { - return GenerateEmojivoto( - "default", - "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", - "ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f", - "ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0", - "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", - "ghcr.io/3u13r/emojivoto-web:coco-1", - false, - ) + vanilla, _ := Emojivoto() + replacements := map[string]string{ + "ghcr.io/edgelesssys/contrast/initializer:latest": "ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f", + "ghcr.io/edgelesssys/contrast/port-forwarder:latest": "ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0", + } + patched := PatchImages(vanilla, replacements) + return patched, nil +} + +// PatchImages replaces images in a set of resources. +func PatchImages(resources []any, replacements map[string]string) []any { + for _, resource := range resources { + switch r := resource.(type) { + case *v1.DeploymentApplyConfiguration: + for _, container := range r.Spec.Template.Spec.Containers { + if replacement, ok := replacements[*container.Image]; ok { + container.Image = &replacement + } + } + } + } + return resources } From a709d93a0ec8d0612cfd76c0e9917dd3c6370a04 Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 12 Apr 2024 03:40:04 +0200 Subject: [PATCH 08/10] e2e: remove demo from main --- e2e/internal/kuberesource/resourcegen/main.go | 2 - e2e/internal/kuberesource/sets.go | 46 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/e2e/internal/kuberesource/resourcegen/main.go b/e2e/internal/kuberesource/resourcegen/main.go index 56528244f1..e69e516578 100644 --- a/e2e/internal/kuberesource/resourcegen/main.go +++ b/e2e/internal/kuberesource/resourcegen/main.go @@ -26,8 +26,6 @@ func main() { resources, err = kuberesource.OpenSSL() case "emojivoto": resources, err = kuberesource.Emojivoto() - case "emojivotodemo": - resources, err = kuberesource.EmojivotoDemo() default: fmt.Printf("Error: unknown set: %s\n", set) os.Exit(1) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index f8c4c084c2..fb6cf49be4 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -2,7 +2,8 @@ package kuberesource import ( "k8s.io/apimachinery/pkg/util/intstr" - v1 "k8s.io/client-go/applyconfigurations/apps/v1" + applyappsv1 "k8s.io/client-go/applyconfigurations/apps/v1" + applycorev1 "k8s.io/client-go/applyconfigurations/core/v1" ) // Simple returns a simple set of resources for testing. @@ -514,7 +515,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma // Emojivoto returns resources for deploying Emojivoto application. func Emojivoto() ([]any, error) { return GenerateEmojivoto( - "default", + "edg-default", "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", "ghcr.io/edgelesssys/contrast/initializer:latest", "ghcr.io/edgelesssys/contrast/port-forwarder:latest", @@ -532,6 +533,7 @@ func EmojivotoDemo() ([]any, error) { "ghcr.io/edgelesssys/contrast/port-forwarder:latest": "ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0", } patched := PatchImages(vanilla, replacements) + patched = PatchNamespaces(patched, "default") return patched, nil } @@ -539,12 +541,44 @@ func EmojivotoDemo() ([]any, error) { func PatchImages(resources []any, replacements map[string]string) []any { for _, resource := range resources { switch r := resource.(type) { - case *v1.DeploymentApplyConfiguration: - for _, container := range r.Spec.Template.Spec.Containers { - if replacement, ok := replacements[*container.Image]; ok { - container.Image = &replacement + case *applyappsv1.DeploymentApplyConfiguration: + for i := 0; i < len(r.Spec.Template.Spec.InitContainers); i++ { + if replacement, ok := replacements[*r.Spec.Template.Spec.InitContainers[i].Image]; ok { + r.Spec.Template.Spec.InitContainers[i].Image = &replacement } } + for i := 0; i < len(r.Spec.Template.Spec.Containers); i++ { + if replacement, ok := replacements[*r.Spec.Template.Spec.Containers[i].Image]; ok { + r.Spec.Template.Spec.Containers[i].Image = &replacement + } + } + case *applycorev1.PodApplyConfiguration: + for i := 0; i < len(r.Spec.Containers); i++ { + if replacement, ok := replacements[*r.Spec.Containers[i].Image]; ok { + r.Spec.Containers[i].Image = &replacement + } + } + case *applycorev1.ServiceApplyConfiguration: + // Do nothing + case *applycorev1.ServiceAccountApplyConfiguration: + // Do nothing + } + } + return resources +} + +// PatchNamespaces replaces namespaces in a set of resources. +func PatchNamespaces(resources []any, namespace string) []any { + for _, resource := range resources { + switch r := resource.(type) { + case *applycorev1.PodApplyConfiguration: + r.Namespace = &namespace + case *applyappsv1.DeploymentApplyConfiguration: + r.Namespace = &namespace + case *applycorev1.ServiceApplyConfiguration: + r.Namespace = &namespace + case *applycorev1.ServiceAccountApplyConfiguration: + r.Namespace = &namespace } } return resources From 705839a937a84df95ff40c9d12a3891f0a7e95ea Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 12 Apr 2024 03:41:28 +0200 Subject: [PATCH 09/10] e2e: fix patching --- e2e/internal/kuberesource/sets.go | 79 ++++++++++++++++--------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index fb6cf49be4..cbcc83d7e5 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -203,25 +203,13 @@ func OpenSSL() ([]any, error) { } // GenerateEmojivoto returns resources for deploying EmojiVoto application with custom images. -func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string, generateCoordinatorService bool) ([]any, error) { +func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string) ([]any, error) { var resources []any if ns == "" { ns = "edg-default" } - namespace := Namespace(ns) - - if generateCoordinatorService { - coordinator := Coordinator(ns).DeploymentApplyConfiguration - coordinatorService := ServiceForDeployment(coordinator) - coordinatorForwarder := PortForwarder("coordinator", ns). - WithListenPort(1313). - WithForwardTarget("coordinator", 1313). - PodApplyConfiguration - resources = append(resources, namespace, coordinator, coordinatorService, coordinatorForwarder) - } - emoji := Deployment("emoji", ns). WithLabels(map[string]string{ "app.kubernetes.io/name": "emoji", @@ -512,31 +500,6 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma return resources, nil } -// Emojivoto returns resources for deploying Emojivoto application. -func Emojivoto() ([]any, error) { - return GenerateEmojivoto( - "edg-default", - "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", - "ghcr.io/edgelesssys/contrast/initializer:latest", - "ghcr.io/edgelesssys/contrast/port-forwarder:latest", - "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", - "ghcr.io/3u13r/emojivoto-web:coco-1", - false, - ) -} - -// EmojivotoDemo returns resources for deploying a simple Emojivoto demo. -func EmojivotoDemo() ([]any, error) { - vanilla, _ := Emojivoto() - replacements := map[string]string{ - "ghcr.io/edgelesssys/contrast/initializer:latest": "ghcr.io/3u13r/contrast/initializer@sha256:3f0e76ffd1c62af460d2a7407ca0ab13cd49b3f07a00d5ef5bd636bcb6d8381f", - "ghcr.io/edgelesssys/contrast/port-forwarder:latest": "ghcr.io/3u13r/contrast/port-forwarder@sha256:00b02378ceb33df7db46a0b6b56fd7fe1e7b2e7dade0404957f16235c01e80e0", - } - patched := PatchImages(vanilla, replacements) - patched = PatchNamespaces(patched, "default") - return patched, nil -} - // PatchImages replaces images in a set of resources. func PatchImages(resources []any, replacements map[string]string) []any { for _, resource := range resources { @@ -583,3 +546,43 @@ func PatchNamespaces(resources []any, namespace string) []any { } return resources } + +// EmojivotoDemo returns patched resources for deploying a simple Emojivoto demo. +func EmojivotoDemo(replacements map[string]string) ([]any, error) { + vanilla, _ := GenerateEmojivoto( + "edg-default", + "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", + "ghcr.io/edgelesssys/contrast/initializer:latest", + "ghcr.io/edgelesssys/contrast/port-forwarder:latest", + "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", + "ghcr.io/3u13r/emojivoto-web:coco-1", + ) + patched := PatchImages(vanilla, replacements) + patched = PatchNamespaces(patched, "default") + return patched, nil +} + +// Emojivoto returns resources for deploying Emojivoto application. +func Emojivoto() ([]any, error) { + resources, _ := GenerateEmojivoto( + "edg-default", + "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", + "ghcr.io/edgelesssys/contrast/initializer:latest", + "ghcr.io/edgelesssys/contrast/port-forwarder:latest", + "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", + "ghcr.io/3u13r/emojivoto-web:coco-1", + ) + + // Add coordinator + ns := "edg-default" + namespace := Namespace(ns) + coordinator := Coordinator(ns).DeploymentApplyConfiguration + coordinatorService := ServiceForDeployment(coordinator) + coordinatorForwarder := PortForwarder("coordinator", ns). + WithListenPort(1313). + WithForwardTarget("coordinator", 1313). + PodApplyConfiguration + resources = append(resources, namespace, coordinator, coordinatorService, coordinatorForwarder) + + return resources, nil +} From 79a68ff95a5f852ac91b3b063cdba098dd47629d Mon Sep 17 00:00:00 2001 From: Martin Irungu <68087402+wirungu@users.noreply.github.com> Date: Fri, 12 Apr 2024 04:07:35 +0200 Subject: [PATCH 10/10] e2e: add demo generation --- e2e/internal/kuberesource/sets.go | 113 +++++++++++------------------- 1 file changed, 42 insertions(+), 71 deletions(-) diff --git a/e2e/internal/kuberesource/sets.go b/e2e/internal/kuberesource/sets.go index a4e6e85c09..7443922d4f 100644 --- a/e2e/internal/kuberesource/sets.go +++ b/e2e/internal/kuberesource/sets.go @@ -1,10 +1,10 @@ package kuberesource import ( - applyappsv1 "k8s.io/client-go/applyconfigurations/apps/v1" - applycorev1 "k8s.io/client-go/applyconfigurations/core/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" + applyappsv1 "k8s.io/client-go/applyconfigurations/apps/v1" + applycorev1 "k8s.io/client-go/applyconfigurations/core/v1" ) // CoordinatorRelease will generate the Coordinator deployment YAML that is published @@ -213,14 +213,9 @@ func OpenSSL() ([]any, error) { return resources, nil } -// GenerateEmojivoto returns resources for deploying EmojiVoto application with custom images. -func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderImage, votingImage, webImage string) ([]any, error) { - var resources []any - - if ns == "" { - ns = "edg-default" - } - +// generateEmojivoto returns resources for deploying Emojivoto application. +func generateEmojivoto() ([]any, error) { + ns := "edg-default" emoji := Deployment("emoji", ns). WithLabels(map[string]string{ "app.kubernetes.io/name": "emoji", @@ -246,7 +241,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithContainers( Container(). WithName("emoji-svc"). - WithImage(emojiImage). + WithImage("ghcr.io/3u13r/emojivoto-emoji-svc:coco-1"). WithPorts( ContainerPort(). WithName("grpc"). @@ -267,11 +262,10 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma ), ), ) - emoji, err := AddInitializer(emoji, Initializer().WithImage(initializerImage)) + emoji, err := AddInitializer(emoji, Initializer()) if err != nil { return nil, err } - resources = append(resources, emoji) emojiService := ServiceForDeployment(emoji). WithName("emoji-svc"). @@ -290,12 +284,10 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithTargetPort(intstr.FromInt(8801)), ), ) - resources = append(resources, emojiService) emojiserviceAccount := ServiceAccount("emoji", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") - resources = append(resources, emojiserviceAccount) voteBot := Deployment("vote-bot", ns). WithLabels(map[string]string{ @@ -320,7 +312,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithContainers( Container(). WithName("vote-bot"). - WithImage(webImage). + WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). WithCommand("emojivoto-vote-bot"). WithEnv(EnvVar().WithName("WEB_HOST").WithValue("web-svc:443")). WithResources(ResourceRequirements(). @@ -330,7 +322,6 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma ), ), ) - resources = append(resources, voteBot) voting := Deployment("voting", ns). WithLabels(map[string]string{ @@ -357,7 +348,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithContainers( Container(). WithName("voting-svc"). - WithImage(votingImage). + WithImage("ghcr.io/3u13r/emojivoto-voting-svc:coco-1"). WithPorts( ContainerPort(). WithName("grpc"). @@ -378,11 +369,10 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma ), ), ) - voting, err = AddInitializer(voting, Initializer().WithImage(initializerImage)) + voting, err = AddInitializer(voting, Initializer()) if err != nil { return nil, err } - resources = append(resources, voting) votingService := ServiceForDeployment(voting). WithName("voting-svc"). @@ -401,12 +391,10 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithTargetPort(intstr.FromInt(8801)), ), ) - resources = append(resources, votingService) votingserviceAccount := ServiceAccount("voting", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") - resources = append(resources, votingserviceAccount) web := Deployment("web", ns). WithLabels(map[string]string{ @@ -433,7 +421,7 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithContainers( Container(). WithName("web-svc"). - WithImage(webImage). + WithImage("ghcr.io/3u13r/emojivoto-web:coco-1"). WithPorts( ContainerPort(). WithName("https"). @@ -454,11 +442,10 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma ), ), ) - web, err = AddInitializer(web, Initializer().WithImage(initializerImage)) + web, err = AddInitializer(web, Initializer()) if err != nil { return nil, err } - resources = append(resources, web) webService := ServiceForDeployment(web). WithName("web-svc"). @@ -474,39 +461,35 @@ func GenerateEmojivoto(ns string, emojiImage, initializerImage, portforwarderIma WithTargetPort(intstr.FromInt(8080)), ), ) - resources = append(resources, webService) webserviceAccount := ServiceAccount("web", ns). WithAPIVersion("v1"). WithKind("ServiceAccount") - resources = append(resources, webserviceAccount) portforwarderCoordinator := PortForwarder("coordinator", ns). WithListenPort(1313). WithForwardTarget("coordinator", 1313). PodApplyConfiguration - resources = append(resources, portforwarderCoordinator) - - portforwarderWeb := PortForwarder("emojivoto-web", ns). - WithSpec(PodSpec(). - WithContainers( - Container(). - WithName("port-forwarder"). - WithImage(portforwarderImage). - WithEnv(EnvVar().WithName("LISTEN_PORT").WithValue("8080")). - WithEnv(EnvVar().WithName("FORWARD_HOST").WithValue("web-svc")). - WithEnv(EnvVar().WithName("FORWARD_PORT").WithValue("443")). - WithCommand("/bin/bash", "-c", "echo Starting port-forward with socat; exec socat -d -d TCP-LISTEN:${LISTEN_PORT},fork TCP:${FORWARD_HOST}:${FORWARD_PORT}"). - WithPorts( - ContainerPort(). - WithContainerPort(8080), - ). - WithResources(ResourceRequirements(). - WithMemoryLimitAndRequest(50), - ), - ), - ) - resources = append(resources, portforwarderWeb) + + portforwarderemojivotoWeb := PortForwarder("emojivoto-web", ns). + WithListenPort(8080). + WithForwardTarget("web-svc", 443). + PodApplyConfiguration + + resources := []any{ + emoji, + emojiService, + emojiserviceAccount, + portforwarderCoordinator, + portforwarderemojivotoWeb, + voteBot, + voting, + votingService, + votingserviceAccount, + web, + webService, + webserviceAccount, + } return resources, nil } @@ -532,10 +515,6 @@ func PatchImages(resources []any, replacements map[string]string) []any { r.Spec.Containers[i].Image = &replacement } } - case *applycorev1.ServiceApplyConfiguration: - // Do nothing - case *applycorev1.ServiceAccountApplyConfiguration: - // Do nothing } } return resources @@ -558,31 +537,23 @@ func PatchNamespaces(resources []any, namespace string) []any { return resources } -// EmojivotoDemo returns patched resources for deploying a simple Emojivoto demo. +// EmojivotoDemo returns patched resources for deploying an Emojivoto demo. func EmojivotoDemo(replacements map[string]string) ([]any, error) { - vanilla, _ := GenerateEmojivoto( - "edg-default", - "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", - "ghcr.io/edgelesssys/contrast/initializer:latest", - "ghcr.io/edgelesssys/contrast/port-forwarder:latest", - "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", - "ghcr.io/3u13r/emojivoto-web:coco-1", - ) - patched := PatchImages(vanilla, replacements) + resources, err := generateEmojivoto() + if err != nil { + return nil, err + } + patched := PatchImages(resources, replacements) patched = PatchNamespaces(patched, "default") return patched, nil } // Emojivoto returns resources for deploying Emojivoto application. func Emojivoto() ([]any, error) { - resources, _ := GenerateEmojivoto( - "edg-default", - "ghcr.io/3u13r/emojivoto-emoji-svc:coco-1", - "ghcr.io/edgelesssys/contrast/initializer:latest", - "ghcr.io/edgelesssys/contrast/port-forwarder:latest", - "ghcr.io/3u13r/emojivoto-voting-svc:coco-1", - "ghcr.io/3u13r/emojivoto-web:coco-1", - ) + resources, err := generateEmojivoto() + if err != nil { + return nil, err + } // Add coordinator ns := "edg-default"