use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project entando-k8s-controller-coordinator by entando-k8s.
the class CrdManagementTest method testCustomResourceEventWithControllerImageOverride.
@Test
@Description("New instances of my CustomResourceDefinitions should result in the image specified in the " + CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP + " ConfigMap to be executed against the resource rather than the one on my CRD's annotation")
void testCustomResourceEventWithControllerImageOverride() throws IOException {
step("Given I have prepared a cluster scoped deployment of the EntandoOperator", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), "*"));
step("And I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(value).editMetadata();
step("And I have a CustomResourceDefinition with", () -> {
step(format("the %s label ", LabelNames.CRD_OF_INTEREST.getName()), () -> {
builder.addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD");
});
step(format("and the %s annotation ", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
});
});
step("And I have registered my custom resource definition", () -> {
client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
});
step(format("But I have overridden the image to 'test/image-override' in the %s ConfigMap", CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP), () -> {
client.patchControllerConfigMap(new ConfigMapBuilder(client.findOrCreateControllerConfigMap(CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP)).addToData("MyCRD.test.org", "test/image-override").build());
});
final SerializedEntandoResource resource = new SerializedEntandoResource();
resource.setMetadata(new ObjectMetaBuilder().withName("my-resource").withNamespace(MY_NAMESPACE).build());
resource.setDefinition(CustomResourceDefinitionContext.fromCrd(builder.endMetadata().build()));
step("When I create a new custom resource based on my CustomResourceDefinition the overriding controller image is used to " + "execute" + " a " + "Controller pod that runs to completion", () -> client.createOrPatchEntandoResource(resource));
step(format("Then a controller pod has been created with the image that I specified in the %s ConfigMap", CoordinatorUtils.CONTROLLER_IMAGE_OVERRIDES_CONFIGMAP), () -> {
await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource)) != null);
assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource)).getSpec().getContainers().get(0).getImage()).contains("test/image-override");
});
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project devspaces-images by redhat-developer.
the class OpenShiftProjectTest method setUp.
@BeforeMethod
public void setUp() throws Exception {
lenient().when(clientFactory.create(anyString())).thenReturn(kubernetesClient);
lenient().when(clientFactory.createOC()).thenReturn(openShiftClient);
lenient().when(clientFactory.createOC(anyString())).thenReturn(openShiftClient);
lenient().when(cheServerOpenshiftClientFactory.createOC()).thenReturn(openShiftCheServerClient);
lenient().when(openShiftClient.adapt(OpenShiftClient.class)).thenReturn(openShiftClient);
final MixedOperation mixedOperation = mock(MixedOperation.class);
final NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
lenient().doReturn(mixedOperation).when(kubernetesClient).serviceAccounts();
lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
lenient().when(namespaceOperation.withName(anyString())).thenReturn(serviceAccountResource);
lenient().when(serviceAccountResource.get()).thenReturn(mock(ServiceAccount.class));
lenient().doReturn(projectRequestOperation).when(openShiftClient).projectrequests();
lenient().doReturn(metadataNested).when(metadataNested).withName(anyString());
openShiftProject = new OpenShiftProject(clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, WORKSPACE_ID, PROJECT_NAME, deployments, services, routes, pvcs, ingresses, secrets, configsMaps);
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project kubernetes-plugin by jenkinsci.
the class PodTemplateUtils method combine.
/**
* Combines a Pod with its parent.
* @param parent The parent Pod (nullable).
* @param template The child Pod
*/
public static Pod combine(Pod parent, Pod template) {
if (template == null) {
throw new IllegalArgumentException("Pod template should not be null");
}
if (parent == null) {
return template;
}
LOGGER.finest(() -> "Combining pods, parent: " + Serialization.asYaml(parent) + " template: " + Serialization.asYaml(template));
Map<String, String> nodeSelector = mergeMaps(parent.getSpec().getNodeSelector(), template.getSpec().getNodeSelector());
String serviceAccount = isNullOrEmpty(template.getSpec().getServiceAccount()) ? parent.getSpec().getServiceAccount() : template.getSpec().getServiceAccount();
String serviceAccountName = isNullOrEmpty(template.getSpec().getServiceAccountName()) ? parent.getSpec().getServiceAccountName() : template.getSpec().getServiceAccountName();
String schedulerName = isNullOrEmpty(template.getSpec().getSchedulerName()) ? parent.getSpec().getSchedulerName() : template.getSpec().getSchedulerName();
Boolean hostNetwork = template.getSpec().getHostNetwork() != null ? template.getSpec().getHostNetwork() : parent.getSpec().getHostNetwork();
Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(), template.getMetadata().getAnnotations());
Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(), template.getMetadata().getLabels());
Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
imagePullSecrets.addAll(template.getSpec().getImagePullSecrets());
// Containers
List<Container> combinedContainers = combineContainers(parent.getSpec().getContainers(), template.getSpec().getContainers());
// Init containers
List<Container> combinedInitContainers = combineContainers(parent.getSpec().getInitContainers(), template.getSpec().getInitContainers());
// Volumes
List<Volume> combinedVolumes = combineVolumes(parent.getSpec().getVolumes(), template.getSpec().getVolumes());
// Tolerations
List<Toleration> combinedTolerations = new LinkedList<>();
Optional.ofNullable(parent.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
Optional.ofNullable(template.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
// WorkspaceVolume workspaceVolume = template.isCustomWorkspaceVolumeEnabled() && template.getWorkspaceVolume() != null ? template.getWorkspaceVolume() : parent.getWorkspaceVolume();
// Tool location node properties
// List<ToolLocationNodeProperty> toolLocationNodeProperties = new ArrayList<>();
// toolLocationNodeProperties.addAll(parent.getNodeProperties());
// toolLocationNodeProperties.addAll(template.getNodeProperties());
MetadataNested<PodBuilder> metadataBuilder = //
new PodBuilder(parent).withNewMetadataLike(parent.getMetadata()).withAnnotations(podAnnotations).withLabels(podLabels);
if (!isNullOrEmpty(template.getMetadata().getName())) {
metadataBuilder.withName(template.getMetadata().getName());
}
if (!isNullOrEmpty(template.getMetadata().getNamespace())) {
metadataBuilder.withNamespace(template.getMetadata().getNamespace());
}
SpecNested<PodBuilder> specBuilder = //
metadataBuilder.endMetadata().withNewSpecLike(//
parent.getSpec()).withNodeSelector(//
nodeSelector).withServiceAccount(//
serviceAccount).withServiceAccountName(//
serviceAccountName).withSchedulerName(schedulerName).withHostNetwork(//
hostNetwork).withContainers(//
combinedContainers).withInitContainers(//
combinedInitContainers).withVolumes(//
combinedVolumes).withTolerations(//
combinedTolerations).withImagePullSecrets(new ArrayList<>(imagePullSecrets));
// Security context
if (template.getSpec().getSecurityContext() != null || parent.getSpec().getSecurityContext() != null) {
specBuilder.editOrNewSecurityContext().withRunAsUser(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsUser() != null ? template.getSpec().getSecurityContext().getRunAsUser() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsUser() != null ? parent.getSpec().getSecurityContext().getRunAsUser() : null)).withRunAsGroup(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsGroup() != null ? template.getSpec().getSecurityContext().getRunAsGroup() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsGroup() != null ? parent.getSpec().getSecurityContext().getRunAsGroup() : null)).endSecurityContext();
}
// podTemplate.setLabel(label);
// podTemplate.setEnvVars(combineEnvVars(parent, template));
// podTemplate.setWorkspaceVolume(workspaceVolume);
// podTemplate.setNodeProperties(toolLocationNodeProperties);
// podTemplate.setNodeUsageMode(nodeUsageMode);
// podTemplate.setYaml(template.getYaml() == null ? parent.getYaml() : template.getYaml());
Pod pod = specBuilder.endSpec().build();
LOGGER.finest(() -> "Pods combined: " + Serialization.asYaml(pod));
return pod;
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project entando-k8s-controller-coordinator by entando-k8s.
the class CrdManagementTest method testCustomResourceEventWithNoControllerImage.
@Test
@Description("The operator should automatically mark new instances of my CustomResourceDefinitions to 'successful' when I don't have " + "a controller image for it (yet)")
void testCustomResourceEventWithNoControllerImage() throws IOException {
step("Given I have prepared a cluster scoped deployment of the EntandoOperator", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), "*"));
step("And I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final CustomResourceDefinition value = objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class);
final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(value).editMetadata();
step("And I have a CustomResourceDefinition with", () -> {
step(format("the %s label ", LabelNames.CRD_OF_INTEREST.getName()), () -> {
builder.addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD");
});
step(format("but no %s annotation ", AnnotationNames.CONTROLLER_IMAGE.getName()), () -> {
});
});
step("And I have registered my custom resource definition", () -> {
client.getCluster().putCustomResourceDefinition(builder.endMetadata().build());
});
final SerializedEntandoResource resource = new SerializedEntandoResource();
resource.setMetadata(new ObjectMetaBuilder().withName("my-resource").withNamespace(MY_NAMESPACE).build());
resource.setDefinition(CustomResourceDefinitionContext.fromCrd(builder.endMetadata().build()));
step("When I create a new custom resource based on my CustomResourceDefinition my controller image is used to execute a " + "Controller pod that runs to completion", () -> client.createOrPatchEntandoResource(resource));
step("The phase on the resource status was updated to 'successful", () -> {
await().atMost(3, TimeUnit.SECONDS).ignoreExceptions().until(() -> client.reload(resource).getStatus().getPhase().equals(EntandoDeploymentPhase.SUCCESSFUL));
});
step("But no controller pod has been created", () -> assertThat(client.loadPod(AbstractK8SClientDouble.CONTROLLER_NAMESPACE, CoordinatorUtils.podLabelsFor(resource))).isNull());
step("And the fact that no image was found was logged", () -> assertThat(LogInterceptor.getLogEntries().stream().anyMatch(s -> s.contains("has neither the entando.org/controller-image annotation, nor is there an entry in the configmap " + "entando-controller-image-overrides")))).isTrue();
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionFluent.MetadataNested in project entando-k8s-controller-coordinator by entando-k8s.
the class CrdManagementTest method crdUpdatesShouldRestartObservers.
@Test
@Description("Updates to my CustomResourceDefinitions should result in their observers being restarted")
void crdUpdatesShouldRestartObservers() throws IOException {
step("Given I have started the Entando Operator", () -> entandoControllerCoordinator.onStartup(new StartupEvent()));
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final ValueHolder<EntandoResourceObserver> observer = new ValueHolder<>();
final ValueHolder<CustomResourceDefinition> crd = new ValueHolder<>();
crd.set(objectMapper.readValue(Thread.currentThread().getContextClassLoader().getResource("mycrds.test.org.crd.yaml"), CustomResourceDefinition.class));
final MetadataNested<CustomResourceDefinitionBuilder> builder = new CustomResourceDefinitionBuilder(crd.get()).editMetadata();
step("And I have registered a CustomResourceDefinition with", () -> {
step(format("the %s label ", LabelNames.CRD_OF_INTEREST.getName()), () -> {
builder.addToLabels(LabelNames.CRD_OF_INTEREST.getName(), "MyCRD");
});
step(format("and the %s annotation ", AnnotationNames.CONTROLLER_IMAGE), () -> {
builder.addToAnnotations(AnnotationNames.CONTROLLER_IMAGE.getName(), "test/my-controller");
});
step("and I have registered my CustomResourceDefinition", () -> {
crd.set(client.getCluster().putCustomResourceDefinition(builder.withGeneration(1L).endMetadata().build()));
});
attachment("CustomResourceDefinition", objectMapper.writeValueAsString(crd.get()));
});
step("And I have waited for the Operator to start observing state changes against the related CustomResources", () -> {
await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())).getCrdGeneration() == 1L);
observer.set(entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())));
});
step("When I apply an updated version of my CustomResourceDefinition ", () -> {
crd.get().getMetadata().setGeneration(2L);
client.getCluster().putCustomResourceDefinition(crd.get());
});
step("Then the Operator has started a new state change observer against the CustomResources", () -> {
await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get())).getCrdGeneration() == 2L);
assertThat(entandoControllerCoordinator.getObserver(CustomResourceDefinitionContext.fromCrd(crd.get()))).isNotSameAs(observer.get());
});
}
Aggregations