Skip to content

Commit

Permalink
release: use resourcegen tool for coordinator.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Apr 4, 2024
1 parent 7592984 commit 0736b3a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 3 additions & 1 deletion e2e/internal/kuberesource/parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kuberesource
import (
"strconv"

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"
Expand Down Expand Up @@ -120,14 +121,15 @@ func (c *CoordinatorConfig) GetDeploymentConfig() *DeploymentConfig {

// ServiceForDeployment creates a service for a deployment by exposing the configured ports
// of the deployment's first container.
func ServiceForDeployment(d *applyappsv1.DeploymentApplyConfiguration) *applycorev1.ServiceApplyConfiguration {
func ServiceForDeployment(d *applyappsv1.DeploymentApplyConfiguration, serviceType *corev1.ServiceType) *applycorev1.ServiceApplyConfiguration {
selector := d.Spec.Selector.MatchLabels
ports := d.Spec.Template.Spec.Containers[0].Ports

s := Service(*d.Name, *d.Namespace).
WithSpec(ServiceSpec().
WithSelector(selector),
)
s.Spec.Type = serviceType

for _, p := range ports {
s.Spec.WithPorts(
Expand Down
2 changes: 2 additions & 0 deletions e2e/internal/kuberesource/resourcegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func main() {
var resources []any
var err error
switch set {
case "coordinator-release":
resources, err = kuberesource.CoordinatorRelease()
case "simple":
resources, err = kuberesource.Simple()
case "openssl":
Expand Down
29 changes: 24 additions & 5 deletions e2e/internal/kuberesource/sets.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package kuberesource

import "k8s.io/apimachinery/pkg/util/intstr"
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

func CoordinatorRelease() ([]any, error) {
coordinator := Coordinator("").DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator, toPtr(corev1.ServiceTypeLoadBalancer))

resources := []any{
coordinator,
coordinatorService,
}

return resources, nil
}

// Simple returns a simple set of resources for testing.
func Simple() ([]any, error) {
ns := "edg-default"

namespace := Namespace(ns)
coordinator := Coordinator(ns).DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator)
coordinatorService := ServiceForDeployment(coordinator, nil)
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -57,7 +72,7 @@ func OpenSSL() ([]any, error) {
ns := "edg-default"
namespace := Namespace(ns)
coordinator := Coordinator(ns).DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator)
coordinatorService := ServiceForDeployment(coordinator, nil)
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -102,7 +117,7 @@ func OpenSSL() ([]any, error) {
return nil, err
}

backendService := ServiceForDeployment(backend)
backendService := ServiceForDeployment(backend, nil)

client := Deployment("openssl-client", ns).
WithSpec(DeploymentSpec().
Expand Down Expand Up @@ -169,7 +184,7 @@ func OpenSSL() ([]any, error) {
return nil, err
}

frontendService := ServiceForDeployment(frontend)
frontendService := ServiceForDeployment(frontend, nil)

portforwarderCoordinator := PortForwarder("coordinator", ns).
WithListenPort(1313).
Expand Down Expand Up @@ -197,3 +212,7 @@ func OpenSSL() ([]any, error) {

return resources, nil
}

func toPtr[T any](v T) *T {
return &v
}
9 changes: 4 additions & 5 deletions packages/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,17 @@ with pkgs;
runtimeInputs = [
yq-go
genpolicy-msft
contrast
];
text = ''
imageRef=$1
tmpdir=$(mktemp -d)
trap 'rm -rf $tmpdir' EXIT
# TODO(burgerdev): consider a dedicated coordinator template instead of the simple one
yq < deployments/simple/coordinator.yml > "$tmpdir/coordinator.yml" \
"del(.metadata.namespace) |
(select(.kind == \"Deployment\") | .spec.template.spec.containers[0].image) = \"$imageRef\" |
(select(.kind == \"Service\") | .spec.type) = \"LoadBalancer\" "
resourcegen coordinator-release "$tmpdir/coordinator_base.yml"
yq < "$tmpdir/coordinator_base.yml" > "$tmpdir/coordinator.yml" \
"(select(.kind == \"Deployment\") | .spec.template.spec.containers[0].image) = \"$imageRef\""
pushd "$tmpdir" >/dev/null
cp ${genpolicy-msft.rules-coordinator}/genpolicy-rules.rego rules.rego
Expand Down

0 comments on commit 0736b3a

Please sign in to comment.