Skip to content

Commit

Permalink
UD-1199: Update helm templates and cronjob creation to enforce correc…
Browse files Browse the repository at this point in the history
…t limits

Signed-off-by: Kevin Conner <kev.conner@getupcloud.com>
  • Loading branch information
knrc committed Feb 15, 2024
1 parent 67d7c74 commit 729d4cb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
19 changes: 18 additions & 1 deletion charts/zora/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Create the name of the service account to use in Operator
{{- end }}

{{- define "zora.clusterName" }}
{{- regexReplaceAll "\\W+" (required "`clusterName` is required." .Values.clusterName) "-" }}
{{- include "truncate.name" (dict "name" (regexReplaceAll "\\W+" (required "`clusterName` is required." .Values.clusterName) "-") "len" 63 ) }}
{{- end }}

{{- define "zora.hourlySchedule" }}
Expand Down Expand Up @@ -113,3 +113,20 @@ Create the name of the service account to use in Operator
{{- define "zora.vulnSchedule" }}
{{- default (include "zora.dailySchedule" .) .Values.scan.vulnerability.schedule }}
{{- end }}

{{/*
Truncate a name to a specific length
@param .name the name of the component
@param .len the maximum length to return
*/}}
{{- define "truncate.name" }}
{{- if gt (len .name) .len }}
{{- $maxLen := int (sub .len 3) }}
{{- $suffixLen := int (div $maxLen 2) }}
{{- $prefixLen := int (sub $maxLen $suffixLen) }}
{{- $suffixStart := int (sub (len .name) $suffixLen) }}
{{- printf "%s---%s" (substr 0 $prefixLen .name) (substr $suffixStart (len .name) .name) }}
{{- else }}
{{- .name }}
{{- end }}
{{- end }}
5 changes: 3 additions & 2 deletions charts/zora/templates/clusterscan/clusterscan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ metadata:
labels:
zora.undistro.io/default: "true"
{{- include "zora.labels" . | nindent 4 }}
name: {{ include "zora.clusterName" . }}-misconfig
name: {{ include "truncate.name" (dict "name" (printf "%s-misconfig" (include "zora.clusterName" .)) "len" 63 ) }}

spec:
clusterRef:
name: {{ include "zora.clusterName" . }}
Expand All @@ -51,7 +52,7 @@ metadata:
labels:
zora.undistro.io/default: "true"
{{- include "zora.labels" . | nindent 4 }}
name: {{ include "zora.clusterName" . }}-vuln
name: {{ include "truncate.name" (dict "name" (printf "%s-vuln" (include "zora.clusterName" .)) "len" 63 ) }}
spec:
clusterRef:
name: {{ include "zora.clusterName" . }}
Expand Down
10 changes: 8 additions & 2 deletions internal/controller/zora/clusterscan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ func (r *ClusterScanReconciler) reconcile(ctx context.Context, clusterscan *v1al
clusterscan.SetReadyStatus(false, "PluginFetchError", err.Error())
return err
}
cronJob := plugins.NewCronJob(fmt.Sprintf("%s-%s", clusterscan.Name, plugin.Name), clusterscan.Namespace)

cronJob := plugins.NewCronJob(getCronJobName(clusterscan.Name, plugin.Name), clusterscan.Namespace)
cronJobMutator := &plugins.CronJobMutator{
Scheme: r.Scheme,
Existing: cronJob,
Expand Down Expand Up @@ -315,7 +316,7 @@ func (r *ClusterScanReconciler) deleteOldPlugins(ctx context.Context, clustersca
oldPlugins := r.getOldPlugins(clusterscan, pluginRefs)
for _, plugin := range oldPlugins {
cj := &batchv1.CronJob{ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", clusterscan.Name, plugin),
Name: getCronJobName(clusterscan.Name, plugin),
Namespace: clusterscan.Namespace,
}}
if err := r.Delete(ctx, cj); err != nil {
Expand Down Expand Up @@ -535,3 +536,8 @@ func (r *ClusterScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&batchv1.CronJob{}).
Complete(r)
}

func getCronJobName(clusterScanName, pluginName string) string {
// cronjob name should not exceed a length of 52 characters
return truncateName(fmt.Sprintf("%s-%s", clusterScanName, pluginName), 52)
}
29 changes: 29 additions & 0 deletions internal/controller/zora/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 Undistro Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package zora

import "fmt"

func truncateName(name string, length int) string {
nameLen := len(name)
if nameLen <= length {
return name
} else {
maxLength := length - 3
suffixLen := maxLength / 2
prefixLen := maxLength - suffixLen
return fmt.Sprintf("%s---%s", name[0:prefixLen], name[nameLen-suffixLen:])
}
}

0 comments on commit 729d4cb

Please sign in to comment.