Skip to content

Commit 70fdd85

Browse files
committed
✨ refactor: use ManagedByLabel for consistency in asset management
1 parent 73a85ca commit 70fdd85

7 files changed

Lines changed: 20 additions & 11 deletions

File tree

controllers/container_image/resources.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.mondoo.com/mondoo-operator/pkg/constants"
1414
"go.mondoo.com/mondoo-operator/pkg/feature_flags"
1515
"go.mondoo.com/mondoo-operator/pkg/utils/k8s"
16+
mondoo "go.mondoo.com/mondoo-operator/pkg/utils/mondoo"
1617
"go.mondoo.com/mql/v13/providers-sdk/v1/inventory"
1718
"gopkg.in/yaml.v2"
1819
batchv1 "k8s.io/api/batch/v1"
@@ -233,7 +234,7 @@ func Inventory(integrationMRN, clusterUID string, m v1alpha2.MondooAuditConfig,
233234
Labels: map[string]string{
234235
"k8s.mondoo.com/kind": "node",
235236
},
236-
ManagedBy: "mondoo-operator-" + clusterUID,
237+
ManagedBy: mondoo.ManagedByLabel(clusterUID),
237238
},
238239
},
239240
},

controllers/k8s_scan/deployment_handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,7 @@ func (n *DeploymentHandler) garbageCollectIfNeeded(ctx context.Context, clusterU
598598
return
599599
}
600600

601-
managedBy := "mondoo-operator-" + clusterUid
602-
if err := n.performGarbageCollection(ctx, managedBy); err != nil {
601+
if err := n.performGarbageCollection(ctx, mondoo.ManagedByLabel(clusterUid)); err != nil {
603602
logger.Error(err, "Failed to perform garbage collection of K8s resource scan assets")
604603
}
605604

controllers/k8s_scan/resources.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.mondoo.com/mondoo-operator/pkg/constants"
1515
"go.mondoo.com/mondoo-operator/pkg/feature_flags"
1616
"go.mondoo.com/mondoo-operator/pkg/utils/k8s"
17+
mondoo "go.mondoo.com/mondoo-operator/pkg/utils/mondoo"
1718
"go.mondoo.com/mql/v13/providers-sdk/v1/inventory"
1819
"gopkg.in/yaml.v2"
1920
batchv1 "k8s.io/api/batch/v1"
@@ -954,7 +955,7 @@ func Inventory(integrationMRN, clusterUID string, m v1alpha2.MondooAuditConfig,
954955
Labels: map[string]string{
955956
"k8s.mondoo.com/kind": "cluster",
956957
},
957-
ManagedBy: "mondoo-operator-" + clusterUID,
958+
ManagedBy: mondoo.ManagedByLabel(clusterUID),
958959
},
959960
},
960961
},
@@ -1024,7 +1025,7 @@ func ExternalClusterInventory(integrationMRN, operatorClusterUID string, cluster
10241025
"mondoo.com/cluster-name": cluster.Name,
10251026
"mondoo.com/external-scan": "true",
10261027
},
1027-
ManagedBy: "mondoo-operator-" + operatorClusterUID,
1028+
ManagedBy: mondoo.ManagedByLabel(operatorClusterUID),
10281029
},
10291030
},
10301031
},

controllers/nodes/deployment_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ func (n *DeploymentHandler) garbageCollectIfNeeded(ctx context.Context, clusterU
422422
return
423423
}
424424

425-
managedBy := "mondoo-operator-" + clusterUid
425+
managedBy := mondoo.ManagedByLabel(clusterUid)
426426
if err := n.performGarbageCollection(ctx, managedBy); err != nil {
427427
logger.Error(err, "Failed to perform garbage collection of node scan assets")
428428
}

controllers/nodes/resources.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"go.mondoo.com/mondoo-operator/pkg/constants"
2222
"go.mondoo.com/mondoo-operator/pkg/utils/gomemlimit"
2323
"go.mondoo.com/mondoo-operator/pkg/utils/k8s"
24+
mondoo "go.mondoo.com/mondoo-operator/pkg/utils/mondoo"
2425
"go.mondoo.com/mql/v13/providers-sdk/v1/inventory"
2526
)
2627

@@ -388,7 +389,7 @@ func Inventory(integrationMRN, clusterUID string, m v1alpha2.MondooAuditConfig)
388389
Labels: map[string]string{
389390
"k8s.mondoo.com/kind": "node",
390391
},
391-
ManagedBy: "mondoo-operator-" + clusterUID,
392+
ManagedBy: mondoo.ManagedByLabel(clusterUID),
392393
},
393394
},
394395
},

controllers/resource_watcher/scanner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sigs.k8s.io/yaml"
1818

1919
"go.mondoo.com/mondoo-operator/pkg/annotations"
20+
mondoo "go.mondoo.com/mondoo-operator/pkg/utils/mondoo"
2021
)
2122

2223
var scannerLogger = ctrl.Log.WithName("resource-watcher-scanner")
@@ -162,10 +163,7 @@ func (s *Scanner) generateInventory(resources []K8sResourceIdentifier) ([]byte,
162163
opts["namespaces-exclude"] = strings.Join(s.config.NamespacesExclude, ",")
163164
}
164165

165-
managedBy := "mondoo-operator"
166-
if s.config.ClusterUID != "" {
167-
managedBy = "mondoo-operator-" + s.config.ClusterUID
168-
}
166+
managedBy := mondoo.ManagedByLabel(s.config.ClusterUID)
169167

170168
inv := &inventory.Inventory{
171169
Metadata: &inventory.ObjectMeta{

pkg/utils/mondoo/gc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ import (
2121

2222
const defaultGCOlderThan = 2 * time.Hour
2323

24+
// ManagedByLabel returns the ManagedBy value for assets owned by this operator instance.
25+
// The cluster UID uniquely identifies which operator instance manages the assets.
26+
func ManagedByLabel(clusterUID string) string {
27+
if clusterUID == "" {
28+
return "mondoo-operator"
29+
}
30+
return "mondoo-operator-" + clusterUID
31+
}
32+
2433
// GCOlderThan returns the duration threshold for garbage collection.
2534
// It defaults to 2h but can be overridden via the MONDOO_GC_OLDER_THAN env var
2635
// (accepts any value parseable by time.ParseDuration, e.g. "5m", "30s").

0 commit comments

Comments
 (0)