@@ -37,9 +37,10 @@ func (k Kubectl) GetByLabels(namespace, kind string, labels map[string]string) (
37
37
38
38
// GetByState returns the full object, including runtime fields for each
39
39
// resource in the state
40
- func (k Kubectl ) GetByState (data manifest.List ) (manifest.List , error ) {
40
+ func (k Kubectl ) GetByState (data manifest.List , opts GetByStateOpts ) (manifest.List , error ) {
41
41
list , err := k .get ("" , "" , []string {"-f" , "-" }, getOpts {
42
- stdin : data .String (),
42
+ ignoreNotFound : opts .IgnoreNotFound ,
43
+ stdin : data .String (),
43
44
})
44
45
if err != nil {
45
46
return nil , err
@@ -49,15 +50,19 @@ func (k Kubectl) GetByState(data manifest.List) (manifest.List, error) {
49
50
}
50
51
51
52
type getOpts struct {
52
- allNamespaces bool
53
- stdin string
53
+ allNamespaces bool
54
+ ignoreNotFound bool
55
+ stdin string
54
56
}
55
57
56
58
func (k Kubectl ) get (namespace , kind string , selector []string , opts getOpts ) (manifest.Manifest , error ) {
57
59
// build cli flags and args
58
60
argv := []string {
59
61
"-o" , "json" ,
60
62
}
63
+ if opts .ignoreNotFound {
64
+ argv = append (argv , "--ignore-not-found" )
65
+ }
61
66
62
67
if opts .allNamespaces {
63
68
argv = append (argv , "--all-namespaces" )
@@ -85,6 +90,12 @@ func (k Kubectl) get(namespace, kind string, selector []string, opts getOpts) (m
85
90
return nil , parseGetErr (err , serr .String ())
86
91
}
87
92
93
+ // return error if nothing was returned
94
+ // because parsing empty output as json would cause errors
95
+ if sout .Len () == 0 {
96
+ return nil , ErrorNothingReturned {}
97
+ }
98
+
88
99
// parse result
89
100
var m manifest.Manifest
90
101
if err := json .Unmarshal (sout .Bytes (), & m ); err != nil {
0 commit comments