Skip to content

Commit

Permalink
Support Additional Announcable CIDRs (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 committed Sep 3, 2024
1 parent e616e8a commit 4b7b53c
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-drafter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
- uses: release-drafter/release-drafter@v6
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.18
FROM alpine:3.20
LABEL maintainer="metal-stack authors <[email protected]>"
COPY bin/metalctl-linux-amd64 /metalctl
ENTRYPOINT ["/metalctl"]
2 changes: 1 addition & 1 deletion Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22
FROM golang:1.23
WORKDIR /work
COPY go.* .
RUN go mod download
Expand Down
2 changes: 1 addition & 1 deletion cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func assertExhaustiveArgs(t *testing.T, args []string, exclude ...string) {
return nil
}
}
return fmt.Errorf("not exhaustive: does not contain " + prefix)
return fmt.Errorf("not exhaustive: does not contain %s", prefix)
}

root := newRootCmd(&config{comp: &completion.Completion{}})
Expand Down
96 changes: 53 additions & 43 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func newNetworkCmd(c *config) *cobra.Command {
cmd.Flags().StringSlice("prefixes", []string{}, "prefixes in this network.")
cmd.Flags().StringSlice("labels", []string{}, "add initial labels, must be in the form of key=value, use it like: --labels \"key1=value1,key2=value2\".")
cmd.Flags().StringSlice("destination-prefixes", []string{}, "destination prefixes in this network.")
cmd.Flags().StringSlice("additional-announcable-cidrs", []string{}, "list of cidrs which are added to the route maps per tenant private network, these are typically pod- and service cidrs, can only be set in a supernetwork")
cmd.Flags().BoolP("privatesuper", "", false, "set private super flag of network, if set to true, this network is used to start machines there.")
cmd.Flags().BoolP("nat", "", false, "set nat flag of network, if set to true, traffic from this network will be natted.")
cmd.Flags().BoolP("underlay", "", false, "set underlay flag of network, if set to true, this is used to transport underlay network traffic")
Expand Down Expand Up @@ -80,6 +81,7 @@ func newNetworkCmd(c *config) *cobra.Command {
cmd.Flags().StringSlice("add-destinationprefixes", []string{}, "destination prefixes to be added to the network [optional]")
cmd.Flags().StringSlice("remove-destinationprefixes", []string{}, "destination prefixes to be removed from the network [optional]")
cmd.Flags().StringSlice("labels", []string{}, "the labels of the network, must be in the form of key=value, use it like: --labels \"key1=value1,key2=value2\". [optional]")
cmd.Flags().StringSlice("additional-announcable-cidrs", []string{}, "list of cidrs which are added to the route maps per tenant private network, these are typically pod- and service cidrs, can only be set in a supernetwork")
cmd.Flags().Bool("shared", false, "marks a network as shared or not [optional]")
},
}
Expand Down Expand Up @@ -208,7 +210,7 @@ func (c networkCmd) Create(rq *models.V1NetworkCreateRequest) (*models.V1Network
}

