diff --git a/cmd/capibmadm/cmd/doc.go b/cmd/capibmadm/cmd/doc.go new file mode 100644 index 000000000..b76634bae --- /dev/null +++ b/cmd/capibmadm/cmd/doc.go @@ -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 cmd contains the capibm cli commands. +package cmd diff --git a/cmd/capibmadm/cmd/powervs/doc.go b/cmd/capibmadm/cmd/powervs/doc.go new file mode 100644 index 000000000..0f64892b8 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/doc.go @@ -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 powervs contains the commands to operate on Power VS resources. +package powervs diff --git a/cmd/capibmadm/cmd/powervs/network/create.go b/cmd/capibmadm/cmd/powervs/network/create.go new file mode 100644 index 000000000..78038ecc5 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/create.go @@ -0,0 +1,66 @@ +/* +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 ( + "fmt" + + "github.com/spf13/cobra" + + logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +type networkCreateOptions struct { + name string + dnsServers []string +} + +// NewNetworkCreateCommand function to create PowerVS network. +func NewNetworkCreateCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "create NETWORK_NAME", + Short: "Create PowerVS network", + Example: ` +# Create PowerVS network with name capi-network +export IBMCLOUD_API_KEY= +capibmadm powervs network create capi-network --service-instance-id `, + } + + 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.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 cmd +} + +func createNetwork(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 + return nil +} diff --git a/cmd/capibmadm/cmd/powervs/network/doc.go b/cmd/capibmadm/cmd/powervs/network/doc.go new file mode 100644 index 000000000..deb35f068 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/doc.go @@ -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 network contains the commands to operate on Power VS Network resources. +package network diff --git a/cmd/capibmadm/cmd/powervs/network/network.go b/cmd/capibmadm/cmd/powervs/network/network.go new file mode 100644 index 000000000..f829e9c0d --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/network.go @@ -0,0 +1,51 @@ +/* +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 ( + "fmt" + + "github.com/spf13/cobra" + + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +// NewNetworkCommand function to add PowerVS network commands. +func NewNetworkCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "network", + Short: "Perform PowerVS network operations", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + // we cannot have multiple PersistentPreRunE for a command + // This block is a workaround mentioned in https://github.com/spf13/cobra/issues/252 + root := cmd + for ; root.HasParent(); root = root.Parent() { + } + if err := root.PersistentPreRunE(cmd, args); err != nil { + return err + } + if options.GlobalOptions.ServiceInstanceID == "" { + return fmt.Errorf("service-instance-id is not provided") + } + return nil + }, + } + options.AddCommonFlags(cmd) + cmd.AddCommand(NewNetworkCreateCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/powervs/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go new file mode 100644 index 000000000..969973c21 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -0,0 +1,34 @@ +/* +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 powervs + +import ( + "github.com/spf13/cobra" + + networkcmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network" +) + +// NewPowerVSCommand initialises and returns powervs command. +func NewPowerVSCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "powervs", + Short: "Commands for operations on PowerVS resources", + } + cmd.AddCommand(networkcmd.NewNetworkCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/root.go b/cmd/capibmadm/cmd/root.go new file mode 100644 index 000000000..65723d4ef --- /dev/null +++ b/cmd/capibmadm/cmd/root.go @@ -0,0 +1,63 @@ +/* +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 cmd + +import ( + "flag" + "fmt" + "os" + + "github.com/spf13/cobra" + + logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" + + powervscmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" +) + +func init() { + verbosity := flag.CommandLine.Int("v", 0, "Set the log level verbosity.") + logf.SetLogger(logf.NewLogger(logf.WithThreshold(verbosity))) +} + +func rootCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "capibmadm", + Short: "Kubernetes Cluster API Provider IBM Cloud Management Utility", + Long: `capibmadm provides helpers for completing the prerequisite operations for creating IBM Cloud Power VS or VPC clusters.`, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + apiKey := os.Getenv(options.IBMCloudAPIKeyEnvName) + if apiKey == "" { + return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName) + } + options.GlobalOptions.IBMCloudAPIKey = apiKey + return nil + }, + } + cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) + cmd.AddCommand(powervscmd.NewPowerVSCommand()) + + return cmd +} + +// Execute executes the root command. +func Execute() { + if err := rootCommand().Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/cmd/capibmadm/main.go b/cmd/capibmadm/main.go new file mode 100644 index 000000000..ee06b28ca --- /dev/null +++ b/cmd/capibmadm/main.go @@ -0,0 +1,26 @@ +/* +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. +*/ + +// main is the main package for the capibm cli tool. +package main + +import ( + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd" +) + +func main() { + cmd.Execute() +} diff --git a/cmd/capibmadm/options/doc.go b/cmd/capibmadm/options/doc.go new file mode 100644 index 000000000..410f683a0 --- /dev/null +++ b/cmd/capibmadm/options/doc.go @@ -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 options implements options code. +package options diff --git a/cmd/capibmadm/options/options.go b/cmd/capibmadm/options/options.go new file mode 100644 index 000000000..2b4a5557e --- /dev/null +++ b/cmd/capibmadm/options/options.go @@ -0,0 +1,36 @@ +/* +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 options contains the reusable and global variables. +package options + +import "github.com/spf13/cobra" + +// IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID. +const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec + +// GlobalOptions holds the global variable struct. +var GlobalOptions = &options{} + +type options struct { + IBMCloudAPIKey string + ServiceInstanceID string +} + +// AddCommonFlags will add a common flag to the cli. +func AddCommonFlags(cmd *cobra.Command) { + cmd.PersistentFlags().StringVar(&GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id") +} diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index 97313e356..2830c3b92 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -17,6 +17,9 @@ - [Creating a cluster with External Cloud Provider](./topics/powervs/external-cloud-provider.md) - [Creating a cluster from ClusterClass](./topics/powervs/clusterclass-cluster.md) - [Using autoscaler with scaling from 0 machine](./topics/powervs/autoscaler-scalling-from-0.md) +- [capibmadm CLI](./topics/capibmadm/index.md) + - [Power VS Commands](./topics/capibmadm/powervs/index.md) + - [Network Commands](./topics/capibmadm/powervs/network.md) - [Developer Guide](./developer/index.md) - [Rapid iterative development with Tilt](./developer/tilt.md) - [Guide for API conversions](./developer/conversion.md) diff --git a/docs/book/src/topics/capibmadm/index.md b/docs/book/src/topics/capibmadm/index.md new file mode 100644 index 000000000..e4fd3dde6 --- /dev/null +++ b/docs/book/src/topics/capibmadm/index.md @@ -0,0 +1,5 @@ +# capibmadm CLI + +Kubernetes Cluster API Provider IBM Cloud Management Utility + +## [1. Power VS commands](./powervs/index.md) diff --git a/docs/book/src/topics/capibmadm/powervs/index.md b/docs/book/src/topics/capibmadm/powervs/index.md new file mode 100644 index 000000000..cd352486d --- /dev/null +++ b/docs/book/src/topics/capibmadm/powervs/index.md @@ -0,0 +1,6 @@ +# capibmadm powervs `` + + +## 1. Power VS commands +- [network](./network.md) + - [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create) diff --git a/docs/book/src/topics/capibmadm/powervs/network.md b/docs/book/src/topics/capibmadm/powervs/network.md new file mode 100644 index 000000000..434772edb --- /dev/null +++ b/docs/book/src/topics/capibmadm/powervs/network.md @@ -0,0 +1,21 @@ +## Power VS Network Commands + +### 1. capibmadm powervs network create + +#### Usage: +Create Power VS network. + +#### Environmental Variable: +IBMCLOUD_API_KEY: IBM Cloud api key. + +#### Arguments: +--service-instance-id: Power VS service instance id. +--dns-servers: Comma separated list of DNS Servers to use for this network, Defaults to 8.8.8.8, 9.9.9.9. + + + +#### Example: +```shell +export IBMCLOUD_API_KEY= +capibmadm powervs network create capi-network --service-instance-id +``` \ No newline at end of file diff --git a/go.mod b/go.mod index 8968efcad..120853e41 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/onsi/ginkgo/v2 v2.6.0 github.com/onsi/gomega v1.24.1 github.com/pkg/errors v0.9.1 + github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.1 golang.org/x/text v0.6.0 @@ -114,7 +115,6 @@ require ( github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/afero v1.9.2 // indirect github.com/spf13/cast v1.5.0 // indirect - github.com/spf13/cobra v1.6.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/viper v1.13.0 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect