Search in sources :

Example 21 with Binding

use of io.fabric8.kubernetes.api.model.Binding in project strimzi by strimzi.

the class EntityUserOperatorTest method testRoleBindingInOtherNamespace.

@ParallelTest
public void testRoleBindingInOtherNamespace() {
    RoleBinding binding = entityUserOperator.generateRoleBindingForRole(namespace, uoWatchedNamespace);
    assertThat(binding.getSubjects().get(0).getNamespace(), is(namespace));
    assertThat(binding.getMetadata().getNamespace(), is(uoWatchedNamespace));
    assertThat(binding.getMetadata().getOwnerReferences().size(), is(0));
    assertThat(binding.getRoleRef().getKind(), is("Role"));
    assertThat(binding.getRoleRef().getName(), is("foo-entity-operator"));
}
Also used : RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 22 with Binding

use of io.fabric8.kubernetes.api.model.Binding in project strimzi-kafka-operator by strimzi.

the class SetupClusterOperator method applyClusterOperatorInstallFiles.

/**
 * Perform application of ServiceAccount, Roles and CRDs needed for proper cluster operator deployment.
 * Configuration files are loaded from packaging/install/cluster-operator directory.
 */
public void applyClusterOperatorInstallFiles(String namespace) {
    List<File> operatorFiles = Arrays.stream(new File(CO_INSTALL_DIR).listFiles()).sorted().filter(File::isFile).filter(file -> !file.getName().matches(".*(Binding|Deployment)-.*")).collect(Collectors.toList());
    for (File operatorFile : operatorFiles) {
        File createFile = operatorFile;
        if (createFile.getName().contains(Constants.CLUSTER_ROLE + "-")) {
            createFile = switchClusterRolesToRolesIfNeeded(createFile);
        }
        final String resourceType = createFile.getName().split("-")[1];
        LOGGER.debug("Installation resource type: {}", resourceType);
        switch(resourceType) {
            case Constants.ROLE:
                Role role = TestUtils.configFromYaml(createFile, Role.class);
                ResourceManager.getInstance().createResource(extensionContext, new RoleBuilder(role).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CLUSTER_ROLE:
                ClusterRole clusterRole = TestUtils.configFromYaml(createFile, ClusterRole.class);
                ResourceManager.getInstance().createResource(extensionContext, clusterRole);
                break;
            case Constants.SERVICE_ACCOUNT:
                ServiceAccount serviceAccount = TestUtils.configFromYaml(createFile, ServiceAccount.class);
                ResourceManager.getInstance().createResource(extensionContext, new ServiceAccountBuilder(serviceAccount).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CONFIG_MAP:
                ConfigMap configMap = TestUtils.configFromYaml(createFile, ConfigMap.class);
                ResourceManager.getInstance().createResource(extensionContext, new ConfigMapBuilder(configMap).editMetadata().withNamespace(namespace).endMetadata().build());
                break;
            case Constants.CUSTOM_RESOURCE_DEFINITION_SHORT:
                CustomResourceDefinition customResourceDefinition = TestUtils.configFromYaml(createFile, CustomResourceDefinition.class);
                ResourceManager.getInstance().createResource(extensionContext, customResourceDefinition);
                break;
            default:
                LOGGER.error("Unknown installation resource type: {}", resourceType);
                throw new RuntimeException("Unknown installation resource type:" + resourceType);
        }
    }
}
Also used : Environment(io.strimzi.systemtest.Environment) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Arrays(java.util.Arrays) OlmResource(io.strimzi.systemtest.resources.operator.specific.OlmResource) RoleBindingResource(io.strimzi.systemtest.resources.kubernetes.RoleBindingResource) OlmInstallationStrategy(io.strimzi.systemtest.enums.OlmInstallationStrategy) NetworkPolicyResource(io.strimzi.systemtest.resources.kubernetes.NetworkPolicyResource) Role(io.fabric8.kubernetes.api.model.rbac.Role) Level(org.apache.logging.log4j.Level) HashMap(java.util.HashMap) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ClusterRoleBindingResource(io.strimzi.systemtest.resources.kubernetes.ClusterRoleBindingResource) ArrayList(java.util.ArrayList) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) HelmResource(io.strimzi.systemtest.resources.operator.specific.HelmResource) KubeClusterResource(io.strimzi.test.k8s.KubeClusterResource) Map(java.util.Map) TestUtils(io.strimzi.test.TestUtils) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) StUtils(io.strimzi.systemtest.utils.StUtils) ClusterOperatorRBACType(io.strimzi.systemtest.enums.ClusterOperatorRBACType) BeforeAllOnce(io.strimzi.systemtest.BeforeAllOnce) OpenShift(io.strimzi.test.k8s.cluster.OpenShift) Predicate(java.util.function.Predicate) CollectorElement(io.strimzi.test.logs.CollectorElement) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) Constants(io.strimzi.systemtest.Constants) IOException(java.io.IOException) ClusterRoleBindingTemplates(io.strimzi.systemtest.templates.kubernetes.ClusterRoleBindingTemplates) Collectors(java.util.stream.Collectors) File(java.io.File) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubeClusterResource.kubeClient(io.strimzi.test.k8s.KubeClusterResource.kubeClient) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Objects(java.util.Objects) RoleResource(io.strimzi.systemtest.resources.kubernetes.RoleResource) List(java.util.List) Exec(io.strimzi.test.executor.Exec) Logger(org.apache.logging.log4j.Logger) ResourceManager(io.strimzi.systemtest.resources.ResourceManager) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) Role(io.fabric8.kubernetes.api.model.rbac.Role) ClusterRole(io.fabric8.kubernetes.api.model.rbac.ClusterRole) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) File(java.io.File)

