use of io.fabric8.kubernetes.api.model.rbac.ClusterRole in project kubernetes-client by fabric8io.
the class ClusterRoleCrudTest method crudTest.
@Test
void crudTest() {
ClusterRole kubernetesClusterRole = new ClusterRoleBuilder().withNewMetadata().withName("node-reader").endMetadata().addToRules(0, new PolicyRuleBuilder().addToApiGroups(0, "").addToNonResourceURLs(0, "/healthz").addToResourceNames(0, "my-node").addToResources(0, "nodes").addToVerbs(0, "get").addToVerbs(1, "watch").addToVerbs(2, "list").build()).build();
// test of creation
kubernetesClusterRole = client.rbac().clusterRoles().create(kubernetesClusterRole);
assertNotNull(kubernetesClusterRole);
assertEquals("ClusterRole", kubernetesClusterRole.getKind());
assertEquals("rbac.authorization.k8s.io/v1", kubernetesClusterRole.getApiVersion());
assertNotNull(kubernetesClusterRole.getMetadata());
assertEquals("node-reader", kubernetesClusterRole.getMetadata().getName());
assertNotNull(kubernetesClusterRole.getRules());
assertEquals(1, kubernetesClusterRole.getRules().size());
assertNotNull(kubernetesClusterRole.getRules().get(0).getApiGroups());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getApiGroups().size());
assertEquals("", kubernetesClusterRole.getRules().get(0).getApiGroups().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getNonResourceURLs());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getNonResourceURLs().size());
assertEquals("/healthz", kubernetesClusterRole.getRules().get(0).getNonResourceURLs().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getResourceNames());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getResourceNames().size());
assertEquals("my-node", kubernetesClusterRole.getRules().get(0).getResourceNames().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getResources());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getResources().size());
assertEquals("nodes", kubernetesClusterRole.getRules().get(0).getResources().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getVerbs());
assertEquals(3, kubernetesClusterRole.getRules().get(0).getVerbs().size());
assertEquals("get", kubernetesClusterRole.getRules().get(0).getVerbs().get(0));
assertEquals("watch", kubernetesClusterRole.getRules().get(0).getVerbs().get(1));
assertEquals("list", kubernetesClusterRole.getRules().get(0).getVerbs().get(2));
// test of list
ClusterRoleList kubernetesClusterRoleList = client.rbac().clusterRoles().list();
assertNotNull(kubernetesClusterRoleList);
assertNotNull(kubernetesClusterRoleList.getItems());
assertEquals(1, kubernetesClusterRoleList.getItems().size());
assertNotNull(kubernetesClusterRoleList.getItems().get(0));
assertEquals("ClusterRole", kubernetesClusterRoleList.getItems().get(0).getKind());
assertEquals("rbac.authorization.k8s.io/v1", kubernetesClusterRoleList.getItems().get(0).getApiVersion());
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getMetadata());
assertEquals("node-reader", kubernetesClusterRoleList.getItems().get(0).getMetadata().getName());
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules());
assertEquals(1, kubernetesClusterRoleList.getItems().get(0).getRules().size());
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getApiGroups());
assertEquals(1, kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getApiGroups().size());
assertEquals("", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getApiGroups().get(0));
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getNonResourceURLs());
assertEquals(1, kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getNonResourceURLs().size());
assertEquals("/healthz", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getNonResourceURLs().get(0));
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResourceNames());
assertEquals(1, kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResourceNames().size());
assertEquals("my-node", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResourceNames().get(0));
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResources());
assertEquals(1, kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResources().size());
assertEquals("nodes", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getResources().get(0));
assertNotNull(kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getVerbs());
assertEquals(3, kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getVerbs().size());
assertEquals("get", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getVerbs().get(0));
assertEquals("watch", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getVerbs().get(1));
assertEquals("list", kubernetesClusterRoleList.getItems().get(0).getRules().get(0).getVerbs().get(2));
// test of updation
kubernetesClusterRole = client.rbac().clusterRoles().withName("node-reader").edit(c -> new ClusterRoleBuilder(c).editRule(0).addToApiGroups(1, "extensions").endRule().build());
assertNotNull(kubernetesClusterRole);
assertEquals("ClusterRole", kubernetesClusterRole.getKind());
assertEquals("rbac.authorization.k8s.io/v1", kubernetesClusterRole.getApiVersion());
assertNotNull(kubernetesClusterRole.getMetadata());
assertEquals("node-reader", kubernetesClusterRole.getMetadata().getName());
assertNotNull(kubernetesClusterRole.getRules());
assertEquals(1, kubernetesClusterRole.getRules().size());
assertNotNull(kubernetesClusterRole.getRules().get(0).getApiGroups());
assertEquals(2, kubernetesClusterRole.getRules().get(0).getApiGroups().size());
assertEquals("", kubernetesClusterRole.getRules().get(0).getApiGroups().get(0));
assertEquals("extensions", kubernetesClusterRole.getRules().get(0).getApiGroups().get(1));
assertNotNull(kubernetesClusterRole.getRules().get(0).getNonResourceURLs());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getNonResourceURLs().size());
assertEquals("/healthz", kubernetesClusterRole.getRules().get(0).getNonResourceURLs().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getResourceNames());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getResourceNames().size());
assertEquals("my-node", kubernetesClusterRole.getRules().get(0).getResourceNames().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getResources());
assertEquals(1, kubernetesClusterRole.getRules().get(0).getResources().size());
assertEquals("nodes", kubernetesClusterRole.getRules().get(0).getResources().get(0));
assertNotNull(kubernetesClusterRole.getRules().get(0).getVerbs());
assertEquals(3, kubernetesClusterRole.getRules().get(0).getVerbs().size());
assertEquals("get", kubernetesClusterRole.getRules().get(0).getVerbs().get(0));
assertEquals("watch", kubernetesClusterRole.getRules().get(0).getVerbs().get(1));
assertEquals("list", kubernetesClusterRole.getRules().get(0).getVerbs().get(2));
// test of deletion
boolean deleted = client.rbac().clusterRoles().delete();
assertTrue(deleted);
kubernetesClusterRoleList = client.rbac().clusterRoles().list();
assertEquals(0, kubernetesClusterRoleList.getItems().size());
}
use of io.fabric8.kubernetes.api.model.rbac.ClusterRole in project elastest-torm by elastest.
the class K8sService method deleteClusterRoleBindingAdmin.
public void deleteClusterRoleBindingAdmin(String name, String namespace) throws Exception {
if (namespace != null && !"default".equals(namespace) && name != null && !"cluster-admin".equals("name")) {
String role = "cluster-admin";
logger.debug("Deleting Cluster Role Binding with name {} in the namespace {} with role {}", name, namespace, role);
ClusterRoleBinding clusterRoleBinding = new ClusterRoleBindingBuilder().withNewMetadata().withName(name).endMetadata().withRoleRef(new RoleRefBuilder().withName(role).withKind("ClusterRole").withApiGroup("rbac.authorization.k8s.io").build()).withSubjects(new SubjectBuilder().withKind("ServiceAccount").withNamespace(namespace).withName("default").build()).build();
client.rbac().clusterRoleBindings().delete(clusterRoleBinding);
logger.debug("Cluster Role Binding with name {} deleted successfully from namespace {}", name, namespace);
}
}
use of io.fabric8.kubernetes.api.model.rbac.ClusterRole in project elastest-torm by elastest.
the class K8sService method createClusterRoleBindingAdmin.
public void createClusterRoleBindingAdmin(String name, String namespace) throws Exception {
String role = "cluster-admin";
logger.debug("Creating Cluster Role Binding with name {} in the namespace {} with role {}", name, namespace, role);
ClusterRoleBinding clusterRoleBinding = new ClusterRoleBindingBuilder().withNewMetadata().withName(name).endMetadata().withRoleRef(new RoleRefBuilder().withName(role).withKind("ClusterRole").withApiGroup("rbac.authorization.k8s.io").build()).withSubjects(new SubjectBuilder().withKind("ServiceAccount").withNamespace(namespace).withName("default").build()).build();
client.rbac().clusterRoleBindings().create(clusterRoleBinding);
logger.debug("Cluster Role Binding with name {} created successfully in the namespace {}", name, namespace);
}
use of io.fabric8.kubernetes.api.model.rbac.ClusterRole in project strimzi by strimzi.
the class ClusterRoleBindingOperatorIT method getOriginal.
@Override
protected ClusterRoleBinding getOriginal() {
Subject ks = new SubjectBuilder().withKind("ServiceAccount").withName("my-service-account").withNamespace("my-namespace").build();
RoleRef roleRef = new RoleRefBuilder().withName("my-cluster-role").withApiGroup("rbac.authorization.k8s.io").withKind("ClusterRole").build();
return new ClusterRoleBindingBuilder().withNewMetadata().withName(resourceName).withLabels(singletonMap("state", "new")).endMetadata().withSubjects(ks).withRoleRef(roleRef).build();
}
use of io.fabric8.kubernetes.api.model.rbac.ClusterRole in project strimzi by strimzi.
the class ClusterRoleBindingOperatorIT method getModified.
@Override
protected ClusterRoleBinding getModified() {
Subject ks = new SubjectBuilder().withKind("ServiceAccount").withName("my-service-account2").withNamespace("my-namespace2").build();
// RoleRef cannot be changed
RoleRef roleRef = new RoleRefBuilder().withName("my-cluster-role").withApiGroup("rbac.authorization.k8s.io").withKind("ClusterRole").build();
return new ClusterRoleBindingBuilder().withNewMetadata().withName(resourceName).withLabels(singletonMap("state", "modified")).endMetadata().withSubjects(ks).withRoleRef(roleRef).build();
}
Aggregations