Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][functions] Fix K8S download function method with auth enabled #17597

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -875,17 +875,7 @@ private List<String> getDownloadCommand(String tenant, String namespace, String
ArrayList<String> cmd = new ArrayList<>(Arrays.asList(
pulsarRootDir + configAdminCLI,
"--admin-url",
pulsarAdminUrl,
"functions",
"download",
"--tenant",
tenant,
"--namespace",
namespace,
"--name",
name,
"--destination-file",
userCodeFilePath));
pulsarAdminUrl));

// add auth plugin and parameters if necessary
if (authenticationEnabled && authConfig != null) {
Expand All @@ -900,6 +890,18 @@ && isNotBlank(authConfig.getClientAuthenticationParameters())
}
}

cmd.addAll(Arrays.asList(
"functions",
"download",
"--tenant",
tenant,
"--namespace",
namespace,
"--name",
name,
"--destination-file",
userCodeFilePath));

if (transformFunction) {
cmd.add("--transform-function");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static org.apache.pulsar.functions.runtime.RuntimeUtils.FUNCTIONS_INSTANCE_CLASSPATH;
Expand Down Expand Up @@ -183,10 +184,21 @@ public void tearDown() {
}
}


KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
double cpuOverCommitRatio, double memoryOverCommitRatio,
Optional<RuntimeCustomizer> manifestCustomizer,
String downloadDirectory) throws Exception {
return createKubernetesRuntimeFactory(extraDepsDir, percentMemoryPadding, cpuOverCommitRatio, memoryOverCommitRatio,
manifestCustomizer, downloadDirectory, null, null);
}

KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
double cpuOverCommitRatio, double memoryOverCommitRatio,
Optional<RuntimeCustomizer> manifestCustomizer,
String downloadDirectory,
Consumer<WorkerConfig> workerConfigConsumer,
AuthenticationConfig authenticationConfig) throws Exception {

KubernetesRuntimeFactory factory = spy(new KubernetesRuntimeFactory());
doNothing().when(factory).setupClient();
Expand Down Expand Up @@ -226,15 +238,28 @@ KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int

manifestCustomizer.ifPresent(runtimeCustomizer -> runtimeCustomizer.initialize(Optional.ofNullable(workerConfig.getRuntimeCustomizerConfig()).orElse(Collections.emptyMap())));

factory.initialize(workerConfig, null, new TestSecretProviderConfigurator(), Mockito.mock(ConnectorsManager.class),
if (workerConfigConsumer != null) {
workerConfigConsumer.accept(workerConfig);
}

factory.initialize(workerConfig, authenticationConfig, new TestSecretProviderConfigurator(), Mockito.mock(ConnectorsManager.class),
Mockito.mock(FunctionsManager.class), Optional.empty(), manifestCustomizer);
return factory;
}

KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
double cpuOverCommitRatio, double memoryOverCommitRatio,
Optional<RuntimeCustomizer> manifestCustomizer) throws Exception {
return createKubernetesRuntimeFactory(extraDepsDir, percentMemoryPadding, cpuOverCommitRatio, memoryOverCommitRatio, manifestCustomizer, null);
return createKubernetesRuntimeFactory(extraDepsDir, percentMemoryPadding, cpuOverCommitRatio, memoryOverCommitRatio, manifestCustomizer,
null, null, null);
}

KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
double cpuOverCommitRatio, double memoryOverCommitRatio,
Optional<RuntimeCustomizer> manifestCustomizer,
Consumer<WorkerConfig> workerConfigConsumer) throws Exception {
return createKubernetesRuntimeFactory(extraDepsDir, percentMemoryPadding, cpuOverCommitRatio, memoryOverCommitRatio, manifestCustomizer,
null, workerConfigConsumer, null);
}

KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
Expand Down Expand Up @@ -810,6 +835,33 @@ public void testCustomKubernetesManifestCustomizer() throws Exception {
assertEquals(spec.getSpec().getTemplate().getSpec().getServiceAccountName(), "my-service-account");
}

