Skip to content

feat: allow loading resources as builders #1335

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

Merged
merged 1 commit into from
Jul 14, 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
@@ -0,0 +1,35 @@

package io.javaoperatorsdk.operator;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class BuilderUtils {

// prevent instantiation of util class
private BuilderUtils() {}

public static final <T, B> B newBuilder(Class<B> builderType, T item) {
Class<T> builderTargetType = builderTargetType(builderType);
try {
Constructor<B> constructor = builderType.getDeclaredConstructor(builderTargetType);
return constructor.newInstance(item);
} catch (NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new OperatorException(
"Failied to instantiate builder: " + builderType.getCanonicalName() + " using: " + item,
e);
}
}

public static final <T, B> Class<T> builderTargetType(Class<B> builderType) {
try {
Method method = builderType.getDeclaredMethod("build");
return (Class<T>) method.getReturnType();
} catch (NoSuchMethodException | SecurityException e) {
throw new OperatorException(
"Failied to determine target type for builder: " + builderType.getCanonicalName(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.builder.Builder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.Serialization;
Expand Down Expand Up @@ -114,6 +115,10 @@ public static Object setSpec(HasMetadata resource, Object spec) {

public static <T> T loadYaml(Class<T> clazz, Class loader, String yaml) {
try (InputStream is = loader.getResourceAsStream(yaml)) {
if (Builder.class.isAssignableFrom(clazz)) {
return BuilderUtils.newBuilder(clazz,
Serialization.unmarshal(is, BuilderUtils.builderTargetType(clazz)));
}
return Serialization.unmarshal(is, clazz);
} catch (IOException ex) {
throw new IllegalStateException("Cannot find yaml on classpath: " + yaml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.PodTemplateSpec;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpec;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
Expand Down Expand Up @@ -88,6 +90,16 @@ void setsSpecWithReflection() {
assertThat(deployment.getSpec().getReplicas()).isEqualTo(1);
}

@Test
void loadYamlAsBuilder() {
DeploymentBuilder builder =
ReconcilerUtils.loadYaml(DeploymentBuilder.class, getClass(), "deployment.yaml");
builder.accept(ContainerBuilder.class, c -> c.withImage("my-image"));

Deployment deployment = builder.editMetadata().withName("my-deployment").and().build();
assertThat(deployment.getMetadata().getName()).isEqualTo("my-deployment");
}

private Deployment createTestDeployment() {
Deployment deployment = new Deployment();
deployment.setSpec(new DeploymentSpec());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: ""
spec:
progressDeadlineSeconds: 600
revisionHistoryLimit: 10
selector:
matchLabels:
app: "test"
replicas: 1
template:
metadata:
labels:
app: "test"
spec:
containers:
- name: nginx
image: nginx:1.17.0
ports:
- containerPort: 80