Search in sources :

Example 11 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class ReSyncTest method namespaceIsProvisioned.

@Test
void namespaceIsProvisioned() {
    final String namespacesUrl = "/api/connector_mgmt/v1/agent/kafka_connector_clusters/.*/namespaces";
    final String deploymentsUrl = "/api/connector_mgmt/v1/agent/kafka_connector_clusters/.*/deployments";
    kubernetesClient.resources(Namespace.class).createOrReplace(new NamespaceBuilder().withMetadata(new ObjectMetaBuilder().withName(Namespaces.generateNamespaceId(DEPLOYMENT_ID)).addToLabels(LABEL_CLUSTER_ID, config.cluster().id()).addToAnnotations(ANNOTATION_NAMESPACE_RESOURCE_VERSION, "20").build()).build());
    kubernetesClient.resources(ManagedConnector.class).inNamespace(ns).createOrReplace(new ManagedConnectorBuilder().withMetadata(new ObjectMetaBuilder().withName(Connectors.generateConnectorId(DEPLOYMENT_ID)).addToLabels(LABEL_CLUSTER_ID, config.cluster().id()).build()).withSpec(new ManagedConnectorSpecBuilder().withDeployment(new DeploymentSpecBuilder().withDeploymentResourceVersion(10L).build()).withClusterId(config.cluster().id()).withConnectorId(CONNECTOR_ID).withDeploymentId(DEPLOYMENT_ID).withOperatorSelector(new OperatorSelectorBuilder().withId(uid()).build()).build()).build());
    RestAssured.given().contentType(MediaType.TEXT_PLAIN).post("/test/provisioner/sync");
    untilAsserted(() -> {
        server.verify(1, getRequestedFor(urlPathMatching(namespacesUrl)).withQueryParam("gt_version", equalTo("0")));
        server.verify(1, getRequestedFor(urlPathMatching(deploymentsUrl)).withQueryParam("gt_version", equalTo("0")));
    });
    RestAssured.given().contentType(MediaType.TEXT_PLAIN).post("/test/provisioner/sync");
    untilAsserted(() -> {
        server.verify(getRequestedFor(urlPathMatching(namespacesUrl)).withQueryParam("gt_version", equalTo("20")));
        server.verify(getRequestedFor(urlPathMatching(deploymentsUrl)).withQueryParam("gt_version", equalTo("10")));
    });
    untilAsserted(() -> {
        RestAssured.given().contentType(MediaType.TEXT_PLAIN).post("/test/provisioner/sync");
        server.verify(2, getRequestedFor(urlPathMatching(namespacesUrl)).withQueryParam("gt_version", equalTo("0")));
        server.verify(2, getRequestedFor(urlPathMatching(deploymentsUrl)).withQueryParam("gt_version", equalTo("0")));
    });
}
Also used : DeploymentSpecBuilder(org.bf2.cos.fleetshard.api.DeploymentSpecBuilder) ManagedConnectorBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorBuilder) ManagedConnectorSpecBuilder(org.bf2.cos.fleetshard.api.ManagedConnectorSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) OperatorSelectorBuilder(org.bf2.cos.fleetshard.api.OperatorSelectorBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 12 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class FleetManagerClient method getNamespaces.

public void getNamespaces(long gv, Consumer<Collection<ConnectorNamespace>> consumer) {
    RestClientHelper.run(() -> {
        LOGGER.debug("polling namespaces with gv: {}", gv);
        final AtomicInteger counter = new AtomicInteger();
        final List<ConnectorNamespace> items = new ArrayList<>();
        for (int i = 1; i < Integer.MAX_VALUE; i++) {
            ConnectorNamespaceList list = controlPlane.getConnectorNamespaces(config.cluster().id(), Integer.toString(i), null, gv);
            if (list == null || list.getItems() == null || list.getItems().isEmpty()) {
                LOGGER.info("No namespace for cluster {}", config.cluster().id());
                break;
            }
            items.addAll(list.getItems());
            consumer.accept(items);
            if (counter.addAndGet(items.size()) >= list.getTotal()) {
                break;
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectorNamespace(org.bf2.cos.fleet.manager.model.ConnectorNamespace) ArrayList(java.util.ArrayList) ConnectorNamespaceList(org.bf2.cos.fleet.manager.model.ConnectorNamespaceList)

Example 13 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class NamespaceProvisionerWithNoQuotaTestBase method namespaceIsProvisioned.

@Test
void namespaceIsProvisioned() {
    final Config cfg = ConfigProvider.getConfig();
    final String nsId1 = cfg.getValue("test.ns.id.1", String.class);
    final NamespacedName pullSecret = new NamespacedName(client.generateNamespaceId(nsId1), config.imagePullSecretsName());
    RestAssured.given().contentType(MediaType.TEXT_PLAIN).body(0L).post("/test/provisioner/namespaces");
    Namespace ns = until(() -> fleetShardClient.getNamespace(nsId1), Objects::nonNull);
    assertThat(ns).satisfies(item -> {
        assertThat(item.getMetadata().getName()).isEqualTo(client.generateNamespaceId(nsId1));
        assertThat(item.getMetadata().getLabels()).containsEntry(Resources.LABEL_CLUSTER_ID, fleetShardClient.getClusterId()).containsEntry(Resources.LABEL_NAMESPACE_ID, nsId1).containsEntry(Resources.LABEL_KUBERNETES_MANAGED_BY, fleetShardClient.getClusterId()).containsEntry(Resources.LABEL_KUBERNETES_CREATED_BY, fleetShardClient.getClusterId()).containsEntry(Resources.LABEL_KUBERNETES_PART_OF, fleetShardClient.getClusterId()).containsEntry(Resources.LABEL_KUBERNETES_COMPONENT, Resources.COMPONENT_NAMESPACE).containsEntry(Resources.LABEL_KUBERNETES_INSTANCE, nsId1).containsKey(Resources.LABEL_UOW);
        assertThat(item.getMetadata().getAnnotations()).containsEntry(Resources.ANNOTATION_NAMESPACE_QUOTA, "false");
    });
    until(() -> fleetShardClient.getSecret(pullSecret).filter(ps -> {
        return Objects.equals(ps.getMetadata().getLabels().get(Resources.LABEL_UOW), ns.getMetadata().getLabels().get(Resources.LABEL_UOW));
    }), Objects::nonNull);
    untilAsserted(() -> {
        ResourceQuota answer = fleetShardClient.getKubernetesClient().resourceQuotas().inNamespace(ns.getMetadata().getName()).withName(ns.getMetadata().getName() + "-quota").get();
        assertThat(answer).isNull();
    });
    untilAsserted(() -> {
        LimitRange answer = fleetShardClient.getKubernetesClient().limitRanges().inNamespace(ns.getMetadata().getName()).withName(ns.getMetadata().getName() + "-limits").get();
        assertThat(answer).isNull();
    });
}
Also used : LimitRange(io.fabric8.kubernetes.api.model.LimitRange) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Config(org.eclipse.microprofile.config.Config) Objects(java.util.Objects) Inject(javax.inject.Inject) Test(org.junit.jupiter.api.Test) MediaType(javax.ws.rs.core.MediaType) NamespacedName(org.bf2.cos.fleetshard.support.resources.NamespacedName) Namespace(io.fabric8.kubernetes.api.model.Namespace) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) SyncTestSupport(org.bf2.cos.fleetshard.sync.it.support.SyncTestSupport) FleetShardClient(org.bf2.cos.fleetshard.sync.client.FleetShardClient) RestAssured(io.restassured.RestAssured) Resources(org.bf2.cos.fleetshard.support.resources.Resources) LimitRange(io.fabric8.kubernetes.api.model.LimitRange) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) Config(org.eclipse.microprofile.config.Config) Objects(java.util.Objects) NamespacedName(org.bf2.cos.fleetshard.support.resources.NamespacedName) Namespace(io.fabric8.kubernetes.api.model.Namespace) Test(org.junit.jupiter.api.Test)

Example 14 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class KeycloakOperatorManager method installKeycloak.

public static CompletableFuture<Void> installKeycloak(KubeClient kubeClient) throws Exception {
    if (SystemTestEnvironment.INSTALL_KEYCLOAK) {
        LOGGER.info("Installing Keycloak : {}", OPERATOR_NS);
        kubeClient.client().namespaces().createOrReplace(new NamespaceBuilder().withNewMetadata().withName(OPERATOR_NS).endMetadata().build());
        SecurityUtils.TlsConfig tls = SecurityUtils.getTLSConfig(OPERATOR_NS + ".svc");
        Secret keycloakCert = new SecretBuilder().withNewMetadata().withName("sso-x509-https-secret").withNamespace(OPERATOR_NS).endMetadata().withType("kubernetes.io/tls").withData(Map.of("tls.crt", new String(Base64.getEncoder().encode(tls.getCert().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8), "tls.key", new String(Base64.getEncoder().encode(tls.getKey().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8))).build();
        kubeClient.client().secrets().inNamespace(OPERATOR_NS).createOrReplace(keycloakCert);
        List<String> keycloakInstallFiles = Arrays.asList("https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/service_account.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/role_binding.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/role.yaml", "https://raw.githubusercontent.com/keycloak/keycloak-operator/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/cluster_roles/cluster_role_binding.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/cluster_roles/cluster_role.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakbackups_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakclients_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakrealms_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloaks_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/crds/keycloak.org_keycloakusers_crd.yaml", "https://github.com/keycloak/keycloak-operator/raw/" + SystemTestEnvironment.KEYCLOAK_VERSION + "/deploy/operator.yaml");
        for (String urlString : keycloakInstallFiles) {
            URL url = new URL(urlString);
            INSTALLED_RESOURCES.add(kubeClient.client().load(url.openStream()).get().get(0));
        }
        for (HasMetadata resource : INSTALLED_RESOURCES) {
            resource.getMetadata().setNamespace(OPERATOR_NS);
            kubeClient.client().resource(resource).inNamespace(OPERATOR_NS).createOrReplace();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("apply", "-f", Paths.get(Environment.SUITE_ROOT, "src", "main", "resources", "keycloak.yml").toAbsolutePath().toString());
        LOGGER.info("Done installing Keycloak : {}", OPERATOR_NS);
        return TestUtils.asyncWaitFor("Keycloak instance ready", 1_000, 600_000, () -> TestUtils.isPodReady(KubeClient.getInstance().client().pods().inNamespace(OPERATOR_NS).list().getItems().stream().filter(pod -> pod.getMetadata().getName().contains("keycloak-0")).findFirst().orElse(null)));
    } else {
        LOGGER.info("Keycloak is not installed suite will use values from env vars for oauth");
        return CompletableFuture.completedFuture(null);
    }
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) SecurityUtils(org.bf2.systemtest.framework.SecurityUtils) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) URL(java.net.URL)

Example 15 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class StrimziOperatorManager method doInstall.

protected CompletableFuture<Void> doInstall(KubeClient kubeClient) throws IOException {
    LOGGER.info("Installing Strimzi : {} version: {}", operatorNs, version);
    Namespace namespace = new NamespaceBuilder().withNewMetadata().withName(operatorNs).endMetadata().build();
    kubeClient.client().namespaces().createOrReplace(namespace);
    URL url = new URL(String.format(STRIMZI_URL_FORMAT, version));
    // modify namespaces, convert rolebinding to clusterrolebindings, update deployment if needed
    String crbID = UUID.randomUUID().toString().substring(0, 5);
    kubeClient.apply(operatorNs, url.openStream(), i -> {
        if (i instanceof Namespaced) {
            i.getMetadata().setNamespace(operatorNs);
        }
        if (i instanceof ClusterRoleBinding) {
            ClusterRoleBinding crb = (ClusterRoleBinding) i;
            crb.getSubjects().forEach(sbj -> sbj.setNamespace(operatorNs));
            crb.getMetadata().setName(crb.getMetadata().getName() + "." + operatorNs);
            clusterWideResourceDeleters.add(unused -> {
                kubeClient.client().rbac().clusterRoleBindings().withName(crb.getMetadata().getName()).delete();
            });
        } else if (i instanceof RoleBinding) {
            RoleBinding rb = (RoleBinding) i;
            rb.getSubjects().forEach(sbj -> sbj.setNamespace(operatorNs));
            ClusterRoleBinding crb = new ClusterRoleBindingBuilder().withNewMetadata().withName(rb.getMetadata().getName() + "-all-ns-" + crbID).withAnnotations(rb.getMetadata().getAnnotations()).withLabels(rb.getMetadata().getLabels()).endMetadata().withRoleRef(rb.getRoleRef()).withSubjects(rb.getSubjects()).build();
            LOGGER.info("Creating {} named {}", crb.getKind(), crb.getMetadata().getName());
            kubeClient.client().rbac().clusterRoleBindings().createOrReplace(crb);
            clusterWideResourceDeleters.add(unused -> {
                kubeClient.client().rbac().clusterRoleBindings().withName(crb.getMetadata().getName()).delete();
            });
        } else if (i instanceof Deployment && "strimzi-cluster-operator".equals(i.getMetadata().getName())) {
            modifyDeployment((Deployment) i);
        }
        return i;
    });
    LOGGER.info("Done installing Strimzi : {}", operatorNs);
    return TestUtils.asyncWaitFor("Strimzi operator ready", 1_000, FleetShardOperatorManager.INSTALL_TIMEOUT_MS, () -> isReady(kubeClient, operatorNs, version));
}
Also used : Namespaced(io.fabric8.kubernetes.api.model.Namespaced) GithubApiClient(org.bf2.systemtest.api.github.GithubApiClient) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) KubeClient(org.bf2.test.k8s.KubeClient) Map(java.util.Map) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) LinkedList(java.util.LinkedList) TestUtils(org.bf2.test.TestUtils) Pod(io.fabric8.kubernetes.api.model.Pod) SystemTestEnvironment(org.bf2.systemtest.framework.SystemTestEnvironment) IOException(java.io.IOException) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) UUID(java.util.UUID) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) Namespaced(io.fabric8.kubernetes.api.model.Namespaced) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Readiness(io.fabric8.kubernetes.client.internal.readiness.Readiness) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) URL(java.net.URL)

Aggregations

Namespace (io.fabric8.kubernetes.api.model.Namespace)7 ConnectorNamespace (org.bf2.cos.fleet.manager.model.ConnectorNamespace)6 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 Objects (java.util.Objects)5 Pod (io.fabric8.kubernetes.api.model.Pod)4 QuarkusTest (io.quarkus.test.junit.QuarkusTest)4 IOException (java.io.IOException)4 KubeClient (org.bf2.test.k8s.KubeClient)4 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)3 PodList (io.fabric8.kubernetes.api.model.PodList)3 Quantity (io.fabric8.kubernetes.api.model.Quantity)3 ResourceQuota (io.fabric8.kubernetes.api.model.ResourceQuota)3 RestAssured (io.restassured.RestAssured)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)2 ContentTypeHeader (com.github.tomakehurst.wiremock.http.ContentTypeHeader)2 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)2 Container (io.fabric8.kubernetes.api.model.Container)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 TestProfile (io.quarkus.test.junit.TestProfile)2