Skip to content

Commit

Permalink
Connect and disconnect the reverse proxy to/from the enclave network.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentluce committed Dec 2, 2023
1 parent 1537d3b commit e9982ed
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,22 @@ func (backend *DockerKurtosisBackend) DestroyReverseProxy(ctx context.Context) e
return nil
}

func (backend *DockerKurtosisBackend) ConnectReverseProxyToNetwork(ctx context.Context, networkId string) error {
if err := reverse_proxy_functions.ConnectReverseProxyToNetwork(ctx, backend.dockerManager, networkId); err != nil {
return stacktrace.Propagate(err, "An error occurred connecting the reverse proxy to the network with ID '%v'", networkId)
}

return nil
}

func (backend *DockerKurtosisBackend) DisconnectReverseProxyFromNetwork(ctx context.Context, networkId string) error {
if err := reverse_proxy_functions.DisconnectReverseProxyFromNetwork(ctx, backend.dockerManager, networkId); err != nil {
return stacktrace.Propagate(err, "An error occurred disconnecting the reverse proxy from the network with ID '%v'", networkId)
}

return nil
}

func (backend *DockerKurtosisBackend) GetAvailableCPUAndMemory(ctx context.Context) (compute_resources.MemoryInMegaBytes, compute_resources.CpuMilliCores, bool, error) {
availableMemory, availableCpu, err := backend.dockerManager.GetAvailableCPUAndMemory(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package docker_kurtosis_backend
import (
"context"
"encoding/json"
"strings"
"time"

"github.com/docker/docker/api/types/volume"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/consts"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/reverse_proxy_functions"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/shared_helpers"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_manager"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_manager/types"
Expand All @@ -14,8 +18,6 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -162,9 +164,23 @@ func (backend *DockerKurtosisBackend) CreateEnclave(ctx context.Context, enclave
}
}()

if err := backend.ConnectReverseProxyToNetwork(ctx, networkId); err != nil {
return nil, stacktrace.Propagate(err, "An error occurred connecting the reverse proxy to the enclave network with ID '%v'", networkId)
}
shouldDisconnectReverseProxyFromNetwork := true
defer func() {
if shouldDisconnectReverseProxyFromNetwork {
err = backend.DisconnectReverseProxyFromNetwork(ctx, networkId)
if err != nil {
logrus.Errorf("Couldn't disconnect the reverse proxy from the enclave network with ID '%v'", networkId)
}
}
}()

shouldDeleteLogsCollector = false
shouldDeleteNetwork = false
shouldDeleteVolume = false
shouldDisconnectReverseProxyFromNetwork = false
return newEnclave, nil
}

Expand Down Expand Up @@ -374,15 +390,34 @@ func (backend *DockerKurtosisBackend) DestroyEnclaves(
erroredEnclaveUuids[enclaveUuid] = volumeRemovalErr
}

// Disconnect the reverse proxy from the networks
networksToDisconnect := map[enclave.EnclaveUUID]string{}
for enclaveUuid := range successfulVolumeRemovalEnclaveUuids {
networkInfo, found := matchingNetworkInfo[enclaveUuid]
if !found {
return nil, nil, stacktrace.NewError("Would have attempted to disconnect enclave network '%v' that didn't match the filters", enclaveUuid)
}
networksToDisconnect[enclaveUuid] = networkInfo.dockerNetwork.GetId()
}

successfulDisconnectReverseProxyFromNetworkEnclaveUuids, erroredDisconnectReverseProxyFromNetworkEnclaveUuids, err := reverse_proxy_functions.DisconnectReverseProxyFromEnclaveNetworks(ctx, backend.dockerManager, networksToDisconnect)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred disconnecting the reverse proxy from the networks for enclaves whose volumes were successfully destroyed: %+v", successfulVolumeRemovalEnclaveUuids)
}
for enclaveUuid, networkDisconnectErr := range erroredDisconnectReverseProxyFromNetworkEnclaveUuids {
erroredEnclaveUuids[enclaveUuid] = networkDisconnectErr
}

