@@ -9,42 +9,53 @@ import (
99 "strings"
1010)
1111
12- func GetCurrentConfig (executor commands.Executor ) ( * ConfigResponse , error ) {
13- command := "kubectl config view -o json "
12+ func PrintAllCopsNamespaces (executor commands.Executor ) error {
13+ command := "kubectl get cns "
1414 out , err := executor .Execute (command )
1515
1616 if err != nil {
17- return nil , err
17+ return err
1818 }
1919
20- config := & ConfigResponse {}
21- json .Unmarshal ([]byte (out ), & config )
22- return config , nil
20+ logrus .Info ("\n Namespaces:\n " + out )
21+ return nil
2322}
2423
25- func PrintAllCopsNamespaces (executor commands.Executor ) error {
26- command := "kubectl get copsnamespaces"
27- out , err := executor .Execute (command )
24+ func ExistsCopsNamespace (executor commands.Executor , namespace string ) (bool , error ) {
25+ response , err := GetCopsNamespace (executor , namespace )
2826
2927 if err != nil {
30- return err
28+ return false , err
3129 }
3230
33- logrus .Info ("\n Namespaces:\n " + out )
34- return nil
31+ if response != nil {
32+ return true , nil
33+ } else {
34+ return false , nil
35+ }
3536}
3637
3738func GetCopsNamespace (executor commands.Executor , namespace string ) (* CopsNamespaceResponse , error ) {
38- command := "kubectl get CopsNamespace " + namespace + " -o json"
39+ // ignore-not-found flag needed to avoid command to fail with hard panic mode (because of globally set PanicOnAnyError=true)
40+ command := "kubectl get cns " + namespace + " -o json --ignore-not-found"
3941 out , err := executor .Execute (command )
4042
4143 if err != nil {
4244 return nil , err
4345 }
4446
45- response := & CopsNamespaceResponse {}
46- json .Unmarshal ([]byte (out ), & response )
47- return response , nil
47+ out = strings .TrimSuffix (out , "\n " )
48+
49+ if out == "" {
50+ return nil , nil
51+ } else {
52+ response := & CopsNamespaceResponse {}
53+ err := json .Unmarshal ([]byte (out ), & response )
54+ if err != nil {
55+ return nil , err
56+ }
57+ return response , nil
58+ }
4859}
4960
5061func Apply (executor commands.Executor , filepath string ) (string , error ) {
0 commit comments