-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7399387
commit 96b900a
Showing
8 changed files
with
280 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
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" | ||
client "github.com/IBM-Cloud/power-go-client/clients/instance" | ||
"github.com/IBM-Cloud/power-go-client/power/models" | ||
"github.com/spf13/cobra" | ||
"k8s.io/klog/v2" | ||
"os" | ||
"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/printer" | ||
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils" | ||
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" | ||
"strings" | ||
) | ||
|
||
type portCreateOptions struct { | ||
networkID string | ||
ipAddress string | ||
description string | ||
} | ||
|
||
func CreateCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create PORT_NUMBER", | ||
Short: "Create PowerVS Port", | ||
Example: ` | ||
# Create PowerVS port, provided networkID and IPAddress | ||
export IBMCLOUD_API_KEY=<api-key> | ||
capibmadm powervs port create --network <network_id> --ip-address <10.10.0.1> --description <description> --service-instance-id <service-instance-id> --zone <zone>`, | ||
} | ||
|
||
var portCreateOption portCreateOptions | ||
cmd.Flags().StringVar(&portCreateOption.networkID, "network", "", "Network ID/ name on which port is to be created (Required)") | ||
cmd.Flags().StringVar(&portCreateOption.ipAddress, "ip-address", "", "IP Address to be assigned to the port") | ||
cmd.Flags().StringVar(&portCreateOption.description, "description", "", "Description of the port") | ||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
if err := createPort(cmd.Context(), portCreateOption); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
options.AddCommonFlags(cmd) | ||
_ = cmd.MarkFlagRequired("network") | ||
return cmd | ||
} | ||
|
||
func createPort(ctx context.Context, portCreateOption portCreateOptions) error { | ||
log := logf.Log | ||
log.Info("Creating Port ", "Network ID/Name", portCreateOption.networkID, "IP Address", portCreateOption.ipAddress, "Description", portCreateOption.description, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "zone", options.GlobalOptions.PowerVSZone) | ||
accountID, err := utils.GetAccountID(ctx, iam.GetIAMAuth()) | ||
if err != nil { | ||
return err | ||
} | ||
session, err := powervs.NewPISession(accountID, options.GlobalOptions.PowerVSZone, options.GlobalOptions.Debug) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
networkClient := client.NewIBMPINetworkClient(ctx, session, options.GlobalOptions.ServiceInstanceID) | ||
networks, err := networkClient.GetAll() | ||
if err != nil { | ||
return err | ||
} | ||
var networkNames, networkIDs []string | ||
for _, net := range networks.Networks { | ||
networkIDs = append(networkIDs, *net.NetworkID) | ||
networkNames = append(networkNames, *net.Name) | ||
} | ||
|
||
var netID string | ||
|
||
if utils.Contains(networkIDs, portCreateOption.networkID) { | ||
netID = portCreateOption.networkID | ||
} else if utils.Contains(networkNames, portCreateOption.networkID) { | ||
for _, n := range networks.Networks { | ||
if *n.Name == portCreateOption.networkID { | ||
netID = *n.NetworkID | ||
} | ||
} | ||
} else { | ||
return fmt.Errorf("not able to find network: \"%s\" by ID or name in the list: ids:[%s], names: [%s]", portCreateOption.networkID, strings.Join(networkIDs, ","), strings.Join(networkNames, ",")) | ||
} | ||
params := &models.NetworkPortCreate{ | ||
IPAddress: portCreateOption.ipAddress, | ||
Description: portCreateOption.description, | ||
} | ||
|
||
port, err := networkClient.CreatePort(netID, params) | ||
if err != nil { | ||
return fmt.Errorf("failed to create a port, err: %v", err) | ||
} | ||
klog.Infof("Successfully created a port, id: %s", *port.PortID) | ||
|
||
listByVersion := PortList{ | ||
Items: []PortSpec{}, | ||
} | ||
|
||
listByVersion.Items = append(listByVersion.Items, PortSpec{ | ||
Description: *port.Description, | ||
ExternalIP: port.ExternalIP, | ||
IPAddress: *port.IPAddress, | ||
MacAddress: *port.MacAddress, | ||
PortID: *port.PortID, | ||
Status: *port.Status, | ||
}) | ||
|
||
printerObj, err := printer.New(options.GlobalOptions.Output, os.Stdout) | ||
if err != nil { | ||
return fmt.Errorf("failed creating output printer: %w", err) | ||
} | ||
|
||
if options.GlobalOptions.Output == printer.PrinterTypeTable { | ||
table := listByVersion.ToTable() | ||
printerObj.Print(table) | ||
} else { | ||
printerObj.Print(listByVersion) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
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" | ||
) | ||
|
||
// Commands function to add PowerVS port commands. | ||
func Commands() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "port", | ||
Short: "Perform PowerVS port operations", | ||
} | ||
cmd.AddCommand(CreateCommand()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
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 ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// PortSpec defines a port | ||
type PortSpec struct { | ||
PortID string `json:"id"` | ||
Description string `json:"description"` | ||
ExternalIP string `json:"externalIP,omitempty"` | ||
IPAddress string `json:"ipAddress"` | ||
MacAddress string `json:"macAddress"` | ||
Status string `json:"status"` | ||
} | ||
|
||
// PortList defines a list of Ports. | ||
type PortList struct { | ||
Items []PortSpec `json:"items"` | ||
} | ||
|
||
// ToTable converts List to *metav1.Table. | ||
func (portList *PortList) ToTable() *metav1.Table { | ||
table := &metav1.Table{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: metav1.SchemeGroupVersion.String(), | ||
Kind: "Table", | ||
}, | ||
ColumnDefinitions: []metav1.TableColumnDefinition{ | ||
{ | ||
Name: "DESCRIPTION", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "EXTERNALIP", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "IPADDRESS", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "MACADDRESS", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "PORTID", | ||
Type: "string", | ||
}, | ||
{ | ||
Name: "STATUS", | ||
Type: "string", | ||
}, | ||
}, | ||
} | ||
|
||
for _, port := range portList.Items { | ||
row := metav1.TableRow{ | ||
Cells: []interface{}{port.Description, port.ExternalIP, port.IPAddress, port.MacAddress, port.PortID, port.Status}, | ||
} | ||
table.Rows = append(table.Rows, row) | ||
} | ||
return table | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters