Skip to content

Commit

Permalink
added multi context support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Sep 22, 2018
1 parent 5b030f4 commit d9e650f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
18 changes: 12 additions & 6 deletions pkg/cmd/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
type LBOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config
//TODO decide what todo with list
list bool

exporter string
output string
args []string
Expand Down Expand Up @@ -62,7 +61,6 @@ func NewCmdLB(streams genericclioptions.IOStreams) *cobra.Command {
return nil
},
}
cmd.Flags().BoolVar(&o.list, "list", o.list, "if true, list")
cmd.Flags().StringVarP(&o.exporter, "exporter", "e", "stdout", "stdout, mm or multiple (comma-separated)")
cmd.Flags().StringVarP(&o.output, "output", "o", "markdown", "markdown or raw")
o.configFlags.AddFlags(cmd.Flags())
Expand Down Expand Up @@ -92,14 +90,22 @@ func (o *LBOptions) Validate() error {

// Run lists all volumes
func (o *LBOptions) Run() error {
for _, context := range strings.Split(*o.configFlags.Context, ",") {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
fmt.Printf("Error listing loadbalancers for %s: %v\n", context, err)
}
}
return nil
}

//fmt.Printf("%t\n", o.list)

func (o *LBOptions) runWithConfig() error {
kubeClient, err := getKubeClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
osProvider, tenantID, err := getOpenStackClient(o.rawConfig)
osProvider, tenantID, err := getOpenStackClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/monitors"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/pools"
"gopkg.in/yaml.v2"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func getVolumes(osProvider *gophercloud.ProviderClient) (map[string]volumes.Volume, error) {
Expand Down Expand Up @@ -149,18 +149,18 @@ func getLB(osProvider *gophercloud.ProviderClient) (map[string]loadbalancers.Loa
return loadBalancersMap, listenersMap, poolsMap, membersMap, monitorsMap, floatingipsMap, nil
}

func getOpenStackClient(rawConfig api.Config) (*gophercloud.ProviderClient, string, error) {
providerClient, tenantID, err := createOpenStackProviderClient(rawConfig)
func getOpenStackClient(flags *genericclioptions.ConfigFlags) (*gophercloud.ProviderClient, string, error) {
providerClient, tenantID, err := createOpenStackProviderClient(flags)
if err != nil {
return nil, tenantID, fmt.Errorf("error creating openstack client: %v", err)
}

return providerClient, tenantID, nil
}

func createOpenStackProviderClient(rawConfig api.Config) (*gophercloud.ProviderClient, string, error) {
func createOpenStackProviderClient(flags *genericclioptions.ConfigFlags) (*gophercloud.ProviderClient, string, error) {

context := rawConfig.CurrentContext
context := *flags.Context
tenantID := strings.Split(context, "-")[0]

openstackConfigFile := os.Getenv("OPENSTACK_CONFIG_FILE")
Expand Down
27 changes: 16 additions & 11 deletions pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/sbueringer/kubectl-openstack-plugin/pkg/output/mattermost"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand All @@ -11,16 +12,14 @@ import (

"strings"

"github.com/sbueringer/kubectl-openstack-plugin/pkg/output/mattermost"
"k8s.io/apimachinery/pkg/api/resource"
)

//TODO
type ServerOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config
//TODO decide what todo with list
list bool

exporter string
output string
args []string
Expand Down Expand Up @@ -60,7 +59,6 @@ func NewCmdServer(streams genericclioptions.IOStreams) *cobra.Command {
return nil
},
}
cmd.Flags().BoolVar(&o.list, "list", o.list, "if true, list")
cmd.Flags().StringVarP(&o.exporter, "exporter", "e", "stdout", "stdout, mm or multiple (comma-separated)")
cmd.Flags().StringVarP(&o.output, "output", "o", "markdown", "markdown or raw")
o.configFlags.AddFlags(cmd.Flags())
Expand Down Expand Up @@ -88,16 +86,24 @@ func (o *ServerOptions) Validate() error {
return nil
}

// Run lists all volumes
// Run lists all server
func (o *ServerOptions) Run() error {
for _, context := range strings.Split(*o.configFlags.Context, ",") {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
fmt.Printf("Error listing server for %s: %v\n", context, err)
}
}
return nil
}

//fmt.Printf("%t\n", o.list)

func (o *ServerOptions) runWithConfig() error {
kubeClient, err := getKubeClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
osProvider, tenantID, err := getOpenStackClient(o.rawConfig)
osProvider, tenantID, err := getOpenStackClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
Expand Down Expand Up @@ -128,15 +134,14 @@ func (o *ServerOptions) Run() error {
var msg string
switch o.output {
case "raw":
msg = fmt.Sprintf("Server for %s:\n\n````\n%s````\n", tenantID, output)
msg = fmt.Sprintf("Server for %s:\n\n````\n%s````\n\n", tenantID, output)
case "markdown":
msg = fmt.Sprintf("Server for %s:\n\n%s\n", tenantID, output)
msg = fmt.Sprintf("Server for %s:\n\n%s\n\n", tenantID, output)
}
mattermost.New().SendMessage(msg)
}
}
}

return nil
}

Expand Down
18 changes: 12 additions & 6 deletions pkg/cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
type VolumesOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config
//TODO decide what todo with list
list bool

exporter string
output string
args []string
Expand Down Expand Up @@ -59,7 +58,6 @@ func NewCmdVolumes(streams genericclioptions.IOStreams) *cobra.Command {
return nil
},
}
cmd.Flags().BoolVar(&o.list, "list", o.list, "if true, list")
cmd.Flags().StringVarP(&o.exporter, "exporter", "e", "stdout", "stdout, mm or multiple (comma-separated)")
cmd.Flags().StringVarP(&o.output, "output", "o", "markdown", "markdown or raw")
o.configFlags.AddFlags(cmd.Flags())
Expand Down Expand Up @@ -89,14 +87,22 @@ func (o *VolumesOptions) Validate() error {

// Run lists all volumes
func (o *VolumesOptions) Run() error {
for _, context := range strings.Split(*o.configFlags.Context, ",") {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
fmt.Printf("Error listing volumes for %s: %v\n", context, err)
}
}
return nil
}

//fmt.Printf("%t\n", o.list)

func (o *VolumesOptions) runWithConfig() error {
kubeClient, err := getKubeClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
osProvider, tenantID, err := getOpenStackClient(o.rawConfig)
osProvider, tenantID, err := getOpenStackClient(o.configFlags)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
Expand Down

0 comments on commit d9e650f

Please sign in to comment.