@@ -6,6 +6,7 @@ package resources
6
6
import (
7
7
"bytes"
8
8
"fmt"
9
+ "slices"
9
10
"strings"
10
11
11
12
"github.com/gobwas/glob"
@@ -110,6 +111,19 @@ func Discover(runtime *plugin.Runtime, features cnquery.Features) (*inventory.In
110
111
return nil , err
111
112
}
112
113
114
+ if asset := conn .Asset (); asset .Platform .Name == "k8s-namespace" {
115
+ nsFilter = NamespaceFilterOpts {include : []string {asset .Name }}
116
+
117
+ od := NewPlatformIdOwnershipIndex (asset .PlatformIds [0 ])
118
+ assets , err := discoverNamespaceAssets (runtime , conn , invConfig , asset .PlatformIds [0 ], k8s , nsFilter , resFilters , od )
119
+ if err != nil {
120
+ return nil , err
121
+ }
122
+ setRelatedAssets (conn , asset , assets , od , features )
123
+ in .Spec .Assets = append (in .Spec .Assets , assets ... )
124
+ return in , nil
125
+ }
126
+
113
127
// If we can discover the cluster asset, then we use that as root and build all
114
128
// platform IDs for the assets based on it. If we cannot discover the cluster, we
115
129
// discover the individual namespaces according to the ns filter and then build
@@ -132,14 +146,14 @@ func Discover(runtime *plugin.Runtime, features cnquery.Features) (*inventory.In
132
146
133
147
od := NewPlatformIdOwnershipIndex (assetId )
134
148
135
- assets , err := discoverAssets ( runtime , conn , invConfig , assetId , k8s , nsFilter , resFilters , od , false )
149
+ assets , err := discoverNamespaces ( conn , invConfig , "" , nsFilter , nil )
136
150
if err != nil {
137
151
return nil , err
138
152
}
139
153
setRelatedAssets (conn , root , assets , od , features )
140
154
in .Spec .Assets = append (in .Spec .Assets , assets ... )
141
155
} else {
142
- nss , err := discoverNamespaces (conn , invConfig , "" , nil , nsFilter )
156
+ nss , err := discoverNamespaces (conn , invConfig , "" , nsFilter , nil )
143
157
if err != nil {
144
158
return nil , err
145
159
}
@@ -149,25 +163,83 @@ func Discover(runtime *plugin.Runtime, features cnquery.Features) (*inventory.In
149
163
}
150
164
151
165
// Discover the assets for each namespace and use the namespace platform ID as root
152
- for _ , ns := range nss {
153
- nsFilter = NamespaceFilterOpts {include : []string {ns .Name }}
166
+ // for _, ns := range nss {
167
+ // nsFilter = NamespaceFilterOpts{include: []string{ns.Name}}
154
168
155
- od := NewPlatformIdOwnershipIndex (ns .PlatformIds [0 ])
169
+ // od := NewPlatformIdOwnershipIndex(ns.PlatformIds[0])
170
+ // assets, err := discoverNamespaceAssets(runtime, conn, invConfig, ns.PlatformIds[0], k8s, nsFilter, resFilters, od)
171
+ // if err != nil {
172
+ // return nil, err
173
+ // }
174
+ // setRelatedAssets(conn, ns, assets, od, features)
175
+ // in.Spec.Assets = append(in.Spec.Assets, assets...)
176
+ // }
177
+ }
156
178
157
- // We don't want to discover the namespaces again since we have already done this above
158
- assets , err := discoverAssets (runtime , conn , invConfig , ns .PlatformIds [0 ], k8s , nsFilter , resFilters , od , true )
179
+ return in , nil
180
+ }
181
+
182
+ func discoverNamespaces (
183
+ conn shared.Connection ,
184
+ invConfig * inventory.Config ,
185
+ clusterId string ,
186
+ nsFilter NamespaceFilterOpts ,
187
+ od * PlatformIdOwnershipIndex ,
188
+ ) ([]* inventory.Asset , error ) {
189
+ if slices .Contains (invConfig .Discover .Targets , DiscoveryNamespaces ) || slices .Contains (invConfig .Discover .Targets , DiscoveryAuto ) {
190
+ // We don't use MQL here since we need to handle k8s permission errors
191
+ nss , err := conn .Namespaces ()
192
+ if err != nil {
193
+ if k8sErrors .IsForbidden (err ) && len (nsFilter .include ) > 0 {
194
+ for _ , ns := range nsFilter .include {
195
+ n , err := conn .Namespace (ns )
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+ nss = append (nss , * n )
200
+ }
201
+ } else {
202
+ return nil , errors .Wrap (err , "failed to list namespaces" )
203
+ }
204
+ }
205
+
206
+ assetList := make ([]* inventory.Asset , 0 , len (nss ))
207
+ for _ , ns := range nss {
208
+ if skip := nsFilter .skipNamespace (ns .Name ); skip {
209
+ continue
210
+ }
211
+
212
+ labels := map [string ]string {}
213
+ for k , v := range ns .Labels {
214
+ labels [k ] = v
215
+ }
216
+ addMondooAssetLabels (labels , & ns .ObjectMeta , clusterId )
217
+ platform , err := createPlatformData (ns .Kind , conn .Runtime ())
159
218
if err != nil {
160
219
return nil , err
161
220
}
162
- setRelatedAssets (conn , ns , assets , od , features )
163
- in .Spec .Assets = append (in .Spec .Assets , assets ... )
221
+ assetList = append (assetList , & inventory.Asset {
222
+ PlatformIds : []string {
223
+ shared .NewNamespacePlatformId (clusterId , ns .Name , string (ns .UID )),
224
+ },
225
+ Name : ns .Name ,
226
+ Platform : platform ,
227
+ Labels : labels ,
228
+ // We don't want a parent connection so there is no central cache for the resources
229
+ // for the complete cluster. We only cache resources for a single namespace
230
+ Connections : []* inventory.Config {invConfig .Clone ()},
231
+ Category : conn .Asset ().Category ,
232
+ })
233
+ if od != nil {
234
+ od .Add (& ns )
235
+ }
164
236
}
237
+ return assetList , nil
165
238
}
166
-
167
- return in , nil
239
+ return nil , nil
168
240
}
169
241
170
- func discoverAssets (
242
+ func discoverNamespaceAssets (
171
243
runtime * plugin.Runtime ,
172
244
conn shared.Connection ,
173
245
invConfig * inventory.Config ,
@@ -176,7 +248,6 @@ func discoverAssets(
176
248
nsFilter NamespaceFilterOpts ,
177
249
resFilters * ResourceFilters ,
178
250
od * PlatformIdOwnershipIndex ,
179
- skipNsDiscovery bool ,
180
251
) ([]* inventory.Asset , error ) {
181
252
var assets []* inventory.Asset
182
253
var err error
@@ -252,13 +323,6 @@ func discoverAssets(
252
323
}
253
324
assets = append (assets , list ... )
254
325
}
255
- if target == DiscoveryNamespaces && ! skipNsDiscovery {
256
- list , err = discoverNamespaces (conn , invConfig , clusterId , od , nsFilter )
257
- if err != nil {
258
- return nil , err
259
- }
260
- assets = append (assets , list ... )
261
- }
262
326
if target == DiscoveryContainerImages || target == DiscoveryAll {
263
327
list , err = discoverContainerImages (conn , runtime , invConfig , clusterId , k8s , nsFilter )
264
328
if err != nil {
@@ -807,61 +871,6 @@ func discoverIngresses(
807
871
return assetList , nil
808
872
}
809
873
810
- func discoverNamespaces (
811
- conn shared.Connection ,
812
- invConfig * inventory.Config ,
813
- clusterId string ,
814
- od * PlatformIdOwnershipIndex ,
815
- nsFilter NamespaceFilterOpts ,
816
- ) ([]* inventory.Asset , error ) {
817
- // We don't use MQL here since we need to handle k8s permission errors
818
- nss , err := conn .Namespaces ()
819
- if err != nil {
820
- if k8sErrors .IsForbidden (err ) && len (nsFilter .include ) > 0 {
821
- for _ , ns := range nsFilter .include {
822
- n , err := conn .Namespace (ns )
823
- if err != nil {
824
- return nil , err
825
- }
826
- nss = append (nss , * n )
827
- }
828
- } else {
829
- return nil , errors .Wrap (err , "failed to list namespaces" )
830
- }
831
- }
832
-
833
- assetList := make ([]* inventory.Asset , 0 , len (nss ))
834
- for _ , ns := range nss {
835
- if skip := nsFilter .skipNamespace (ns .Name ); skip {
836
- continue
837
- }
838
-
839
- labels := map [string ]string {}
840
- for k , v := range ns .Labels {
841
- labels [k ] = v
842
- }
843
- addMondooAssetLabels (labels , & ns .ObjectMeta , clusterId )
844
- platform , err := createPlatformData (ns .Kind , conn .Runtime ())
845
- if err != nil {
846
- return nil , err
847
- }
848
- assetList = append (assetList , & inventory.Asset {
849
- PlatformIds : []string {
850
- shared .NewNamespacePlatformId (clusterId , ns .Name , string (ns .UID )),
851
- },
852
- Name : ns .Name ,
853
- Platform : platform ,
854
- Labels : labels ,
855
- Connections : []* inventory.Config {invConfig .Clone (inventory .WithoutDiscovery (), inventory .WithParentConnectionId (invConfig .Id ))}, // pass-in the parent connection config
856
- Category : conn .Asset ().Category ,
857
- })
858
- if od != nil {
859
- od .Add (& ns )
860
- }
861
- }
862
- return assetList , nil
863
- }
864
-
865
874
func discoverContainerImages (conn shared.Connection , runtime * plugin.Runtime , invConfig * inventory.Config , clusterId string , k8s * mqlK8s , nsFilter NamespaceFilterOpts ) ([]* inventory.Asset , error ) {
866
875
pods := k8s .GetPods ()
867
876
if pods .Error != nil {
0 commit comments