Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 913b057

Browse files
authored
Merge pull request #30 from flanksource/fix/handle-nil-resource
Fix/handle nil resource
2 parents f483c4a + b6b2a34 commit 913b057

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

controllers/output.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ func (r *KonfigReconciler) getResources(config pkg.Config) ([]pkg.Resource, erro
9393
if err != nil {
9494
return nil, err
9595
}
96-
resources = append(resources, pkg.Resource{Item: obj})
96+
if obj != nil {
97+
resources = append(resources, pkg.Resource{Item: obj})
98+
}
99+
97100
}
98101
return resources, nil
99102
}

pkg/hierarchy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (item Item) String() string {
3939

4040
func (item Item) FindIn(resources []Resource) *Resource {
4141
for _, resource := range resources {
42-
if resource.Item.GetKind() == item.Kind &&
42+
if resource.Item != nil && resource.Item.GetKind() == item.Kind &&
4343
resource.Item.GetName() == item.Name &&
4444
(item.Namespace == "" || item.Namespace == resource.Item.GetNamespace()) {
4545
return &resource
@@ -105,7 +105,7 @@ func (config Config) GenerateJsPropertiesFile(resources []Resource) string {
105105
if len(list) == 0 {
106106
continue
107107
}
108-
properties += fmt.Sprintf("#\n# %s\n#\n", item.String())
108+
properties += fmt.Sprintf("//\n// %s\n//\n", item.String())
109109
for _, property := range list {
110110
if _, err := strconv.Atoi(property.Value); err == nil {
111111
properties += fmt.Sprintf("window['__%v__']=%v;\n", property.Key, property.Value)

test/e2e_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func TestGenerateJs(t *testing.T) {
7979
t.Error(err)
8080
}
8181

82+
// Adding an empty resource tests the ability of the function to exclude them without panicking.
83+
resources = append([]pkg.Resource{pkg.Resource{}}, resources...)
84+
8285
for _, name := range test.applications {
8386
hierarchy, err := pkg.GetHierarchy(test.config, name)
8487
if err != nil {
@@ -124,12 +127,14 @@ func TestGenerate(t *testing.T) {
124127
t.Error(err)
125128
}
126129

130+
// Adding an empty resource tests the ability of the function to exclude them without panicking.
131+
resources = append([]pkg.Resource{pkg.Resource{}}, resources...)
132+
127133
for _, name := range test.applications {
128134
hierarchy, err := pkg.GetHierarchy(test.config, name)
129135
if err != nil {
130136
t.Error(err)
131137
}
132-
133138
file := hierarchy.GeneratePropertiesFile(resources)
134139

135140
p := properties.MustLoadString(file)

0 commit comments

Comments
 (0)