From 397564c457ec3d74feeead8ed354c2f6c8a7728d Mon Sep 17 00:00:00 2001 From: Forest Eckhardt Date: Wed, 12 Mar 2025 14:44:50 +0000 Subject: [PATCH] Update to the latest switchblade version --- go.mod | 2 +- go.sum | 2 ++ src/python/integration/init_test.go | 1 + .../cloudfoundry/switchblade/cloudfoundry.go | 18 +++++++--- .../cloudfoundry/switchblade/docker.go | 20 +++++++---- .../internal/cloudfoundry/deinitialize.go | 17 +++++++++ .../internal/docker/deinitialize.go | 36 +++++++++++++++++++ .../switchblade/internal/docker/initialize.go | 31 ++++++++++++++-- .../switchblade/internal/docker/setup.go | 6 ---- .../switchblade/internal/docker/start.go | 27 +++++++++++--- .../switchblade/internal/docker/teardown.go | 14 +------- .../cloudfoundry/switchblade/platform.go | 21 ++++++++--- vendor/modules.txt | 4 +-- 13 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 vendor/github.com/cloudfoundry/switchblade/internal/cloudfoundry/deinitialize.go create mode 100644 vendor/github.com/cloudfoundry/switchblade/internal/docker/deinitialize.go diff --git a/go.mod b/go.mod index 46e8118f8..1fa9c8566 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Dynatrace/libbuildpack-dynatrace v1.8.0 github.com/blang/semver v3.5.1+incompatible github.com/cloudfoundry/libbuildpack v0.0.0-20240717165421-f2ae8069fcba - github.com/cloudfoundry/switchblade v0.6.0 + github.com/cloudfoundry/switchblade v0.9.0 github.com/golang/mock v1.6.0 github.com/kr/text v0.2.0 github.com/onsi/ginkgo v1.16.5 diff --git a/go.sum b/go.sum index fc481bf65..8a02e8710 100644 --- a/go.sum +++ b/go.sum @@ -809,6 +809,8 @@ github.com/cloudfoundry/libbuildpack v0.0.0-20240717165421-f2ae8069fcba h1:RdbOe github.com/cloudfoundry/libbuildpack v0.0.0-20240717165421-f2ae8069fcba/go.mod h1:kn4FHMwI8bTd9gT92wPGjXHzUvGcj8CkPxG8q3AGBAQ= github.com/cloudfoundry/switchblade v0.6.0 h1:Opid6YAp7a9+lzdvwlYIUUGnxV87QfCpizV+FYMt1MQ= github.com/cloudfoundry/switchblade v0.6.0/go.mod h1:3nNqkW8dEecI9bxj8p1cHz3XG6AgPfp7jcmGHZYleos= +github.com/cloudfoundry/switchblade v0.9.0 h1:4yxlAb526G4XIDEoH9Vp+erCCYcXTD3aYaz5THKhskY= +github.com/cloudfoundry/switchblade v0.9.0/go.mod h1:lbxYZXU/aSVmEafP0NZgQaxrf9nBfdT8t2EIiwrP4bU= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= diff --git a/src/python/integration/init_test.go b/src/python/integration/init_test.go index c7f751800..986937d79 100644 --- a/src/python/integration/init_test.go +++ b/src/python/integration/init_test.go @@ -122,6 +122,7 @@ func TestIntegration(t *testing.T) { Expect(platform.Delete.Execute(dynatraceName)).To(Succeed()) Expect(os.Remove(os.Getenv("BUILDPACK_FILE"))).To(Succeed()) Expect(os.Remove(goBuildpackFile)).To(Succeed()) + Expect(platform.Deinitialize()).To(Succeed()) } func downloadBuildpack(name string) (string, error) { diff --git a/vendor/github.com/cloudfoundry/switchblade/cloudfoundry.go b/vendor/github.com/cloudfoundry/switchblade/cloudfoundry.go index 100548f9f..91b22a77d 100644 --- a/vendor/github.com/cloudfoundry/switchblade/cloudfoundry.go +++ b/vendor/github.com/cloudfoundry/switchblade/cloudfoundry.go @@ -9,15 +9,17 @@ import ( ) //go:generate faux --package github.com/cloudfoundry/switchblade/internal/cloudfoundry --interface InitializePhase --name CloudFoundryInitializePhase --output fakes/cloudfoundry_initialize_phase.go +//go:generate faux --package github.com/cloudfoundry/switchblade/internal/cloudfoundry --interface DeinitializePhase --name CloudFoundryDeinitializePhase --output fakes/cloudfoundry_deinitialize_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/cloudfoundry --interface SetupPhase --name CloudFoundrySetupPhase --output fakes/cloudfoundry_setup_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/cloudfoundry --interface StagePhase --name CloudFoundryStagePhase --output fakes/cloudfoundry_stage_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/cloudfoundry --interface TeardownPhase --name CloudFoundryTeardownPhase --output fakes/cloudfoundry_teardown_phase.go -func NewCloudFoundry(initialize cloudfoundry.InitializePhase, setup cloudfoundry.SetupPhase, stage cloudfoundry.StagePhase, teardown cloudfoundry.TeardownPhase, workspace string) Platform { +func NewCloudFoundry(initialize cloudfoundry.InitializePhase, deinitialize cloudfoundry.DeinitializePhase, setup cloudfoundry.SetupPhase, stage cloudfoundry.StagePhase, teardown cloudfoundry.TeardownPhase, workspace string) Platform { return Platform{ - initialize: cloudFoundryInitializeProcess{initialize: initialize}, - Deploy: cloudFoundryDeployProcess{setup: setup, stage: stage, workspace: workspace}, - Delete: cloudFoundryDeleteProcess{teardown: teardown, workspace: workspace}, + initialize: cloudFoundryInitializeProcess{initialize: initialize}, + deinitialize: cloudFoundryDeinitializeProcess{deinitialize: deinitialize}, + Deploy: cloudFoundryDeployProcess{setup: setup, stage: stage, workspace: workspace}, + Delete: cloudFoundryDeleteProcess{teardown: teardown, workspace: workspace}, } } @@ -37,6 +39,14 @@ func (p cloudFoundryInitializeProcess) Execute(buildpacks ...Buildpack) error { return p.initialize.Run(bps) } +type cloudFoundryDeinitializeProcess struct { + deinitialize cloudfoundry.DeinitializePhase +} + +func (p cloudFoundryDeinitializeProcess) Execute() error { + return p.deinitialize.Run() +} + type cloudFoundryDeployProcess struct { setup cloudfoundry.SetupPhase stage cloudfoundry.StagePhase diff --git a/vendor/github.com/cloudfoundry/switchblade/docker.go b/vendor/github.com/cloudfoundry/switchblade/docker.go index de528fd20..e940b6faf 100644 --- a/vendor/github.com/cloudfoundry/switchblade/docker.go +++ b/vendor/github.com/cloudfoundry/switchblade/docker.go @@ -9,16 +9,18 @@ import ( ) //go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface InitializePhase --name DockerInitializePhase --output fakes/docker_initialize_phase.go +//go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface DeinitializePhase --name DockerDeinitializePhase --output fakes/docker_deinitialize_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface SetupPhase --name DockerSetupPhase --output fakes/docker_setup_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface StagePhase --name DockerStagePhase --output fakes/docker_stage_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface StartPhase --name DockerStartPhase --output fakes/docker_start_phase.go //go:generate faux --package github.com/cloudfoundry/switchblade/internal/docker --interface TeardownPhase --name DockerTeardownPhase --output fakes/docker_teardown_phase.go -func NewDocker(initialize docker.InitializePhase, setup docker.SetupPhase, stage docker.StagePhase, start docker.StartPhase, teardown docker.TeardownPhase) Platform { +func NewDocker(initialize docker.InitializePhase, deinitialize docker.DeinitializePhase, setup docker.SetupPhase, stage docker.StagePhase, start docker.StartPhase, teardown docker.TeardownPhase) Platform { return Platform{ - initialize: dockerInitializeProcess{initialize: initialize}, - Deploy: dockerDeployProcess{setup: setup, stage: stage, start: start}, - Delete: dockerDeleteProcess{teardown: teardown}, + initialize: dockerInitializeProcess{initialize: initialize}, + deinitialize: dockerDeinitializeProcess{deinitialize: deinitialize}, + Deploy: dockerDeployProcess{setup: setup, stage: stage, start: start}, + Delete: dockerDeleteProcess{teardown: teardown}, } } @@ -35,9 +37,15 @@ func (p dockerInitializeProcess) Execute(buildpacks ...Buildpack) error { }) } - p.initialize.Run(bps) + return p.initialize.Run(bps) +} - return nil +type dockerDeinitializeProcess struct { + deinitialize docker.DeinitializePhase +} + +func (p dockerDeinitializeProcess) Execute() error { + return p.deinitialize.Run() } type dockerDeployProcess struct { diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/cloudfoundry/deinitialize.go b/vendor/github.com/cloudfoundry/switchblade/internal/cloudfoundry/deinitialize.go new file mode 100644 index 000000000..061ac2421 --- /dev/null +++ b/vendor/github.com/cloudfoundry/switchblade/internal/cloudfoundry/deinitialize.go @@ -0,0 +1,17 @@ +package cloudfoundry + +// This is noop functionality for the time being to reflect the docker structure + +type DeinitializePhase interface { + Run() error +} + +type Deinitialize struct{} + +func NewDeinitialize() Deinitialize { + return Deinitialize{} +} + +func (d Deinitialize) Run() error { + return nil +} diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/docker/deinitialize.go b/vendor/github.com/cloudfoundry/switchblade/internal/docker/deinitialize.go new file mode 100644 index 000000000..8bd447812 --- /dev/null +++ b/vendor/github.com/cloudfoundry/switchblade/internal/docker/deinitialize.go @@ -0,0 +1,36 @@ +package docker + +import ( + "context" + "fmt" +) + +//go:generate faux --interface DeinitializeNetworkManager --output fakes/deinitialize_network_manager.go +type DeinitializeNetworkManager interface { + Delete(ctx context.Context, name string) error +} + +type DeinitializePhase interface { + Run() error +} + +type Deinitialize struct { + network DeinitializeNetworkManager +} + +func NewDeinitialize(network DeinitializeNetworkManager) Deinitialize { + return Deinitialize{ + network: network, + } +} + +func (d Deinitialize) Run() error { + ctx := context.Background() + + err := d.network.Delete(ctx, InternalNetworkName) + if err != nil { + return fmt.Errorf("failed to delete network: %w", err) + } + + return nil +} diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/docker/initialize.go b/vendor/github.com/cloudfoundry/switchblade/internal/docker/initialize.go index b3040f5bb..5422c71e5 100644 --- a/vendor/github.com/cloudfoundry/switchblade/internal/docker/initialize.go +++ b/vendor/github.com/cloudfoundry/switchblade/internal/docker/initialize.go @@ -1,19 +1,44 @@ package docker +import ( + "context" + "fmt" +) + +const ( + InternalNetworkName = "switchblade-internal" +) + +//go:generate faux --interface InitializeNetworkManager --output fakes/initialize_network_manager.go +type InitializeNetworkManager interface { + Create(ctx context.Context, name, driver string, internal bool) error +} + type InitializePhase interface { - Run([]Buildpack) + Run([]Buildpack) error } type Initialize struct { registry BPRegistry + network InitializeNetworkManager } -func NewInitialize(registry BPRegistry) Initialize { +func NewInitialize(registry BPRegistry, network InitializeNetworkManager) Initialize { return Initialize{ registry: registry, + network: network, } } -func (i Initialize) Run(buildpacks []Buildpack) { +func (i Initialize) Run(buildpacks []Buildpack) error { i.registry.Override(buildpacks...) + + ctx := context.Background() + + err := i.network.Create(ctx, InternalNetworkName, "bridge", true) + if err != nil { + return fmt.Errorf("failed to create network: %w", err) + } + + return nil } diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/docker/setup.go b/vendor/github.com/cloudfoundry/switchblade/internal/docker/setup.go index ca83fa582..a9735f95b 100644 --- a/vendor/github.com/cloudfoundry/switchblade/internal/docker/setup.go +++ b/vendor/github.com/cloudfoundry/switchblade/internal/docker/setup.go @@ -19,7 +19,6 @@ import ( const ( BuildpackAppLifecycleRepoURL = "https://github.com/cloudfoundry/buildpackapplifecycle/archive/refs/heads/main.zip" - InternalNetworkName = "switchblade-internal" BridgeNetworkName = "bridge" ) @@ -118,11 +117,6 @@ func (s Setup) Run(ctx context.Context, logs io.Writer, name, path string) (stri return "", fmt.Errorf("failed to copy image pull logs: %w", err) } - err = s.networks.Create(ctx, InternalNetworkName, "bridge", true) - if err != nil { - return "", fmt.Errorf("failed to create network: %w", err) - } - env := []string{fmt.Sprintf("CF_STACK=%s", s.stack)} for key, value := range s.env { env = append(env, fmt.Sprintf("%s=%s", key, value)) diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/docker/start.go b/vendor/github.com/cloudfoundry/switchblade/internal/docker/start.go index d438f4332..314512855 100644 --- a/vendor/github.com/cloudfoundry/switchblade/internal/docker/start.go +++ b/vendor/github.com/cloudfoundry/switchblade/internal/docker/start.go @@ -4,15 +4,18 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/url" + "os" + "path/filepath" + "sort" + "strings" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/network" "github.com/docker/go-connections/nat" specs "github.com/opencontainers/image-spec/specs-go/v1" - "io" - "os" - "path/filepath" - "sort" ) type StartPhase interface { @@ -169,7 +172,7 @@ func (s Start) Run(ctx context.Context, logs io.Writer, name, command string) (s if ok { for _, binding := range bindings { if binding.HostIP == "0.0.0.0" { - externalURL = fmt.Sprintf("http://%s:%s", binding.HostIP, binding.HostPort) + externalURL = fmt.Sprintf("http://%s:%s", host(), binding.HostPort) } } } @@ -183,6 +186,20 @@ func (s Start) Run(ctx context.Context, logs io.Writer, name, command string) (s return externalURL, internalURL, nil } +func host() string { + val, ok := os.LookupEnv("DOCKER_HOST") + if !ok || strings.HasPrefix(val, "unix://") { + return "localhost" + } + + url, err := url.Parse(val) + if err != nil { + return "localhost" + } + + return url.Hostname() +} + func (s Start) WithStack(stack string) StartPhase { s.stack = stack return s diff --git a/vendor/github.com/cloudfoundry/switchblade/internal/docker/teardown.go b/vendor/github.com/cloudfoundry/switchblade/internal/docker/teardown.go index c0cd6db60..c2f5a580c 100644 --- a/vendor/github.com/cloudfoundry/switchblade/internal/docker/teardown.go +++ b/vendor/github.com/cloudfoundry/switchblade/internal/docker/teardown.go @@ -20,21 +20,14 @@ type TeardownClient interface { ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error } -//go:generate faux --interface TeardownNetworkManager --output fakes/teardown_network_manager.go -type TeardownNetworkManager interface { - Delete(ctx context.Context, name string) error -} - type Teardown struct { client TeardownClient - networks TeardownNetworkManager workspace string } -func NewTeardown(client TeardownClient, networks TeardownNetworkManager, workspace string) Teardown { +func NewTeardown(client TeardownClient, workspace string) Teardown { return Teardown{ client: client, - networks: networks, workspace: workspace, } } @@ -45,11 +38,6 @@ func (t Teardown) Run(ctx context.Context, name string) error { return fmt.Errorf("failed to remove container: %w", err) } - err = t.networks.Delete(ctx, InternalNetworkName) - if err != nil { - return fmt.Errorf("failed to delete network: %w", err) - } - err = os.Remove(filepath.Join(t.workspace, "droplets", fmt.Sprintf("%s.tar.gz", name))) if err != nil && !errors.Is(err, os.ErrNotExist) { return fmt.Errorf("failed to delete droplet tarball: %w", err) diff --git a/vendor/github.com/cloudfoundry/switchblade/platform.go b/vendor/github.com/cloudfoundry/switchblade/platform.go index c452502c8..a23df2a95 100644 --- a/vendor/github.com/cloudfoundry/switchblade/platform.go +++ b/vendor/github.com/cloudfoundry/switchblade/platform.go @@ -19,7 +19,8 @@ type Buildpack struct { type Service map[string]interface{} type Platform struct { - initialize initializeProcess + initialize initializeProcess + deinitialize deinitializeProcess Deploy DeployProcess Delete DeleteProcess @@ -44,6 +45,10 @@ type initializeProcess interface { Execute(buildpacks ...Buildpack) error } +type deinitializeProcess interface { + Execute() error +} + const ( CloudFoundry = "cf" Docker = "docker" @@ -60,11 +65,12 @@ func NewPlatform(platformType, token, stack string) (Platform, error) { cli := pexec.NewExecutable("cf") initialize := cloudfoundry.NewInitialize(cli, stack) + deinitialize := cloudfoundry.NewDeinitialize() setup := cloudfoundry.NewSetup(cli, filepath.Join(home, ".cf"), stack) stage := cloudfoundry.NewStage(cli) teardown := cloudfoundry.NewTeardown(cli) - return NewCloudFoundry(initialize, setup, stage, teardown, os.TempDir()), nil + return NewCloudFoundry(initialize, deinitialize, setup, stage, teardown, os.TempDir()), nil case Docker: client, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { @@ -81,13 +87,14 @@ func NewPlatform(platformType, token, stack string) (Platform, error) { buildpacksManager := docker.NewBuildpacksManager(archiver, buildpacksCache, buildpacksRegistry) networkManager := docker.NewNetworkManager(client) - initialize := docker.NewInitialize(buildpacksRegistry) + initialize := docker.NewInitialize(buildpacksRegistry, networkManager) + deinitialize := docker.NewDeinitialize(networkManager) setup := docker.NewSetup(client, lifecycleManager, buildpacksManager, archiver, networkManager, workspace, stack) stage := docker.NewStage(client, archiver, workspace) start := docker.NewStart(client, networkManager, workspace, stack) - teardown := docker.NewTeardown(client, networkManager, workspace) + teardown := docker.NewTeardown(client, workspace) - return NewDocker(initialize, setup, stage, start, teardown), nil + return NewDocker(initialize, deinitialize, setup, stage, start, teardown), nil } return Platform{}, fmt.Errorf("unknown platform type: %q", platformType) @@ -96,3 +103,7 @@ func NewPlatform(platformType, token, stack string) (Platform, error) { func (p Platform) Initialize(buildpacks ...Buildpack) error { return p.initialize.Execute(buildpacks...) } + +func (p Platform) Deinitialize() error { + return p.deinitialize.Execute() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 8e97e569e..02f55f264 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -30,8 +30,8 @@ github.com/cloudfoundry/libbuildpack/cutlass/docker github.com/cloudfoundry/libbuildpack/cutlass/glow github.com/cloudfoundry/libbuildpack/packager github.com/cloudfoundry/libbuildpack/snapshot -# github.com/cloudfoundry/switchblade v0.6.0 -## explicit; go 1.19 +# github.com/cloudfoundry/switchblade v0.9.0 +## explicit; go 1.23.0 github.com/cloudfoundry/switchblade github.com/cloudfoundry/switchblade/internal/cloudfoundry github.com/cloudfoundry/switchblade/internal/docker