func (c networkCmd) Update(rq *models.V1NetworkUpdateRequest) (*models.V1NetworkResponse, error) {
resp, err := c.client.Network().UpdateNetwork(network.NewUpdateNetworkParams().WithBody(rq), nil)
resp, err := c.client.Network().UpdateNetwork(network.NewUpdateNetworkParams().WithBody(rq).WithForce(pointer.Pointer(viper.GetBool(forceFlag))), nil)
if err != nil {
return nil, err
}
Expand All @@ -225,33 +227,35 @@ func (c networkCmd) Convert(r *models.V1NetworkResponse) (string, *models.V1Netw

func networkResponseToCreate(r *models.V1NetworkResponse) *models.V1NetworkCreateRequest {
return &models.V1NetworkCreateRequest{
Description: r.Description,
Destinationprefixes: r.Destinationprefixes,
ID: r.ID,
Labels: r.Labels,
Name: r.Name,
Nat: r.Nat,
Parentnetworkid: r.Parentnetworkid,
Partitionid: r.Partitionid,
Prefixes: r.Prefixes,
Privatesuper: r.Privatesuper,
Projectid: r.Projectid,
Shared: r.Shared,
Underlay: r.Underlay,
Vrf: r.Vrf,
Vrfshared: r.Vrfshared,
Description: r.Description,
Destinationprefixes: r.Destinationprefixes,
ID: r.ID,
Labels: r.Labels,
Name: r.Name,
Nat: r.Nat,
Parentnetworkid: r.Parentnetworkid,
Partitionid: r.Partitionid,
Prefixes: r.Prefixes,
Privatesuper: r.Privatesuper,
Projectid: r.Projectid,
Shared: r.Shared,
Underlay: r.Underlay,
Vrf: r.Vrf,
Vrfshared: r.Vrfshared,
AdditionalAnnouncableCIDRs: r.AdditionalAnnouncableCIDRs,
}
}

func networkResponseToUpdate(r *models.V1NetworkResponse) *models.V1NetworkUpdateRequest {
return &models.V1NetworkUpdateRequest{
Description: r.Description,
Destinationprefixes: r.Destinationprefixes,
ID: r.ID,
Labels: r.Labels,
Name: r.Name,
Prefixes: r.Prefixes,
Shared: r.Shared,
Description: r.Description,
Destinationprefixes: r.Destinationprefixes,
ID: r.ID,
Labels: r.Labels,
Name: r.Name,
Prefixes: r.Prefixes,
Shared: r.Shared,
AdditionalAnnouncableCIDRs: r.AdditionalAnnouncableCIDRs,
}
}

Expand All @@ -262,19 +266,20 @@ func (c *networkCmd) createRequestFromCLI() (*models.V1NetworkCreateRequest, err
}

return &models.V1NetworkCreateRequest{
ID: pointer.Pointer(viper.GetString("id")),
Description: viper.GetString("description"),
Name: viper.GetString("name"),
Partitionid: viper.GetString("partition"),
Projectid: viper.GetString("project"),
Prefixes: viper.GetStringSlice("prefixes"),
Destinationprefixes: viper.GetStringSlice("destination-prefixes"),
Privatesuper: pointer.Pointer(viper.GetBool("privatesuper")),
Nat: pointer.Pointer(viper.GetBool("nat")),
Underlay: pointer.Pointer(viper.GetBool("underlay")),
Vrf: viper.GetInt64("vrf"),
Vrfshared: viper.GetBool("vrfshared"),
Labels: lbs,
ID: pointer.Pointer(viper.GetString("id")),
Description: viper.GetString("description"),
Name: viper.GetString("name"),
Partitionid: viper.GetString("partition"),
Projectid: viper.GetString("project"),
Prefixes: viper.GetStringSlice("prefixes"),
Destinationprefixes: viper.GetStringSlice("destination-prefixes"),
Privatesuper: pointer.Pointer(viper.GetBool("privatesuper")),
Nat: pointer.Pointer(viper.GetBool("nat")),
Underlay: pointer.Pointer(viper.GetBool("underlay")),
Vrf: viper.GetInt64("vrf"),
Vrfshared: viper.GetBool("vrfshared"),
Labels: lbs,
AdditionalAnnouncableCIDRs: viper.GetStringSlice("additional-announcable-cidrs"),
}, nil
}

Expand Down Expand Up @@ -356,15 +361,20 @@ func (c *networkCmd) updateRequestFromCLI(args []string) (*models.V1NetworkUpdat
shared = viper.GetBool("shared")
}

additionalCidrs := resp.AdditionalAnnouncableCIDRs
if viper.IsSet("additional-announcable-cidrs") {
additionalCidrs = viper.GetStringSlice("additional-announcable-cidrs")
}
var (
ur = &models.V1NetworkUpdateRequest{
Description: viper.GetString("description"),
Destinationprefixes: nil,
ID: pointer.Pointer(id),
Labels: labels,
Name: viper.GetString("name"),
Prefixes: nil,
Shared: shared,
Description: viper.GetString("description"),
Destinationprefixes: nil,
ID: pointer.Pointer(id),
Labels: labels,
Name: viper.GetString("name"),
Prefixes: nil,
Shared: shared,
AdditionalAnnouncableCIDRs: additionalCidrs,
}
addPrefixes = sets.New(viper.GetStringSlice("add-prefixes")...)
removePrefixes = sets.New(viper.GetStringSlice("remove-prefixes")...)
Expand Down
38 changes: 22 additions & 16 deletions cmd/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ var (
UsedIps: pointer.Pointer(int64(300)),
UsedPrefixes: pointer.Pointer(int64(400)),
},
Vrf: 50,
Vrfshared: true,
Vrf: 50,
Vrfshared: true,
AdditionalAnnouncableCIDRs: []string{"10.240.0.0/12"},
}
network1child = &models.V1NetworkResponse{
Description: "child 1",
Expand All @@ -62,8 +63,9 @@ var (
UsedIps: pointer.Pointer(int64(300)),
UsedPrefixes: pointer.Pointer(int64(400)),
},
Vrf: 50,
Vrfshared: true,
Vrf: 50,
Vrfshared: true,
AdditionalAnnouncableCIDRs: []string{},
}
network2 = &models.V1NetworkResponse{
Description: "network 2",
Expand All @@ -85,8 +87,9 @@ var (
UsedIps: pointer.Pointer(int64(200)),
UsedPrefixes: pointer.Pointer(int64(100)),
},
Vrf: 60,
Vrfshared: true,
Vrf: 60,
Vrfshared: true,
AdditionalAnnouncableCIDRs: []string{},
}
)

