Skip to content

Commit

Permalink
release: use resourcegen tool for coordinator.yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Malte Poll <[email protected]>
Co-authored-by: Paul Meyer <[email protected]>
  • Loading branch information
malt3 and katexochen committed Apr 8, 2024
1 parent 28c0201 commit a1711bb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
10 changes: 8 additions & 2 deletions 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,19 @@ 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).
var ns string
if d.Namespace != nil {
ns = *d.Namespace
}
s := Service(*d.Name, ns).
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
39 changes: 30 additions & 9 deletions e2e/internal/kuberesource/sets.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package kuberesource

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

// CoordinatorRelease will generate the Coordinator deployment YAML that is published
// as release artifact with a pre-generated policy (which is not contained in this function).
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, toPtr(corev1.ServiceTypeClusterIP))
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -57,7 +74,7 @@ func OpenSSL() ([]any, error) {
ns := "edg-default"
namespace := Namespace(ns)
coordinator := Coordinator(ns).DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator)
coordinatorService := ServiceForDeployment(coordinator, toPtr(corev1.ServiceTypeClusterIP))
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -102,7 +119,7 @@ func OpenSSL() ([]any, error) {
return nil, err
}

backendService := ServiceForDeployment(backend)
backendService := ServiceForDeployment(backend, toPtr(corev1.ServiceTypeClusterIP))

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

frontendService := ServiceForDeployment(frontend)
frontendService := ServiceForDeployment(frontend, toPtr(corev1.ServiceTypeClusterIP))

portforwarderCoordinator := PortForwarder("coordinator", ns).
WithListenPort(1313).
Expand Down Expand Up @@ -203,7 +220,7 @@ func Emojivoto() ([]any, error) {
ns := "edg-default"
namespace := Namespace(ns)
coordinator := Coordinator(ns).DeploymentApplyConfiguration
coordinatorService := ServiceForDeployment(coordinator)
coordinatorService := ServiceForDeployment(coordinator, toPtr(corev1.ServiceTypeClusterIP))
coordinatorForwarder := PortForwarder("coordinator", ns).
WithListenPort(1313).
WithForwardTarget("coordinator", 1313).
Expand Down Expand Up @@ -260,7 +277,7 @@ func Emojivoto() ([]any, error) {
return nil, err
}

emojiService := ServiceForDeployment(emoji).
emojiService := ServiceForDeployment(emoji, toPtr(corev1.ServiceTypeClusterIP)).
WithName("emoji-svc").
WithSpec(ServiceSpec().
WithSelector(
Expand Down Expand Up @@ -367,7 +384,7 @@ func Emojivoto() ([]any, error) {
return nil, err
}

votingService := ServiceForDeployment(voting).
votingService := ServiceForDeployment(voting, toPtr(corev1.ServiceTypeClusterIP)).
WithName("voting-svc").
WithSpec(ServiceSpec().
WithSelector(
Expand Down Expand Up @@ -440,7 +457,7 @@ func Emojivoto() ([]any, error) {
return nil, err
}

webService := ServiceForDeployment(web).
webService := ServiceForDeployment(web, toPtr(corev1.ServiceTypeClusterIP)).
WithName("web-svc").
WithSpec(ServiceSpec().
WithSelector(
Expand Down Expand Up @@ -490,3 +507,7 @@ func Emojivoto() ([]any, error) {

return resources, nil
}

func toPtr[T any](v T) *T {
return &v
}
12 changes: 10 additions & 2 deletions e2e/internal/kuberesource/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type DeploymentConfig struct {

// Deployment creates a new DeploymentConfig.
func Deployment(name, namespace string) *DeploymentConfig {
return &DeploymentConfig{applyappsv1.Deployment(name, namespace)}
d := applyappsv1.Deployment(name, namespace)
if namespace == "" && d.ObjectMetaApplyConfiguration != nil {
d.ObjectMetaApplyConfiguration.Namespace = nil
}
return &DeploymentConfig{d}
}

// DeploymentSpecConfig wraps applyappsv1.DeploymentSpecApplyConfiguration.
Expand Down Expand Up @@ -175,7 +179,11 @@ type ServiceConfig struct {

// Service creates a new ServiceConfig.
func Service(name, namespace string) *ServiceConfig {
return &ServiceConfig{applycorev1.Service(name, namespace)}
s := applycorev1.Service(name, namespace)
if namespace == "" && s.ObjectMetaApplyConfiguration != nil {
s.ObjectMetaApplyConfiguration.Namespace = nil
}
return &ServiceConfig{s}
}

// ServiceSpecConfig wraps applycorev1.ServiceSpecApplyConfiguration.
Expand Down
9 changes: 4 additions & 5 deletions packages/scripts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,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 a1711bb

Please sign in to comment.