fix(harbor): keep the trivy volume class of the live StatefulSet

A Harbor with trivy enabled and no storageClass could not be changed
after install: the next upgrade of the release failed with

  StatefulSet.apps "<release>-trivy" is invalid: spec: Forbidden: updates
  to statefulset spec for fields other than 'replicas', 'ordinals',
  'template', 'updateStrategy', 'revisionHistoryLimit',
  'persistentVolumeClaimRetentionPolicy' and 'minReadySeconds' are
  forbidden

The chart took the class for the trivy volumeClaimTemplate from the
data-<release>-trivy-0 PVC. The default-StorageClass admission stamps
the class onto that PVC but never onto the StatefulSet, so the render
put a class into a template created without one. Read the class from
the template of the live StatefulSet instead.

Fixes: bdf520bf74 ("fix(harbor): preserve jobservice and trivy storageClass on upgrade")
Assisted-by: LLM
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
This commit is contained in:
Yan Bondarenko committed 2026-12-01 19:28:46 +01:00
1 parent ce04ec3fb8
commit 1ca2a943cf
2 files changed
+122 -10

No files matched your search

+14 -10
View File
@@ -61,10 +61,12 @@
storageClassName on a PVC is immutable, and a StatefulSet's volumeClaimTemplate
cannot be patched at all. The upstream chart renders both the jobservice PVC and
the trivy VCT without a storageClassName unless one is passed, so on first install
the default-StorageClass admission stamps the cluster default onto the live object.
To avoid an immutable-field rejection on upgrade we keep the rendered value equal
to the live one: preserve an existing object's storageClassName, and only fall back
to the configured storageClass for a fresh install. See issue #2368.
the default-StorageClass admission stamps the cluster default onto the jobservice
PVC, while the StatefulSet keeps its template as rendered and only the PVCs it
creates get the default. To avoid an immutable-field rejection on upgrade we keep
the rendered value equal to the live one: preserve the storageClassName of the
jobservice PVC and of the trivy StatefulSet's template, and only fall back to the
configured storageClass for a fresh install. See issue #2368.
A live object pinned to an explicit "" (no dynamic provisioning) must round-trip
too: upstream maps the "-" sentinel back to storageClassName: "", so forward "-"
@@ -83,13 +85,15 @@
{{- end }}
{{- end }}
{{- /* The trivy StatefulSet's VCT is named "data", so its PVCs are
data-<release>-trivy-0..N; the class is identical across ordinals, so the
first ordinal is representative. */}}
{{- $trivyPvc := lookup "v1" "PersistentVolumeClaim" .Release.Namespace (printf "data-%s-trivy-0" .Release.Name) }}
{{- $trivySts := lookup "apps/v1" "StatefulSet" .Release.Namespace (printf "%s-trivy" .Release.Name) }}
{{- $trivyStorageClass := .Values.storageClass }}
{{- if $trivyPvc }}
{{- $trivySpec := $trivyPvc.spec | default dict }}
{{- if $trivySts }}
{{- $trivySpec := dict }}
{{- range ($trivySts.spec.volumeClaimTemplates | default list) }}
{{- if eq .metadata.name "data" }}
{{- $trivySpec = .spec | default dict }}
{{- end }}
{{- end }}
{{- if hasKey $trivySpec "storageClassName" }}
{{- $trivyStorageClass = $trivySpec.storageClassName | default "-" }}
{{- else }}
@@ -0,0 +1,108 @@
suite: trivy storageClass follows the live StatefulSet template
release:
name: harbor
namespace: tenant-test
# The default-StorageClass admission stamps the class onto the PVCs the trivy
# StatefulSet creates, not onto the StatefulSet's volumeClaimTemplate. Reading
# the PVC rendered that stamped class into an immutable template that was
# created without one, and the next upgrade of the release failed with
# "updates to statefulset spec for fields other than ... are forbidden".
set:
_namespace:
host: example.org
ingress: nginx
gateway: ""
seaweedfs: seaweedfs
_cluster:
solver: http01
scheme: &scheme
"apps/v1/StatefulSet":
gvr:
group: apps
version: v1
resource: statefulsets
namespaced: true
"v1/PersistentVolumeClaim":
gvr:
group: ""
version: v1
resource: persistentvolumeclaims
namespaced: true
stsWithoutClass: &stsWithoutClass
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: harbor-trivy
namespace: tenant-test
spec:
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ReadWriteOnce]
stsWithClass: &stsWithClass
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: harbor-trivy
namespace: tenant-test
spec:
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ReadWriteOnce]
storageClassName: replicated
defaultedPvc: &defaultedPvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-harbor-trivy-0
namespace: tenant-test
spec:
accessModes: [ReadWriteOnce]
storageClassName: local
tests:
- it: keeps the class omitted when the live template has none, whatever the PVC got
template: templates/harbor.yaml
documentSelector:
path: kind
value: HelmRelease
kubernetesProvider:
scheme: *scheme
objects: [*stsWithoutClass, *defaultedPvc]
asserts:
- notExists:
path: spec.values.harbor.persistence.persistentVolumeClaim.trivy.storageClass
- it: keeps the class omitted even when storageClass is set later
template: templates/harbor.yaml
documentSelector:
path: kind
value: HelmRelease
set:
storageClass: local-3rep
kubernetesProvider:
scheme: *scheme
objects: [*stsWithoutClass, *defaultedPvc]
asserts:
- notExists:
path: spec.values.harbor.persistence.persistentVolumeClaim.trivy.storageClass
- it: keeps the class the live template carries
template: templates/harbor.yaml
documentSelector:
path: kind
value: HelmRelease
set:
storageClass: local-3rep
kubernetesProvider:
scheme: *scheme
objects: [*stsWithClass, *defaultedPvc]
asserts:
- equal:
path: spec.values.harbor.persistence.persistentVolumeClaim.trivy.storageClass
value: replicated