ModelUtils { public static void parsePodTemplate(AbstractModel model, PodTemplate pod) { if (pod != null) { if (pod.getMetadata() != null) { model.templatePodLabels = pod.getMetadata().getLabels(); model.templatePodAnnotations = pod.getMetadata().getAnnotations(); } if (pod.getAffinity() != null) { model.setUserAffinity(pod.getAffinity()); } if (pod.getTolerations() != null) { model.setTolerations(pod.getTolerations()); } model.templateTerminationGracePeriodSeconds = pod.getTerminationGracePeriodSeconds(); model.templateImagePullSecrets = pod.getImagePullSecrets(); model.templateSecurityContext = pod.getSecurityContext(); model.templatePodPriorityClassName = pod.getPriorityClassName(); model.templatePodSchedulerName = pod.getSchedulerName(); model.templatePodHostAliases = pod.getHostAliases(); } } private  ModelUtils(); static int getCertificateValidity(CertificateAuthority certificateAuthority); static int getRenewalDays(CertificateAuthority certificateAuthority); static String formatTimestamp(Date date); static String defaultResourceLabels(String cluster); static Map<String, String> getContainerEnv(StatefulSet sts, String containerName); static Secret buildSecret(ClusterCa clusterCa, Secret secret, String namespace, String secretName,
            String commonName, String keyCertName, Labels labels, OwnerReference ownerReference, boolean isMaintenanceTimeWindowsSatisfied); static Secret createSecret(String name, String namespace, Labels labels, OwnerReference ownerReference, Map<String, String> data); static void parsePodDisruptionBudgetTemplate(AbstractModel model, PodDisruptionBudgetTemplate pdb); static void parsePodTemplate(AbstractModel model, PodTemplate pod); static boolean containsPersistentStorage(Storage storage); static Storage decodeStorageFromJson(String json); static String encodeStorageToJson(Storage storage); static Map<String, String> getCustomLabelsOrAnnotations(String envVarName); static boolean doExistingCertificatesDiffer(Secret current, Secret desired); static List<T> asListOrEmptyList(List<T> list); static String getJavaSystemPropertiesToString(List<SystemProperty> javaSystemProperties); static List<String> getLinesWithoutCommentsAndEmptyLines(String config); static void removeEmptyValuesFromTolerations(List<Toleration> tolerations); static List<Toleration> tolerations(String tolerations, List<Toleration> tolerationList, String templateTolerations, PodTemplate podTemplate); static AffinityBuilder populateAffinityBuilderWithRackLabelSelector(AffinityBuilder builder, Affinity userAffinity, String topologyKey); static final String TLS_SIDECAR_LOG_LEVEL; }
@Test public void testParsePodTemplate() { Kafka kafka = new KafkaBuilder() .withNewMetadata() .withName("my-cluster") .withNamespace("my-namespace") .endMetadata() .build(); LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret"); LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret"); Affinity affinity = new AffinityBuilder() .withNewNodeAffinity() .withNewRequiredDuringSchedulingIgnoredDuringExecution() .withNodeSelectorTerms(new NodeSelectorTermBuilder() .addNewMatchExpression() .withNewKey("key1") .withNewOperator("In") .withValues("value1", "value2") .endMatchExpression() .build()) .endRequiredDuringSchedulingIgnoredDuringExecution() .endNodeAffinity() .build(); List<Toleration> tolerations = singletonList(new TolerationBuilder() .withEffect("NoExecute") .withKey("key1") .withOperator("Equal") .withValue("value1") .build()); PodTemplate template = new PodTemplateBuilder() .withNewMetadata() .withAnnotations(Collections.singletonMap("annoKey", "annoValue")) .withLabels(Collections.singletonMap("labelKey", "labelValue")) .endMetadata() .withSecurityContext(new PodSecurityContextBuilder().withFsGroup(123L).withRunAsGroup(456L).withRunAsUser(789L).build()) .withImagePullSecrets(secret1, secret2) .withTerminationGracePeriodSeconds(123) .withAffinity(affinity) .withTolerations(tolerations) .build(); Model model = new Model(kafka); ModelUtils.parsePodTemplate(model, template); assertThat(model.templatePodLabels, is(Collections.singletonMap("labelKey", "labelValue"))); assertThat(model.templatePodAnnotations, is(Collections.singletonMap("annoKey", "annoValue"))); assertThat(model.templateTerminationGracePeriodSeconds, is(123)); assertThat(model.templateImagePullSecrets.size(), is(2)); assertThat(model.templateImagePullSecrets.contains(secret1), is(true)); assertThat(model.templateImagePullSecrets.contains(secret2), is(true)); assertThat(model.templateSecurityContext, is(notNullValue())); assertThat(model.templateSecurityContext.getFsGroup(), is(Long.valueOf(123))); assertThat(model.templateSecurityContext.getRunAsGroup(), is(Long.valueOf(456))); assertThat(model.templateSecurityContext.getRunAsUser(), is(Long.valueOf(789))); assertThat(model.getUserAffinity(), is(affinity)); assertThat(model.getTolerations(), is(tolerations)); }
@Test public void testParseNullPodTemplate() { Kafka kafka = new KafkaBuilder() .withNewMetadata() .withName("my-cluster") .withNamespace("my-namespace") .endMetadata() .build(); Model model = new Model(kafka); ModelUtils.parsePodTemplate(model, null); assertThat(model.templatePodLabels, is(nullValue())); assertThat(model.templatePodAnnotations, is(nullValue())); assertThat(model.templateImagePullSecrets, is(nullValue())); assertThat(model.templateSecurityContext, is(nullValue())); assertThat(model.templateTerminationGracePeriodSeconds, is(30)); }