use of io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder in project fabric8-maven-plugin by fabric8io.
the class EnricherManagerTest method enrichSimple.
@Test
public void enrichSimple() {
new Expectations() {
{
context.getConfiguration();
result = new Configuration.Builder().processorConfig(new ProcessorConfig(Arrays.asList("fmp-metadata", "fmp-project-label"), null, new HashMap<>())).build();
}
};
EnricherManager manager = new EnricherManager(null, context, Optional.empty());
KubernetesListBuilder builder = new KubernetesListBuilder();
builder.addToItems(new ReplicaSetBuilder().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withName("test").withImage("busybox").endContainer().endSpec().endTemplate().endSpec().build());
manager.enrich(PlatformMode.kubernetes, builder);
KubernetesList list = builder.build();
assertEquals(1, list.getItems().size());
ReplicaSet pod = (ReplicaSet) list.getItems().get(0);
ObjectMeta metadata = pod.getMetadata();
assertNotNull(metadata);
Map<String, String> labels = metadata.getLabels();
assertNotNull(labels);
assertEquals("fabric8", labels.get("provider"));
}
use of io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder in project halyard by spinnaker.
the class KubernetesV1DistributedService method ensureRunning.
default void ensureRunning(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration, List<ConfigSource> configSources, boolean recreate) {
ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
String namespace = getNamespace(settings);
String serviceName = getServiceName();
String replicaSetName = serviceName + "-v000";
int port = settings.getPort();
SpinnakerMonitoringDaemonService monitoringService = getMonitoringDaemonService();
ServiceSettings monitoringSettings = runtimeSettings.getServiceSettings(monitoringService);
KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
KubernetesV1ProviderUtils.createNamespace(details, namespace);
Map<String, String> serviceSelector = new HashMap<>();
serviceSelector.put("load-balancer-" + serviceName, "true");
Map<String, String> replicaSetSelector = new HashMap<>();
replicaSetSelector.put("replication-controller", replicaSetName);
Map<String, String> podLabels = new HashMap<>();
podLabels.putAll(replicaSetSelector);
podLabels.putAll(serviceSelector);
Map<String, String> serviceLabels = new HashMap<>();
serviceLabels.put("app", "spin");
serviceLabels.put("stack", getCanonicalName());
ServiceBuilder serviceBuilder = new ServiceBuilder();
serviceBuilder = serviceBuilder.withNewMetadata().withName(serviceName).withNamespace(namespace).withLabels(serviceLabels).endMetadata().withNewSpec().withSelector(serviceSelector).withPorts(new ServicePortBuilder().withPort(port).withName("http").build(), new ServicePortBuilder().withPort(monitoringSettings.getPort()).withName("monitoring").build()).endSpec();
boolean create = true;
if (client.services().inNamespace(namespace).withName(serviceName).get() != null) {
if (recreate) {
client.services().inNamespace(namespace).withName(serviceName).delete();
} else {
create = false;
}
}
if (create) {
client.services().inNamespace(namespace).create(serviceBuilder.build());
}
List<Container> containers = new ArrayList<>();
DeploymentEnvironment deploymentEnvironment = details.getDeploymentConfiguration().getDeploymentEnvironment();
containers.add(ResourceBuilder.buildContainer(serviceName, settings, configSources, deploymentEnvironment));
for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
String sidecarName = sidecarService.getService().getServiceName();
ServiceSettings sidecarSettings = resolvedConfiguration.getServiceSettings(sidecarService.getService());
containers.add(ResourceBuilder.buildContainer(sidecarName, sidecarSettings, configSources, deploymentEnvironment));
}
List<Volume> volumes = configSources.stream().map(c -> {
return new VolumeBuilder().withName(c.getId()).withSecret(new SecretVolumeSourceBuilder().withSecretName(c.getId()).build()).build();
}).collect(Collectors.toList());
ReplicaSetBuilder replicaSetBuilder = new ReplicaSetBuilder();
List<LocalObjectReference> imagePullSecrets = getImagePullSecrets(settings);
Map componentSizing = deploymentEnvironment.getCustomSizing().get(serviceName);
replicaSetBuilder = replicaSetBuilder.withNewMetadata().withName(replicaSetName).withNamespace(namespace).endMetadata().withNewSpec().withReplicas(retrieveKubernetesTargetSize(componentSizing)).withNewSelector().withMatchLabels(replicaSetSelector).endSelector().withNewTemplate().withNewMetadata().withAnnotations(settings.getKubernetes().getPodAnnotations()).withLabels(podLabels).endMetadata().withNewSpec().withContainers(containers).withTerminationGracePeriodSeconds(5L).withVolumes(volumes).withImagePullSecrets(imagePullSecrets).endSpec().endTemplate().endSpec();
create = true;
if (client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).get() != null) {
if (recreate) {
client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).delete();
RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
while (runningServiceDetails.getLatestEnabledVersion() != null) {
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
}
} else {
create = false;
}
}
if (create) {
client.extensions().replicaSets().inNamespace(namespace).create(replicaSetBuilder.build());
}
RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
Integer version = runningServiceDetails.getLatestEnabledVersion();
while (version == null || runningServiceDetails.getInstances().get(version).stream().anyMatch(i -> !(i.isHealthy() && i.isRunning()))) {
DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
version = runningServiceDetails.getLatestEnabledVersion();
}
}
use of io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder in project fabric8-maven-plugin by fabric8io.
the class ImageEnricherTest method checkEnrichReplicaSet.
@Test
public void checkEnrichReplicaSet() throws Exception {
KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new ReplicaSetBuilder().build());
imageEnricher.create(PlatformMode.kubernetes, builder);
assertCorrectlyGeneratedResources(builder.build(), "ReplicaSet");
}
use of io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder in project fabric8-maven-plugin by fabric8io.
the class TriggersAnnotationEnricherTest method testReplicaSetEnrichment.
@Test
public void testReplicaSetEnrichment() throws IOException {
KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new ReplicaSetBuilder().withNewSpec().withNewTemplate().withNewSpec().withContainers(createContainers("c1", "is", "c2", "a-docker-user/is:latest")).endSpec().endTemplate().endSpec().build());
TriggersAnnotationEnricher enricher = new TriggersAnnotationEnricher(context);
enricher.enrich(PlatformMode.kubernetes, builder);
ReplicaSet res = (ReplicaSet) builder.build().getItems().get(0);
String triggers = res.getMetadata().getAnnotations().get("image.openshift.io/triggers");
assertNotNull(triggers);
List<ImageChangeTrigger> triggerList = OBJECT_MAPPER.readValue(triggers, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, ImageChangeTrigger.class));
assertEquals(1, triggerList.size());
ImageChangeTrigger trigger = triggerList.get(0);
assertEquals("ImageStreamTag", trigger.getFrom().getKind());
assertEquals("is:latest", trigger.getFrom().getName());
assertTrue(trigger.getAdditionalProperties().containsKey("fieldPath"));
}
Aggregations