Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add powervs network create and delete cmd #1159

Merged
merged 3 commits into from
Mar 15, 2023
Merged
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
103 changes: 86 additions & 17 deletions cmd/capibmadm/cmd/powervs/network/create.go
Original file line number Diff line number Diff line change
@@ -17,50 +17,119 @@ limitations under the License.
package network

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

v "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"

logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

type networkCreateOptions struct {
name string
dnsServers []string
name string
private bool
public bool
cidr string
dnsServers []string
gateway string
jumbo bool
ipAddressRanges []string
}

// CreateCommand function to create PowerVS network.
func CreateCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create NETWORK_NAME",
Use: "create",
Short: "Create PowerVS network",
Example: `
# Create PowerVS network with name capi-network
# Create PowerVS network
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network create capi-network --service-instance-id <service-instance-id>`,
Public network: capibmadm powervs network create --public --service-instance-id <service-instance-id> --zone <zone>
Private network: capibmadm powervs network create --private --cidr <cidr> --service-instance-id <service-instance-id> --zone <zone>
Private network with ip address ranges: capibmadm powervs network create --private --cidr <cidr> --ip-ranges <start-ip>-<end-ip>,<start-ip>-<end-ip> --service-instance-id <service-instance-id> --zone <zone>
`,
}

var netCreateOption networkCreateOptions
cmd.Flags().StringSliceVar(&netCreateOption.dnsServers, "dns-servers", []string{"8.8.8.8", "9.9.9.9"}, "Comma separated list of DNS Servers to use for this network")
cmd.Flags().StringVar(&netCreateOption.name, "name", "", "The name of the network")
cmd.Flags().BoolVar(&netCreateOption.public, "public", true, "Public (pub-vlan) network type")
cmd.Flags().BoolVar(&netCreateOption.private, "private", false, "Private (vlan) network type")
cmd.Flags().StringVar(&netCreateOption.cidr, "cidr", "", "The network CIDR. Required for private network type")
cmd.Flags().StringVar(&netCreateOption.name, "gateway", "", "The gateway ip address")
cmd.Flags().StringSliceVar(&netCreateOption.dnsServers, "dns-servers", []string{"8.8.8.8", "9.9.9.9"}, "Comma separated list of DNS Servers to use")
cmd.Flags().StringSliceVar(&netCreateOption.ipAddressRanges, "ip-ranges", []string{}, "Comma separated IP Address Ranges")
cmd.Flags().BoolVar(&netCreateOption.jumbo, "jumbo", false, "Enable MTU Jumbo Network")

// both cannot be provided, default is public
cmd.MarkFlagsMutuallyExclusive("private", "public")
// cidr is required for private vlan
cmd.MarkFlagsRequiredTogether("private", "cidr")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("network name is not provided")
}
netCreateOption.name = args[0]
if err := createNetwork(netCreateOption); err != nil {
return err
}
return nil
return createNetwork(cmd.Context(), netCreateOption)
}
return cmd
}

func createNetwork(netCreateOption networkCreateOptions) error {
func createNetwork(ctx context.Context, netCreateOption networkCreateOptions) error {
log := logf.Log
log.Info("Creating Power VS network", "name", netCreateOption.name, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "dns-servers", netCreateOption.dnsServers)
//TODO: add network creation logic here
log.Info("Creating PowerVS network", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}

networkClient := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)

// default is public network
ntype := "pub-vlan"
if netCreateOption.private {
ntype = "vlan"
}

body := &models.NetworkCreate{
Name: netCreateOption.name,
Type: &ntype,
Cidr: netCreateOption.cidr,
DNSServers: netCreateOption.dnsServers,
Gateway: netCreateOption.gateway,
Jumbo: netCreateOption.jumbo,
}

var ipAddressRanges []*models.IPAddressRange
for _, ipRange := range netCreateOption.ipAddressRanges {
if ipRange != "" {
ipAddresses := strings.Split(ipRange, "-")
if len(ipAddresses) != 2 {
return fmt.Errorf("failed to read ip range, provide a range of IP addresses \"startIP-endIP\"")
}
ipAddressRanges = append(ipAddressRanges, &models.IPAddressRange{
StartingIPAddress: &ipAddresses[0],
EndingIPAddress: &ipAddresses[1],
})
}
}
body.IPAddressRanges = ipAddressRanges

network, err := networkClient.Create(body)
if err != nil {
return fmt.Errorf("failed to create a network, err: %v", err)
}
log.Info("Successfully created a network", "networkID", *network.NetworkID)

return nil
}
76 changes: 76 additions & 0 deletions cmd/capibmadm/cmd/powervs/network/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package network

import (
"context"

"github.com/spf13/cobra"

v "github.com/IBM-Cloud/power-go-client/clients/instance"

logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/iam"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/clients/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
)

// DeleteCommand function to delete network.
func DeleteCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete PowerVS network",
Example: `
# Delete PowerVS network
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network delete --network <network-name/network-id> --service-instance-id <service-instance-id> --zone <zone>`,
}

var networkID string
cmd.Flags().StringVar(&networkID, "network", "", "Network ID or Name")
_ = cmd.MarkFlagRequired("network")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
return deleteNetwork(cmd.Context(), networkID)
}
return cmd
}

func deleteNetwork(ctx context.Context, networkID string) error {
log := logf.Log
log.Info("Deleting PowerVS network", "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone)

accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth())
if err != nil {
return err
}
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}

networkClient := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)

if err = networkClient.Delete(networkID); err != nil {
return err
}

log.Info("Successfully deleted a network", "network", networkID)
return nil
}
5 changes: 1 addition & 4 deletions cmd/capibmadm/cmd/powervs/network/list.go
Original file line number Diff line number Diff line change
@@ -44,10 +44,7 @@ func ListCommand() *cobra.Command {
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network list --service-instance-id <service-instance-id> --zone <zone>`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := listNetwork(cmd.Context()); err != nil {
return err
}
return nil
return listNetwork(cmd.Context())
},
}

