Skip to content

Commit c6769c7

Browse files
[FLINK-39508][flink-kubernetes-webhook] Fix flink-kubernetes-webhook module tests
1 parent c63cd67 commit c6769c7

13 files changed

Lines changed: 1689 additions & 23 deletions

File tree

flink-kubernetes-webhook/pom.xml

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ under the License.
3232
<packaging>jar</packaging>
3333

3434
<properties>
35-
<surefire-plugin.version>2.22.2</surefire-plugin.version>
35+
<plugins.tmp.dir>${project.build.directory}/plugins</plugins.tmp.dir>
36+
<surefire.module.config>
37+
<!-- required by FlinkOperatorWebhookTest -->
38+
--add-opens=java.base/java.util=ALL-UNNAMED
39+
</surefire.module.config>
3640
</properties>
3741

38-
3942
<dependencies>
4043
<dependency>
4144
<groupId>org.apache.flink</groupId>
@@ -84,11 +87,37 @@ under the License.
8487
<version>${okhttp.version}</version>
8588
<scope>test</scope>
8689
</dependency>
90+
91+
<dependency>
92+
<groupId>org.junit.jupiter</groupId>
93+
<artifactId>junit-jupiter-params</artifactId>
94+
<scope>test</scope>
95+
</dependency>
8796
</dependencies>
8897

8998
<build>
9099

91100
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-assembly-plugin</artifactId>
104+
<executions>
105+
<execution>
106+
<id>create-test-plugin-jar</id>
107+
<phase>process-test-classes</phase>
108+
<goals>
109+
<goal>single</goal>
110+
</goals>
111+
<configuration>
112+
<finalName>test-plugins</finalName>
113+
<attach>false</attach>
114+
<descriptors>
115+
<descriptor>src/test/assembly/test-plugins-assembly.xml</descriptor>
116+
</descriptors>
117+
</configuration>
118+
</execution>
119+
</executions>
120+
</plugin>
92121
<plugin>
93122
<groupId>org.apache.maven.plugins</groupId>
94123
<artifactId>maven-shade-plugin</artifactId>
@@ -116,11 +145,6 @@ under the License.
116145
</execution>
117146
</executions>
118147
</plugin>
119-
<plugin>
120-
<groupId>org.apache.maven.plugins</groupId>
121-
<artifactId>maven-surefire-plugin</artifactId>
122-
<version>${surefire-plugin.version}</version>
123-
</plugin>
124148
</plugins>
125149
</build>
126150
</project>

flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/FlinkOperatorWebhook.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.flink.kubernetes.operator.admission;
1919

20+
import org.apache.flink.annotation.VisibleForTesting;
2021
import org.apache.flink.kubernetes.operator.admission.informer.InformerManager;
2122
import org.apache.flink.kubernetes.operator.admission.mutator.FlinkMutator;
2223
import org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot;
@@ -45,6 +46,8 @@
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
4748

