Skip to content

Commit

Permalink
GCE: Fix subnets vs zones formatting of instance groups
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Oct 3, 2017
1 parent 518e97d commit 5045fdb
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/kops/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ go_library(
"//pkg/dns:go_default_library",
"//pkg/edit:go_default_library",
"//pkg/featureflag:go_default_library",
"//pkg/formatter:go_default_library",
"//pkg/instancegroups:go_default_library",
"//pkg/kopscodecs:go_default_library",
"//pkg/kubeconfig:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func RunGet(context Factory, out io.Writer, options *GetOptions) error {
return err
}
fmt.Fprintf(os.Stdout, "\nInstance Groups\n")
err = igOutputTable(instancegroups, out)
err = igOutputTable(cluster, instancegroups, out)
if err != nil {
return err
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/kops/get_instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ package main

import (
"fmt"
"io"
"os"
"strconv"
"strings"

"io"

"github.com/golang/glog"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/model"
"k8s.io/kops/pkg/formatter"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
Expand Down Expand Up @@ -117,7 +119,7 @@ func RunGetInstanceGroups(options *GetInstanceGroupsOptions, args []string) erro
switch options.output {

case OutputTable:
return igOutputTable(instancegroups, out)
return igOutputTable(cluster, instancegroups, out)
case OutputYaml:
return igOutputYAML(instancegroups, out)
case OutputJSON:
Expand Down Expand Up @@ -155,7 +157,7 @@ func buildInstanceGroups(args []string, list *api.InstanceGroupList) ([]*api.Ins
return instancegroups, nil
}

func igOutputTable(instancegroups []*api.InstanceGroup, out io.Writer) error {
func igOutputTable(cluster *api.Cluster, instancegroups []*api.InstanceGroup, out io.Writer) error {
t := &tables.Table{}
t.AddColumn("NAME", func(c *api.InstanceGroup) string {
return c.ObjectMeta.Name
Expand All @@ -166,16 +168,16 @@ func igOutputTable(instancegroups []*api.InstanceGroup, out io.Writer) error {
t.AddColumn("MACHINETYPE", func(c *api.InstanceGroup) string {
return c.Spec.MachineType
})
t.AddColumn("SUBNETS", func(c *api.InstanceGroup) string {
return strings.Join(c.Spec.Subnets, ",")
})
t.AddColumn("SUBNETS", formatter.RenderInstanceGroupSubnets(cluster))
t.AddColumn("ZONES", formatter.RenderInstanceGroupZones(cluster))
t.AddColumn("MIN", func(c *api.InstanceGroup) string {
return int32PointerToString(c.Spec.MinSize)
})
t.AddColumn("MAX", func(c *api.InstanceGroup) string {
return int32PointerToString(c.Spec.MaxSize)
})
return t.Render(instancegroups, os.Stdout, "NAME", "ROLE", "MACHINETYPE", "MIN", "MAX", "SUBNETS")
// SUBNETS is not not selected by default - not as useful as ZONES
return t.Render(instancegroups, os.Stdout, "NAME", "ROLE", "MACHINETYPE", "MIN", "MAX", "ZONES")
}

func igOutputJson(instanceGroups []*api.InstanceGroup, out io.Writer) error {
Expand Down
1 change: 1 addition & 0 deletions hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ k8s.io/kops/pkg/dns
k8s.io/kops/pkg/edit
k8s.io/kops/pkg/featureflag
k8s.io/kops/pkg/flagbuilder
k8s.io/kops/pkg/formatter
k8s.io/kops/pkg/instancegroups
k8s.io/kops/pkg/jsonutils
k8s.io/kops/pkg/k8scodecs
Expand Down
19 changes: 19 additions & 0 deletions pkg/formatter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["instancegroup.go"],
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops:go_default_library",
"//pkg/apis/kops/model:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["instancegroup_test.go"],
library = ":go_default_library",
deps = ["//pkg/apis/kops:go_default_library"],
)
30 changes: 30 additions & 0 deletions pkg/formatter/instancegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package formatter

import (
"github.com/golang/glog"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/model"
"strings"
)

// InstanceGroupRenderFunction is a render function for an InstanceGroup
type InstanceGroupRenderFunction func(ig *kops.InstanceGroup) string

//RenderInstanceGroupSubnets renders the subnet names for an InstanceGroup
func RenderInstanceGroupSubnets(cluster *kops.Cluster) InstanceGroupRenderFunction {
return func(ig *kops.InstanceGroup) string {
return strings.Join(ig.Spec.Subnets, ",")
}
}

//RenderInstanceGroupZones renders the zone names for an InstanceGroup
func RenderInstanceGroupZones(cluster *kops.Cluster) InstanceGroupRenderFunction {
return func(ig *kops.InstanceGroup) string {
zones, err := model.FindZonesForInstanceGroup(cluster, ig)
if err != nil {
glog.Warningf("error fetch zones for instancegroup: %v", err)
return ""
}
return strings.Join(zones, ",")
}
}
56 changes: 56 additions & 0 deletions pkg/formatter/instancegroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package formatter

import (
"k8s.io/kops/pkg/apis/kops"
"testing"
)

func TestRenderInstanceGroupZones(t *testing.T) {
cluster := &kops.Cluster{
Spec: kops.ClusterSpec{
Subnets: []kops.ClusterSubnetSpec{
{Name: "subnet1", Zone: "subnet1zone"},
{Name: "subnet2", Zone: "subnet2zone"},
{Name: "subnet3", Zone: "subnet3zone"},
},
},
}

grid := []struct {
ig *kops.InstanceGroup
expected string
}{
{
ig: &kops.InstanceGroup{
Spec: kops.InstanceGroupSpec{
Zones: []string{"test1", "test2"},
},
},
expected: "test1,test2",
},
{
ig: &kops.InstanceGroup{
Spec: kops.InstanceGroupSpec{
Subnets: []string{"subnet1", "subnet2"},
},
},
expected: "subnet1zone,subnet2zone",
},
{
ig: &kops.InstanceGroup{
Spec: kops.InstanceGroupSpec{
Subnets: []string{"badsubnet"},
},
},
expected: "",
},
}
for _, g := range grid {
f := RenderInstanceGroupZones(cluster)
actual := f(g.ig)
if actual != g.expected {
t.Errorf("unexpected output: %q vs %q", g.expected, actual)
continue
}
}
}

0 comments on commit 5045fdb

Please sign in to comment.