Skip to content

Commit

Permalink
Added Port Delete command to capibmadm cli
Browse files Browse the repository at this point in the history
fix PVS e2e - introduce ginkgo timeout (kubernetes-sigs#1082)

Signed-off-by: Prajyot-Parab <[email protected]>

Add common flags for capibmadm cli (kubernetes-sigs#1081)

Add validation for output flag
  • Loading branch information
Rajalakshmi-Girish committed Feb 8, 2023
1 parent 5eff1b0 commit 990f508
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ test: generate fmt vet setup-envtest $(GOTESTSUM) ## Run tests
GINKGO_FOCUS ?= Workload cluster creation
GINKGO_NODES ?= 3
GINKGO_NOCOLOR ?= false
GINKGO_TIMEOUT ?= 2h
E2E_FLAVOR ?= powervs-md-remediation
JUNIT_FILE ?= junit.e2e_suite.1.xml
GINKGO_ARGS ?= -v --trace --tags=e2e --focus=$(GINKGO_FOCUS) --nodes=$(GINKGO_NODES) --no-color=$(GINKGO_NOCOLOR) --output-dir="$(ARTIFACTS)" --junit-report="$(JUNIT_FILE)"
GINKGO_ARGS ?= -v --trace --tags=e2e --timeout=$(GINKGO_TIMEOUT) --focus=$(GINKGO_FOCUS) --nodes=$(GINKGO_NODES) --no-color=$(GINKGO_NOCOLOR) --output-dir="$(ARTIFACTS)" --junit-report="$(JUNIT_FILE)"
ARTIFACTS ?= $(REPO_ROOT)/_artifacts
SKIP_CLEANUP ?= false
SKIP_CREATE_MGMT_CLUSTER ?= false
Expand Down
5 changes: 1 addition & 4 deletions cmd/capibmadm/clients/powervs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ limitations under the License.
package powervs

import (
"fmt"

"github.com/IBM-Cloud/power-go-client/ibmpisession"

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

// NewPISession creates new powervs client.
// To-Do: Need to handle custom endpoint URL if user wants to use staging env.
func NewPISession(accountID string, region string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
func NewPISession(accountID string, zone string, debug bool) (*ibmpisession.IBMPISession, error) {
return ibmpisession.NewIBMPISession(&ibmpisession.IBMPIOptions{Authenticator: iam.GetIAMAuth(),
Debug: debug,
URL: fmt.Sprintf("https://%s.power-iaas.cloud.ibm.com", region),
UserAccount: accountID,
Zone: zone})
}
3 changes: 0 additions & 3 deletions cmd/capibmadm/cmd/powervs/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package network

import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

// Commands function to add PowerVS network commands.
Expand All @@ -28,7 +26,6 @@ func Commands() *cobra.Command {
Use: "network",
Short: "Perform PowerVS network operations",
}
options.AddPowerVSCommonFlags(cmd)

cmd.AddCommand(CreateCommand())

Expand Down
82 changes: 82 additions & 0 deletions cmd/capibmadm/cmd/powervs/port/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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 port

import (
"context"
"fmt"

"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"
)

type portDeleteOptions struct {
network string
portID string
}

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

var portDeleteOption portDeleteOptions
cmd.Flags().StringVar(&portDeleteOption.portID, "port-id", "", "Port ID to be deleted")
cmd.Flags().StringVar(&portDeleteOption.network, "network", "", "Network ID or Name(preference will be given to the ID over Name")
_ = cmd.MarkFlagRequired("port-id")
_ = cmd.MarkFlagRequired("network")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if err := deletePort(cmd.Context(), portDeleteOption); err != nil {
return err
}
return nil
}
return cmd
}

func deletePort(ctx context.Context, portDeleteOption portDeleteOptions) error {
log := logf.Log
log.Info("Deleting Power VS network port", "of network", portDeleteOption.network, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "port-id", portDeleteOption.portID)
auth := iam.GetIAMAuth()
accountID, _ := utils.GetAccountID(ctx, auth)
sess, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug)
if err != nil {
return err
}
c := v.NewIBMPINetworkClient(ctx, sess, options.GlobalOptions.ServiceInstanceID)
errDel := c.DeletePort(portDeleteOption.network, portDeleteOption.portID)
if errDel != nil {
return errDel
}
fmt.Println("Successfully deleted a port, id:", portDeleteOption.portID)
return nil
}
18 changes: 18 additions & 0 deletions cmd/capibmadm/cmd/powervs/port/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
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 port contains the commands to operate on Power VS Port resources.
package port
35 changes: 35 additions & 0 deletions cmd/capibmadm/cmd/powervs/port/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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 port

import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

// Commands function to add PowerVS port commands.
func Commands() *cobra.Command {
cmd := &cobra.Command{
Use: "port",
Short: "Perform PowerVS port operations",
}
options.AddPowerVSCommonFlags(cmd)

cmd.AddCommand(DeleteCommand())
return cmd
}
10 changes: 10 additions & 0 deletions cmd/capibmadm/cmd/powervs/powervs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/port"
)

// Commands initialises and returns powervs command.
Expand All @@ -28,7 +29,16 @@ func Commands() *cobra.Command {
Use: "powervs",
Short: "Commands for operations on PowerVS resources",
}

