Skip to content

Commit b97450e

Browse files
committed
✨ Refactor: Remove legacy CronJob name functions and update label selectors for consistency
1 parent 768639a commit b97450e

4 files changed

Lines changed: 5 additions & 29 deletions

File tree

controllers/container_image/resources.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,6 @@ func CronJobName(prefix string) string {
233233
return k8s.CronJobName("container-scan", prefix)
234234
}
235235

236-
// LegacyCronJobName returns the pre-v14 name format: {prefix}-containers-scan
237-
func LegacyCronJobName(prefix string) string {
238-
return fmt.Sprintf("%s%s", prefix, CronJobNameSuffix)
239-
}
240-
241236
func ConfigMap(integrationMRN, clusterUID string, m v1alpha2.MondooAuditConfig, cfg v1alpha2.MondooOperatorConfig) (*corev1.ConfigMap, error) {
242237
inv, err := Inventory(integrationMRN, clusterUID, m, cfg)
243238
if err != nil {

controllers/k8s_scan/deployment_handler.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,7 @@ func (n *DeploymentHandler) cleanupOrphanedExternalClusterResources(ctx context.
404404
cronJobs := &batchv1.CronJobList{}
405405
listOpts := &client.ListOptions{
406406
Namespace: n.Mondoo.Namespace,
407-
LabelSelector: labels.SelectorFromSet(map[string]string{
408-
"app": "mondoo-k8s-scan",
409-
"mondoo_cr": n.Mondoo.Name,
410-
}),
407+
LabelSelector: labels.SelectorFromSet(CronJobLabels(*n.Mondoo)),
411408
}
412409
if err := n.KubeClient.List(ctx, cronJobs, listOpts); err != nil {
413410
return err
@@ -573,10 +570,7 @@ func (n *DeploymentHandler) garbageCollectIfNeeded(ctx context.Context, clusterU
573570
cronJobs := &batchv1.CronJobList{}
574571
listOpts := &client.ListOptions{
575572
Namespace: n.Mondoo.Namespace,
576-
LabelSelector: labels.SelectorFromSet(map[string]string{
577-
"app": "mondoo-k8s-scan",
578-
"mondoo_cr": n.Mondoo.Name,
579-
}),
573+
LabelSelector: labels.SelectorFromSet(CronJobLabels(*n.Mondoo)),
580574
}
581575
if err := n.KubeClient.List(ctx, cronJobs, listOpts); err != nil {
582576
logger.Error(err, "Failed to list CronJobs for garbage collection")
@@ -731,10 +725,7 @@ func (n *DeploymentHandler) cleanupStaleCronJobs(ctx context.Context) error {
731725
cronJobs := &batchv1.CronJobList{}
732726
listOpts := &client.ListOptions{
733727
Namespace: n.Mondoo.Namespace,
734-
LabelSelector: labels.SelectorFromSet(map[string]string{
735-
"app": "mondoo-k8s-scan",
736-
"mondoo_cr": n.Mondoo.Name,
737-
}),
728+
LabelSelector: labels.SelectorFromSet(CronJobLabels(*n.Mondoo)),
738729
}
739730
if err := n.KubeClient.List(ctx, cronJobs, listOpts); err != nil {
740731
return err

controllers/k8s_scan/resources.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -594,16 +594,6 @@ func ExternalClusterCronJobName(prefix, clusterName string) string {
594594
return k8s.CronJobNameWithCluster("k8s-scan", prefix, clusterName)
595595
}
596596

597-
// LegacyCronJobName returns the pre-v14 name format: {prefix}-k8s-scan
598-
func LegacyCronJobName(prefix string) string {
599-
return fmt.Sprintf("%s%s", prefix, CronJobNameSuffix)
600-
}
601-
602-
// LegacyExternalClusterCronJobName returns the pre-v14 name format: {prefix}-k8s-scan-{clusterName}
603-
func LegacyExternalClusterCronJobName(prefix, clusterName string) string {
604-
return fmt.Sprintf("%s%s-%s", prefix, CronJobNameSuffix, clusterName)
605-
}
606-
607597
func ConfigMapName(prefix string) string {
608598
return fmt.Sprintf("%s%s", prefix, InventoryConfigMapBase)
609599
}

pkg/utils/k8s/naming.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func integrationID(auditConfigName string) string {
109109
if auditConfigName == "mondoo-client" {
110110
return ""
111111
}
112-
if strings.HasPrefix(auditConfigName, "mondoo-client-") {
113-
return strings.TrimPrefix(auditConfigName, "mondoo-client-")
112+
if id, ok := strings.CutPrefix(auditConfigName, "mondoo-client-"); ok && id != "" {
113+
return id
114114
}
115115
return auditConfigName
116116
}

0 commit comments

Comments
 (0)