Skip to content

Commit

Permalink
added regex context match, added state filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Sep 22, 2018
1 parent d9e650f commit 078caaa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 18 deletions.
21 changes: 20 additions & 1 deletion pkg/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cmd

import (
"fmt"
"regexp"
"strings"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"strings"
"k8s.io/client-go/tools/clientcmd/api"
)

func getKubeClient(flags *genericclioptions.ConfigFlags) (*kubernetes.Clientset, error) {
Expand Down Expand Up @@ -67,3 +70,19 @@ func getPersistentVolumes(kubeClient *kubernetes.Clientset) (map[string]v1.Persi
}
return pvMap, nil
}

func getMatchingContexts(contexts map[string]*api.Context, contextRegexps string) map[string]bool {
matchingContexts := map[string]bool{}
for _, contextRegexp := range strings.Split(contextRegexps, ",") {
regex, err := regexp.Compile(contextRegexp)
if err != nil {
fmt.Printf("Regex %s does not compile: %v\n", contextRegexp, err)
}
for context := range contexts {
if regex.Match([]byte(context)) {
matchingContexts[context] = true
}
}
}
return matchingContexts
}
18 changes: 12 additions & 6 deletions pkg/cmd/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,16 @@ func (o *LBOptions) Validate() error {
return nil
}

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

for context := range getMatchingContexts(o.rawConfig.Contexts, *o.configFlags.Context) {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
Expand Down Expand Up @@ -120,7 +127,7 @@ func (o *LBOptions) runWithConfig() error {
return fmt.Errorf("error getting servers from OpenStack: %v", err)
}

output, err := getPrettyLBList(servicesMap, loadBalancersMap, listenersMap, poolsMap, membersMap, monitorsMap, floatingipsMap, o.output)
output, err := o.getPrettyLBList(servicesMap, loadBalancersMap, listenersMap, poolsMap, membersMap, monitorsMap, floatingipsMap)
if err != nil {
return fmt.Errorf("error creating output: %v", err)
}
Expand All @@ -144,11 +151,10 @@ func (o *LBOptions) runWithConfig() error {
}
}
}

return nil
}

