Skip to content

Commit a7891ef

Browse files
committed
fix: improve GetTargets function to handle empty entries and validate format
Signed-off-by: XploY04 <[email protected]>
1 parent ff07962 commit a7891ef

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pkg/types/types.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/litmuschaos/litmus-go/pkg/cerrors"
12+
"github.com/litmuschaos/litmus-go/pkg/log"
1213
"github.com/litmuschaos/litmus-go/pkg/utils/stringutils"
1314

1415
"github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1"
@@ -170,20 +171,27 @@ func GetTargets(targets string) []AppDetails {
170171
}
171172
t := strings.Split(targets, ";")
172173
for _, k := range t {
173-
val := strings.Split(strings.TrimSpace(k), ":")
174+
trimmed := strings.TrimSpace(k)
175+
if trimmed == "" {
176+
continue
177+
}
178+
val := strings.Split(trimmed, ":")
179+
if len(val) < 3 {
180+
log.Fatalf("invalid TARGETS entry %q: expected format kind:namespace:[labels|names][:mode]", trimmed)
181+
}
174182
data := AppDetails{
175183
Kind: val[0],
176184
Namespace: val[1],
177185
LabelMatchMode: "union",
178186
}
179-
187+
180188
if len(val) > 3 {
181189
mode := strings.TrimSpace(val[3])
182190
if mode == "intersection" || mode == "union" {
183191
data.LabelMatchMode = mode
184192
}
185193
}
186-
194+
187195
if strings.Contains(val[2], "=") {
188196
data.Labels = parse(val[2])
189197
} else {

0 commit comments

Comments
 (0)