You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/10/13 08:45:55 UTC

[GitHub] [flink] xintongsong commented on a change in pull request #13255: [FLINK-18971] Support to mount kerberos conf as ConfigMap and Keytab …

xintongsong commented on a change in pull request #13255:
URL: https://github.com/apache/flink/pull/13255#discussion_r491805839



##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/configuration/KubernetesConfigOptions.java
##########
@@ -220,6 +220,13 @@
 			.withDescription("The user-specified annotations that are set to the rest Service. The value should be " +
 				"in the form of a1:v1,a2:v2");
 
+	public static final ConfigOption<String> KERBEROS_KRB5_PATH =
+		key("kubernetes.kerberos.krb5-conf.path")
+			.stringType()
+			.noDefaultValue()
+			.withDescription("Specify the local location of the krb5.conf file to be mounted on the JobManager and " +
+				"TaskManager for Kerberos. Note: The KDC defined needs to be visible from inside the containers.");

Review comment:
       Why is this configuration option only needed for the Kubernetes deployment? My gut feeling is that this should be a common feature for all the deployments. Why does the Kubernetes deployment needs a configuration option that other deployments don't need?

##########
File path: flink-kubernetes/src/test/java/org/apache/flink/kubernetes/kubeclient/decorators/KerberosMountDecoratorTest.java
##########
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.kubeclient.decorators;
+
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import org.apache.flink.kubernetes.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerTestBase;
+import org.apache.flink.kubernetes.utils.Constants;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.KeyToPathBuilder;
+import io.fabric8.kubernetes.api.model.Volume;
+import io.fabric8.kubernetes.api.model.VolumeBuilder;
+import io.fabric8.kubernetes.api.model.VolumeMount;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+/**
+ * General tests for the {@link KerberosMountDecoratorTest}.
+ */
+public class KerberosMountDecoratorTest extends KubernetesJobManagerTestBase {

Review comment:
       Why is this class extends `KubernetesJobManagerTestBase`? It seems this test class can extends either `KubernetesJobManagerTestBase` or `KubernetesTaskManagerTestBase`, but cannot extends `KubernetesTestBase`. I think this is an indicator of bad abstraction. We may have another common base class for jobmanager/taskmanager test bases.

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/KerberosMountDecorator.java
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.kubeclient.decorators;
+
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import org.apache.flink.kubernetes.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+import org.apache.flink.kubernetes.utils.Constants;
+import org.apache.flink.runtime.security.SecurityConfiguration;
+import org.apache.flink.util.StringUtils;
+
+import org.apache.flink.shaded.guava18.com.google.common.io.Files;
+
+import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.KeyToPathBuilder;
+import io.fabric8.kubernetes.api.model.PodBuilder;
+import io.fabric8.kubernetes.api.model.SecretBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * Mount the custom Kerberos Configuration and Credential to the JobManager(s)/TaskManagers.
+ */
+public class KerberosMountDecorator extends AbstractKubernetesStepDecorator {
+	private static final Logger LOG = LoggerFactory.getLogger(KerberosMountDecorator.class);
+
+	private final AbstractKubernetesParameters kubernetesParameters;
+	private final SecurityConfiguration securityConfig;
+
+	public KerberosMountDecorator(AbstractKubernetesParameters kubernetesParameters) {
+		this.kubernetesParameters = checkNotNull(kubernetesParameters);
+		this.securityConfig = new SecurityConfiguration(kubernetesParameters.getFlinkConfiguration());
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		PodBuilder podBuilder = new PodBuilder(flinkPod.getPod());
+		ContainerBuilder containerBuilder = new ContainerBuilder(flinkPod.getMainContainer());
+
+		if (!StringUtils.isNullOrWhitespaceOnly(securityConfig.getKeytab()) && !StringUtils.isNullOrWhitespaceOnly(securityConfig.getPrincipal())) {
+			final File keytab = new File(securityConfig.getKeytab());
+			podBuilder = podBuilder
+				.editOrNewSpec()
+					.addNewVolume()
+						.withName(Constants.KERBEROS_KEYTAB_VOLUME)
+						.withNewSecret()
+							.withSecretName(getKerberosKeytabSecretName(kubernetesParameters.getClusterId()))
+							.endSecret()
+					.endVolume()
+				.endSpec();
+
+			containerBuilder = containerBuilder
+				.addNewVolumeMount()
+					.withName(Constants.KERBEROS_KEYTAB_VOLUME)
+					.withMountPath(Constants.KERBEROS_KEYTAB_MOUNT_POINT)
+				.endVolumeMount()
+				.addNewEnv()
+					.withName(Constants.ENV_KERBEROS_KEYTAB_PATH)
+					.withValue(String.format("%s/%s", Constants.KERBEROS_KEYTAB_MOUNT_POINT, keytab.getName()))
+				.endEnv();
+		}
+
+		if (!StringUtils.isNullOrWhitespaceOnly(kubernetesParameters.getFlinkConfiguration().get(KubernetesConfigOptions.KERBEROS_KRB5_PATH))) {
+			final File krb5Conf = new File(kubernetesParameters.getFlinkConfiguration().get(KubernetesConfigOptions.KERBEROS_KRB5_PATH));
+			podBuilder = podBuilder
+				.editOrNewSpec()
+				.addNewVolume()
+					.withName(Constants.KERBEROS_KRB5CONF_VOLUME)
+					.withNewConfigMap()
+					.withName(getKerberosKrb5confConfigMapName(kubernetesParameters.getClusterId()))
+					.withItems(new KeyToPathBuilder()
+						.withKey(krb5Conf.getName())
+						.withPath(krb5Conf.getName())
+						.build())
+					.endConfigMap()

Review comment:
       minor: indention

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/KerberosMountDecorator.java
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.kubeclient.decorators;
+
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import org.apache.flink.kubernetes.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+import org.apache.flink.kubernetes.utils.Constants;
+import org.apache.flink.runtime.security.SecurityConfiguration;
+import org.apache.flink.util.StringUtils;
+
+import org.apache.flink.shaded.guava18.com.google.common.io.Files;
+
+import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.KeyToPathBuilder;
+import io.fabric8.kubernetes.api.model.PodBuilder;
+import io.fabric8.kubernetes.api.model.SecretBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * Mount the custom Kerberos Configuration and Credential to the JobManager(s)/TaskManagers.
+ */
+public class KerberosMountDecorator extends AbstractKubernetesStepDecorator {
+	private static final Logger LOG = LoggerFactory.getLogger(KerberosMountDecorator.class);
+
+	private final AbstractKubernetesParameters kubernetesParameters;
+	private final SecurityConfiguration securityConfig;
+
+	public KerberosMountDecorator(AbstractKubernetesParameters kubernetesParameters) {
+		this.kubernetesParameters = checkNotNull(kubernetesParameters);
+		this.securityConfig = new SecurityConfiguration(kubernetesParameters.getFlinkConfiguration());
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		PodBuilder podBuilder = new PodBuilder(flinkPod.getPod());
+		ContainerBuilder containerBuilder = new ContainerBuilder(flinkPod.getMainContainer());
+
+		if (!StringUtils.isNullOrWhitespaceOnly(securityConfig.getKeytab()) && !StringUtils.isNullOrWhitespaceOnly(securityConfig.getPrincipal())) {
+			final File keytab = new File(securityConfig.getKeytab());
+			podBuilder = podBuilder
+				.editOrNewSpec()
+					.addNewVolume()
+						.withName(Constants.KERBEROS_KEYTAB_VOLUME)
+						.withNewSecret()
+							.withSecretName(getKerberosKeytabSecretName(kubernetesParameters.getClusterId()))
+							.endSecret()
+					.endVolume()
+				.endSpec();
+
+			containerBuilder = containerBuilder
+				.addNewVolumeMount()
+					.withName(Constants.KERBEROS_KEYTAB_VOLUME)
+					.withMountPath(Constants.KERBEROS_KEYTAB_MOUNT_POINT)
+				.endVolumeMount()
+				.addNewEnv()
+					.withName(Constants.ENV_KERBEROS_KEYTAB_PATH)
+					.withValue(String.format("%s/%s", Constants.KERBEROS_KEYTAB_MOUNT_POINT, keytab.getName()))

Review comment:
       Instead of decorating the keytab path as an environment variable, I think we can write it to the configuration directly.
   See `FlinkConfMountDecorator#buildAccompanyingKubernetesResources` for an example.

##########
File path: flink-kubernetes/src/test/java/org/apache/flink/kubernetes/kubeclient/decorators/KerberosMountDecoratorTest.java
##########
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.kubernetes.kubeclient.decorators;
+
+import org.apache.flink.configuration.SecurityOptions;
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import org.apache.flink.kubernetes.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerTestBase;
+import org.apache.flink.kubernetes.utils.Constants;
+
+import io.fabric8.kubernetes.api.model.ConfigMap;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.HasMetadata;
+import io.fabric8.kubernetes.api.model.KeyToPathBuilder;
+import io.fabric8.kubernetes.api.model.Volume;
+import io.fabric8.kubernetes.api.model.VolumeBuilder;
+import io.fabric8.kubernetes.api.model.VolumeMount;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+/**
+ * General tests for the {@link KerberosMountDecoratorTest}.
+ */
+public class KerberosMountDecoratorTest extends KubernetesJobManagerTestBase {
+
+	private KerberosMountDecorator kerberosMountDecorator;
+
+	@Override
+	protected void setupFlinkConfig() {
+		super.setupFlinkConfig();
+
+		flinkConfig.set(SecurityOptions.KERBEROS_LOGIN_KEYTAB, kerberosDir.toString() + "/" + KEYTAB_FILE);
+		flinkConfig.set(SecurityOptions.KERBEROS_LOGIN_PRINCIPAL, "test");
+		flinkConfig.set(KubernetesConfigOptions.KERBEROS_KRB5_PATH, kerberosDir.toString() + "/" + KRB5_CONF_FILE);
+	}
+
+	@Override
+	protected void onSetup() throws Exception {
+		super.onSetup();
+
+		generateKerberosFileItems();
+
+		this.kerberosMountDecorator = new KerberosMountDecorator(kubernetesJobManagerParameters);
+	}
+
+	@Test
+	public void testWhetherPodOrContainerIsDecorated() {
+		final FlinkPod resultFlinkPod = kerberosMountDecorator.decorateFlinkPod(baseFlinkPod);
+		assertNotEquals(baseFlinkPod.getPod(), resultFlinkPod.getPod());
+		assertNotEquals(baseFlinkPod.getMainContainer(), resultFlinkPod.getMainContainer());
+	}
+
+	@Test
+	public void testConfigMap() throws IOException {
+		final List<HasMetadata> additionalResources = kerberosMountDecorator.buildAccompanyingKubernetesResources();
+		assertEquals(2, additionalResources.size());
+
+		final ConfigMap resultConfigMap = (ConfigMap) additionalResources
+			.stream()
+			.filter(x -> x instanceof ConfigMap)
+			.collect(Collectors.toList()).get(0);
+
+		assertEquals(Constants.API_VERSION, resultConfigMap.getApiVersion());
+
+		assertEquals(KerberosMountDecorator.getKerberosKrb5confConfigMapName(CLUSTER_ID),
+			resultConfigMap.getMetadata().getName());
+
+		Map<String, String> resultDatas = resultConfigMap.getData();
+		assertThat(resultDatas.size(), is(1));
+		assertEquals("some conf", resultDatas.get(KRB5_CONF_FILE));
+	}
+
+	@Test
+	public void testDecoratedFlinkPod() {
+		final FlinkPod resultFlinkPod = kerberosMountDecorator.decorateFlinkPod(baseFlinkPod);
+		assertEquals(2, resultFlinkPod.getPod().getSpec().getVolumes().size());
+
+		final List<Volume> expectedVolumes = Arrays.asList(
+			new VolumeBuilder()
+				.withName(Constants.KERBEROS_KEYTAB_VOLUME)
+				.withNewSecret()
+				.withSecretName(KerberosMountDecorator.getKerberosKeytabSecretName(CLUSTER_ID))
+				.endSecret()
+				.build(),
+			new VolumeBuilder()
+				.withName(Constants.KERBEROS_KRB5CONF_VOLUME)
+				.withNewConfigMap()
+				.withName(KerberosMountDecorator.getKerberosKrb5confConfigMapName(CLUSTER_ID))
+				.withItems(new KeyToPathBuilder()
+					.withKey(KRB5_CONF_FILE)
+					.withPath(KRB5_CONF_FILE)
+					.build())
+				.endConfigMap()
+				.build());
+		assertEquals(expectedVolumes, resultFlinkPod.getPod().getSpec().getVolumes());
+	}

Review comment:
       I think this test case is not needed. It is already covered by `testDecoratedFlinkContainer`. Despite it is required to mount the volumes on the pods before mounting them on the containers, from the contract we only cares about accessing the resources in container.




----------------------------------------------------------------
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:
users@infra.apache.org