func getPrettyLBList(services map[int32]v1.Service, loadbalancers map[string]loadbalancers.LoadBalancer, listeners map[string]listeners.Listener, pools map[string]pools.Pool, members map[string]pools.Member, monitors map[string]monitors.Monitor, floatingIPs map[string]floatingips.FloatingIP, output string) (string, error) {
func (o *LBOptions) getPrettyLBList(services map[int32]v1.Service, loadbalancers map[string]loadbalancers.LoadBalancer, listeners map[string]listeners.Listener, pools map[string]pools.Pool, members map[string]pools.Member, monitors map[string]monitors.Monitor, floatingIPs map[string]floatingips.FloatingIP) (string, error) {

header := []string{"NAME", "FLOATING_IPS", "VIP_ADDRESS", "PORTS", "SERVICES"}

Expand Down Expand Up @@ -192,7 +198,7 @@ func getPrettyLBList(services map[int32]v1.Service, loadbalancers map[string]loa
lines = append(lines, []string{lb.Name, floatingIPsString, lb.VipAddress, portMapping, svcs})
}
}
return convertToTable(table{header, lines, 0, output})
return convertToTable(table{header, lines, 0, o.output})
}
func getFloatingIPForLB(lb loadbalancers.LoadBalancer, floatingIPs map[string]floatingips.FloatingIP) []string {
var fips []string
Expand Down
31 changes: 26 additions & 5 deletions pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ServerOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config

states string

exporter string
output string
args []string
Expand Down Expand Up @@ -59,6 +61,7 @@ func NewCmdServer(streams genericclioptions.IOStreams) *cobra.Command {
return nil
},
}
cmd.Flags().StringVar(&o.states, "states", "", "filter by states, default list all")
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,7 +91,14 @@ func (o *ServerOptions) Validate() error {

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

for context := range getMatchingContexts(o.rawConfig.Contexts, *o.configFlags.Context) {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
Expand Down Expand Up @@ -118,7 +128,7 @@ func (o *ServerOptions) runWithConfig() error {
return fmt.Errorf("error getting servers from OpenStack: %v", err)
}

output, err := getPrettyServerList(nodesMap, serversMap, o.output)
output, err := o.getPrettyServerList(nodesMap, serversMap)
if err != nil {
return fmt.Errorf("error creating output: %v", err)
}
Expand All @@ -145,7 +155,7 @@ func (o *ServerOptions) runWithConfig() error {
return nil
}

func getPrettyServerList(nodes map[string]v1.Node, server map[string]servers.Server, output string) (string, error) {
func (o *ServerOptions) getPrettyServerList(nodes map[string]v1.Node, server map[string]servers.Server) (string, error) {

header := []string{"NODE_NAME", "STATUS", "KUBELET_VERSION", "KUBEPROXY_VERSION", "RUNTIME_VERSION", "DHC_VERSION", "SERVER_ID", "STATE", "CPU", "RAM", "IP"}

Expand Down Expand Up @@ -183,7 +193,18 @@ func getPrettyServerList(nodes map[string]v1.Node, server map[string]servers.Ser
}
}
}
lines = append(lines, []string{name, status, kubeletVersion, kubeProxyVersion, containerRuntimeVersion, dhcVersion, s.ID, s.Status, cpu, ram, ip})

matchesStates := false
for _, state := range strings.Split(o.states, ",") {
if s.Status == state {
matchesStates = true
break
}
}

if matchesStates || o.states == "" {
lines = append(lines, []string{name, status, kubeletVersion, kubeProxyVersion, containerRuntimeVersion, dhcVersion, s.ID, s.Status, cpu, ram, ip})
}
}
return convertToTable(table{header, lines, 0, output})
return convertToTable(table{header, lines, 0, o.output})
}
32 changes: 26 additions & 6 deletions pkg/cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type VolumesOptions struct {
configFlags *genericclioptions.ConfigFlags
rawConfig api.Config

states string

exporter string
output string
args []string
Expand Down Expand Up @@ -58,6 +60,7 @@ func NewCmdVolumes(streams genericclioptions.IOStreams) *cobra.Command {
return nil
},
}
cmd.Flags().StringVar(&o.states, "states", "", "filter by states, default list all")
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 @@ -87,7 +90,14 @@ func (o *VolumesOptions) Validate() error {

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

for context := range getMatchingContexts(o.rawConfig.Contexts, *o.configFlags.Context) {
o.configFlags.Context = &context
err := o.runWithConfig()
if err != nil {
Expand Down Expand Up @@ -122,7 +132,7 @@ func (o *VolumesOptions) runWithConfig() error {
return fmt.Errorf("error getting servers from OpenStack: %v", err)
}

output, err := getPrettyVolumeList(pvMap, volumesMap, serversMap, o.output)
output, err := o.getPrettyVolumeList(pvMap, volumesMap, serversMap)
if err != nil {
return fmt.Errorf("error creating output: %v", err)
}
Expand All @@ -146,11 +156,10 @@ func (o *VolumesOptions) runWithConfig() error {
}
}
}

return nil
}

func getPrettyVolumeList(pvs map[string]v1.PersistentVolume, volumes map[string]volumes.Volume, server map[string]servers.Server, output string) (string, error) {
func (o *VolumesOptions) getPrettyVolumeList(pvs map[string]v1.PersistentVolume, volumes map[string]volumes.Volume, server map[string]servers.Server) (string, error) {

header := []string{"CLAIM", "PV_NAME", "CINDER_ID", "SERVERS", "STATUS"}

Expand All @@ -169,7 +178,18 @@ func getPrettyVolumeList(pvs map[string]v1.PersistentVolume, volumes map[string]
pvClaim = fmt.Sprintf("%s/%s", pv.Spec.ClaimRef.Namespace, pv.Spec.ClaimRef.Name)

}
lines = append(lines, []string{pvClaim, pvName, v.ID, strings.Join(attachServers, " "), v.Status})

matchesStates := false
for _, state := range strings.Split(o.states, ",") {
if v.Status == state {
matchesStates = true
break
}
}

if matchesStates || o.states == "" {
lines = append(lines, []string{pvClaim, pvName, v.ID, strings.Join(attachServers, " "), v.Status})
}
}
return convertToTable(table{header, lines, 0, output})
return convertToTable(table{header, lines, 0, o.output})
}

0 comments on commit 078caaa

Please sign in to comment.