Skip to content

Commit

Permalink
e2e: add emojivoto demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wirungu committed Apr 15, 2024
1 parent ac080d7 commit cccb2a5
Showing 1 changed file with 79 additions and 14 deletions.
93 changes: 79 additions & 14 deletions e2e/internal/kuberesource/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package kuberesource
import (
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
Expand Down Expand Up @@ -211,17 +213,9 @@ func OpenSSL() ([]any, error) {
return resources, nil
}

// Emojivoto returns resources for deploying EmojiVoto application.
func Emojivoto() ([]any, error) {
// generateEmojivoto returns resources for deploying Emojivoto application.
func generateEmojivoto() ([]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

emoji := Deployment("emoji", ns).
WithLabels(map[string]string{
"app.kubernetes.io/name": "emoji",
Expand Down Expand Up @@ -483,10 +477,6 @@ func Emojivoto() ([]any, error) {
PodApplyConfiguration

resources := []any{
namespace,
coordinator,
coordinatorService,
coordinatorForwarder,
emoji,
emojiService,
emojiserviceAccount,
Expand All @@ -503,3 +493,78 @@ func Emojivoto() ([]any, error) {

return resources, 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 *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
}
}
}
}
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
}

// EmojivotoDemo returns patched resources for deploying an Emojivoto demo.
func EmojivotoDemo(replacements map[string]string) ([]any, error) {
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, err := generateEmojivoto()
if err != nil {
return nil, err
}

// 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
}

0 comments on commit cccb2a5

Please sign in to comment.