66 "fmt"
77 "os"
88 "sort"
9+ "strconv"
910 "strings"
1011 "time"
1112
@@ -121,7 +122,7 @@ func runInit(cmd *cobra.Command, args []string) error {
121122 fmt .Printf ("%s Discovering ingresses... " , colorInfo ("→" ))
122123 ingresses , err := discoverIngresses (ctx , namespaces )
123124 if err != nil {
124- fmt .Println (colorWarning ("⚠" ), colorDim ("(skipped)" ))
125+ fmt .Println (colorWarning ("⚠" ), colorDim (fmt . Sprintf ( "(skipped: %s)" , err ) ))
125126 } else {
126127 fmt .Println (colorSuccess ("✓" ), colorDim (fmt .Sprintf ("(%d found)" , len (ingresses ))))
127128 }
@@ -130,7 +131,7 @@ func runInit(cmd *cobra.Command, args []string) error {
130131 fmt .Printf ("%s Checking existing NetworkPolicies... " , colorInfo ("→" ))
131132 existingPolicies , err := discoverNetworkPolicies (ctx , namespaces )
132133 if err != nil {
133- fmt .Println (colorWarning ("⚠" ), colorDim ("(skipped)" ))
134+ fmt .Println (colorWarning ("⚠" ), colorDim (fmt . Sprintf ( "(skipped: %s)" , err ) ))
134135 } else {
135136 fmt .Println (colorSuccess ("✓" ), colorDim (fmt .Sprintf ("(%d found)" , len (existingPolicies ))))
136137 }
@@ -272,6 +273,7 @@ func discoverWorkloads(ctx context.Context, namespaces []string) (*workloadInfo,
272273 "-o" , `jsonpath={range .items[*]}{.metadata.labels.app\.kubernetes\.io/name}{"|"}{.metadata.labels.app}{"|"}{range .spec.containers[*]}{range .ports[*]}{.containerPort}{" "}{end}{end}{"\n"}{end}` ,
273274 }, nil )
274275 if err != nil {
276+ fmt .Fprintf (os .Stderr , " %s namespace %s: %s\n " , colorWarning ("⚠" ), ns , err )
275277 continue
276278 }
277279
@@ -305,8 +307,10 @@ func discoverWorkloads(ctx context.Context, namespaces []string) (*workloadInfo,
305307 // Parse ports
306308 if len (parts ) > 2 {
307309 for _ , p := range strings .Fields (parts [2 ]) {
308- var port int
309- fmt .Sscanf (p , "%d" , & port )
310+ port , err := strconv .Atoi (strings .TrimSpace (p ))
311+ if err != nil {
312+ continue
313+ }
310314 if port > 0 {
311315 app .ports = appendUnique (app .ports , port )
312316 }
@@ -321,6 +325,7 @@ func discoverWorkloads(ctx context.Context, namespaces []string) (*workloadInfo,
321325 "-o" , `jsonpath={range .items[*]}{.spec.selector.app\.kubernetes\.io/name}{"|"}{.spec.selector.app}{"|"}{range .spec.ports[*]}{.port}{" "}{end}{"\n"}{end}` ,
322326 }, nil )
323327 if err != nil {
328+ fmt .Fprintf (os .Stderr , " %s namespace %s: %s\n " , colorWarning ("⚠" ), ns , err )
324329 continue
325330 }
326331
@@ -354,8 +359,10 @@ func discoverWorkloads(ctx context.Context, namespaces []string) (*workloadInfo,
354359 // Parse ports from service
355360 if len (parts ) > 2 {
356361 for _ , p := range strings .Fields (parts [2 ]) {
357- var port int
358- fmt .Sscanf (p , "%d" , & port )
362+ port , err := strconv .Atoi (strings .TrimSpace (p ))
363+ if err != nil {
364+ continue
365+ }
359366 if port > 0 {
360367 app .ports = appendUnique (app .ports , port )
361368 }
@@ -385,6 +392,7 @@ func discoverIngresses(ctx context.Context, namespaces []string) ([]ingressInfo,
385392 "-o" , "jsonpath={range .items[*]}{.metadata.name}|{.spec.rules[*].host}|{.spec.rules[*].http.paths[*].backend.service.name}|{.spec.rules[*].http.paths[*].backend.service.port.number}\\ n{end}" ,
386393 }, nil )
387394 if err != nil {
395+ fmt .Fprintf (os .Stderr , " %s namespace %s: %s\n " , colorWarning ("⚠" ), ns , err )
388396 continue
389397 }
390398
@@ -404,7 +412,9 @@ func discoverIngresses(ctx context.Context, namespaces []string) ([]ingressInfo,
404412 serviceName : parts [2 ],
405413 }
406414 if len (parts ) > 3 {
407- fmt .Sscanf (parts [3 ], "%d" , & ing .servicePort )
415+ if port , err := strconv .Atoi (strings .TrimSpace (parts [3 ])); err == nil {
416+ ing .servicePort = port
417+ }
408418 }
409419 ingresses = append (ingresses , ing )
410420 }
@@ -422,6 +432,7 @@ func discoverNetworkPolicies(ctx context.Context, namespaces []string) ([]string
422432 "-o" , "jsonpath={.items[*].metadata.name}" ,
423433 }, nil )
424434 if err != nil {
435+ fmt .Fprintf (os .Stderr , " %s namespace %s: %s\n " , colorWarning ("⚠" ), ns , err )
425436 continue
426437 }
427438 for _ , name := range strings .Fields (result .Stdout ) {
0 commit comments