Example 23 with Binding

use of io.fabric8.kubernetes.api.model.Binding in project cos-fleetshard by bf2fc6cc711aee1a0c2a.

the class KameletBindingSteps method klb_phase_and_conditions.

@SuppressWarnings("unchecked")
@When("the klb phase is {string} with conditions:")
public void klb_phase_and_conditions(String phase, DataTable table) {
    // TODO: investigate using KubernetesClient.resources(KameletBinding.class) result in a bad patch
    kubernetesClient.genericKubernetesResources(KameletBinding.RESOURCE_DEFINITION).inNamespace(ctx.connector().getMetadata().getNamespace()).withName(ctx.connector().getMetadata().getName()).editStatus(binding -> {
        Map<String, Object> status = (Map<String, Object>) binding.getAdditionalProperties().get("status");
        if (status == null) {
            status = new HashMap<>();
        }
        List<Map<String, String>> rows = table.asMaps(String.class, String.class);
        List<Condition> conditions = new ArrayList<>(rows.size());
        for (Map<String, String> columns : rows) {
            conditions.add(new ConditionBuilder().withMessage(columns.get("message")).withReason(columns.get("reason")).withStatus(columns.get("status")).withType(columns.get("type")).withLastTransitionTime(columns.get("lastTransitionTime")).build());
        }
        status.put("phase", phase);
        status.put("conditions", conditions);
        binding.getAdditionalProperties().put("status", status);
        return binding;
    });
// kubernetesClient.resources(KameletBinding.class)
// .inNamespace(ctx.connector().getMetadata().getNamespace())
// .withName(ctx.connector().getMetadata().getName())
// .editStatus(binding -> {
// if (binding.getStatus() == null) {
// binding.setStatus(new KameletBindingStatus());
// }
// 
// List<Map<String, String>> rows = table.asMaps(String.class, String.class);
// List<Condition> conditions = new ArrayList<>(rows.size());
// 
// for (Map<String, String> columns : rows) {
// conditions.add(new ConditionBuilder()
// .withMessage(columns.get("message"))
// .withReason(columns.get("reason"))
// .withStatus(columns.get("status"))
// .withType(columns.get("type"))
// .withLastTransitionTime(columns.get("lastTransitionTime"))
// .build());
// }
// 
// binding.getStatus().setPhase(phase);
// binding.getStatus().setConditions(conditions);
// 
// return binding;
// });
}
Also used : Condition(io.fabric8.kubernetes.api.model.Condition) ConditionBuilder(io.fabric8.kubernetes.api.model.ConditionBuilder) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) When(io.cucumber.java.en.When)

Example 24 with Binding

use of io.fabric8.kubernetes.api.model.Binding in project strimzi-kafka-operator by strimzi.

the class EntityUserOperatorTest method testRoleBindingInOtherNamespace.

