Skip to content

Commit 9b2df86

Browse files
authored
Add initial code for capibmadm tool (#1027)
1 parent e698476 commit 9b2df86

File tree

15 files changed

+370
-1
lines changed

15 files changed

+370
-1
lines changed

cmd/capibmadm/cmd/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package cmd contains the capibm cli commands.
18+
package cmd

cmd/capibmadm/cmd/powervs/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package powervs contains the commands to operate on Power VS resources.
18+
package powervs
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package network
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
25+
26+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
27+
)
28+
29+
type networkCreateOptions struct {
30+
name string
31+
dnsServers []string
32+
}
33+
34+
// NewNetworkCreateCommand function to create PowerVS network.
35+
func NewNetworkCreateCommand() *cobra.Command {
36+
cmd := &cobra.Command{
37+
Use: "create NETWORK_NAME",
38+
Short: "Create PowerVS network",
39+
Example: `
40+
# Create PowerVS network with name capi-network
41+
export IBMCLOUD_API_KEY=<api-key>
42+
capibmadm powervs network create capi-network --service-instance-id <service-instance-id>`,
43+
}
44+
45+
var netCreateOption networkCreateOptions
46+
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")
47+
48+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
49+
if len(args) < 1 {
50+
return fmt.Errorf("network name is not provided")
51+
}
52+
netCreateOption.name = args[0]
53+
if err := createNetwork(netCreateOption); err != nil {
54+
return err
55+
}
56+
return nil
57+
}
58+
return cmd
59+
}
60+
61+
func createNetwork(netCreateOption networkCreateOptions) error {
62+
log := logf.Log
63+
log.Info("Creating Power VS network", "name", netCreateOption.name, "service-instance-id", options.GlobalOptions.ServiceInstanceID, "dns-servers", netCreateOption.dnsServers)
64+
//TODO: add network creation logic here
65+
return nil
66+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package network contains the commands to operate on Power VS Network resources.
18+
package network
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package network
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
23+
)
24+
25+
// NewNetworkCommand function to add PowerVS network commands.
26+
func NewNetworkCommand() *cobra.Command {
27+
cmd := &cobra.Command{
28+
Use: "network",
29+
Short: "Perform PowerVS network operations",
30+
}
31+
options.AddPowerVSCommonFlags(cmd)
32+
33+
cmd.AddCommand(NewNetworkCreateCommand())
34+
35+
return cmd
36+
}

cmd/capibmadm/cmd/powervs/powervs.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package powervs
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
networkcmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs/network"
23+
)
24+
25+
// NewPowerVSCommand initialises and returns powervs command.
26+
func NewPowerVSCommand() *cobra.Command {
27+
cmd := &cobra.Command{
28+
Use: "powervs",
29+
Short: "Commands for operations on PowerVS resources",
30+
}
31+
cmd.AddCommand(networkcmd.NewNetworkCommand())
32+
33+
return cmd
34+
}

cmd/capibmadm/cmd/root.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"os"
23+
24+
"github.com/spf13/cobra"
25+
26+
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
27+
28+
powervscmd "sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd/powervs"
29+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/options"
30+
)
31+
32+
func init() {
33+
verbosity := flag.CommandLine.Int("v", 0, "Set the log level verbosity.")
34+
logf.SetLogger(logf.NewLogger(logf.WithThreshold(verbosity)))
35+
}
36+
37+
func rootCommand() *cobra.Command {
38+
cmd := &cobra.Command{
39+
Use: "capibmadm",
40+
Short: "Kubernetes Cluster API Provider IBM Cloud Management Utility",
41+
Long: `capibmadm provides helpers for completing the prerequisite operations for creating IBM Cloud Power VS or VPC clusters.`,
42+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
43+
apiKey := os.Getenv(options.IBMCloudAPIKeyEnvName)
44+
if apiKey == "" {
45+
return fmt.Errorf("ibmcloud api key is not provided, set %s environmental variable", options.IBMCloudAPIKeyEnvName)
46+
}
47+
options.GlobalOptions.IBMCloudAPIKey = apiKey
48+
return nil
49+
},
50+
}
51+
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
52+
cmd.AddCommand(powervscmd.NewPowerVSCommand())
53+
54+
return cmd
55+
}
56+
57+
// Execute executes the root command.
58+
func Execute() {
59+
if err := rootCommand().Execute(); err != nil {
60+
fmt.Fprintln(os.Stderr, err)
61+
os.Exit(1)
62+
}
63+
}

cmd/capibmadm/main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// main is the main package for the capibm cli tool.
18+
package main
19+
20+
import (
21+
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/cmd"
22+
)
23+
24+
func main() {
25+
cmd.Execute()
26+
}

cmd/capibmadm/options/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package options implements options code.
18+
package options

cmd/capibmadm/options/options.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package options contains the reusable and global variables.
18+
package options
19+
20+
import "github.com/spf13/cobra"
21+
22+
// IBMCloudAPIKeyEnvName holds the environmental variable name to set PowerVS service instance ID.
23+
const IBMCloudAPIKeyEnvName = "IBMCLOUD_API_KEY" //nolint:gosec
24+
25+
// GlobalOptions holds the global variable struct.
26+
var GlobalOptions = &options{}
27+
28+
type options struct {
29+
IBMCloudAPIKey string
30+
ServiceInstanceID string
31+
}
32+
33+
// AddPowerVSCommonFlags will add a common Power VS flag to the cli.
34+
func AddPowerVSCommonFlags(cmd *cobra.Command) {
35+
cmd.PersistentFlags().StringVar(&GlobalOptions.ServiceInstanceID, "service-instance-id", "", "PowerVS service instance id")
36+
37+
_ = cmd.MarkPersistentFlagRequired("service-instance-id")
38+
}

0 commit comments

Comments
 (0)