Skip to content

Commit

Permalink
Merge pull request #3903 from weaveworks/fix-kust-inventory-npe
Browse files Browse the repository at this point in the history
fix npe when inventory object is nil
  • Loading branch information
Chanwit Kaewkasi authored Aug 22, 2023
2 parents 9808968 + fbd0974 commit 2687831
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/server/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ func (cs *coreServer) getKustomizationInventory(ctx context.Context, clusterName
return nil, fmt.Errorf("failed to get kustomization: %w", err)
}

if kust.Status.Inventory == nil {
return nil, nil
}

if kust.Status.Inventory.Entries == nil {
return []*pb.InventoryEntry{}, nil
return nil, nil
}

result := []*pb.InventoryEntry{}
Expand Down
68 changes: 68 additions & 0 deletions core/server/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,74 @@ func TestGetInventoryKustomization(t *testing.T) {
g.Expect(res.Entries[0].Tenant).To(Equal("tenant"))
}

func TestGetBlankInventoryKustomization(t *testing.T) {
g := NewGomegaWithT(t)

ctx := context.Background()

automationName := "my-automation"
ns := "test-namespace"

deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "my-deployment",
Namespace: ns,
UID: "this-is-not-an-uid",
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
types.AppLabel: automationName,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{types.AppLabel: automationName},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "nginx",
Image: "nginx",
}},
},
},
},
}

kust := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: automationName,
Namespace: ns,
},
Spec: kustomizev1.KustomizationSpec{
SourceRef: kustomizev1.CrossNamespaceSourceReference{
Kind: sourcev1.GitRepositoryKind,
},
},
Status: kustomizev1.KustomizationStatus{
Inventory: nil, // blank inventory
},
}

scheme, err := kube.CreateScheme()
g.Expect(err).To(BeNil())

client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(kust, deployment).Build()
cfg := makeServerConfig(client, t, "")
c := makeServer(cfg, t)

res, err := c.GetInventory(ctx, &pb.GetInventoryRequest{
Namespace: ns,
ClusterName: cluster.DefaultCluster,
Kind: "Kustomization",
Name: kust.Name,
WithChildren: true,
})

g.Expect(err).NotTo(HaveOccurred())
g.Expect(res.Entries).To(HaveLen(0))
}

func TestGetInventoryHelmRelease(t *testing.T) {
g := NewGomegaWithT(t)

Expand Down

0 comments on commit 2687831

Please sign in to comment.