use of io.fabric8.kubernetes.api.model.ServiceAccountBuilder in project strimzi by strimzi.
the class SetupDrainCleaner method applyInstallFiles.
public void applyInstallFiles(ExtensionContext extensionContext) {
List<File> drainCleanerFiles = Arrays.stream(new File(PATH_TO_DC_CONFIG).listFiles()).sorted().filter(File::isFile).collect(Collectors.toList());
drainCleanerFiles.forEach(file -> {
if (!file.getName().contains("README") && !file.getName().contains("Namespace") && !file.getName().contains("Deployment")) {
final String resourceType = file.getName().split("-")[1].split(".yaml")[0];
switch(resourceType) {
case Constants.CLUSTER_ROLE:
ClusterRole clusterRole = TestUtils.configFromYaml(file, ClusterRole.class);
ResourceManager.getInstance().createResource(extensionContext, clusterRole);
break;
case Constants.SERVICE_ACCOUNT:
ServiceAccount serviceAccount = TestUtils.configFromYaml(file, ServiceAccount.class);
ResourceManager.getInstance().createResource(extensionContext, new ServiceAccountBuilder(serviceAccount).editMetadata().withNamespace(Constants.DRAIN_CLEANER_NAMESPACE).endMetadata().build());
break;
case Constants.CLUSTER_ROLE_BINDING:
ClusterRoleBinding clusterRoleBinding = TestUtils.configFromYaml(file, ClusterRoleBinding.class);
ResourceManager.getInstance().createResource(extensionContext, new ClusterRoleBindingBuilder(clusterRoleBinding).build());
break;
case Constants.SECRET:
Secret secret = TestUtils.configFromYaml(file, Secret.class);
ResourceManager.getInstance().createResource(extensionContext, secret);
break;
case Constants.SERVICE:
Service service = TestUtils.configFromYaml(file, Service.class);
ResourceManager.getInstance().createResource(extensionContext, service);
break;
case Constants.VALIDATION_WEBHOOK_CONFIG:
ValidatingWebhookConfiguration webhookConfiguration = TestUtils.configFromYaml(file, ValidatingWebhookConfiguration.class);
ResourceManager.getInstance().createResource(extensionContext, webhookConfiguration);
break;
default:
LOGGER.error("Unknown installation resource type: {}", resourceType);
throw new RuntimeException("Unknown installation resource type:" + resourceType);
}
}
});
}
use of io.fabric8.kubernetes.api.model.ServiceAccountBuilder in project strimzi-kafka-operator by strimzi.
the class ServiceAccountOperatorTest method testSecretsPatching.
@Test
public void testSecretsPatching(VertxTestContext context) {
List<ObjectReference> secrets = List.of(new ObjectReferenceBuilder().withName("secretName1").build(), new ObjectReferenceBuilder().withName("secretName2").build());
ServiceAccount current = new ServiceAccountBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(RESOURCE_NAME).endMetadata().withSecrets(secrets).build();
ServiceAccount desired = new ServiceAccountBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(RESOURCE_NAME).withLabels(Map.of("lKey", "lValue")).withAnnotations(Map.of("aKey", "aValue")).endMetadata().build();
Resource mockResource = mock(resourceType());
when(mockResource.get()).thenReturn(current);
ArgumentCaptor<ServiceAccount> saCaptor = ArgumentCaptor.forClass(ServiceAccount.class);
when(mockResource.patch(saCaptor.capture())).thenReturn(desired);
when(mockResource.withPropagationPolicy(DeletionPropagation.FOREGROUND)).thenReturn(mockResource);
NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);
MixedOperation mockCms = mock(MixedOperation.class);
when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
KubernetesClient mockClient = mock(clientType());
mocker(mockClient, mockCms);
ServiceAccountOperator op = new ServiceAccountOperator(vertx, mockClient, true);
Checkpoint async = context.checkpoint();
op.reconcile(Reconciliation.DUMMY_RECONCILIATION, NAMESPACE, RESOURCE_NAME, desired).onComplete(context.succeeding(rr -> {
verify(mockResource, times(1)).patch(any(ServiceAccount.class));
assertThat(saCaptor.getValue(), is(notNullValue()));
assertThat(saCaptor.getValue().getSecrets().size(), is(2));
assertThat(saCaptor.getValue().getSecrets(), is(secrets));
assertThat(saCaptor.getValue().getMetadata().getLabels().get("lKey"), is("lValue"));
assertThat(saCaptor.getValue().getMetadata().getAnnotations().get("aKey"), is("aValue"));
async.flag();
}));
}
use of io.fabric8.kubernetes.api.model.ServiceAccountBuilder 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);
}
}
}
use of io.fabric8.kubernetes.api.model.ServiceAccountBuilder in project strimzi-kafka-operator by strimzi.
the class SetupDrainCleaner method applyInstallFiles.
public void applyInstallFiles(ExtensionContext extensionContext) {
List<File> drainCleanerFiles = Arrays.stream(new File(PATH_TO_DC_CONFIG).listFiles()).sorted().filter(File::isFile).collect(Collectors.toList());
drainCleanerFiles.forEach(file -> {
if (!file.getName().contains("README") && !file.getName().contains("Namespace") && !file.getName().contains("Deployment")) {
final String resourceType = file.getName().split("-")[1].split(".yaml")[0];
switch(resourceType) {
case Constants.CLUSTER_ROLE:
ClusterRole clusterRole = TestUtils.configFromYaml(file, ClusterRole.class);
ResourceManager.getInstance().createResource(extensionContext, clusterRole);
break;
case Constants.SERVICE_ACCOUNT:
ServiceAccount serviceAccount = TestUtils.configFromYaml(file, ServiceAccount.class);
ResourceManager.getInstance().createResource(extensionContext, new ServiceAccountBuilder(serviceAccount).editMetadata().withNamespace(Constants.DRAIN_CLEANER_NAMESPACE).endMetadata().build());
break;
case Constants.CLUSTER_ROLE_BINDING:
ClusterRoleBinding clusterRoleBinding = TestUtils.configFromYaml(file, ClusterRoleBinding.class);
ResourceManager.getInstance().createResource(extensionContext, new ClusterRoleBindingBuilder(clusterRoleBinding).build());
break;
case Constants.SECRET:
Secret secret = TestUtils.configFromYaml(file, Secret.class);
ResourceManager.getInstance().createResource(extensionContext, secret);
break;
case Constants.SERVICE:
Service service = TestUtils.configFromYaml(file, Service.class);
ResourceManager.getInstance().createResource(extensionContext, service);
break;
case Constants.VALIDATION_WEBHOOK_CONFIG:
ValidatingWebhookConfiguration webhookConfiguration = TestUtils.configFromYaml(file, ValidatingWebhookConfiguration.class);
ResourceManager.getInstance().createResource(extensionContext, webhookConfiguration);
break;
default:
LOGGER.error("Unknown installation resource type: {}", resourceType);
throw new RuntimeException("Unknown installation resource type:" + resourceType);
}
}
});
}
use of io.fabric8.kubernetes.api.model.ServiceAccountBuilder in project flink-kubernetes-operator by apache.
the class FlinkOperatorITCase method rbacSetup.
private void rbacSetup() {
LOG.info("Creating service account {}", SERVICE_ACCOUNT);
ServiceAccount serviceAccount = new ServiceAccountBuilder().withNewMetadata().withName(SERVICE_ACCOUNT).withNamespace(TEST_NAMESPACE).endMetadata().build();
client.serviceAccounts().inNamespace(TEST_NAMESPACE).createOrReplace(serviceAccount);
ClusterRoleBinding current = client.rbac().clusterRoleBindings().withName(CLUSTER_ROLE_BINDING).get();
boolean exists = current.getSubjects().stream().anyMatch(s -> SERVICE_ACCOUNT.equals(s.getName()) && TEST_NAMESPACE.equals(s.getNamespace()));
if (!exists) {
LOG.info("Patching crb {}", CLUSTER_ROLE_BINDING);
client.rbac().clusterRoleBindings().withName(CLUSTER_ROLE_BINDING).edit(crb -> new ClusterRoleBindingBuilder(crb).addNewSubject().withKind("ServiceAccount").withName(SERVICE_ACCOUNT).withNamespace(TEST_NAMESPACE).endSubject().build());
}
}
Aggregations