Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
feat: kube config not updated by default, flag to turn it on in annot…
Browse files Browse the repository at this point in the history
…ation (#75)
  • Loading branch information
csviri authored Apr 6, 2023
1 parent 95e67f5 commit b30df6c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 32 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,3 @@ In general, it is a best practice to use additional standard frameworks to imple
like [kubernetes-webooks-framework](https://github.com/java-operator-sdk/kubernetes-webooks-framework)
with Quarkus or Spring. However, we demonstrate how it works
in [this test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubernetesMutationHookHandlingTest.java#L53-L53)

### How does it work

In the background Kubernetes and etcd (and kubectl) binaries are downloaded if not found locally.

All the certificates for the Kube API Server and for the client is generated. The client config file
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.

#### Downloading binaries

Binaries are downloaded automatically under ~/.jenvtest/k8s/[target-platform-and-version] if no binary found locally.
If there are multiple binaries found, the latest if selected (unless a target version is not specified).

Also [`setup-envtest`](https://pkg.go.dev/sigs.k8s.io/controller-runtime/tools/setup-envtest#section-readme) can be used
to download binaries manually. By executing `setup-envtest use --bin-dir ~/.jenvtest` will download the latest required
binaries to the default directory. This is useful if always running the tests in offline mode.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class KubeAPIServerConfig {
*/
private final List<String> apiServerFlags;

/**
* If kube config (in ~/kube/config ) file should be updated.
*/
private final boolean updateKubeConfig;

KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean offlineMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class KubeAPIServerConfigBuilder {
private String jenvtestDir;
private String apiServerVersion;
private Boolean offlineMode;
private boolean updateKubeConfig = true;
private boolean updateKubeConfig = false;
private final List<String> apiServerFlags = new ArrayList<>(0);

public KubeAPIServerConfigBuilder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
String kubeAPIVersion() default NOT_SET;

String[] apiServerFlags() default {};

boolean updateKubeConfigFile() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,30 @@ private void initialize(ExtensionContext extensionContext, boolean staticContext
.filter(h -> h.isTargetFieldAvailable(extensionContext, staticContext))
.collect(Collectors.toList());

startIfAnnotationPresent(extensionContext, targetInjectors.isEmpty());
startIfAnnotationPresent(extensionContext, !targetInjectors.isEmpty());

targetInjectors.forEach(i -> i.inject(extensionContext, staticContext, kubeApiServer));
}

private void startIfAnnotationPresent(ExtensionContext extensionContext,
boolean updateKubeConfig) {
boolean willInjectClient) {
extensionContext.getElement().ifPresent(ae -> {
var annotation = getExtensionAnnotationInstance(ae);
annotation.ifPresent(a -> startApiServer(a, updateKubeConfig));

annotation.ifPresent(a -> {
if (!willInjectClient && !a.updateKubeConfigFile()) {
log.warn(
"Neither kube config file will be updated or client info will be injected into the test. "
+
"This is probably a miss configuration since server won't be easily accessible.");
}
startApiServer(a);
});
});
}

private void startApiServer(EnableKubeAPIServer annotation, boolean updateKubeConfig) {
kubeApiServer = new KubeAPIServer(annotationToConfig(annotation, updateKubeConfig));
private void startApiServer(EnableKubeAPIServer annotation) {
kubeApiServer = new KubeAPIServer(annotationToConfig(annotation));
kubeApiServer.start();
}

Expand All @@ -84,8 +93,7 @@ private void stopIfAnnotationPresent(ExtensionContext extensionContext) {
});
}

private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation,
boolean updateKubeConfig) {
private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation) {
var builder = KubeAPIServerConfigBuilder.anAPIServerConfig();
var version = annotation.kubeAPIVersion();
if (!NOT_SET.equals(version)) {
Expand All @@ -94,7 +102,8 @@ private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation,
if (annotation.apiServerFlags().length > 0) {
builder.withApiServerFlags(List.of(annotation.apiServerFlags()));
}
builder.withUpdateKubeConfig(updateKubeConfig);

builder.withUpdateKubeConfig(annotation.updateKubeConfigFile());
return builder.build();
}

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

import static io.javaoperatorsdk.jenvtest.sample.TestUtils.simpleTest;

@EnableKubeAPIServer
@EnableKubeAPIServer(updateKubeConfigFile = true)
class JUnitExtensionKubeConfigUpdateTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.javaoperatorsdk.jenvtest.KubeAPIServer;
import io.javaoperatorsdk.jenvtest.KubeAPIServerConfigBuilder;
Expand All @@ -25,14 +26,27 @@ void apiServerWithSpecificVersion() {
.build()));
}

@Test
void usingKubeConfigFileToInitClient() {
var kubeApi = new KubeAPIServer(KubeAPIServerConfigBuilder.anAPIServerConfig()
.withUpdateKubeConfig(true)
.build());
kubeApi.start();

var client = new KubernetesClientBuilder().build();

TestUtils.simpleTest(client);
}


@Test
void usingWildcardVersion() {
var kubeApi = new KubeAPIServer(KubeAPIServerConfigBuilder.anAPIServerConfig()
.withApiServerVersion("1.26.*")
.build());
kubeApi.start();

var client = new KubernetesClientBuilder().build();
var client = createClient(kubeApi.getKubeConfigYaml());
TestUtils.simpleTest(client);
assertThat(client.getKubernetesVersion().getMinor()).isEqualTo("26");

Expand All @@ -46,17 +60,21 @@ void creatingClientFromConfigString() {
.build());
kubeApi.start();

var client =
new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeApi.getKubeConfigYaml()))
.build();
var client = createClient(kubeApi.getKubeConfigYaml());
TestUtils.simpleTest(client);

kubeApi.stop();
}

void testWithAPIServer(KubeAPIServer kubeApi) {
kubeApi.start();
simpleTest();
var client = createClient(kubeApi.getKubeConfigYaml());
simpleTest(client);
kubeApi.stop();
}

KubernetesClient createClient(String yaml) {
return new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(yaml)).build();
}

}
23 changes: 23 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


## Configuration Options





## How does it work

In the background Kubernetes and etcd (and kubectl) binaries are downloaded if not found locally.

All the certificates for the Kube API Server and for the client is generated. The client config file
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.

## Downloading binaries

Binaries are downloaded automatically under ~/.jenvtest/k8s/[target-platform-and-version] if no binary found locally.
If there are multiple binaries found, the latest if selected (unless a target version is not specified).

Also [`setup-envtest`](https://pkg.go.dev/sigs.k8s.io/controller-runtime/tools/setup-envtest#section-readme) can be used
to download binaries manually. By executing `setup-envtest use --bin-dir ~/.jenvtest` will download the latest required
binaries to the default directory. This is useful if always running the tests in offline mode.

0 comments on commit b30df6c

Please sign in to comment.