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/08/12 13:55:20 UTC

[GitHub] [flink] wangyang0918 commented on a change in pull request #12899: [FLINK-16699][k8s] Support accessing secured services via K8s secrets

wangyang0918 commented on a change in pull request #12899:
URL: https://github.com/apache/flink/pull/12899#discussion_r469246970



##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/configuration/KubernetesConfigOptions.java
##########
@@ -221,6 +221,22 @@
 	/** Defines the configuration key of that external resource in Kubernetes. This is used as a suffix in an actual config. */
 	public static final String EXTERNAL_RESOURCE_KUBERNETES_CONFIG_KEY_SUFFIX = "kubernetes.config-key";
 
+	public static final ConfigOption<Map<String, String>> KUBERNETES_SECRETS =
+		key("kubernetes.secrets")
+			.mapType()
+			.noDefaultValue()
+			.withDescription("The user-specified secrets that are mounted into Flink container. The value should be in " +

Review comment:
       Could we update the description to the following?
   The user-specified secrets that will be mounted into Flink container. The value should be in the form of foo:/opt/secrets-foo,bar:/opt/secrets-bar.
   
   Users could also specify this config option via `-D` cli option.

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/MountSecretsDecorator.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodBuilder;
+import io.fabric8.kubernetes.api.model.Volume;
+import io.fabric8.kubernetes.api.model.VolumeBuilder;
+import io.fabric8.kubernetes.api.model.VolumeMount;
+import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * support mounting Secrets on the JobManager or TaskManager pod..
+ */
+public class MountSecretsDecorator extends AbstractKubernetesStepDecorator {
+
+	private final AbstractKubernetesParameters kubernetesComponentConf;
+
+	public MountSecretsDecorator(AbstractKubernetesParameters kubernetesComponentConf) {
+		this.kubernetesComponentConf = checkNotNull(kubernetesComponentConf);
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		final Pod podWithMount = decoratePod(flinkPod.getPod());
+		final Container containerWithMount = decorateMainContainer(flinkPod.getMainContainer());
+
+		return new FlinkPod.Builder(flinkPod)
+			.withPod(podWithMount)
+			.withMainContainer(containerWithMount)
+			.build();
+	}
+
+	private Container decorateMainContainer(Container container) {
+		VolumeMount[] volumeMounts = kubernetesComponentConf
+			.getSecretNamesToMountPaths()
+			.entrySet()
+			.stream()
+			.map(secretNameToPath ->
+				new VolumeMountBuilder()
+					.withName(secretVolumeName(secretNameToPath.getKey()))
+					.withMountPath(secretNameToPath.getValue())
+					.build()
+			).toArray(VolumeMount[]::new);
+
+		return new ContainerBuilder(container)
+			.addToVolumeMounts(volumeMounts)
+			.build();
+	}
+
+	private Pod decoratePod(Pod pod) {
+		Volume[] volumes = kubernetesComponentConf.getSecretNamesToMountPaths()

Review comment:
       ```suggestion
   		final Volume[] volumes = kubernetesComponentConf.getSecretNamesToMountPaths()
   ```

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/configuration/KubernetesConfigOptions.java
##########
@@ -221,6 +221,22 @@
 	/** Defines the configuration key of that external resource in Kubernetes. This is used as a suffix in an actual config. */
 	public static final String EXTERNAL_RESOURCE_KUBERNETES_CONFIG_KEY_SUFFIX = "kubernetes.config-key";
 
+	public static final ConfigOption<Map<String, String>> KUBERNETES_SECRETS =
+		key("kubernetes.secrets")
+			.mapType()
+			.noDefaultValue()
+			.withDescription("The user-specified secrets that are mounted into Flink container. The value should be in " +
+				"the form of a1:v1,a2:v2. For example for mounting secrets foo to path /opt/secrets , just need to set " +
+				"kubernetes.secrets: foo:/opt/secrets in the flink-conf.yaml");
+
+	public static final ConfigOption<List<Map<String, String>>> KUBERNETES_ENV_SECRET_KEY_REF =
+		key("kubernetes.env.secretKeyRef")
+			.mapType()
+			.asList()
+			.noDefaultValue()
+			.withDescription("the user-specified secrets to set env variable in Flink container. The value should be in " +
+				"the form of env:MY_ENV1,secret:my_secret1,key:my_key1;env:MY_ENV2,secret:my_secret2,key:my_key2");

Review comment:
       env:MY_ENV1,secret:my_secret1,key:my_key1;env:MY_ENV2,secret:my_secret2,key:my_key2
   
   I prefer not using **my** in the description. So it could be like following.
   env:FOO_ENV,secret:foo_secret,key:key;env:BAR_ENV,secret:bar_secret,key:key

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/EnvSecretsDecorator.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesEnvVar;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.EnvVar;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * support setting environment variables via Secrets.
+ */
+public class EnvSecretsDecorator extends AbstractKubernetesStepDecorator {
+
+	private final AbstractKubernetesParameters kubernetesComponentConf;
+
+	public EnvSecretsDecorator(AbstractKubernetesParameters kubernetesComponentConf) {
+		this.kubernetesComponentConf = checkNotNull(kubernetesComponentConf);
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		final Container basicMainContainer = decorateMainContainer(flinkPod.getMainContainer());

Review comment:
       ```suggestion
   		final Container decoratedContainer = new ContainerBuilder(flinkPod.getMainContainer())
   			.addAllToEnv(getSecretEnvs())
   			.build();
   ```

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/configuration/KubernetesConfigOptions.java
##########
@@ -221,6 +221,22 @@
 	/** Defines the configuration key of that external resource in Kubernetes. This is used as a suffix in an actual config. */
 	public static final String EXTERNAL_RESOURCE_KUBERNETES_CONFIG_KEY_SUFFIX = "kubernetes.config-key";
 
+	public static final ConfigOption<Map<String, String>> KUBERNETES_SECRETS =
+		key("kubernetes.secrets")
+			.mapType()
+			.noDefaultValue()
+			.withDescription("The user-specified secrets that are mounted into Flink container. The value should be in " +
+				"the form of a1:v1,a2:v2. For example for mounting secrets foo to path /opt/secrets , just need to set " +
+				"kubernetes.secrets: foo:/opt/secrets in the flink-conf.yaml");
+
+	public static final ConfigOption<List<Map<String, String>>> KUBERNETES_ENV_SECRET_KEY_REF =
+		key("kubernetes.env.secretKeyRef")
+			.mapType()
+			.asList()
+			.noDefaultValue()
+			.withDescription("the user-specified secrets to set env variable in Flink container. The value should be in " +

Review comment:
       ```suggestion
   			.withDescription("The user-specified secrets to set env variables in Flink container. The value should be in " +
   ```

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/MountSecretsDecorator.java
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodBuilder;
+import io.fabric8.kubernetes.api.model.Volume;
+import io.fabric8.kubernetes.api.model.VolumeBuilder;
+import io.fabric8.kubernetes.api.model.VolumeMount;
+import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * support mounting Secrets on the JobManager or TaskManager pod..
+ */
+public class MountSecretsDecorator extends AbstractKubernetesStepDecorator {
+
+	private final AbstractKubernetesParameters kubernetesComponentConf;
+
+	public MountSecretsDecorator(AbstractKubernetesParameters kubernetesComponentConf) {
+		this.kubernetesComponentConf = checkNotNull(kubernetesComponentConf);
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		final Pod podWithMount = decoratePod(flinkPod.getPod());
+		final Container containerWithMount = decorateMainContainer(flinkPod.getMainContainer());
+
+		return new FlinkPod.Builder(flinkPod)
+			.withPod(podWithMount)
+			.withMainContainer(containerWithMount)
+			.build();
+	}
+
+	private Container decorateMainContainer(Container container) {
+		VolumeMount[] volumeMounts = kubernetesComponentConf

Review comment:
       ```suggestion
   		final VolumeMount[] volumeMounts = kubernetesComponentConf
   ```

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/resources/KubernetesEnvVar.java
##########
@@ -0,0 +1,50 @@
+/*
+ * 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.resources;
+
+import io.fabric8.kubernetes.api.model.EnvVar;
+import io.fabric8.kubernetes.api.model.EnvVarBuilder;
+
+import java.util.Map;
+
+/**
+ * Represent EnvVar resource in kubernetes.
+ */
+public class KubernetesEnvVar extends KubernetesResource<EnvVar> {
+
+	private static final String ENV = "env";
+	private static final String SECRET = "secret";
+	private static final String KEY = "key";
+
+	private KubernetesEnvVar(EnvVar envVar) {
+		super(envVar);
+	}
+
+	public static KubernetesEnvVar fromMap(Map<String, String> stringMap) {

Review comment:
       Maybe this class should be `KubernetesSecretEnvVar` since we could only support secret key reference.

##########
File path: flink-kubernetes/src/main/java/org/apache/flink/kubernetes/kubeclient/decorators/EnvSecretsDecorator.java
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.kubeclient.FlinkPod;
+import org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesEnvVar;
+
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerBuilder;
+import io.fabric8.kubernetes.api.model.EnvVar;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * support setting environment variables via Secrets.
+ */
+public class EnvSecretsDecorator extends AbstractKubernetesStepDecorator {
+
+	private final AbstractKubernetesParameters kubernetesComponentConf;
+
+	public EnvSecretsDecorator(AbstractKubernetesParameters kubernetesComponentConf) {
+		this.kubernetesComponentConf = checkNotNull(kubernetesComponentConf);
+	}
+
+	@Override
+	public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
+		final Container basicMainContainer = decorateMainContainer(flinkPod.getMainContainer());
+		return new FlinkPod.Builder(flinkPod)
+			.withPod(flinkPod.getPod())
+			.withMainContainer(basicMainContainer)
+			.build();
+	}
+
+	private Container decorateMainContainer(Container container) {

Review comment:
       ```suggestion
   ```




----------------------------------------------------------------
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