Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: handle missing clusters correctly #259

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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