Skip to content

Commit

Permalink
Add initial code for capibmadm tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik-K-N committed Jan 20, 2023
1 parent 31d5a80 commit c3579ea
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 1 deletion.
18 changes: 18 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions 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
66 changes: 66 additions & 0 deletions 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=<api-key>
capibmadm powervs network create capi-network --service-instance-id <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
}
18 changes: 18 additions & 0 deletions 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
51 changes: 51 additions & 0 deletions 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
}
34 changes: 34 additions & 0 deletions 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
}
63 changes: 63 additions & 0 deletions 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)
}
}
26 changes: 26 additions & 0 deletions 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()
}
18 changes: 18 additions & 0 deletions 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
36 changes: 36 additions & 0 deletions 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")
}
3 changes: 3 additions & 0 deletions docs/book/src/SUMMARY.md
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions 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)
6 changes: 6 additions & 0 deletions docs/book/src/topics/capibmadm/powervs/index.md
@@ -0,0 +1,6 @@
# capibmadm powervs `<commands>`


## 1. Power VS commands
- [network](./network.md)
- [create](/topics/capibmadm/powervs/network.html#1-capibmadm-powervs-network-create)

0 comments on commit c3579ea

Please sign in to comment.