jerrypeng commented on a change in pull request #3735: Implementing 
authentication for Pulsar Functions
URL: https://github.com/apache/pulsar/pull/3735#discussion_r262192649
 
 

 ##########
 File path: 
pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/runtime/KubernetesRuntime.java
 ##########
 @@ -244,6 +258,66 @@ public void start() throws Exception {
         }
     }
 
+    private String generateServiceAccount(InstanceConfig instanceConfig) {
+        return instanceConfig.getFunctionDetails().getTenant()
+                + "-" + instanceConfig.getFunctionDetails().getNamespace()
+                + "-" + instanceConfig.getFunctionDetails().getName()
+                + "-" + 
instanceConfig.getFunctionAuthenticationSpec().getData();
+    }
+
+    private void createServiceAccount() throws ApiException, 
InterruptedException {
+
+        String fqfn = 
FunctionDetailsUtils.getFullyQualifiedName(instanceConfig.getFunctionDetails());
+        Function.FunctionAuthenticationSpec authenticationSpec = 
instanceConfig.getFunctionAuthenticationSpec();
+        String serviceAccountName = generateServiceAccount(instanceConfig);
+        V1ServiceAccount serviceAccount = new V1ServiceAccount()
+                .metadata(
+                        new V1ObjectMeta()
+                                .name(serviceAccountName)
+                                .namespace(jobNamespace));
+
+        // configure service account for auth data if necessary
+        
functionAuthDataCacheProvider.configureAuthDataKubernetesServiceAccount(authenticationSpec,
 serviceAccount);
+
+        log.info("Creating service account with the following spec to k8 {} 
for function {}", 
appsClient.getApiClient().getJSON().serialize(serviceAccount), fqfn);
+
+        RuntimeUtils.Actions.Action createServiceAccount = 
RuntimeUtils.Actions.Action.builder()
+                .actionName(String.format("Creating service account %s for 
function %s", serviceAccountName, fqfn))
+                .numRetries(KubernetesRuntimeFactory.NUM_RETRIES)
+                
.sleepBetweenInvocationsMs(KubernetesRuntimeFactory.SLEEP_BETWEEN_RETRIES_MS)
+                .supplier(() -> {
+                    try {
+                        
coreClient.createNamespacedServiceAccount(jobNamespace, serviceAccount, "true");
+                     } catch (ApiException e) {
+                        // already exists
+                        if (e.getCode() == HTTP_CONFLICT) {
+                            log.warn("Service account {} already present for 
function {}", serviceAccountName, fqfn);
+                            return 
RuntimeUtils.Actions.ActionResult.builder().success(true).build();
+                        }
+
+                        String errorMsg = e.getResponseBody() != null ? 
e.getResponseBody() : e.getMessage();
+                        return RuntimeUtils.Actions.ActionResult.builder()
+                                .success(false)
+                                .errorMsg(errorMsg)
+                                .build();
+                    }
+
+                    return 
RuntimeUtils.Actions.ActionResult.builder().success(true).build();
+                })
+                .build();
+
+        AtomicBoolean success = new AtomicBoolean(false);
+        RuntimeUtils.Actions.newBuilder()
+                .addAction(createServiceAccount.toBuilder()
+                        .onSuccess(() -> success.set(true))
+                        .build())
+                .run();
+
+        if (!success.get()) {
+            throw new RuntimeException(String.format("Failed to create service 
account %s for function %s", serviceAccountName, fqfn));
 
 Review comment:
   it will be logged else where

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to