// Remove the networks
networksToDestroy := map[enclave.EnclaveUUID]string{}
for enclaveUuid := range successfulVolumeRemovalEnclaveUuids {
for enclaveUuid := range successfulDisconnectReverseProxyFromNetworkEnclaveUuids {
networkInfo, found := matchingNetworkInfo[enclaveUuid]
if !found {
return nil, nil, stacktrace.NewError("Would have attempted to destroy enclave '%v' that didn't match the filters", enclaveUuid)
}
networksToDestroy[enclaveUuid] = networkInfo.dockerNetwork.GetId()
}

successfulNetworkRemovalEnclaveUuids, erroredNetworkRemovalEnclaveUuids, err := destroyEnclaveNetworks(ctx, backend.dockerManager, networksToDestroy)
if err != nil {
return nil, nil, stacktrace.Propagate(err, "An error occurred destroying the networks for enclaves whose volumes were successfully destroyed: %+v", successfulVolumeRemovalEnclaveUuids)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package reverse_proxy_functions

import (
"context"
"net"

"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_manager"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_operation_parallelizer"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
)

const (
emptyAliasForLogsCollector = ""
)

var (
autoAssignIpAddressToLogsCollector net.IP = nil
)

func ConnectReverseProxyToNetwork(ctx context.Context, dockerManager *docker_manager.DockerManager, networkId string) error {
_, maybeReverseProxyContainerId, err := getReverseProxyObjectAndContainerId(ctx, dockerManager)
if err != nil {
logrus.Warnf("Attempted to connect reverse proxy to a network but no reverse proxy container was found.")
return nil
}

if maybeReverseProxyContainerId == "" {
return nil
}

if err = dockerManager.ConnectContainerToNetwork(ctx, networkId, maybeReverseProxyContainerId, autoAssignIpAddressToLogsCollector, emptyAliasForLogsCollector); err != nil {
return stacktrace.Propagate(err, "An error occurred while connecting container '%v' to the enclave network '%v'", maybeReverseProxyContainerId, networkId)
}

return nil
}

func DisconnectReverseProxyFromNetwork(ctx context.Context, dockerManager *docker_manager.DockerManager, networkId string) error {
_, maybeReverseProxyContainerId, err := getReverseProxyObjectAndContainerId(ctx, dockerManager)
if err != nil {
logrus.Warnf("Attempted to disconnect reverse proxy from a network but no reverse proxy container was found.")
return nil
}

if maybeReverseProxyContainerId == "" {
return nil
}

if err = dockerManager.DisconnectContainerFromNetwork(ctx, maybeReverseProxyContainerId, networkId); err != nil {
return stacktrace.Propagate(err, "An error occurred while disconnecting container '%v' from the enclave network '%v'", maybeReverseProxyContainerId, networkId)
}

return nil
}

func DisconnectReverseProxyFromEnclaveNetworks(
ctx context.Context,
dockerManager *docker_manager.DockerManager,
enclaveNetworkIds map[enclave.EnclaveUUID]string,
) (
map[enclave.EnclaveUUID]bool,
map[enclave.EnclaveUUID]error,
error,
) {
networkIdsToRemove := map[string]bool{}
enclaveUuidsForNetworkIds := map[string]enclave.EnclaveUUID{}
for enclaveUuid, networkId := range enclaveNetworkIds {
networkIdsToRemove[networkId] = true
enclaveUuidsForNetworkIds[networkId] = enclaveUuid
}

var disconnectNetworkOperation docker_operation_parallelizer.DockerOperation = func(ctx context.Context, dockerManager *docker_manager.DockerManager, dockerObjectId string) error {
if err := DisconnectReverseProxyFromNetwork(ctx, dockerManager, dockerObjectId); err != nil {
return stacktrace.Propagate(err, "An error occurred disconnecting the reverse proxy from the enclave network with ID '%v'", dockerObjectId)
}
return nil
}

successfulNetworkIds, erroredNetworkIds := docker_operation_parallelizer.RunDockerOperationInParallel(
ctx,
networkIdsToRemove,
dockerManager,
disconnectNetworkOperation,
)

successfulEnclaveUuids := map[enclave.EnclaveUUID]bool{}
for networkId := range successfulNetworkIds {
enclaveUuid, found := enclaveUuidsForNetworkIds[networkId]
if !found {
return nil, nil, stacktrace.NewError("The reverse proxy was successfully disconnected from the Docker network '%v', but wasn't requested to be disconnected", networkId)
}
successfulEnclaveUuids[enclaveUuid] = true
}

erroredEnclaveUuids := map[enclave.EnclaveUUID]error{}
for networkId, networkRemovalErr := range erroredNetworkIds {
enclaveUuid, found := enclaveUuidsForNetworkIds[networkId]
if !found {
return nil, nil, stacktrace.NewError("Docker network '%v' had the following error during disconnect, but wasn't requested to be disconnected:\n%v", networkId, networkRemovalErr)
}
erroredEnclaveUuids[enclaveUuid] = networkRemovalErr
}
return successfulEnclaveUuids, erroredEnclaveUuids, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/reverse_proxy"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -459,6 +460,23 @@ func (backend *KubernetesKurtosisBackend) DestroyLogsCollectorForEnclave(ctx con
return stacktrace.NewError("Destroy the logs collector for enclave isn't yet implemented on Kubernetes")
}

func (backend *KubernetesKurtosisBackend) GetReverseProxy(
ctx context.Context,
) (*reverse_proxy.ReverseProxy, error) {
// TODO IMPLEMENT
return nil, stacktrace.NewError("Getting the reverse proxy isn't yet implemented on Kubernetes")
}

func (backend *KubernetesKurtosisBackend) CreateReverseProxy(ctx context.Context) (*reverse_proxy.ReverseProxy, error) {
// TODO IMPLEMENT
return nil, stacktrace.NewError("Creating the reverse proxy isn't yet implemented on Kubernetes")
}

func (backend *KubernetesKurtosisBackend) DestroyReverseProxy(ctx context.Context) error {
// TODO IMPLEMENT
return stacktrace.NewError("Destroying the reverse proxy isn't yet implemented on Kubernetes")
}

// ====================================================================================================
//
// Private Helper Functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/reverse_proxy"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/stacktrace"
)
Expand Down Expand Up @@ -438,6 +439,18 @@ func (backend *MetricsReportingKurtosisBackend) DestroyLogsCollectorForEnclave(c
return nil
}

func (backend *MetricsReportingKurtosisBackend) CreateReverseProxy(ctx context.Context) (*reverse_proxy.ReverseProxy, error) {
return backend.underlying.CreateReverseProxy(ctx)
}

func (backend *MetricsReportingKurtosisBackend) GetReverseProxy(ctx context.Context) (*reverse_proxy.ReverseProxy, error) {
return backend.underlying.GetReverseProxy(ctx)
}

func (backend *MetricsReportingKurtosisBackend) DestroyReverseProxy(ctx context.Context) error {
return backend.underlying.DestroyReverseProxy(ctx)
}

func (backend *MetricsReportingKurtosisBackend) GetAvailableCPUAndMemory(ctx context.Context) (compute_resources.MemoryInMegaBytes, compute_resources.CpuMilliCores, bool, error) {
availableMemory, availableCpu, isResourceInformationComplete, err := backend.underlying.GetAvailableCPUAndMemory(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/image_download_mode"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_aggregator"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/logs_collector"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/reverse_proxy"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
)

Expand Down Expand Up @@ -337,6 +338,13 @@ type KurtosisBackend interface {
// Destroy the logs collector for enclave with UUID
DestroyLogsCollectorForEnclave(ctx context.Context, enclaveUuid enclave.EnclaveUUID) error

CreateReverseProxy(ctx context.Context) (*reverse_proxy.ReverseProxy, error)

// Returns nil if logs aggregator was not found
GetReverseProxy(ctx context.Context) (*reverse_proxy.ReverseProxy, error)

DestroyReverseProxy(ctx context.Context) error

// GetAvailableCPUAndMemory - gets available memory in megabytes and cpu in millicores, the boolean indicates whether the information is complete
GetAvailableCPUAndMemory(ctx context.Context) (compute_resources.MemoryInMegaBytes, compute_resources.CpuMilliCores, bool, error)
}
Loading

0 comments on commit e9982ed

Please sign in to comment.