|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent.kubernetes; |
| 2 | + |
| 3 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 4 | +import io.fabric8.kubernetes.api.model.PersistentVolumeClaimStatus; |
| 5 | +import io.fabric8.kubernetes.api.model.Secret; |
| 6 | +import io.fabric8.kubernetes.api.model.apps.StatefulSet; |
| 7 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 8 | + |
| 9 | +public class DesiredResourceSanitizer { |
| 10 | + |
| 11 | + private DesiredResourceSanitizer() {} |
| 12 | + |
| 13 | + public static <R, P extends HasMetadata> void sanitizeDesired(R desired, R actual, P primary, |
| 14 | + Context<P> context, boolean useSSA) { |
| 15 | + if (useSSA) { |
| 16 | + if (desired instanceof StatefulSet) { |
| 17 | + fillDefaultsOnVolumeClaimTemplate((StatefulSet) desired); |
| 18 | + } |
| 19 | + if (desired instanceof Secret) { |
| 20 | + checkIfStringDataUsed((Secret) desired); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private static void checkIfStringDataUsed(Secret secret) { |
| 26 | + if (secret.getStringData() != null && !secret.getStringData().isEmpty()) { |
| 27 | + throw new IllegalStateException( |
| 28 | + "There is a known issue using StringData with SSA. Use data instead."); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static void fillDefaultsOnVolumeClaimTemplate(StatefulSet statefulSet) { |
| 33 | + if (!statefulSet.getSpec().getVolumeClaimTemplates().isEmpty()) { |
| 34 | + statefulSet.getSpec().getVolumeClaimTemplates().forEach(t -> { |
| 35 | + if (t.getSpec().getVolumeMode() == null) { |
| 36 | + t.getSpec().setVolumeMode("Filesystem"); |
| 37 | + } |
| 38 | + if (t.getStatus() == null) { |
| 39 | + t.setStatus(new PersistentVolumeClaimStatus()); |
| 40 | + } |
| 41 | + if (t.getStatus().getPhase() == null) { |
| 42 | + t.getStatus().setPhase("pending"); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments