From a9a89fe4180ef41cc157c688997edf62c659d1f4 Mon Sep 17 00:00:00 2001 From: Karthik K N Date: Thu, 12 Jan 2023 18:27:21 +0530 Subject: [PATCH] Add initial code for capibmadm tool --- cmd/capibmadm/cmd/doc.go | 18 ++++++ cmd/capibmadm/cmd/powervs/doc.go | 18 ++++++ cmd/capibmadm/cmd/powervs/network/create.go | 65 +++++++++++++++++++++ cmd/capibmadm/cmd/powervs/network/doc.go | 18 ++++++ cmd/capibmadm/cmd/powervs/powervs.go | 52 +++++++++++++++++ cmd/capibmadm/cmd/root.go | 45 ++++++++++++++ cmd/capibmadm/main.go | 26 +++++++++ cmd/capibmadm/options/doc.go | 18 ++++++ cmd/capibmadm/options/options.go | 29 +++++++++ cmd/capibmadm/util/doc.go | 18 ++++++ cmd/capibmadm/util/util.go | 39 +++++++++++++ go.mod | 2 +- 12 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 cmd/capibmadm/cmd/doc.go create mode 100644 cmd/capibmadm/cmd/powervs/doc.go create mode 100644 cmd/capibmadm/cmd/powervs/network/create.go create mode 100644 cmd/capibmadm/cmd/powervs/network/doc.go create mode 100644 cmd/capibmadm/cmd/powervs/powervs.go create mode 100644 cmd/capibmadm/cmd/root.go create mode 100644 cmd/capibmadm/main.go create mode 100644 cmd/capibmadm/options/doc.go create mode 100644 cmd/capibmadm/options/options.go create mode 100644 cmd/capibmadm/util/doc.go create mode 100644 cmd/capibmadm/util/util.go diff --git a/cmd/capibmadm/cmd/doc.go b/cmd/capibmadm/cmd/doc.go new file mode 100644 index 0000000000..b76634bae7 --- /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 0000000000..0f64892b8d --- /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 0000000000..aeba68e857 --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/network/create.go @@ -0,0 +1,65 @@ +/* +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" +) + +type networkCreateOptions struct { + name string + dnsServers []string +} + +// NewNetworkCreateCommand function to create PowerVS network. +func NewNetworkCreateCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "network-create NETWORK_NAME", + Aliases: []string{"netc"}, + 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 { + fmt.Printf("Creating Power VS network with name: %s, service instance id: %s, dns-servers %s\n", netCreateOption.name, options.ServiceInstanceID, 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 0000000000..deb35f068c --- /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/powervs.go b/cmd/capibmadm/cmd/powervs/powervs.go new file mode 100644 index 0000000000..f2ecb29f2c --- /dev/null +++ b/cmd/capibmadm/cmd/powervs/powervs.go @@ -0,0 +1,52 @@ +/* +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 ( + "fmt" + + "github.com/spf13/cobra" + + networkcmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options" + "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/util" +) + +// NewPowerVSCommand initialises and returns powervs command. +func NewPowerVSCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "powervs", + Short: "Commands for operations on PowerVS resources", + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + apiKey, err := util.GetEnv(options.IBMCloudAPIKeyEnvName) + if err != nil { + return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName) + } + options.IBMCloudAPIKey = apiKey + + if options.ServiceInstanceID == "" { + return fmt.Errorf("service-instance-id is not provided") + } + return nil + }, + } + cmd.PersistentFlags().StringVar(&options.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id") + + cmd.AddCommand(networkcmd.NewNetworkCreateCommand()) + + return cmd +} diff --git a/cmd/capibmadm/cmd/root.go b/cmd/capibmadm/cmd/root.go new file mode 100644 index 0000000000..1c087789b2 --- /dev/null +++ b/cmd/capibmadm/cmd/root.go @@ -0,0 +1,45 @@ +/* +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 ( + "fmt" + "os" + + "github.com/spf13/cobra" + + powervscmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs" +) + +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.`, + } + 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 0000000000..ee06b28cad --- /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 0000000000..410f683a08 --- /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 0000000000..11d7443422 --- /dev/null +++ b/cmd/capibmadm/options/options.go @@ -0,0 +1,29 @@ +/* +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 + +// IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID. +const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec + +var ( + // IBMCloudAPIKey is IBM Cloud API key. + IBMCloudAPIKey string + + // ServiceInstanceID is PowerVS service instance ID. + ServiceInstanceID string +) diff --git a/cmd/capibmadm/util/doc.go b/cmd/capibmadm/util/doc.go new file mode 100644 index 0000000000..1a98600bfe --- /dev/null +++ b/cmd/capibmadm/util/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 util implements util code. +package util diff --git a/cmd/capibmadm/util/util.go b/cmd/capibmadm/util/util.go new file mode 100644 index 0000000000..f48df3b11b --- /dev/null +++ b/cmd/capibmadm/util/util.go @@ -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 util + +import ( + "fmt" + "os" +) + +// ErrEnvironmentVariableNotFound is an error string for environment variable not found error. +type ErrEnvironmentVariableNotFound string + +// Error defines the error interface for ErrEnvironmentVariableNotFound. +func (e ErrEnvironmentVariableNotFound) Error() string { + return fmt.Sprintf("environment variable %q not found", string(e)) +} + +// GetEnv will lookup and return an environment variable. +func GetEnv(key string) (string, error) { + val, ok := os.LookupEnv(key) + if !ok || val == "" { + return "", ErrEnvironmentVariableNotFound(key) + } + return val, nil +} diff --git a/go.mod b/go.mod index 8968efcad6..120853e41c 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