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));
}
}
});
}
}
}
}
}
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;
}
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;
}
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()));
}
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;
}
Aggregations