Search in sources :

Example 56 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project jkube by eclipse.

the class ControllerViaPluginConfigurationEnricher method create.

@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
    final String name = getConfig(Config.NAME, JKubeProjectUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId()));
    ResourceConfig xmlResourceConfig = getConfiguration().getResource();
    final ResourceConfig config = ResourceConfig.builder().controllerName(name).imagePullPolicy(getImagePullPolicy(xmlResourceConfig, getConfig(Config.PULL_POLICY))).replicas(getReplicaCount(builder, xmlResourceConfig, Configs.asInt(getConfig(Config.REPLICA_COUNT)))).build();
    final List<ImageConfiguration> images = getImages();
    // Check if at least a replica set is added. If not add a default one
    if (KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
        // At least one image must be present, otherwise the resulting config will be invalid
        if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
            final StatefulSetSpec spec = statefulSetHandler.get(config, images).getSpec();
            if (spec != null) {
                builder.accept(new TypedVisitor<StatefulSetBuilder>() {

                    @Override
                    public void visit(StatefulSetBuilder statefulSetBuilder) {
                        statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                        mergeStatefulSetSpec(statefulSetBuilder, spec);
                    }
                });
                if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                    final PodSpec podSpec = spec.getTemplate().getSpec();
                    builder.accept(new TypedVisitor<PodSpecBuilder>() {

                        @Override
                        public void visit(PodSpecBuilder builder) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        } else {
            final DeploymentSpec spec = deployHandler.get(config, images).getSpec();
            if (spec != null) {
                builder.accept(new TypedVisitor<DeploymentBuilder>() {

                    @Override
                    public void visit(DeploymentBuilder deploymentBuilder) {
                        deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                        mergeDeploymentSpec(deploymentBuilder, spec);
                    }
                });
                if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                    final PodSpec podSpec = spec.getTemplate().getSpec();
                    builder.accept(new TypedVisitor<PodSpecBuilder>() {

                        @Override
                        public void visit(PodSpecBuilder builder) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.apps.StatefulSetSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 57 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class AdminServer method deploymentFrom.

/* test */
@Override
public Deployment deploymentFrom(ManagedKafka managedKafka, Deployment current) {
    String adminServerName = adminServerName(managedKafka);
    DeploymentBuilder builder = current != null ? new DeploymentBuilder(current) : new DeploymentBuilder();
    builder.editOrNewMetadata().withName(adminServerName).withNamespace(adminServerNamespace(managedKafka)).withLabels(buildLabels(adminServerName)).endMetadata().editOrNewSpec().withReplicas(1).editOrNewSelector().withMatchLabels(buildSelectorLabels(adminServerName)).endSelector().editOrNewTemplate().editOrNewMetadata().withAnnotations(buildAnnotations(managedKafka)).withLabels(buildLabels(adminServerName)).endMetadata().editOrNewSpec().withContainers(buildContainers(managedKafka)).withImagePullSecrets(imagePullSecretManager.getOperatorImagePullSecrets(managedKafka)).withVolumes(buildVolumes(managedKafka)).endSpec().endTemplate().endSpec();
    if (this.config.getAdminserver().isColocateWithZookeeper()) {
        builder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().withAffinity(OperandUtils.buildZookeeperPodAffinity(managedKafka)).endSpec().endTemplate().endSpec();
    }
    Deployment deployment = builder.build();
    // setting the ManagedKafka has owner of the Admin Server deployment resource is needed
    // by the operator sdk to handle events on the Deployment resource properly
    OperandUtils.setAsOwner(managedKafka, deployment);
    return deployment;
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 58 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class Canary method deploymentFrom.

@Override
public Deployment deploymentFrom(ManagedKafka managedKafka, Deployment current) {
    String canaryName = canaryName(managedKafka);
    DeploymentBuilder builder = current != null ? new DeploymentBuilder(current) : new DeploymentBuilder();
    builder.editOrNewMetadata().withName(canaryName).withNamespace(canaryNamespace(managedKafka)).withLabels(buildLabels(canaryName)).endMetadata().editOrNewSpec().withReplicas(1).editOrNewSelector().withMatchLabels(buildSelectorLabels(canaryName)).endSelector().editOrNewTemplate().editOrNewMetadata().withLabels(buildLabels(canaryName)).endMetadata().editOrNewSpec().withInitContainers().withContainers(buildContainers(managedKafka, current)).withImagePullSecrets(imagePullSecretManager.getOperatorImagePullSecrets(managedKafka)).withVolumes(buildVolumes(managedKafka)).endSpec().endTemplate().endSpec();
    if (this.config.getCanary().isColocateWithZookeeper()) {
        builder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().withAffinity(OperandUtils.buildZookeeperPodAffinity(managedKafka)).endSpec().endTemplate().endSpec();
    }
    if (initEnabled && !hasClusterSpecificBootstrapDomain(managedKafka)) {
        builder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().withInitContainers(buildInitContainer(managedKafka, current)).endSpec().endTemplate().endSpec();
    }
    Deployment deployment = builder.build();
    // setting the ManagedKafka has owner of the Canary deployment resource is needed
    // by the operator sdk to handle events on the Deployment resource properly
    OperandUtils.setAsOwner(managedKafka, deployment);
    return deployment;
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 59 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ImagePullSecretManagerTest method testWithSecrets.

@Test
void testWithSecrets() {
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(OperandUtils.FLEETSHARD_OPERATOR_NAME).endMetadata().withNewSpec().withNewTemplate().withNewSpec().withImagePullSecrets(new LocalObjectReferenceBuilder().withName("name").build(), new LocalObjectReferenceBuilder().withName("other").build()).endSpec().endTemplate().endSpec().build();
    client.apps().deployments().inNamespace(client.getNamespace()).create(deployment);
    imagePullSecretManager.initialize();
    ManagedKafka managedKafka = new ManagedKafka();
    managedKafka.setMetadata(new ObjectMetaBuilder().withName("mk").withNamespace("testWithSecrets").build());
    assertEquals(Set.of("mk-pull-name", "mk-pull-other"), imagePullSecretManager.getOperatorImagePullSecrets(managedKafka).stream().map(l -> l.getName()).collect(Collectors.toSet()));
    // no-ops - the secrets aren't yet retrieved
    imagePullSecretManager.propagateSecrets(managedKafka);
    imagePullSecretManager.deleteSecrets(managedKafka);
    imagePullSecretManager.checkSecret();
    client.secrets().inNamespace(client.getNamespace()).create(new SecretBuilder().withNewMetadata().withName("name").endMetadata().build());
    client.secrets().inNamespace(client.getNamespace()).create(new SecretBuilder().withNewMetadata().withName("other").endMetadata().build());
    // should succeed, but there's no ManagedKafkas so nothing happens
    imagePullSecretManager.checkSecret();
    assertTrue(client.secrets().inNamespace("testWithSecrets").list().getItems().isEmpty());
    imagePullSecretManager.propagateSecrets(managedKafka);
    assertEquals(Set.of("mk-pull-name", "mk-pull-other"), client.secrets().inNamespace("testWithSecrets").list().getItems().stream().map(s -> s.getMetadata().getName()).collect(Collectors.toSet()));
}
Also used : SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) LocalObjectReferenceBuilder(io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 60 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project nivio by dedica-team.

the class InputFormatHandlerKubernetesTest method setK8sTestEnvironment.

private Map<String, String> setK8sTestEnvironment() {
    var identifier = new HashMap<String, String>();
    var service = new ServiceBuilder().withNewMetadata().withName("deployment").withCreationTimestamp("testCreation").withUid("serviceUid").withLabels(Map.of("testLabelKey", "testLabelValue")).withOwnerReferences(new OwnerReferenceBuilder().withUid("testOwnerUid").build()).withNamespace("test").endMetadata().withNewSpec().withClusterIP("testIP").withNewType("testType").withSessionAffinity("testSessionAffinity").endSpec().build();
    kubernetesClient.services().create(service);
    identifier.put("service", kubernetesClient.services().list().getItems().get(0).getMetadata().getUid());
    var deployment = new DeploymentBuilder().withNewMetadata().withName("deployment").withCreationTimestamp("testCreation").withUid("deploymentUid").withLabels(Map.of("testLabelKey", "testLabelValue", "1", "2")).withOwnerReferences(new OwnerReferenceBuilder().withUid("testOwnerUid").build()).withNamespace("test").endMetadata().withNewSpec().withNewStrategy().withNewType("strategyType").endStrategy().endSpec().withNewStatus().addNewCondition().withType("testType").withStatus("testStatus").endCondition().endStatus().build();
    kubernetesClient.apps().deployments().create(deployment);
    identifier.put("deployment", kubernetesClient.apps().deployments().list().getItems().get(0).getMetadata().getUid());
    var replicaSet = new ReplicaSetBuilder().withNewMetadata().withName("replicaSet").withCreationTimestamp("testCreation").withUid("replicaSetUid").withLabels(Map.of("testLabelKey", "testLabelValue")).withOwnerReferences(new OwnerReferenceBuilder().withUid(identifier.get("deployment")).build()).withNamespace("test").endMetadata().withNewSpec().withReplicas(1).endSpec().withNewStatus().addNewCondition().withType("testType").withStatus("testStatus").endCondition().withReadyReplicas(1).endStatus().build();
    kubernetesClient.apps().replicaSets().create(replicaSet);
    identifier.put("replicaSet", kubernetesClient.apps().replicaSets().list().getItems().get(0).getMetadata().getUid());
    var statefulSet = new StatefulSetBuilder().withNewMetadata().withName("statefulSet").withCreationTimestamp("testCreation").withUid("statefulSetUid").withLabels(Map.of("testLabelKey", "testLabelValue", "1", "2")).withOwnerReferences(new OwnerReferenceBuilder().withUid("testOwnerUid").build()).withNamespace("test").endMetadata().withNewSpec().withReplicas(1).endSpec().withNewStatus().addNewCondition().withType("testType").withStatus("testStatus").endCondition().withReadyReplicas(1).endStatus().build();
    kubernetesClient.apps().statefulSets().create(statefulSet);
    identifier.put("statefulSet", kubernetesClient.apps().statefulSets().list().getItems().get(0).getMetadata().getUid());
    var pod = new PodBuilder().withNewMetadata().withName("pod").withCreationTimestamp("testCreation").withUid("podUid").withLabels(Map.of("testLabelKey", "testLabelValue")).withOwnerReferences(new OwnerReferenceBuilder().withUid(identifier.get("replicaSet")).build()).withNamespace("test").endMetadata().withNewSpec().addNewVolume().withNewPersistentVolumeClaim().withNewClaimName("persistentVolumeClaim").endPersistentVolumeClaim().endVolume().endSpec().withNewStatus().addNewCondition().withType("testType").withStatus("testStatus").endCondition().endStatus().build();
    kubernetesClient.pods().create(pod);
    identifier.put("pod", kubernetesClient.pods().list().getItems().get(0).getMetadata().getUid());
    var persistentVolumeClaim = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentVolumeClaim").withCreationTimestamp("testCreation").withUid("persistentVolumeClaimUid").withLabels(Map.of("testLabelKey", "testLabelValue")).withOwnerReferences(new OwnerReferenceBuilder().withUid("testOwnerUid").build()).withNamespace("test").endMetadata().withNewSpec().withStorageClassName("testStorageName").endSpec().withNewStatus().endStatus().build();
    kubernetesClient.persistentVolumeClaims().create(persistentVolumeClaim);
    identifier.put("persistentVolumeClaim", kubernetesClient.persistentVolumeClaims().list().getItems().get(0).getMetadata().getUid());
    var persistentVolume = new PersistentVolumeBuilder().withNewMetadata().withName("persistentVolume").withCreationTimestamp("testCreation").withUid("persistentVolumeUid").withLabels(Map.of("testLabelKey", "testLabelValue")).withOwnerReferences(new OwnerReferenceBuilder().withUid("testOwnerUid").build()).withNamespace("test").endMetadata().withNewSpec().withStorageClassName("testStorageName").withPersistentVolumeReclaimPolicy("testReclaimPolicy").addNewAccessMode("testAccessMode").addToCapacity("testCapacity", new Quantity("8", "Gi")).withClaimRef(new ObjectReferenceBuilder().withUid(identifier.get("persistentVolumeClaim")).build()).endSpec().withNewStatus().endStatus().build();
    kubernetesClient.persistentVolumes().create(persistentVolume);
    identifier.put("persistentVolume", kubernetesClient.persistentVolumes().list().getItems().get(0).getMetadata().getUid());
    return identifier;
}
Also used : ReplicaSetBuilder(io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)

Aggregations

DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)153 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)80 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)60 Test (org.junit.Test)60 Test (org.junit.jupiter.api.Test)32 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)26 Expectations (mockit.Expectations)26 Test (org.testng.annotations.Test)22 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 ArrayList (java.util.ArrayList)17 Container (io.fabric8.kubernetes.api.model.Container)16 HashMap (java.util.HashMap)16 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)15 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)15 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)14 Properties (java.util.Properties)14 Pod (io.fabric8.kubernetes.api.model.Pod)13 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)13 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)12 InternalRecipe (org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe)12