-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extracts Kubernetes related classes
- Loading branch information
Showing
9 changed files
with
218 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/io/github/m4gshm/testcontainers/PodBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package io.github.m4gshm.testcontainers; | ||
|
||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.ContainerPort; | ||
import io.fabric8.kubernetes.api.model.ContainerPortBuilder; | ||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.fabric8.kubernetes.api.model.PodBuilder; | ||
import io.fabric8.kubernetes.api.model.PodSecurityContextBuilder; | ||
import io.fabric8.kubernetes.api.model.PodSpecBuilder; | ||
import io.fabric8.kubernetes.api.model.SecurityContextBuilder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.UnaryOperator; | ||
|
||
@Getter | ||
@Setter | ||
public class PodBuilderFactory { | ||
private final Map<String, String> labels = new HashMap<>(); | ||
private final Map<Integer, Integer> hostPorts = new HashMap<>(); | ||
|
||
private String dockerImageName; | ||
private Boolean runAsNonRoot; | ||
private Long runAsUser; | ||
private Long runAsGroup; | ||
private Long fsGroup; | ||
private boolean privilegedMode; | ||
private boolean allowPrivilegeEscalation; | ||
private String imagePullPolicy = "Always"; | ||
private String imagePullSecretName; | ||
private UnaryOperator<PodBuilder> podBuilderCustomizer; | ||
private UnaryOperator<ContainerBuilder> containerBuilderCustomizer; | ||
private String podContainerName = "main"; | ||
private List<String> entryPoint; | ||
private String portProtocol = "TCP"; | ||
private List<EnvVar> vars; | ||
private String[] args; | ||
private List<Integer> exposedPorts = new ArrayList<>(); | ||
private boolean hostPortEnabled = false; | ||
|
||
public PodBuilder newPodBuilder() { | ||
var containerBuilder = new ContainerBuilder() | ||
.withImagePullPolicy(getImagePullPolicy()) | ||
.withImage(getDockerImageName()) | ||
.withSecurityContext(new SecurityContextBuilder() | ||
.withRunAsNonRoot(getRunAsNonRoot()) | ||
.withRunAsUser(getRunAsUser()) | ||
.withRunAsGroup(getRunAsGroup()) | ||
.withPrivileged(isPrivilegedMode()) | ||
.withAllowPrivilegeEscalation(isAllowPrivilegeEscalation()) | ||
.build()) | ||
.withName(getPodContainerName()) | ||
.withArgs(getArgs()) | ||
.withCommand(getEntryPoint()) | ||
.withPorts(getContainerPorts()) | ||
.withEnv(getVars()); | ||
if (getContainerBuilderCustomizer() != null) { | ||
containerBuilder = getContainerBuilderCustomizer().apply(containerBuilder); | ||
} | ||
var podBuilder = new PodBuilder() | ||
.withMetadata(new ObjectMetaBuilder() | ||
.addToLabels(getLabels()) | ||
.addToLabels(Map.of( | ||
PodEngine.ORG_TESTCONTAINERS_TYPE, PodEngine.KUBECONTAINERS | ||
)) | ||
.build()) | ||
.withSpec(new PodSpecBuilder() | ||
.withSecurityContext(new PodSecurityContextBuilder() | ||
.withRunAsNonRoot(getRunAsNonRoot()) | ||
.withRunAsUser(getRunAsUser()) | ||
.withFsGroup(getFsGroup()) | ||
.build()) | ||
.withImagePullSecrets(new LocalObjectReferenceBuilder() | ||
.withName(getImagePullSecretName()) | ||
.build()) | ||
.withContainers(containerBuilder.build()) | ||
.build()); | ||
|
||
if (getPodBuilderCustomizer() != null) { | ||
podBuilder = getPodBuilderCustomizer().apply(podBuilder); | ||
} | ||
return podBuilder; | ||
} | ||
|
||
@NotNull | ||
protected List<ContainerPort> getContainerPorts() { | ||
return exposedPorts.stream().map(port -> { | ||
var portBuilder = new ContainerPortBuilder() | ||
.withContainerPort(port) | ||
.withProtocol(getPortProtocol()); | ||
if (isHostPortEnabled()) { | ||
portBuilder.withHostPort(getHostPorts().get(port)); | ||
} | ||
return portBuilder.build(); | ||
}).toList(); | ||
} | ||
|
||
public void addHostPort(Integer port, Integer hostPort) { | ||
getHostPorts().put(port, hostPort); | ||
} | ||
|
||
} |
Oops, something went wrong.