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 18, 2023
1 parent 31d5a80 commit a9a89fe
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cmd/capibmadm/cmd/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 cmd contains the capibm cli commands.
package cmd
18 changes: 18 additions & 0 deletions cmd/capibmadm/cmd/powervs/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 powervs contains the commands to operate on Power VS resources.
package powervs
65 changes: 65 additions & 0 deletions cmd/capibmadm/cmd/powervs/network/create.go
Original file line number Diff line number Diff line change
@@ -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=<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 {
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
}
18 changes: 18 additions & 0 deletions cmd/capibmadm/cmd/powervs/network/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 network contains the commands to operate on Power VS Network resources.
package network
52 changes: 52 additions & 0 deletions cmd/capibmadm/cmd/powervs/powervs.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions cmd/capibmadm/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
26 changes: 26 additions & 0 deletions cmd/capibmadm/main.go
Original file line number Diff line number Diff line change
@@ -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
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 options implements options code.
package options
29 changes: 29 additions & 0 deletions cmd/capibmadm/options/options.go
Original file line number Diff line number Diff line change
@@ -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
)
18 changes: 18 additions & 0 deletions cmd/capibmadm/util/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 util implements util code.
package util
39 changes: 39 additions & 0 deletions cmd/capibmadm/util/util.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 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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a9a89fe

Please sign in to comment.