@Test
public void testCustomKubernetesDownloadCommandsWithAuth() throws Exception {
InstanceConfig config = createJavaInstanceConfig(FunctionDetails.Runtime.JAVA, false);
config.setFunctionAuthenticationSpec(Function.FunctionAuthenticationSpec.newBuilder().build());
config.setFunctionDetails(createFunctionDetails(FunctionDetails.Runtime.JAVA, false));

factory = createKubernetesRuntimeFactory(null,
10, 1.0, 1.0, Optional.empty(), null, wconfig -> {
wconfig.setAuthenticationEnabled(true);
}, AuthenticationConfig.builder()
.clientAuthenticationPlugin("com.MyAuth")
.clientAuthenticationParameters("{\"authParam1\": \"authParamValue1\"}")
.build());

KubernetesRuntime container = factory.createContainer(config, userJarFile, userJarFile, null, null, 30l);
V1StatefulSet spec = container.createStatefulSet();
String expectedDownloadCommand = "pulsar-admin --admin-url " + pulsarAdminUrl
+ " --auth-plugin com.MyAuth --auth-params {\"authParam1\": \"authParamValue1\"}"
+ " functions download "
+ "--tenant " + TEST_TENANT
+ " --namespace " + TEST_NAMESPACE
+ " --name " + TEST_NAME
+ " --destination-file " + pulsarRootDir + "/" + userJarFile;
String containerCommand = spec.getSpec().getTemplate().getSpec().getContainers().get(0).getCommand().get(2);
assertTrue(containerCommand.contains(expectedDownloadCommand), "Found:" + containerCommand);
}

InstanceConfig createGolangInstanceConfig() {
InstanceConfig config = new InstanceConfig();

Expand Down Expand Up @@ -915,6 +967,16 @@ KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int
double cpuOverCommitRatio, double memoryOverCommitRatio,
String manifestCustomizerClassName,
Map<String, Object> runtimeCustomizerConfig) throws Exception {
return createKubernetesRuntimeFactory(extraDepsDir, percentMemoryPadding, cpuOverCommitRatio,
memoryOverCommitRatio, manifestCustomizerClassName, runtimeCustomizerConfig, null, null);
}

KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int percentMemoryPadding,
double cpuOverCommitRatio, double memoryOverCommitRatio,
String manifestCustomizerClassName,
Map<String, Object> runtimeCustomizerConfig,
Consumer<WorkerConfig> workerConfigConsumer,
AuthenticationConfig authenticationConfig) throws Exception {
KubernetesRuntimeFactory factory = spy(new KubernetesRuntimeFactory());
doNothing().when(factory).setupClient();

Expand Down Expand Up @@ -957,8 +1019,11 @@ KubernetesRuntimeFactory createKubernetesRuntimeFactory(String extraDepsDir, int
manifestCustomizer = Optional.of(RuntimeCustomizer.getRuntimeCustomizer(workerConfig.getRuntimeCustomizerClassName()));
manifestCustomizer.get().initialize(Optional.ofNullable(workerConfig.getRuntimeCustomizerConfig()).orElse(Collections.emptyMap()));
}
if (workerConfigConsumer != null) {
workerConfigConsumer.accept(workerConfig);
}

factory.initialize(workerConfig, null, new TestSecretProviderConfigurator(),
factory.initialize(workerConfig, authenticationConfig, new TestSecretProviderConfigurator(),
Mockito.mock(ConnectorsManager.class), Mockito.mock(FunctionsManager.class), Optional.empty(), manifestCustomizer);
return factory;
}
Expand Down Expand Up @@ -1094,7 +1159,8 @@ public void testBasicKubernetesManifestCustomizerWithRuntimeCustomizerConfigOver
public void testJavaConstructorWithoutDownloadDirectoryDefined() throws Exception {
InstanceConfig config = createJavaInstanceConfig(FunctionDetails.Runtime.JAVA, false);

factory = createKubernetesRuntimeFactory(null, 10, 1.0, 1.0, Optional.empty(), null);
factory = createKubernetesRuntimeFactory(null, 10, 1.0, 1.0,
Optional.empty());

verifyJavaInstance(config, pulsarRootDir + "/instances/deps", false, factory.getDownloadDirectory());
}
Expand Down