Search in sources :

Example 46 with Deployment

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);
}
Also used : InformerEventSource(io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource) EventSource(io.javaoperatorsdk.operator.processing.event.source.EventSource) InformerEventSource(io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment)

Example 47 with Deployment

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();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AfterAll(org.junit.jupiter.api.AfterAll) OperatorProducer(io.quarkiverse.operatorsdk.runtime.OperatorProducer) Reconciler(io.javaoperatorsdk.operator.api.reconciler.Reconciler) Operator(io.javaoperatorsdk.operator.Operator) BeforeAll(org.junit.jupiter.api.BeforeAll) TypeLiteral(javax.enterprise.util.TypeLiteral) Duration(java.time.Duration) Log(io.quarkus.logging.Log) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Instance(javax.enterprise.inject.Instance) FileWriter(java.io.FileWriter) CDI(javax.enterprise.inject.spi.CDI) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Keycloak(org.keycloak.operator.v2alpha1.crds.Keycloak) TimeUnit(java.util.concurrent.TimeUnit) Config(io.fabric8.kubernetes.client.Config) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) QuarkusConfigurationService(io.quarkiverse.operatorsdk.runtime.QuarkusConfigurationService) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Awaitility(org.awaitility.Awaitility) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) FileInputStream(java.io.FileInputStream)

Example 48 with Deployment

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);
    }
}
Also used : HorizontalPodAutoscaler(io.fabric8.kubernetes.api.model.HorizontalPodAutoscaler) HorizontalPodAutoscalerBuilder(io.fabric8.kubernetes.api.model.HorizontalPodAutoscalerBuilder) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 49 with Deployment

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);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) Container(io.fabric8.kubernetes.api.model.Container) HorizontalPodAutoscaler(io.fabric8.kubernetes.api.model.HorizontalPodAutoscaler) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) File(java.io.File) Test(org.testng.annotations.Test)

Example 50 with Deployment

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);
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ImageStream(io.fabric8.openshift.api.model.ImageStream) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)62 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)52 Test (org.testng.annotations.Test)50 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)47 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)43 Container (io.fabric8.kubernetes.api.model.Container)32 Service (io.fabric8.kubernetes.api.model.Service)28 Test (org.junit.Test)28 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)27 InputStream (java.io.InputStream)26 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)25 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)25 Deployment (org.jboss.arquillian.container.test.api.Deployment)21 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)21 Asset (org.jboss.shrinkwrap.api.asset.Asset)21 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)21 IOException (java.io.IOException)20 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 FileInputStream (java.io.FileInputStream)19 CommandSupport (io.fabric8.itests.support.CommandSupport)18