Skip to content

Commit

Permalink
fix fabric8io#4633: improving run operations
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed Dec 20, 2022
1 parent 1e42516 commit 722dc49
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs

#### Improvements
* Fix #4633: provided inline access to all RunConfig builder methods via run().withNewRunConfig()
* Fix #4670: the initial informer listing will use a resourceVersion of 0 to utilize the watch cache if possible. This means that the initial cache state when the informer is returned, or the start future is completed, may not be as fresh as the previous behavior which forced the latest version. It will of course become more consistent as the watch will already have been established.

#### Dependency Upgrade
Expand All @@ -13,6 +14,7 @@

#### _**Note**_: Breaking changes
* Fix #4574: fromServer has been deprecated - it no longer needs to be called. All get() operations will fetch the resource(s) from the api server. If you need the context item that was passed in from a resource, load, or resourceList methods, use the item or items method.
* Fix #4633: client.run().withRunConfig was deprecated. Use withNewRunConfig instead.

### 6.3.1 (2022-12-15)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,32 @@

public class RunOperations {

public interface RunConfigNested extends RunConfigFluent<RunConfigNested> {

Pod done();

}

private class RunConfigNestedImpl extends RunConfigFluentImpl<RunConfigNested> implements RunConfigNested {

RunConfigNestedImpl() {
super(runConfig); // copy the existing properties
}

@Override
public Pod done() {
RunConfig runConfig = new RunConfigBuilder(this).build();
return withRunConfig(runConfig).done();
}

}

private final KubernetesClient client;
private final RunConfigBuilder runConfigBuilder;
private final RunConfig runConfig;

public RunOperations(KubernetesClient client, RunConfigBuilder runConfigBuilder) {
public RunOperations(KubernetesClient client, RunConfig runConfig) {
this.client = client;
this.runConfigBuilder = runConfigBuilder;
this.runConfig = runConfig;
}

/**
Expand All @@ -37,7 +57,7 @@ public RunOperations(KubernetesClient client, RunConfigBuilder runConfigBuilder)
* @return {@link RunOperations} with injected namespace
*/
public RunOperations inNamespace(String namespace) {
return new RunOperations(client.adapt(NamespacedKubernetesClient.class).inNamespace(namespace), runConfigBuilder);
return new RunOperations(client.adapt(NamespacedKubernetesClient.class).inNamespace(namespace), runConfig);
}

/**
Expand All @@ -47,7 +67,7 @@ public RunOperations inNamespace(String namespace) {
* @return {@link RunOperations} with image injected into {@link RunConfig}
*/
public RunOperations withImage(String image) {
return new RunOperations(client, runConfigBuilder.withImage(image));
return new RunOperations(client, new RunConfigBuilder(runConfig).withImage(image).build());
}

/**
Expand All @@ -57,18 +77,32 @@ public RunOperations withImage(String image) {
* @return {@link RunOperations} with name injected into {@link RunConfig}
*/
public RunOperations withName(String name) {
return new RunOperations(client, runConfigBuilder.withName(name));
return new RunOperations(client, new RunConfigBuilder(runConfig).withName(name).build());
}

/**
* Specify complex configuration for Pod creating using {@link RunConfig}
* Specify complex configuration for Pod creating using {@link RunConfig}.
* <br>
* WARNING: this will overwrite any of the previous calls, such as withName to this context
*
* @param generatorRunConfig {@link RunConfig} which allows to provide configuring environment variables, labels, resources,
* ports etc
* @return {@link RunOperations} with specified configuration
* @deprecated use {@link #withNewRunConfig()} instead
*/
@Deprecated
public RunOperations withRunConfig(RunConfig generatorRunConfig) {
return new RunOperations(client, new RunConfigBuilder(generatorRunConfig));
return new RunOperations(client, new RunConfigBuilder(generatorRunConfig).build());
}

/**
* Provides access to a nested {@link RunConfig} builder that inherits the current
* values for name and image if they are already set. Call the {@link RunConfigNested#done()} method to create the {@link Pod}
*
* @return
*/
public RunConfigNested withNewRunConfig() {
return new RunConfigNestedImpl();
}

/**
Expand All @@ -81,10 +115,9 @@ public Pod done() {
}

Pod convertRunConfigIntoPod() {
RunConfig finalGeneratorConfig = runConfigBuilder.build();
return new PodBuilder()
.withMetadata(RunConfigUtil.getObjectMetadataFromRunConfig(finalGeneratorConfig))
.withSpec(RunConfigUtil.getPodSpecFromRunConfig(finalGeneratorConfig))
.withMetadata(RunConfigUtil.getObjectMetadataFromRunConfig(runConfig))
.withSpec(RunConfigUtil.getPodSpecFromRunConfig(runConfig))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testConvertRunConfigIntoPod() {
.withPort(5701)
.withLimits(limits)
.withRequests(requests);
RunOperations deploymentGenerator = new RunOperations(Mockito.mock(KubernetesClient.class), generatorRunConfig);
RunOperations deploymentGenerator = new RunOperations(Mockito.mock(KubernetesClient.class), generatorRunConfig.build());

// When
Pod pod = deploymentGenerator.convertRunConfigIntoPod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ public MixedOperation<Lease, LeaseList, Resource<Lease>> leases() {
*/
@Override
public RunOperations run() {
return new RunOperations(this, new RunConfigBuilder());
return new RunOperations(this, new RunConfigBuilder().build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class PodRunEquivalent {
public static void main(String[] args) {
try (final KubernetesClient k8s = new KubernetesClientBuilder().build()) {
k8s.run().inNamespace("default")
k8s.run().inNamespace("default").withNewRunConfig()
.withImage("nginx:mainline-alpine")
.withName("my-pod")
.done();
Expand Down

0 comments on commit 722dc49

Please sign in to comment.