Skip to content

Commit

Permalink
cluster: handle missing clusters correctly (#259)
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Santos <nick.santos@docker.com>

Signed-off-by: Nick Santos <nick.santos@docker.com>
  • Loading branch information
nicks authored Oct 17, 2022
1 parent e4c0c6a commit b9b3bc9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,16 @@ func (c *Controller) Get(ctx context.Context, name string) (*api.Cluster, error)
if !ok {
return nil, apierrors.NewNotFound(groupResource, name)
}

configCluster, ok := config.Clusters[ct.Cluster]
if !ok {
return nil, apierrors.NewNotFound(groupResource, name)
}

cluster := &api.Cluster{
TypeMeta: typeMeta,
Name: name,
Product: clusterid.ProductFromContext(ct, config.Clusters[ct.Cluster]).String(),
Product: clusterid.ProductFromContext(ct, configCluster).String(),
}
c.populateCluster(ctx, cluster)

Expand All @@ -954,7 +960,13 @@ func (c *Controller) List(ctx context.Context, options ListOptions) (*api.Cluste

config := c.configCopy()
names := make([]string, 0, len(c.config.Contexts))
for name := range config.Contexts {
for name, ct := range config.Contexts {
_, ok := config.Clusters[ct.Cluster]
if !ok {
// Filter out malformed contexts.
continue
}

names = append(names, name)
}
sort.Strings(names)
Expand Down
31 changes: 31 additions & 0 deletions pkg/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func TestClusterList(t *testing.T) {
assert.Equal(t, "microk8s", clusters.Items[1].Name)
}

// Make sure that an empty config doesn't confuse ctlptl.
func TestClusterListEmptyConfig(t *testing.T) {
c := newFakeController(t)
c.config.Contexts["kind"] = &clientcmdapi.Context{}

clusters, err := c.List(context.Background(), ListOptions{})
assert.NoError(t, err)
require.Equal(t, 2, len(clusters.Items))
assert.Equal(t, "docker-desktop", clusters.Items[0].Name)
assert.Equal(t, "microk8s", clusters.Items[1].Name)
}

func TestClusterListSelectorMatch(t *testing.T) {
c := newFakeController(t)
clusters, err := c.List(context.Background(), ListOptions{FieldSelector: "product=microk8s"})
Expand Down Expand Up @@ -114,6 +126,25 @@ func TestClusterApplyKIND(t *testing.T) {
assert.Equal(t, "kind-kind", result.Name)
}

// Make sure an empty context doesn't confuse ctlptl.
func TestClusterApplyKINDEmptyConfig(t *testing.T) {
f := newFixture(t)
f.setOS("darwin")

f.config.Contexts["kind"] = &clientcmdapi.Context{}

assert.Equal(t, false, f.d4m.started)
kindAdmin := f.newFakeAdmin(clusterid.ProductKIND)

result, err := f.controller.Apply(context.Background(), &api.Cluster{
Product: string(clusterid.ProductKIND),
})
require.NoError(t, err)
assert.Equal(t, true, f.d4m.started)
assert.Equal(t, "kind-kind", kindAdmin.created.Name)
assert.Equal(t, "kind-kind", result.Name)
}

func TestClusterApplyFailsToStart(t *testing.T) {
f := newFixture(t)
f.setOS("darwin")
Expand Down

0 comments on commit b9b3bc9

Please sign in to comment.