cmd.PersistentFlags().StringVar(&options.GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
cmd.PersistentFlags().StringVar(&options.GlobalOptions.PowerVSZone, "zone", options.GlobalOptions.PowerVSZone, "IBM cloud PowerVS zone (Required)")
cmd.PersistentFlags().BoolVar(&options.GlobalOptions.Debug, "debug", false, "Enable/Disable http transport debugging log")

_ = cmd.MarkPersistentFlagRequired("service-instance-id")
_ = cmd.MarkPersistentFlagRequired("zone")

cmd.AddCommand(network.Commands())
cmd.AddCommand(port.Commands())

return cmd
}
3 changes: 3 additions & 0 deletions cmd/capibmadm/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"

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

Expand All @@ -51,8 +52,10 @@ func rootCommand() *cobra.Command {
return nil
},
}

cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
cmd.AddCommand(powervs.Commands())
cmd.AddCommand(vpc.Commands())

return cmd
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/capibmadm/cmd/vpc/vpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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 vpc contains the commands to operate on vpc resources.
package vpc

import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
)

// Commands initialises and returns VPC command.
func Commands() *cobra.Command {
cmd := &cobra.Command{
Use: "vpc",
Short: "Commands for operations on VPC resources",
}

cmd.PersistentFlags().StringVar(&options.GlobalOptions.VPCRegion, "region", options.GlobalOptions.VPCRegion, "IBM cloud vpc region. (Required)")
cmd.PersistentFlags().StringVar(&options.GlobalOptions.ResourceGroupName, "resource-group-name", options.GlobalOptions.ResourceGroupName, "IBM cloud resource group name")

_ = cmd.MarkPersistentFlagRequired("region")

return cmd
}
11 changes: 10 additions & 1 deletion cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
// Package options contains the reusable and global variables.
package options

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"

"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/printer"
)

// IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID.
const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec
Expand All @@ -28,11 +32,16 @@ var GlobalOptions = &options{}
type options struct {
IBMCloudAPIKey string
ServiceInstanceID string
PowerVSZone string
Debug bool
}

// AddPowerVSCommonFlags will add a common Power VS flag to the cli.
func AddPowerVSCommonFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
cmd.PersistentFlags().StringVar(&GlobalOptions.PowerVSZone, "zone", GlobalOptions.PowerVSZone, "IBM cloud PowerVS location. (Required)")
cmd.PersistentFlags().BoolVar(&GlobalOptions.Debug, "debug", false, "To display the debugging output of API Call ")

_ = cmd.MarkPersistentFlagRequired("service-instance-id")
_ = cmd.MarkPersistentFlagRequired("zone")
}
46 changes: 34 additions & 12 deletions cmd/capibmadm/utils/printer.go → cmd/capibmadm/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package utils
// Package printer implements printing functionality for cli.
package printer

import (
"encoding/json"
Expand All @@ -27,14 +28,35 @@ import (
"k8s.io/cli-runtime/pkg/printers"
)

// PrinterType is a type declaration for a printer type.
type PrinterType string
// PType is a type declaration for a printer type.
type PType string

var (
// PrinterTypeTable is a table printer type.
PrinterTypeTable = PrinterType("table")
// PrinterTypeJSON is a json printer type.
PrinterTypeJSON = PrinterType("json")
// String type casts to string.
func (p *PType) String() string {
return string(*p)
}

// Set sets value for var.
func (p *PType) Set(s string) error {
switch s {
case string(PrinterTypeTable), string(PrinterTypeJSON):
*p = PType(s)
return nil
default:
return ErrUnknowPrinterType
}
}

// Type returns type in string format.
func (p *PType) Type() string {
return "PType"
}

const (
// PrinterTypeTable is a table printer PType.
PrinterTypeTable = PType("table")
// PrinterTypeJSON is a json printer PType.
PrinterTypeJSON = PType("json")
)

var (
Expand All @@ -51,12 +73,12 @@ type Printer interface {
Print(in interface{}) error
}

// NewPrinter creates a new printer.
func NewPrinter(printerType string, writer io.Writer) (Printer, error) {
// New creates a new printer.
func New(printerType PType, writer io.Writer) (Printer, error) {
switch printerType {
case string(PrinterTypeTable):
case PrinterTypeTable:
return &tablePrinter{writer: writer}, nil
case string(PrinterTypeJSON):
case PrinterTypeJSON:
return &jsonPrinter{writer: writer}, nil
default:
return nil, ErrUnknowPrinterType
Expand Down
5 changes: 3 additions & 2 deletions scripts/ci-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mkdir -p "${ARTIFACTS}/logs/"

ARCH=$(uname -m)
OS=$(uname -s)
IBMCLOUD_CLI_VERSION=${IBMCLOUD_CLI_VERSION:-"2.13.0"}
IBMCLOUD_CLI_VERSION=${IBMCLOUD_CLI_VERSION:-"2.14.0"}
PVSADM_VERSION=${PVSADM_VERSION:-"v0.1.9"}
E2E_FLAVOR=${E2E_FLAVOR:-}
REGION=${REGION:-"jp-osa"}
Expand Down Expand Up @@ -74,11 +74,12 @@ install_ibmcloud_cli(){
create_powervs_network_instance(){
install_ibmcloud_cli

ibmcloud config --check-version=false
# Login to IBM Cloud using the API Key
ibmcloud login -a cloud.ibm.com -r ${REGION}

# Install power-iaas command-line plug-in and target the required service instance
ibmcloud plugin install power-iaas
ibmcloud plugin install power-iaas -f
CRN=$(ibmcloud resource service-instance ${IBMPOWERVS_SERVICE_INSTANCE_ID} --output json | jq -r '.[].crn')
ibmcloud pi service-target ${CRN}

Expand Down
Loading

0 comments on commit 990f508

Please sign in to comment.