@@ -5,14 +5,12 @@ package nodes
55
66import (
77 "context"
8- "fmt"
98 "maps"
109 "slices"
1110 "time"
1211
1312 "go.mondoo.com/mondoo-operator/api/v1alpha2"
1413 "go.mondoo.com/mondoo-operator/pkg/client/mondooclient"
15- "go.mondoo.com/mondoo-operator/pkg/constants"
1614 "go.mondoo.com/mondoo-operator/pkg/utils/k8s"
1715 "go.mondoo.com/mondoo-operator/pkg/utils/mondoo"
1816 appsv1 "k8s.io/api/apps/v1"
@@ -437,59 +435,14 @@ func (n *DeploymentHandler) garbageCollectIfNeeded(ctx context.Context, clusterU
437435
438436// performGarbageCollection calls the Mondoo API to garbage collect stale node scan assets.
439437func (n * DeploymentHandler ) performGarbageCollection (ctx context.Context , managedBy string ) error {
440- if n .MondooClientBuilder == nil {
441- logger .Info ("MondooClientBuilder not configured, skipping node scan garbage collection" )
442- return nil
443- }
444-
445- // Read service account credentials from the creds secret
446- credsSecret := & corev1.Secret {}
447- credsSecretKey := client.ObjectKey {
448- Namespace : n .Mondoo .Namespace ,
449- Name : n .Mondoo .Spec .MondooCredsSecretRef .Name ,
450- }
451- if err := n .KubeClient .Get (ctx , credsSecretKey , credsSecret ); err != nil {
452- return fmt .Errorf ("failed to get credentials secret: %w" , err )
453- }
454-
455- saData , ok := credsSecret .Data [constants .MondooCredsSecretServiceAccountKey ]
456- if ! ok {
457- return fmt .Errorf ("credentials secret missing key %q" , constants .MondooCredsSecretServiceAccountKey )
458- }
459-
460- sa , err := mondoo .LoadServiceAccountFromFile (saData )
461- if err != nil {
462- return fmt .Errorf ("failed to load service account: %w" , err )
463- }
464-
465- token , err := mondoo .GenerateTokenFromServiceAccount (* sa , logger )
466- if err != nil {
467- return fmt .Errorf ("failed to generate token: %w" , err )
468- }
469-
470- opts := mondooclient.MondooClientOptions {
471- ApiEndpoint : sa .ApiEndpoint ,
472- Token : token ,
473- }
474- if n .MondooOperatorConfig != nil {
475- opts .HttpProxy = n .MondooOperatorConfig .Spec .HttpProxy
476- opts .HttpsProxy = n .MondooOperatorConfig .Spec .HttpsProxy
477- opts .NoProxy = n .MondooOperatorConfig .Spec .NoProxy
478- }
479-
480- mondooClient , err := n .MondooClientBuilder (opts )
481- if err != nil {
482- return fmt .Errorf ("failed to create mondoo client: %w" , err )
483- }
484-
485438 gcOpts := & mondooclient.GarbageCollectOptions {
486439 ManagedBy : managedBy ,
487440 Labels : map [string ]string {"k8s.mondoo.com/kind" : "node" },
488441 OlderThan : time .Now ().Add (- 2 * time .Hour ).Format (time .RFC3339 ),
489442 }
490443
491- if err := mondooClient .GarbageCollectAssets (ctx , gcOpts ); err != nil {
492- return fmt . Errorf ( "garbage collection API call failed: %w" , err )
444+ if err := mondoo .GarbageCollectAssets (ctx , n . KubeClient , n . Mondoo , n . MondooOperatorConfig , n . MondooClientBuilder , gcOpts , logger ); err != nil {
445+ return err
493446 }
494447
495448 logger .Info ("Successfully performed garbage collection of node scan assets" )
0 commit comments