Skip to content

Commit

Permalink
Engine traefik docker labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentluce committed Dec 20, 2023
1 parent 925cbcd commit e5de42e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
// be stored in the port spec label
KurtosisInternalContainerGrpcPortId = "grpc"

// The ID of the REST API port.
KurtosisInternalContainerRESTAPIPortId = "rest-api"

// The engine server uses gRPC so MUST listen on TCP (no other protocols are supported)
EngineTransportProtocol = port_spec.TransportProtocol_TCP

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func CreateEngine(
engineGuid,
consts.KurtosisInternalContainerGrpcPortId,
privateGrpcPortSpec,
consts.KurtosisInternalContainerRESTAPIPortId,
restAPIPortSpec,
)
if err != nil {
return nil, stacktrace.Propagate(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package object_attributes_provider

import (
"fmt"
"strconv"
"strings"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/object_attributes_provider/docker_label_key"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/object_attributes_provider/docker_label_value"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/object_attributes_provider/docker_object_name"
Expand All @@ -10,21 +14,23 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/engine"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec"
"github.com/kurtosis-tech/stacktrace"
"strings"
)

const (
engineServerNamePrefix = "kurtosis-engine"
logsAggregatorName = "kurtosis-logs-aggregator"
logsStorageVolumeName = "kurtosis-logs-storage"
reverseProxyName = "kurtosis-reverse-proxy"
engineRESTAPIPortStr = "engine-rest-api"
)

type DockerObjectAttributesProvider interface {
ForEngineServer(
guid engine.EngineGUID,
grpcPortId string,
grpcPortSpec *port_spec.PortSpec,
restAPIPortId string,
restAPIPortSpec *port_spec.PortSpec,
) (DockerObjectAttributes, error)
ForEnclave(enclaveUuid enclave.EnclaveUUID) (DockerEnclaveObjectAttributesProvider, error)
ForLogsAggregator() (DockerObjectAttributes, error)
Expand All @@ -47,6 +53,8 @@ func (provider *dockerObjectAttributesProviderImpl) ForEngineServer(
guid engine.EngineGUID,
grpcPortId string,
grpcPortSpec *port_spec.PortSpec,
restAPIPortId string,
restAPIPortSpec *port_spec.PortSpec,
) (DockerObjectAttributes, error) {

nameStr := strings.Join(
Expand Down Expand Up @@ -85,6 +93,14 @@ func (provider *dockerObjectAttributesProviderImpl) ForEngineServer(
docker_label_key.GUIDDockerLabelKey: guidLabelValue,
}

traefikLabels, err := provider.getTraefikLabelsForEngine(restAPIPortSpec)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting traefik labels for engine server")
}
for traefikLabelKey, traefikLabelValue := range traefikLabels {
labels[traefikLabelKey] = traefikLabelValue
}

objectAttributes, err := newDockerObjectAttributesImpl(name, labels)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred while creating the ObjectAttributesImpl with the name '%s' and labels '%+v'", name, labels)
Expand Down Expand Up @@ -153,3 +169,67 @@ func (provider *dockerObjectAttributesProviderImpl) ForReverseProxy() (DockerObj
}
return objectAttributes, nil
}

// Return Traefik labels
// Including the labels required to route traffic to the engine rest api port if the Host header is set to "engine".
//
// "traefik.enable": "true",
// "traefik.http.routers.engine-rest-api.rule": "Host(`engine`)",
// "traefik.http.routers.engine-rest-api.service": "engine-rest-api",
// "traefik.http.services.engine-rest-api.loadbalancer.server.port": "<engine rest api port number>"
func (provider *dockerObjectAttributesProviderImpl) getTraefikLabelsForEngine(restAPIPortSpec *port_spec.PortSpec) (map[*docker_label_key.DockerLabelKey]*docker_label_value.DockerLabelValue, error) {
labels := map[*docker_label_key.DockerLabelKey]*docker_label_value.DockerLabelValue{}

// Header Host rule
ruleKeySuffix := fmt.Sprintf("http.routers.%s.rule", engineRESTAPIPortStr)
ruleLabelKey, err := docker_label_key.CreateNewDockerTraefikLabelKey(ruleKeySuffix)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the traefik rule label key with suffix '%v'", ruleKeySuffix)
}
ruleValue := fmt.Sprintf("Host(`%s`)", engine.RESTAPIPortHostHeader)
ruleLabelValue, err := docker_label_value.CreateNewDockerLabelValue(ruleValue)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the traefik rule label value with value '%v'", ruleValue)
}
labels[ruleLabelKey] = ruleLabelValue

// Service name
serviceKeySuffix := fmt.Sprintf("http.routers.%s.service", engineRESTAPIPortStr)
serviceLabelKey, err := docker_label_key.CreateNewDockerTraefikLabelKey(serviceKeySuffix)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the traefik service label key with suffix '%v'", serviceKeySuffix)
}
serviceValue := engineRESTAPIPortStr
serviceLabelValue, err := docker_label_value.CreateNewDockerLabelValue(serviceValue)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the traefik service label value with value '%v'", serviceValue)
}
labels[serviceLabelKey] = serviceLabelValue

// Service port number
portKeySuffix := fmt.Sprintf("http.services.%s.loadbalancer.server.port", engineRESTAPIPortStr)
portLabelKey, err := docker_label_key.CreateNewDockerTraefikLabelKey(portKeySuffix)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the traefik port label key with suffix '%v'", portKeySuffix)
}
portValue := strconv.Itoa(int(restAPIPortSpec.GetNumber()))
portLabelValue, err := docker_label_value.CreateNewDockerLabelValue(portValue)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the traefik port label value with value '%v'", portValue)
}
labels[portLabelKey] = portLabelValue

// Enable Traefik
traefikEnableLabelKey, err := docker_label_key.CreateNewDockerTraefikLabelKey("enable")
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred getting the traefik enable label key")
}
traefikEnableValue := "true"
traefikEnableLabelValue, err := docker_label_value.CreateNewDockerLabelValue(traefikEnableValue)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the traefik enable label value with value '%v'", traefikEnableValue)
}
labels[traefikEnableLabelKey] = traefikEnableLabelValue

return labels, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const (
maxWaitForEngineContainerAvailabilityRetries = 30
timeBetweenWaitForEngineContainerAvailabilityRetries = 1 * time.Second
httpApplicationProtocol = "http"

restAPIPortHost = "engine"
)

var noWait *port_spec.Wait = nil
Expand Down Expand Up @@ -584,7 +582,7 @@ func getEngineIngressRules(
) ([]netv1.IngressRule, error) {
var ingressRules []netv1.IngressRule
ingressRule := netv1.IngressRule{
Host: restAPIPortHost,
Host: engine.RESTAPIPortHostHeader,
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

const (
RESTAPIPortAddr uint16 = 9779
RESTAPIHostIP string = "0.0.0.0"
RESTAPIPortAddr uint16 = 9779
RESTAPIHostIP string = "0.0.0.0"
RESTAPIPortHostHeader string = "engine"
)

0 comments on commit e5de42e

Please sign in to comment.