@ParallelTest
public void testRoleBindingInOtherNamespace() {
    RoleBinding binding = entityUserOperator.generateRoleBindingForRole(namespace, uoWatchedNamespace);
    assertThat(binding.getSubjects().get(0).getNamespace(), is(namespace));
    assertThat(binding.getMetadata().getNamespace(), is(uoWatchedNamespace));
    assertThat(binding.getMetadata().getOwnerReferences().size(), is(0));
    assertThat(binding.getRoleRef().getKind(), is("Role"));
    assertThat(binding.getRoleRef().getName(), is("foo-entity-operator"));
}
Also used : RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 25 with Binding

use of io.fabric8.kubernetes.api.model.Binding in project quarkus-operator-sdk by quarkiverse.

the class AddRoleBindingsDecorator method visit.

@Override
public void visit(KubernetesListBuilder list) {
    final var serviceAccountName = getMandatoryDeploymentMetadata(list).getName();
    for (Entry<String, QuarkusControllerConfiguration> entry : configs.entrySet()) {
        String controllerName = entry.getKey();
        QuarkusControllerConfiguration<?> config = entry.getValue();
        if (config.watchCurrentNamespace()) {
            // create a RoleBinding that will be applied in the current namespace if watching only the current NS
            list.addToItems(new RoleBindingBuilder().withNewMetadata().withName(controllerName + "-role-binding").endMetadata().withNewRoleRef(RBAC_AUTHORIZATION_GROUP, CLUSTER_ROLE, getClusterRoleName(controllerName)).addNewSubject(null, SERVICE_ACCOUNT, serviceAccountName, null).build());
        } else if (config.watchAllNamespaces()) {
            handleClusterRoleBinding(list, serviceAccountName, controllerName, controllerName + "-cluster-role-binding", "watch all namespaces", getClusterRoleName(controllerName));
        } else {
            config.getEffectiveNamespaces().forEach(ns -> list.addToItems(new RoleBindingBuilder().withNewMetadata().withName(controllerName + "-role-binding").withNamespace(ns).endMetadata().withNewRoleRef(RBAC_AUTHORIZATION_GROUP, CLUSTER_ROLE, getClusterRoleName(controllerName)).addNewSubject(null, SERVICE_ACCOUNT, serviceAccountName, null).build()));
        }
        // if we validate the CRDs, also create a binding for the CRD validating role
        if (validateCRDs) {
            final var crBindingName = controllerName + "-crd-validating-role-binding";
            handleClusterRoleBinding(list, serviceAccountName, controllerName, crBindingName, "validate CRDs", AddClusterRolesDecorator.JOSDK_CRD_VALIDATING_CLUSTER_ROLE);
        }
    }
}
Also used : AddClusterRolesDecorator.getClusterRoleName(io.quarkiverse.operatorsdk.deployment.AddClusterRolesDecorator.getClusterRoleName) ResourceProvidingDecorator(io.dekorate.kubernetes.decorator.ResourceProvidingDecorator) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) Map(java.util.Map) Entry(java.util.Map.Entry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Optional(java.util.Optional) QuarkusControllerConfiguration(io.quarkiverse.operatorsdk.runtime.QuarkusControllerConfiguration) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) ConcurrentMap(java.util.concurrent.ConcurrentMap) RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) QuarkusControllerConfiguration(io.quarkiverse.operatorsdk.runtime.QuarkusControllerConfiguration)

Aggregations

IOException (java.io.IOException)11 List (java.util.List)11 Test (org.junit.jupiter.api.Test)11 RoleBinding (io.fabric8.kubernetes.api.model.rbac.RoleBinding)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 ClusterRoleBinding (io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding)8 Subject (io.fabric8.kubernetes.api.model.rbac.Subject)8 ParallelTest (io.strimzi.test.annotations.ParallelTest)8 Arrays (java.util.Arrays)8 HashMap (java.util.HashMap)8 ServiceBinding (io.dekorate.servicebinding.model.ServiceBinding)7 File (java.io.File)7 Path (java.nio.file.Path)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 RoleRefBuilder (io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder)6 SubjectBuilder (io.fabric8.kubernetes.api.model.rbac.SubjectBuilder)6 AppArtifact (io.quarkus.bootstrap.model.AppArtifact)6 Version (io.quarkus.builder.Version)6 ProdBuildResults (io.quarkus.test.ProdBuildResults)6 ProdModeTestResults (io.quarkus.test.ProdModeTestResults)6