Skip to content

Commit

Permalink
refactor: code style
Browse files Browse the repository at this point in the history
Signed-off-by: xstefank <[email protected]>
  • Loading branch information
xstefank committed Feb 27, 2025
1 parent c00d897 commit 805f676
Show file tree
Hide file tree
Showing 656 changed files with 9,574 additions and 7,931 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class Bootstrapper {
private static final Map<String, String> TOP_LEVEL_STATIC_FILES =
Map.of("_.gitignore", ".gitignore", "README.md", "README.md");
private static final List<String> JAVA_FILES =
List.of("CustomResource.java", "Reconciler.java",
"Spec.java", "Status.java");
List.of("CustomResource.java", "Reconciler.java", "Spec.java", "Status.java");

public void create(File targetDir, String groupId, String artifactId) {
try {
Expand Down Expand Up @@ -61,42 +60,62 @@ private void addJavaFiles(File projectDir, String groupId, String artifactId) {
var targetTestDir = new File(projectDir, "src/test/java/" + packages);
FileUtils.forceMkdir(targetDir);
var classFileNamePrefix = artifactClassId(artifactId);
JAVA_FILES.forEach(f -> addTemplatedFile(projectDir, f, groupId, artifactId, targetDir,
classFileNamePrefix + f));
JAVA_FILES.forEach(
f ->
addTemplatedFile(
projectDir, f, groupId, artifactId, targetDir, classFileNamePrefix + f));

addTemplatedFile(projectDir, "Runner.java", groupId, artifactId, targetDir, null);
addTemplatedFile(projectDir, "ConfigMapDependentResource.java", groupId, artifactId,
targetDir, null);
addTemplatedFile(projectDir, "ReconcilerIntegrationTest.java", groupId,
addTemplatedFile(
projectDir, "ConfigMapDependentResource.java", groupId, artifactId, targetDir, null);
addTemplatedFile(
projectDir,
"ReconcilerIntegrationTest.java",
groupId,
artifactId,
targetTestDir, artifactClassId(artifactId) + "ReconcilerIntegrationTest.java");
targetTestDir,
artifactClassId(artifactId) + "ReconcilerIntegrationTest.java");
} catch (IOException e) {
throw new RuntimeException(e);
}

}

private void addTemplatedFiles(File projectDir, String groupId, String artifactId) {
addTemplatedFile(projectDir, "pom.xml", groupId, artifactId);
addTemplatedFile(projectDir, "k8s/test-resource.yaml", groupId, artifactId);
}

private void addTemplatedFile(File projectDir, String fileName, String groupId,
String artifactId) {
private void addTemplatedFile(
File projectDir, String fileName, String groupId, String artifactId) {
addTemplatedFile(projectDir, fileName, groupId, artifactId, null, null);
}

private void addTemplatedFile(File projectDir, String fileName, String groupId, String artifactId,
File targetDir, String targetFileName) {
private void addTemplatedFile(
File projectDir,
String fileName,
String groupId,
String artifactId,
File targetDir,
String targetFileName) {
try {
var values = Map.of("groupId", groupId, "artifactId", artifactId,
"artifactClassId", artifactClassId(artifactId),
"josdkVersion", Versions.JOSDK,
"fabric8Version", Versions.KUBERNETES_CLIENT);
var values =
Map.of(
"groupId",
groupId,
"artifactId",
artifactId,
"artifactClassId",
artifactClassId(artifactId),
"josdkVersion",
Versions.JOSDK,
"fabric8Version",
Versions.KUBERNETES_CLIENT);

var mustache = mustacheFactory.compile("templates/" + fileName);
var targetFile = new File(targetDir == null ? projectDir : targetDir,
targetFileName == null ? fileName : targetFileName);
var targetFile =
new File(
targetDir == null ? projectDir : targetDir,
targetFileName == null ? fileName : targetFileName);
FileUtils.forceMkdir(targetFile.getParentFile());
var writer = new FileWriter(targetFile);
mustache.execute(writer, values);
Expand All @@ -114,8 +133,8 @@ private void addStaticFile(File targetDir, String fileName, String targetFileNam
addStaticFile(targetDir, fileName, targetFileName, null);
}

private void addStaticFile(File targetDir, String fileName, String targetFilename,
String subDir) {
private void addStaticFile(
File targetDir, String fileName, String targetFilename, String subDir) {
String sourcePath = subDir == null ? "/static/" : "/static/" + subDir;
String path = sourcePath + fileName;
try (var is = Bootstrapper.class.getResourceAsStream(path)) {
Expand All @@ -127,14 +146,12 @@ private void addStaticFile(File targetDir, String fileName, String targetFilenam
} catch (IOException e) {
throw new RuntimeException("File path: " + path, e);
}

}

public static String artifactClassId(String artifactId) {
var parts = artifactId.split("-");
return Arrays.stream(parts).map(p -> p.substring(0, 1)
.toUpperCase() + p.substring(1))
return Arrays.stream(parts)
.map(p -> p.substring(0, 1).toUpperCase() + p.substring(1))
.collect(Collectors.joining(""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "create", requiresProject = false)
public class BootstrapperMojo
extends AbstractMojo {
public class BootstrapperMojo extends AbstractMojo {

@Parameter(defaultValue = "${projectGroupId}")
protected String projectGroupId;

@Parameter(defaultValue = "${projectArtifactId}")
protected String projectArtifactId;

public void execute()
throws MojoExecutionException {
public void execute() throws MojoExecutionException {
String userDir = System.getProperty("user.dir");
new Bootstrapper().create(new File(userDir), projectGroupId, projectArtifactId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ void copiesFilesToTarget() {

private void assertProjectCompiles() {
try {
var process = Runtime.getRuntime()
.exec(
"mvn clean install -f target/test-project/pom.xml -DskipTests -Dspotless.apply.skip");
var process =
Runtime.getRuntime()
.exec(
"mvn clean install -f target/test-project/pom.xml -DskipTests"
+ " -Dspotless.apply.skip");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(process.getInputStream()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.github.benmanes.caffeine.cache.Cache;

/**
* Caffeine cache wrapper to be used in a {@link BoundedItemStore}
*/
/** Caffeine cache wrapper to be used in a {@link BoundedItemStore} */
public class CaffeineBoundedCache<K, R> implements BoundedCache<K, R> {

private final Cache<K, R> cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import com.github.benmanes.caffeine.cache.Caffeine;

/**
* A factory for <a href="https://github.com/ben-manes/caffeine">Caffeine</a>-backed
* {@link BoundedItemStore}. The implementation uses a {@link CaffeineBoundedCache} to store
* resources and progressively evict them if they haven't been used in a while. The idea about
* A factory for <a href="https://github.com/ben-manes/caffeine">Caffeine</a>-backed {@link
* BoundedItemStore}. The implementation uses a {@link CaffeineBoundedCache} to store resources and
* progressively evict them if they haven't been used in a while. The idea about
* CaffeinBoundedItemStore-s is that, caffeine will cache the resources which were recently used,
* and will evict resource, which are not used for a while. This is ideal for startup performance
* and efficiency when all resources should be cached to avoid undue load on the API server. This is
Expand All @@ -20,11 +20,11 @@
* happen that some / many of these resources are then seldom or even reconciled anymore. In that
* situation, large amounts of memory might be consumed to cache resources that are never used
* again.
* <p>
* Note that if a resource is reconciled and is not present anymore in cache, it will transparently
* be fetched again from the API server. Similarly, since associated secondary resources are usually
* reconciled too, they might need to be fetched and populated to the cache, and will remain there
* for some time, for subsequent reconciliations.
*
* <p>Note that if a resource is reconciled and is not present anymore in cache, it will
* transparently be fetched again from the API server. Similarly, since associated secondary
* resources are usually reconciled too, they might need to be fetched and populated to the cache,
* and will remain there for some time, for subsequent reconciliations.
*/
public class CaffeineBoundedItemStores {

Expand All @@ -39,17 +39,13 @@ private CaffeineBoundedItemStores() {}
*/
@SuppressWarnings("unused")
public static <R extends HasMetadata> BoundedItemStore<R> boundedItemStore(
KubernetesClient client, Class<R> rClass,
Duration accessExpireDuration) {
Cache<String, R> cache = Caffeine.newBuilder()
.expireAfterAccess(accessExpireDuration)
.build();
KubernetesClient client, Class<R> rClass, Duration accessExpireDuration) {
Cache<String, R> cache = Caffeine.newBuilder().expireAfterAccess(accessExpireDuration).build();
return boundedItemStore(client, rClass, cache);
}

public static <R extends HasMetadata> BoundedItemStore<R> boundedItemStore(
KubernetesClient client, Class<R> rClass, Cache<String, R> cache) {
return new BoundedItemStore<>(new CaffeineBoundedCache<>(cache), rClass, client);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

public abstract class BoundedCacheTestBase<P extends CustomResource<BoundedCacheTestSpec, BoundedCacheTestStatus>> {
public abstract class BoundedCacheTestBase<
P extends CustomResource<BoundedCacheTestSpec, BoundedCacheTestStatus>> {

private static final Logger log = LoggerFactory.getLogger(BoundedCacheTestBase.class);

Expand All @@ -42,34 +43,46 @@ void reconciliationWorksWithLimitedCache() {
}

private void assertConfigMapsDeleted() {
await().atMost(Duration.ofSeconds(30))
.untilAsserted(() -> IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST).forEach(i -> {
var cm = extension().get(ConfigMap.class, RESOURCE_NAME_PREFIX + i);
assertThat(cm).isNull();
}));
await()
.atMost(Duration.ofSeconds(30))
.untilAsserted(
() ->
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(
i -> {
var cm = extension().get(ConfigMap.class, RESOURCE_NAME_PREFIX + i);
assertThat(cm).isNull();
}));
}

private void deleteTestResources() {
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST).forEach(i -> {
var cm = extension().get(customResourceClass(), RESOURCE_NAME_PREFIX + i);
var deleted = extension().delete(cm);
if (!deleted) {
log.warn("Custom resource might not be deleted: {}", cm);
}
});
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(
i -> {
var cm = extension().get(customResourceClass(), RESOURCE_NAME_PREFIX + i);
var deleted = extension().delete(cm);
if (!deleted) {
log.warn("Custom resource might not be deleted: {}", cm);
}
});
}

private void updateTestResources() {
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST).forEach(i -> {
var cm = extension().get(ConfigMap.class, RESOURCE_NAME_PREFIX + i);
cm.getData().put(DATA_KEY, UPDATED_PREFIX + i);
extension().replace(cm);
});
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(
i -> {
var cm = extension().get(ConfigMap.class, RESOURCE_NAME_PREFIX + i);
cm.getData().put(DATA_KEY, UPDATED_PREFIX + i);
extension().replace(cm);
});
}

void assertConfigMapData(String dataPrefix) {
await().untilAsserted(() -> IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(i -> assertConfigMap(i, dataPrefix)));
await()
.untilAsserted(
() ->
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(i -> assertConfigMap(i, dataPrefix)));
}

private void assertConfigMap(int i, String prefix) {
Expand All @@ -79,17 +92,16 @@ private void assertConfigMap(int i, String prefix) {
}

private void createTestResources() {
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST).forEach(i -> {
extension().create(createTestResource(i));
});
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST)
.forEach(
i -> {
extension().create(createTestResource(i));
});
}

abstract P createTestResource(int index);

abstract Class<P> customResourceClass();

abstract LocallyRunOperatorExtension extension();



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ public class CaffeineBoundedCacheClusterScopeIT
@RegisterExtension
LocallyRunOperatorExtension extension =
LocallyRunOperatorExtension.builder()
.withReconciler(new BoundedCacheClusterScopeTestReconciler(), o -> {
o.withItemStore(boundedItemStore(
new KubernetesClientBuilder().build(),
BoundedCacheClusterScopeTestCustomResource.class,
Duration.ofMinutes(1),
1));
})
.withReconciler(
new BoundedCacheClusterScopeTestReconciler(),
o -> {
o.withItemStore(
boundedItemStore(
new KubernetesClientBuilder().build(),
BoundedCacheClusterScopeTestCustomResource.class,
Duration.ofMinutes(1),
1));
})
.build();

@Override
BoundedCacheClusterScopeTestCustomResource createTestResource(int index) {
var res = new BoundedCacheClusterScopeTestCustomResource();
res.setMetadata(new ObjectMetaBuilder()
.withName(RESOURCE_NAME_PREFIX + index)
.build());
res.setMetadata(new ObjectMetaBuilder().withName(RESOURCE_NAME_PREFIX + index).build());
res.setSpec(new BoundedCacheTestSpec());
res.getSpec().setData(INITIAL_DATA_PREFIX + index);
res.getSpec().setTargetNamespace(extension.getNamespace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class CaffeineBoundedCacheNamespacedIT

@RegisterExtension
LocallyRunOperatorExtension extension =
LocallyRunOperatorExtension.builder().withReconciler(new BoundedCacheTestReconciler(), o -> {
o.withItemStore(boundedItemStore(
new KubernetesClientBuilder().build(), BoundedCacheTestCustomResource.class,
Duration.ofMinutes(1),
1));
})
LocallyRunOperatorExtension.builder()
.withReconciler(
new BoundedCacheTestReconciler(),
o -> {
o.withItemStore(
boundedItemStore(
new KubernetesClientBuilder().build(),
BoundedCacheTestCustomResource.class,
Duration.ofMinutes(1),
1));
})
.build();

BoundedCacheTestCustomResource createTestResource(int index) {
var res = new BoundedCacheTestCustomResource();
res.setMetadata(new ObjectMetaBuilder()
.withName(RESOURCE_NAME_PREFIX + index)
.build());
res.setMetadata(new ObjectMetaBuilder().withName(RESOURCE_NAME_PREFIX + index).build());
res.setSpec(new BoundedCacheTestSpec());
res.getSpec().setData(INITIAL_DATA_PREFIX + index);
res.getSpec().setTargetNamespace(extension.getNamespace());
Expand All @@ -46,5 +49,4 @@ Class<BoundedCacheTestCustomResource> customResourceClass() {
LocallyRunOperatorExtension extension() {
return extension;
}

}
Loading

0 comments on commit 805f676

Please sign in to comment.