Expand Down Expand Up @@ -156,7 +159,7 @@ nw2 network-2
mocks: &client.MetalMockFns{
Network: func(mock *mock.Mock) {
mock.On("CreateNetwork", testcommon.MatchIgnoreContext(t, network.NewCreateNetworkParams().WithBody(networkResponseToCreate(network1))), nil).Return(nil, &network.CreateNetworkConflict{}).Once()
mock.On("UpdateNetwork", testcommon.MatchIgnoreContext(t, network.NewUpdateNetworkParams().WithBody(networkResponseToUpdate(network1))), nil).Return(&network.UpdateNetworkOK{
mock.On("UpdateNetwork", testcommon.MatchIgnoreContext(t, network.NewUpdateNetworkParams().WithBody(networkResponseToUpdate(network1)).WithForce(pointer.Pointer(false))), nil).Return(&network.UpdateNetworkOK{
Payload: network1,
}, nil)
mock.On("CreateNetwork", testcommon.MatchIgnoreContext(t, network.NewCreateNetworkParams().WithBody(networkResponseToCreate(network2))), nil).Return(&network.CreateNetworkCreated{
Expand Down Expand Up @@ -198,7 +201,7 @@ nw2 network-2
},
mocks: &client.MetalMockFns{
Network: func(mock *mock.Mock) {
mock.On("UpdateNetwork", testcommon.MatchIgnoreContext(t, network.NewUpdateNetworkParams().WithBody(networkResponseToUpdate(network1))), nil).Return(&network.UpdateNetworkOK{
mock.On("UpdateNetwork", testcommon.MatchIgnoreContext(t, network.NewUpdateNetworkParams().WithBody(networkResponseToUpdate(network1)).WithForce(pointer.Pointer(false))), nil).Return(&network.UpdateNetworkOK{
Payload: network1,
}, nil)
},
Expand Down Expand Up @@ -297,6 +300,7 @@ nw1 network-1
"--underlay", strconv.FormatBool(*want.Underlay),
"--vrf", strconv.FormatInt(want.Vrf, 10),
"--vrfshared", strconv.FormatBool(want.Vrfshared),
"--additional-announcable-cidrs", "10.240.0.0/12",
}
assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
Expand All @@ -322,6 +326,7 @@ nw1 network-1
fmt.Sprintf("--shared=%t", want.Shared),
"--labels", "a=b",
"--name", want.Name,
"--additional-announcable-cidrs", "10.240.0.0/12",
}
assertExhaustiveArgs(t, args, commonExcludedFileArgs()...)
return args
Expand All @@ -337,14 +342,15 @@ nw1 network-1
Payload: networkToUpdate,
}, nil)
mock.On("UpdateNetwork", testcommon.MatchIgnoreContext(t, network.NewUpdateNetworkParams().WithBody(&models.V1NetworkUpdateRequest{
ID: network1.ID,
Name: network1.Name,
Description: network1.Description,
Destinationprefixes: network1.Destinationprefixes,
Prefixes: network1.Prefixes,
Labels: network1.Labels,
Shared: network1.Shared,
})), nil).Return(&network.UpdateNetworkOK{
ID: network1.ID,
Name: network1.Name,
Description: network1.Description,
Destinationprefixes: network1.Destinationprefixes,
Prefixes: network1.Prefixes,
Labels: network1.Labels,
Shared: network1.Shared,
AdditionalAnnouncableCIDRs: network1.AdditionalAnnouncableCIDRs,
}).WithForce(pointer.Pointer(false))), nil).Return(&network.UpdateNetworkOK{
Payload: network1,
}, nil)
},
Expand Down
59 changes: 30 additions & 29 deletions docs/metalctl_network_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ metalctl network create [flags]
### Options

```
--bulk-output when used with --file (bulk operation): prints results at the end as a list. default is printing results intermediately during the operation, which causes single entities to be printed in a row.
-d, --description string description of the network to create. [optional]
--destination-prefixes strings destination prefixes in this network.
-f, --file string filename of the create or update request in yaml format, or - for stdin.
Example:
$ metalctl network describe network-1 -o yaml > network.yaml
$ vi network.yaml
$ # either via stdin
$ cat network.yaml | metalctl network create -f -
$ # or via file
$ metalctl network create -f network.yaml
the file can also contain multiple documents and perform a bulk operation.
-h, --help help for create
--id string id of the network to create. [optional]
--labels strings add initial labels, must be in the form of key=value, use it like: --labels "key1=value1,key2=value2".
-n, --name string name of the network to create. [optional]
--nat set nat flag of network, if set to true, traffic from this network will be natted.
-p, --partition string partition where this network should exist.
--prefixes strings prefixes in this network.
--privatesuper set private super flag of network, if set to true, this network is used to start machines there.
--project string project of the network to create. [optional]
--skip-security-prompts skips security prompt for bulk operations
--timestamps when used with --file (bulk operation): prints timestamps in-between the operations
--underlay set underlay flag of network, if set to true, this is used to transport underlay network traffic
--vrf int vrf of this network
--vrfshared vrf shared allows multiple networks to share a vrf
--additional-announcable-cidrs strings list of cidrs which are added to the route maps per tenant private network, these are typically pod- and service cidrs, can only be set in a supernetwork
--bulk-output when used with --file (bulk operation): prints results at the end as a list. default is printing results intermediately during the operation, which causes single entities to be printed in a row.
-d, --description string description of the network to create. [optional]
--destination-prefixes strings destination prefixes in this network.
-f, --file string filename of the create or update request in yaml format, or - for stdin.
Example:
$ metalctl network describe network-1 -o yaml > network.yaml
$ vi network.yaml
$ # either via stdin
$ cat network.yaml | metalctl network create -f -
$ # or via file
$ metalctl network create -f network.yaml
the file can also contain multiple documents and perform a bulk operation.
-h, --help help for create
--id string id of the network to create. [optional]
--labels strings add initial labels, must be in the form of key=value, use it like: --labels "key1=value1,key2=value2".
-n, --name string name of the network to create. [optional]
--nat set nat flag of network, if set to true, traffic from this network will be natted.
-p, --partition string partition where this network should exist.
--prefixes strings prefixes in this network.
--privatesuper set private super flag of network, if set to true, this network is used to start machines there.
--project string project of the network to create. [optional]
--skip-security-prompts skips security prompt for bulk operations
--timestamps when used with --file (bulk operation): prints timestamps in-between the operations
--underlay set underlay flag of network, if set to true, this is used to transport underlay network traffic
--vrf int vrf of this network
--vrfshared vrf shared allows multiple networks to share a vrf
```

### Options inherited from parent commands
Expand Down
Loading

0 comments on commit 4b7b53c

Please sign in to comment.