Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ func buildImageOptions(image *types.ServiceVolumeImage) *mount.ImageOptions {
// name-matched networks is decided by the reconciler from the observed state.
func (s *composeService) createNetwork(ctx context.Context, n *types.NetworkConfig) error {
var ipam *network.IPAM
if n.Ipam.Config != nil {
if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 || len(n.Ipam.Options) > 0 {
var config []network.IPAMConfig
for _, pool := range n.Ipam.Config {
c, err := parseIPAMPool(pool)
Expand All @@ -1413,8 +1413,9 @@ func (s *composeService) createNetwork(ctx context.Context, n *types.NetworkConf
config = append(config, c)
}
ipam = &network.IPAM{
Driver: n.Ipam.Driver,
Config: config,
Driver: n.Ipam.Driver,
Config: config,
Options: n.Ipam.Options,
}
}
hash, err := NetworkHash(n)
Expand All @@ -1433,22 +1434,6 @@ func (s *composeService) createNetwork(ctx context.Context, n *types.NetworkConf
EnableIPv4: n.EnableIPv4,
}

if n.Ipam.Driver != "" || len(n.Ipam.Config) > 0 {
createOpts.IPAM = &network.IPAM{}
}

if n.Ipam.Driver != "" {
createOpts.IPAM.Driver = n.Ipam.Driver
}

for _, ipamConfig := range n.Ipam.Config {
c, err := parseIPAMPool(ipamConfig)
if err != nil {
return err
}
createOpts.IPAM.Config = append(createOpts.IPAM.Config, c)
}

networkEventName := fmt.Sprintf("Network %s", n.Name)
s.events.On(creatingEvent(networkEventName))

Expand Down
69 changes: 60 additions & 9 deletions pkg/compose/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/compose-spec/compose-go/v2/types"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/client"
"go.uber.org/mock/gomock"
"gotest.tools/v3/assert"
Expand All @@ -39,6 +40,17 @@ func (noopEventProcessor) Start(_ context.Context, _ string) {}
func (noopEventProcessor) On(_ ...api.Resource) {}
func (noopEventProcessor) Done(_ string, _ bool) {}

const (
executorTestProjectName = "test"
executorTestNetworkKey = "default"
executorTestNetworkName = "test_default"
executorTestNetworkResource = "network:" + executorTestNetworkKey
executorTestNotFoundCause = "not found"
executorTestCreatedNetworkID = "net1"
ipamOptionsKey = "ipam-option"
ipamOptionsValue = "enabled"
)

Comment thread
ousamabenyounes marked this conversation as resolved.
func newTestService(t *testing.T) (*composeService, *mocks.MockAPIClient) {
t.Helper()
mockCtrl := gomock.NewController(t)
Expand All @@ -53,34 +65,73 @@ func newTestService(t *testing.T) (*composeService, *mocks.MockAPIClient) {

func TestExecutePlanEmpty(t *testing.T) {
svc, _ := newTestService(t)
err := svc.executePlan(t.Context(), &types.Project{Name: "test"}, emptyObservedState("test"), &Plan{})
err := svc.executePlan(t.Context(), &types.Project{Name: executorTestProjectName}, emptyObservedState(executorTestProjectName), &Plan{})
assert.NilError(t, err)
}

func TestExecutePlanCreateNetwork(t *testing.T) {
svc, apiClient := newTestService(t)

nw := types.NetworkConfig{Name: "test_default"}
nw := types.NetworkConfig{Name: executorTestNetworkName}
project := &types.Project{
Name: "test",
Networks: types.Networks{"default": nw},
Name: executorTestProjectName,
Networks: types.Networks{executorTestNetworkKey: nw},
}

// createNetwork issues a plain NetworkCreate (divergence/reuse decisions are
// made by the reconciler from the observed state, not here).
apiClient.EXPECT().NetworkCreate(gomock.Any(), "test_default", gomock.Any()).
Return(client.NetworkCreateResult{ID: "net1"}, nil)
apiClient.EXPECT().NetworkCreate(gomock.Any(), executorTestNetworkName, gomock.Any()).
Return(client.NetworkCreateResult{ID: executorTestCreatedNetworkID}, nil)

plan := &Plan{}
plan.addNode(Operation{
Type: OpCreateNetwork,
ResourceID: "network:default",
Cause: "not found",
ResourceID: executorTestNetworkResource,
Cause: executorTestNotFoundCause,
Name: nw.Name,
Network: &nw,
}, "")

err := svc.executePlan(t.Context(), project, emptyObservedState(executorTestProjectName), plan)
assert.NilError(t, err)
}

func TestExecutePlanCreateNetworkWithIPAMOptions(t *testing.T) {
svc, apiClient := newTestService(t)

nw := types.NetworkConfig{
Name: executorTestNetworkName,
Ipam: types.IPAMConfig{
Options: types.Options{
ipamOptionsKey: ipamOptionsValue,
},
},
}
project := &types.Project{
Name: executorTestProjectName,
Networks: types.Networks{executorTestNetworkKey: nw},
}

apiClient.EXPECT().NetworkCreate(gomock.Any(), executorTestNetworkName, gomock.Any()).
DoAndReturn(func(_ context.Context, _ string, opts client.NetworkCreateOptions) (client.NetworkCreateResult, error) {
assert.DeepEqual(t, opts.IPAM, &network.IPAM{
Options: map[string]string{
ipamOptionsKey: ipamOptionsValue,
},
})
return client.NetworkCreateResult{ID: executorTestCreatedNetworkID}, nil
})

plan := &Plan{}
plan.addNode(Operation{
Type: OpCreateNetwork,
ResourceID: executorTestNetworkResource,
Cause: executorTestNotFoundCause,
Name: nw.Name,
Network: &nw,
}, "")

err := svc.executePlan(t.Context(), project, emptyObservedState("test"), plan)
err := svc.executePlan(t.Context(), project, emptyObservedState(executorTestProjectName), plan)
assert.NilError(t, err)
}

Expand Down