1 change: 1 addition & 0 deletions cmd/capibmadm/cmd/powervs/network/network.go
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ func Commands() *cobra.Command {

cmd.AddCommand(CreateCommand())
cmd.AddCommand(ListCommand())
cmd.AddCommand(DeleteCommand())

return cmd
}
7 changes: 4 additions & 3 deletions docs/book/src/topics/capibmadm/powervs/index.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# capibmadm powervs `<commands>`


## 1. Power VS commands
## 1. PowerVS commands
- [key](./key.md)
- [create](/topics/capibmadm/powervs/key.html#1-capibmadm-powervs-key-create)
- [delete](/topics/capibmadm/powervs/key.html#2-capibmadm-powervs-key-delete)
- [network](./network.md)
- [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create)
- [list](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-list)
- [delete](/topics/capibmadm/powervs/network.html#2-capibmadm-powervs-network-delete)
- [list](/topics/capibmadm/powervs/network.html#3-capibmadm-powervs-network-list)
- [port](./port.md)
- [create](/topics/capibmadm/powervs/port.html#1-capibmadm-powervs-port-create)
- [delete](/topics/capibmadm/powervs/port.html#2-capibmadm-powervs-port-delete)
- [list](/topics/capibmadm/powervs/port.html#3-capibmadm-powervs-port-list)
- [image](./image.md)
- [list] (/topics/capibmadm/powervs/image.html#1-capibmadm-powervs-image-list))

55 changes: 48 additions & 7 deletions docs/book/src/topics/capibmadm/powervs/network.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,74 @@
## Power VS Network Commands
## PowerVS Network Commands

### 1. capibmadm powervs network create

#### Usage:
Create Power VS network.
Create PowerVS network.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud api key.
IBMCLOUD_API_KEY: IBM Cloud API key.

#### Arguments:
--service-instance-id: Power VS service instance id.
--service-instance-id: PowerVS service instance id.

--cidr: The network CIDR. Required for private network type.

--name: The name of the network.

--public: Public (pub-vlan) network type (default true)

--private: Private (vlan) network type (default false)

--gateway: The gateway ip address.

--dns-servers: Comma separated list of DNS Servers to use for this network, Defaults to 8.8.8.8, 9.9.9.9.

--ip-ranges: Comma separated IP Address Ranges.

--jumbo: Enable MTU Jumbo Network.


#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network create <network-name> --service-instance-id <service-instance-id>
# Public network:
capibmadm powervs network create --public --service-instance-id <service-instance-id> --zone <zone>
# Private network:
capibmadm powervs network create --private --cidr <cidr> --service-instance-id <service-instance-id> --zone <zone>
# Private network with ip address ranges:
capibmadm powervs network create --private --cidr <cidr> --ip-ranges <start-ip>-<end-ip>,<start-ip>-<end-ip> --service-instance-id <service-instance-id> --zone <zone>
```


### 2. capibmadm powervs network delete

#### Usage:
Delete PowerVS network.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud API key.

#### Arguments:
--service-instance-id: PowerVS service instance id.

--zone: PowerVS service instance zone.

--network: Network ID or Name.

#### Example:
```shell
export IBMCLOUD_API_KEY=<api-key>
capibmadm powervs network delete --network <network-name/network-id> --service-instance-id <service-instance-id> --zone <zone>
```


### 2. capibmadm powervs network list
### 3. capibmadm powervs network list

#### Usage:
List PowerVS networks.

#### Environmental Variable:
IBMCLOUD_API_KEY: IBM Cloud api key.
IBMCLOUD_API_KEY: IBM Cloud API key.

#### Arguments:
--service-instance-id: PowerVS service instance id.