From 43c05bfefacb39ef3210dd13b1e27e5d8d20aa40 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Sun, 28 Apr 2024 16:57:38 +0300 Subject: [PATCH] some optimization --- .../class/volsync/_replicationDestination.tpl | 5 ++-- .../class/volsync/_replicationSource.tpl | 28 ++++++++----------- .../lib/volsync/_moverSecurityContext.tpl | 27 +++++++++--------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/library/common/templates/class/volsync/_replicationDestination.tpl b/library/common/templates/class/volsync/_replicationDestination.tpl index f3370a1a..95516c00 100644 --- a/library/common/templates/class/volsync/_replicationDestination.tpl +++ b/library/common/templates/class/volsync/_replicationDestination.tpl @@ -16,12 +16,11 @@ objectData: {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} {{- $volsyncData := .volsyncData }} - --- apiVersion: volsync.backube/v1alpha1 -kind: replicationdestination +kind: ReplicationDestination metadata: - name: {{ $objectData.name }}-{{ $volsyncData.name }}-dest + name: {{ printf "%s-%s-dest" $objectData.name $volsyncData.name }} namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "replicationdestination") }} {{- $labels := (mustMerge ($volsyncData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} diff --git a/library/common/templates/class/volsync/_replicationSource.tpl b/library/common/templates/class/volsync/_replicationSource.tpl index 6668459e..e0392b4c 100644 --- a/library/common/templates/class/volsync/_replicationSource.tpl +++ b/library/common/templates/class/volsync/_replicationSource.tpl @@ -22,25 +22,21 @@ objectData: {{- $schedule = $volsyncData.src.trigger.schedule -}} {{- end }} - {{- $hourly := 24 -}} - {{- $daily := 7 -}} - {{- $weekly := 5 -}} - {{- if and $volsyncData.src.retain -}} - {{- with $volsyncData.src.retain.hourly -}} - {{- $hourly = . }} - {{- end -}} - {{- with $volsyncData.src.retain.daily -}} - {{- $daily = . }} - {{- end -}} - {{- with $volsyncData.src.retain.weekly -}} - {{- $weekly = . }} + {{- $retain := dict "hourly" 24 "daily" 7 "weekly" 5 -}} + {{- $items := list "hourly" "daily" "weekly" -}} + + {{- if $volsyncData.src.retain -}} + {{- range $item := $items -}} + {{- with get $volsyncData.src.retain $item -}} + {{- $_ := set $retain $item . -}} + {{- end -}} {{- end -}} {{- end }} --- apiVersion: volsync.backube/v1alpha1 kind: ReplicationSource metadata: - name: {{ $objectData.name }}-{{ $volsyncData.name }} + name: {{ printf "%s-%s" $objectData.name $volsyncData.name }} namespace: {{ include "tc.v1.common.lib.metadata.namespace" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "replicationsource") }} {{- $labels := (mustMerge ($volsyncData.labels | default dict) (include "tc.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)) -}} {{- with (include "tc.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }} @@ -66,7 +62,7 @@ spec: {{- include "tc.v1.common.lib.volsync.moversecuritycontext" (dict "rootCtx" $rootCtx "objectData" $objectData "volsyncData" $volsyncData "target" "src") | trim | nindent 4 }} retain: - hourly: {{ $hourly }} - daily: {{ $daily }} - weekly: {{ $weekly }} + hourly: {{ $retain.hourly }} + daily: {{ $retain.daily }} + weekly: {{ $retain.weekly }} {{- end }} diff --git a/library/common/templates/lib/volsync/_moverSecurityContext.tpl b/library/common/templates/lib/volsync/_moverSecurityContext.tpl index f6bcdfd2..49bdbcd2 100644 --- a/library/common/templates/lib/volsync/_moverSecurityContext.tpl +++ b/library/common/templates/lib/volsync/_moverSecurityContext.tpl @@ -6,24 +6,23 @@ {{- $volsyncData := .volsyncData -}} {{- $target := get $volsyncData .target -}} - {{- $runAsUser := $rootCtx.Values.securityContext.container.runAsUser -}} - {{- $runAsGroup := $rootCtx.Values.securityContext.container.runAsGroup -}} - {{- $fsGroup := $rootCtx.Values.securityContext.pod.fsGroup -}} + {{- $sec := dict + "runAsUser" $rootCtx.Values.securityContext.container.runAsUser + "runAsGroup" $rootCtx.Values.securityContext.container.runAsGroup + "fsGroup" $rootCtx.Values.securityContext.pod.fsGroup + -}} + {{- $items := list "runAsUser" "runAsGroup" "fsGroup" -}} {{- if $target.moverSecurityContext -}} - {{- with $target.moverSecurityContext.runAsUser -}} {{/* TODO: Handle 0 here */}} - {{- $runAsUser = . | default $runAsUser -}} - {{- end -}} - {{- with $target.moverSecurityContext.runAsGroup -}} {{/* TODO: Handle 0 here */}} - {{- $runAsGroup = . | default $runAsGroup -}} - {{- end -}} - {{- with $target.moverSecurityContext.fsGroup -}} {{/* TODO: Handle 0 here */}} - {{- $fsGroup = . | default $fsGroup -}} + {{- range $item := $items -}} + {{- if hasKey $target.moverSecurityContext $item -}} + {{- $_ := set $sec $item (get $target.moverSecurityContext $item) -}} + {{- end -}} {{- end -}} {{- end }} moverSecurityContext: - runAsUser: {{ $runAsUser }} - runAsGroup: {{ $runAsGroup }} - fsGroup: {{ $fsGroup }} + runAsUser: {{ $sec.runAsUser }} + runAsGroup: {{ $sec.runAsGroup }} + fsGroup: {{ $sec.fsGroup }} {{- end -}}