49+
import javax.annotation.Nullable;
50+
4851
import java.net.InetAddress;
4952
import java.net.InetSocketAddress;
5053
import java.nio.file.Path;
@@ -58,26 +61,38 @@ public class FlinkOperatorWebhook {
5861

5962
private static FileSystemWatchService fileSystemWatchService;
6063

61-
public static void main(String[] args) throws Exception {
62-
EnvUtils.logEnvironmentInfo(LOG, "Flink Kubernetes Webhook", args);
63-
var informerManager = new InformerManager(new KubernetesClientBuilder().build());
64-
var configManager =
65-
new FlinkConfigManager(
66-
informerManager::setNamespaces,
67-
KubernetesClientUtils.isCrdInstalled(FlinkStateSnapshot.class));
64+
@VisibleForTesting final Set<FlinkResourceValidator> validators;
65+
@VisibleForTesting final Set<FlinkResourceMutator> mutators;
66+
@VisibleForTesting final AdmissionHandler admissionHandler;
67+
68+
@VisibleForTesting
69+
FlinkOperatorWebhook(
70+
@Nullable InformerManager informerManager, @Nullable FlinkConfigManager configManager) {
71+
if (informerManager == null) {
72+
informerManager = new InformerManager(new KubernetesClientBuilder().build());
73+
}
74+
if (configManager == null) {
75+
configManager =
76+
new FlinkConfigManager(
77+
informerManager::setNamespaces,
78+
KubernetesClientUtils.isCrdInstalled(FlinkStateSnapshot.class));
79+
}
80+
6881
var operatorConfig = configManager.getOperatorConfiguration();
6982
if (!operatorConfig.isDynamicNamespacesEnabled()) {
7083
informerManager.setNamespaces(operatorConfig.getWatchedNamespaces());
7184
}
72-
Set<FlinkResourceValidator> validators = ValidatorUtils.discoverValidators(configManager);
73-
Set<FlinkResourceMutator> mutators = MutatorUtils.discoverMutators(configManager);
7485

75-
AdmissionHandler endpoint =
86+
this.validators = ValidatorUtils.discoverValidators(configManager);
87+
this.mutators = MutatorUtils.discoverMutators(configManager);
88+
this.admissionHandler =
7689
new AdmissionHandler(
7790
new FlinkValidator(validators, informerManager),
7891
new FlinkMutator(mutators, informerManager));
92+
}
7993

80-
ChannelInitializer<SocketChannel> initializer = createChannelInitializer(endpoint);
94+
public void run() throws Exception {
95+
ChannelInitializer<SocketChannel> initializer = createChannelInitializer(admissionHandler);
8196
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
8297
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
8398
try {
@@ -103,12 +118,18 @@ public static void main(String[] args) throws Exception {
103118
}
104119
}
105120

121+
public static void main(String[] args) throws Exception {
122+
EnvUtils.logEnvironmentInfo(LOG, "Flink Kubernetes Webhook", args);
123+
new FlinkOperatorWebhook(null, null).run();
124+
}
125+
106126
private static int getPort() {
107127
String portString = EnvUtils.getRequired(EnvUtils.ENV_WEBHOOK_SERVER_PORT);
108128
return Integer.parseInt(portString);
109129
}
110130

111-
private static ChannelInitializer<SocketChannel> createChannelInitializer(
131+
@VisibleForTesting
132+
static ChannelInitializer<SocketChannel> createChannelInitializer(
112133
AdmissionHandler admissionHandler) throws Exception {
113134
SslContext sslContext = createSslContext();
114135

@@ -150,7 +171,7 @@ private static SslContext createSslContext() throws Exception {
150171
stopFileSystemWatchService();
151172
final String realKeystoreFileName =
152173
Path.of(keystorePathOpt.get()).toRealPath().getFileName().toString();
153-
LOG.info("Keystore path is resolved to real filename: " + realKeystoreFileName);
174+
LOG.info("Keystore path is resolved to real filename: {}", realKeystoreFileName);
154175
fileSystemWatchService =
155176
new FileSystemWatchService(Path.of(keystorePathOpt.get()).getParent().toString()) {
156177
@Override
@@ -160,7 +181,8 @@ protected void onFileOrDirectoryModified(Path relativePath) {
160181
reloadableSslContext.reload();
161182
LOG.info("SSL context reloaded successfully");
162183
} catch (Exception e) {
163-
LOG.error("SSL context reload received exception: " + e);
184+
LOG.error(
185+
"SSL context reload received exception: {}", String.valueOf(e));
164186
}
165187
}
166188
};

flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/mutator/DefaultRequestMutator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public AdmissionResponse handle(AdmissionRequest admissionRequest) {
7474
return admissionResponse;
7575
}
7676

77-
public static AdmissionResponse admissionResponseFromMutation(
77+
private static AdmissionResponse admissionResponseFromMutation(
7878
KubernetesResource originalResource, KubernetesResource mutatedResource) {
7979
AdmissionResponse admissionResponse = new AdmissionResponse();
8080
admissionResponse.setAllowed(true);

flink-kubernetes-webhook/src/test/assembly/test-plugins-assembly.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ under the License.
2828
<outputDirectory>/</outputDirectory>
2929
<!-- the service impl -->
3030
<includes>
31-
<include>org/apache/flink/kubernetes/operator/admission/TestMutator.java</include>
31+
<include>org/apache/flink/kubernetes/operator/mutator/TestMutator.class</include>
32+
<include>org/apache/flink/kubernetes/operator/validation/TestValidator.class</include>
3233
</includes>
3334
</fileSet>
3435
<fileSet>

0 commit comments

Comments
 (0)