use of io.fabric8.kubernetes.api.model.apps.Deployment in project keycloak by keycloak.
the class KeycloakController method prepareEventSources.
@Override
public List<EventSource> prepareEventSources(EventSourceContext<Keycloak> context) {
SharedIndexInformer<Deployment> deploymentInformer = client.apps().deployments().inNamespace(context.getConfigurationService().getClientConfiguration().getNamespace()).withLabels(Constants.DEFAULT_LABELS).runnableInformer(0);
EventSource deploymentEvent = new InformerEventSource<>(deploymentInformer, Mappers.fromOwnerReference());
return List.of(deploymentEvent);
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project keycloak by keycloak.
the class ClusterOperatorTest method createRBACresourcesAndOperatorDeployment.
private static void createRBACresourcesAndOperatorDeployment() throws FileNotFoundException {
Log.info("Creating RBAC into Namespace " + namespace);
List<HasMetadata> hasMetadata = k8sclient.load(new FileInputStream(TARGET_KUBERNETES_GENERATED_YML_FOLDER + deploymentTarget + ".yml")).inNamespace(namespace).get();
hasMetadata.stream().map(b -> {
if ("Deployment".equalsIgnoreCase(b.getKind()) && b.getMetadata().getName().contains("operator")) {
((Deployment) b).getSpec().getTemplate().getSpec().getContainers().get(0).setImagePullPolicy("Never");
}
return b;
}).forEach(c -> {
Log.info("processing part : " + c.getKind() + "--" + c.getMetadata().getName() + " -- " + namespace);
k8sclient.resource(c).inNamespace(namespace).createOrReplace();
});
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.
the class HPAHandler method generate.
private void generate(PodAutoscalerModel podAutoscalerModel) throws KubernetesPluginException {
HorizontalPodAutoscaler horizontalPodAutoscaler = new HorizontalPodAutoscalerBuilder().withNewMetadata().withName(podAutoscalerModel.getName()).withNamespace(dataHolder.getNamespace()).withLabels(podAutoscalerModel.getLabels()).endMetadata().withNewSpec().withMaxReplicas(podAutoscalerModel.getMaxReplicas()).withMinReplicas(podAutoscalerModel.getMinReplicas()).withMetrics(generateTargetCPUUtilizationPercentage(podAutoscalerModel.getCpuPercentage())).withNewScaleTargetRef("apps/v1", "Deployment", podAutoscalerModel.getDeployment()).endSpec().build();
try {
String serviceContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(horizontalPodAutoscaler);
KubernetesUtils.writeToFile(serviceContent, HPA_FILE_POSTFIX + YAML);
} catch (IOException e) {
String errorMessage = "error while generating yaml file for autoscaler: " + podAutoscalerModel.getName();
throw new KubernetesPluginException(errorMessage, e);
}
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.
the class MainFunctionDeploymentTest method mainFuncDeploymentTest.
/**
* Build bal file with main function and annotations.
*
* @throws IOException Error when loading the generated yaml.
* @throws InterruptedException Error when compiling the ballerina file.
* @throws KubernetesPluginException Error when deleting the generated artifacts folder.
*/
@Test
public void mainFuncDeploymentTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "main_function.bal"), 0);
// Check if docker image exists and correct
validateDockerfile();
validateDockerImage();
// Validate deployment yaml
File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("main_function_deployment.yaml").toFile();
Assert.assertTrue(deploymentYAML.exists());
Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
Assert.assertEquals(deployment.getMetadata().getLabels().size(), 2, "Invalid number of labels found.");
Assert.assertEquals(deployment.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "main_function", "Invalid label found.");
Assert.assertEquals(deployment.getMetadata().getLabels().get("task_type"), "printer", "Invalid label found.");
// Validate volume claim yaml.
File volumeClaimYaml = KUBERNETES_TARGET_PATH.resolve("main_function_volume_claim.yaml").toFile();
Assert.assertTrue(volumeClaimYaml.exists());
PersistentVolumeClaim volumeClaim = KubernetesTestUtils.loadYaml(volumeClaimYaml);
Assert.assertNotNull(volumeClaim);
Assert.assertEquals(volumeClaim.getMetadata().getName(), "local-pv-2");
Assert.assertEquals(volumeClaim.getSpec().getAccessModes().size(), 1);
// Validate secret.
File secretYaml = KUBERNETES_TARGET_PATH.resolve("main_function_secret.yaml").toFile();
Assert.assertTrue(secretYaml.exists());
Secret privateSecret = KubernetesTestUtils.loadYaml(secretYaml);
Assert.assertNotNull(privateSecret);
Assert.assertEquals(privateSecret.getData().size(), 1);
// Validate horizontal pod scalar.
File hpaYaml = KUBERNETES_TARGET_PATH.resolve("main_function_hpa.yaml").toFile();
Assert.assertTrue(hpaYaml.exists());
HorizontalPodAutoscaler podAutoscaler = KubernetesTestUtils.loadYaml(hpaYaml);
Assert.assertNotNull(podAutoscaler);
Assert.assertEquals(podAutoscaler.getMetadata().getName(), "main-function-hpa");
Assert.assertEquals(podAutoscaler.getMetadata().getLabels().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "main_function");
Assert.assertEquals(podAutoscaler.getSpec().getMaxReplicas().intValue(), 2);
Assert.assertEquals(podAutoscaler.getSpec().getMinReplicas().intValue(), 1);
Assert.assertEquals(podAutoscaler.getSpec().getMetrics().size(), 1);
Assert.assertEquals(podAutoscaler.getSpec().getMetrics().get(0).getResource().getTarget().getAverageUtilization().intValue(), 50);
Assert.assertEquals(podAutoscaler.getSpec().getScaleTargetRef().getName(), "pizzashack");
Container container = deployment.getSpec().getTemplate().getSpec().getContainers().get(0);
Assert.assertEquals(container.getEnv().get(0).getName(), "CONFIG_FILE");
Assert.assertEquals(container.getEnv().get(0).getValue(), "/home/ballerina/conf/ballerina.conf");
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
use of io.fabric8.kubernetes.api.model.apps.Deployment in project kubernetes by ballerinax.
the class OpenShiftBuildConfigTest method simpleBuildConfigTest.
/**
* Test case openshift build config annotation with default values.
*/
@Test(groups = { "openshift" })
public void simpleBuildConfigTest() throws IOException, InterruptedException, KubernetesPluginException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "simple_bc.bal"), 0);
File yamlFile = new File(KUBERNETES_TARGET_PATH + File.separator + "simple_bc.yaml");
Assert.assertTrue(yamlFile.exists());
KubernetesClient client = new DefaultKubernetesClient();
List<HasMetadata> k8sItems = client.load(new FileInputStream(yamlFile)).get();
for (HasMetadata data : k8sItems) {
switch(data.getKind()) {
case "Service":
case "Deployment":
break;
case "BuildConfig":
BuildConfig bc = (BuildConfig) data;
// metadata
Assert.assertNotNull(bc.getMetadata());
Assert.assertEquals(bc.getMetadata().getName(), "helloep-openshift-bc", "Invalid name found.");
Assert.assertNotNull(bc.getMetadata().getLabels(), "Labels are missing");
Assert.assertEquals(bc.getMetadata().getLabels().size(), 1, "Labels are missing");
Assert.assertNotNull(bc.getMetadata().getLabels().get("build"), "'build' label is missing");
Assert.assertEquals(bc.getMetadata().getLabels().get("build"), "helloep-openshift-bc", "Invalid label 'build' label value.");
// spec
Assert.assertNotNull(bc.getSpec());
Assert.assertNotNull(bc.getSpec().getOutput());
Assert.assertNotNull(bc.getSpec().getOutput().getTo());
Assert.assertEquals(bc.getSpec().getOutput().getTo().getKind(), "ImageStreamTag", "Invalid output kind.");
Assert.assertEquals(bc.getSpec().getOutput().getTo().getName(), "simple_bc:latest", "Invalid image stream name.");
Assert.assertNotNull(bc.getSpec().getSource());
Assert.assertNotNull(bc.getSpec().getSource().getBinary(), "Binary source is missing");
Assert.assertNotNull(bc.getSpec().getStrategy());
Assert.assertNotNull(bc.getSpec().getStrategy().getDockerStrategy(), "Docker strategy is missing.");
Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getBuildArgs().size(), 0, "Invalid number of build args.");
Assert.assertEquals(bc.getSpec().getStrategy().getDockerStrategy().getDockerfilePath(), "kubernetes/docker/Dockerfile", "Invalid docker path.");
Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getForcePull(), "Force pull image set to false");
Assert.assertFalse(bc.getSpec().getStrategy().getDockerStrategy().getNoCache(), "No cache for image build set to false");
break;
case "ImageStream":
ImageStream is = (ImageStream) data;
Assert.assertNotNull(is.getMetadata());
Assert.assertEquals(is.getMetadata().getName(), "simple_bc", "Invalid name found.");
Assert.assertEquals(is.getMetadata().getLabels().size(), 1, "Labels are missing");
Assert.assertNotNull(is.getMetadata().getLabels().get("build"), "'build' label is missing");
Assert.assertEquals(is.getMetadata().getLabels().get("build"), "helloep-openshift-bc", "Invalid label 'build' label value.");
Assert.assertNull(is.getSpec());
break;
default:
Assert.fail("Unexpected k8s resource found: " + data.getKind());
break;
}
}
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
}
Aggregations