44 "strings"
55
66 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7- "k8s.io/klog/v2"
87)
98
109func ExtractImagesFromResource (resource * unstructured.Unstructured ) []string {
@@ -13,39 +12,24 @@ func ExtractImagesFromResource(resource *unstructured.Unstructured) []string {
1312 }
1413
1514 kind := strings .ToLower (resource .GetKind ())
16- ns , name := resource .GetNamespace (), resource .GetName ()
1715 var out []string
1816
1917 switch kind {
2018 case "deployment" , "statefulset" , "daemonset" , "replicaset" :
21- if found , imgs , err := extractImagesFromPath (resource , "spec" , "template" , "spec" , "containers" ); err != nil {
22- klog .Warningf ("failed to extract containers from %s %s/%s: %v" , resource .GetKind (), ns , name , err )
23- return nil
24- } else if ! found {
25- return nil
26- } else {
19+ if found , imgs , err := extractImagesFromPath (resource , "spec" , "template" , "spec" , "containers" ); err == nil && found {
2720 out = append (out , imgs ... )
2821 }
2922
30- if _ , imgs , err := extractImagesFromPath (resource , "spec" , "template" , "spec" , "initContainers" ); err != nil {
31- klog .Infof ("Error extracting init containers from %s %s/%s: %v" , resource .GetKind (), ns , name , err )
32- } else {
23+ if found , imgs , err := extractImagesFromPath (resource , "spec" , "template" , "spec" , "initContainers" ); err == nil && found {
3324 out = append (out , imgs ... )
3425 }
3526
3627 case "pod" :
37- if found , imgs , err := extractImagesFromPath (resource , "spec" , "containers" ); err != nil {
38- klog .Warningf ("failed to extract containers from pod %s/%s: %v" , ns , name , err )
39- return nil
40- } else if ! found {
41- return nil
42- } else {
28+ if found , imgs , err := extractImagesFromPath (resource , "spec" , "containers" ); err == nil && found {
4329 out = append (out , imgs ... )
4430 }
4531
46- if _ , imgs , err := extractImagesFromPath (resource , "spec" , "initContainers" ); err != nil {
47- klog .Infof ("Error extracting init containers from Pod %s/%s: %v" , ns , name , err )
48- } else {
32+ if found , imgs , err := extractImagesFromPath (resource , "spec" , "initContainers" ); err == nil && found {
4933 out = append (out , imgs ... )
5034 }
5135 }
@@ -116,11 +100,7 @@ func (f *fqdnSet) result() []string {
116100}
117101
118102func extractIngressFQDNs (u * unstructured.Unstructured , add func (string )) {
119- ns , name := u .GetNamespace (), u .GetName ()
120-
121- if rules , found , err := unstructured .NestedSlice (u .Object , "spec" , "rules" ); err != nil {
122- logExtractErr ("Ingress" , ns , name , "rules" , err )
123- } else if found {
103+ if rules , found , err := unstructured .NestedSlice (u .Object , "spec" , "rules" ); err == nil && found {
124104 for _ , r := range rules {
125105 if m , ok := r .(map [string ]interface {}); ok {
126106 if host , ok := m ["host" ].(string ); ok {
@@ -130,9 +110,7 @@ func extractIngressFQDNs(u *unstructured.Unstructured, add func(string)) {
130110 }
131111 }
132112
133- if tls , found , err := unstructured .NestedSlice (u .Object , "spec" , "tls" ); err != nil {
134- logExtractErr ("Ingress" , ns , name , "tls" , err )
135- } else if found {
113+ if tls , found , err := unstructured .NestedSlice (u .Object , "spec" , "tls" ); err == nil && found {
136114 for _ , t := range tls {
137115 m , ok := t .(map [string ]interface {})
138116 if ! ok {
@@ -150,11 +128,7 @@ func extractIngressFQDNs(u *unstructured.Unstructured, add func(string)) {
150128}
151129
152130func extractHTTPRouteFQDNs (u * unstructured.Unstructured , add func (string )) {
153- ns , name , kind := u .GetNamespace (), u .GetName (), u .GetKind ()
154-
155- if hn , found , err := unstructured .NestedSlice (u .Object , "spec" , "hostnames" ); err != nil {
156- logExtractErr (kind , ns , name , "hostnames" , err )
157- } else if found {
131+ if hn , found , err := unstructured .NestedSlice (u .Object , "spec" , "hostnames" ); err == nil && found {
158132 for _ , h := range hn {
159133 if s , ok := h .(string ); ok {
160134 add (s )
@@ -164,11 +138,7 @@ func extractHTTPRouteFQDNs(u *unstructured.Unstructured, add func(string)) {
164138}
165139
166140func extractGatewayFQDNs (u * unstructured.Unstructured , add func (string )) {
167- ns , name := u .GetNamespace (), u .GetName ()
168-
169- if listeners , found , err := unstructured .NestedSlice (u .Object , "spec" , "listeners" ); err != nil {
170- logExtractErr ("Gateway" , ns , name , "listeners" , err )
171- } else if found {
141+ if listeners , found , err := unstructured .NestedSlice (u .Object , "spec" , "listeners" ); err == nil && found {
172142 for _ , l := range listeners {
173143 if m , ok := l .(map [string ]interface {}); ok {
174144 if s , ok := m ["hostname" ].(string ); ok {
@@ -178,7 +148,3 @@ func extractGatewayFQDNs(u *unstructured.Unstructured, add func(string)) {
178148 }
179149 }
180150}
181-
182- func logExtractErr (kind , ns , name , field string , err error ) {
183- klog .Infof ("Error extracting %s from %s %s/%s: %v" , field , kind , ns , name , err )
184- }
0 commit comments