Skip to content

Commit

Permalink
fix #4014: adding support for build version, and removing the context
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Nov 28, 2022
1 parent 55394f3 commit 60fd6c7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#### Improvements
* Fix #4355: for exec, attach, upload, and copy operations the container id/name will be validated or chosen prior to the remote call. You may also use the kubectl.kubernetes.io/default-container annotation to specify the default container.
* Fix #4530: generalizing the Serialization logic to allow for primitive values and clarifying the type expectations.
* Fix #4014: added support for OpenShift Build log version.

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*/
package io.fabric8.openshift.client.server.mock;

import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.openshift.client.OpenShiftClient;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@EnableOpenShiftMockClient
@EnableKubernetesMockClient
class BuildTest {
OpenShiftMockServer server;
KubernetesMockServer server;
OpenShiftClient openShiftClient;

@Test
Expand All @@ -40,4 +42,12 @@ void testLogWithTimestamps() {
String log = openShiftClient.builds().inNamespace("ns1").withName("test-build").usingTimestamps().getLog();
assertEquals("test build output", log);
}

@Test
void testLogWithVersion() {
server.expect().withPath("/apis/build.openshift.io/v1/namespaces/ns1/builds/test-build/log?pretty=false&version=2").andReturn(200, "test build output").times(2);

String log = openShiftClient.builds().inNamespace("ns1").withName("test-build").withVersion(2).getLog();
assertEquals("test build output", log);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@

public interface BuildResource extends Resource<Build>,
TimestampBytesLimitTerminateTimeTailPrettyLoggable {

TimestampBytesLimitTerminateTimeTailPrettyLoggable withVersion(Integer version);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
import io.fabric8.kubernetes.client.dsl.PrettyLoggable;
import io.fabric8.kubernetes.client.dsl.TailPrettyLoggable;
import io.fabric8.kubernetes.client.dsl.TimeTailPrettyLoggable;
import io.fabric8.kubernetes.client.dsl.TimestampBytesLimitTerminateTimeTailPrettyLoggable;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.LogWatchCallback;
import io.fabric8.kubernetes.client.dsl.internal.OperationContext;
import io.fabric8.kubernetes.client.dsl.internal.PodOperationContext;
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.kubernetes.client.utils.internal.PodOperationUtil;
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.BuildList;
import io.fabric8.openshift.client.dsl.BuildResource;
import io.fabric8.openshift.client.dsl.internal.BuildOperationContext;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -50,69 +51,37 @@ public class BuildOperationsImpl extends HasMetadataOperation<Build, BuildList,
BuildResource {

public static final String OPENSHIFT_IO_BUILD_NAME = "openshift.io/build.name";
private final boolean withTerminatedStatus;
private final boolean withTimestamps;
private final String sinceTimestamp;
private final Integer sinceSeconds;
private final Integer withTailingLines;
private final boolean withPrettyOutput;
private final String version;
private final Integer limitBytes;
private Integer version;
private static final Integer DEFAULT_POD_LOG_WAIT_TIMEOUT = 5;
private Integer podLogWaitTimeout;
private final BuildOperationContext buildOperationContext;
private final PodOperationContext operationContext;

public BuildOperationsImpl(Client client) {
this(new BuildOperationContext(), HasMetadataOperationsImpl.defaultContext(client));
this(new PodOperationContext(), HasMetadataOperationsImpl.defaultContext(client), null);
}

public BuildOperationsImpl(BuildOperationContext context, OperationContext superContext) {
public BuildOperationsImpl(PodOperationContext context, OperationContext superContext, Integer version) {
super(superContext.withApiGroupName(BUILD)
.withPlural("builds"), Build.class, BuildList.class);
this.buildOperationContext = context;
this.withTerminatedStatus = context.isTerminatedStatus();
this.withTimestamps = context.isTimestamps();
this.sinceTimestamp = context.getSinceTimestamp();
this.sinceSeconds = context.getSinceSeconds();
this.withTailingLines = context.getTailingLines();
this.withPrettyOutput = context.isPrettyOutput();
this.version = context.getVersion();
this.limitBytes = context.getLimitBytes();
this.operationContext = context;
this.context = superContext;
this.version = version;
}

@Override
public BuildOperationsImpl newInstance(OperationContext context) {
return new BuildOperationsImpl(buildOperationContext, context);
return new BuildOperationsImpl(operationContext, context, version);
}

BuildOperationContext getContext() {
return buildOperationContext;
PodOperationContext getContext() {
return operationContext;
}

protected String getLogParameters() {
StringBuilder sb = new StringBuilder();
sb.append("log?pretty=").append(withPrettyOutput);
if (version != null && !version.isEmpty()) {
sb.append("&version=").append(version);
String params = operationContext.getLogParameters();
if (version != null) {
params += ("&version=" + version);
}
if (withTerminatedStatus) {
sb.append("&previous=true");
}
if (sinceSeconds != null) {
sb.append("&sinceSeconds=").append(sinceSeconds);
} else if (sinceTimestamp != null) {
sb.append("&sinceTime=").append(sinceTimestamp);
}
if (withTailingLines != null) {
sb.append("&tailLines=").append(withTailingLines);
}
if (limitBytes != null) {
sb.append("&limitBytes=").append(limitBytes);
}
if (withTimestamps) {
sb.append("&timestamps=true");
}
return sb.toString();
return params;
}

protected <T> T doGetLog(Class<T> type) {
Expand All @@ -131,7 +100,7 @@ public String getLog() {

@Override
public String getLog(boolean isPretty) {
return new BuildOperationsImpl(getContext().withPrettyOutput(isPretty), context).getLog();
return new BuildOperationsImpl(getContext().withPrettyOutput(isPretty), context, version).getLog();
}

/**
Expand Down Expand Up @@ -174,52 +143,55 @@ public LogWatch watchLog(OutputStream out) {

@Override
public Loggable withLogWaitTimeout(Integer logWaitTimeout) {
BuildOperationsImpl result = newInstance(context);
result.podLogWaitTimeout = logWaitTimeout;
return result;
return new BuildOperationsImpl(getContext().withLogWaitTimeout(logWaitTimeout), context, version);
}

@Override
public Loggable withPrettyOutput() {
return new BuildOperationsImpl(getContext().withPrettyOutput(true), context);
return new BuildOperationsImpl(getContext().withPrettyOutput(true), context, version);
}

@Override
public PrettyLoggable tailingLines(int tailingLines) {
return new BuildOperationsImpl(getContext().withTailingLines(tailingLines), context);
return new BuildOperationsImpl(getContext().withTailingLines(tailingLines), context, version);
}

@Override
public TimeTailPrettyLoggable terminated() {
return new BuildOperationsImpl(getContext().withTerminatedStatus(true), context);
return new BuildOperationsImpl(getContext().withTerminatedStatus(true), context, version);
}

@Override
public TailPrettyLoggable sinceTime(String sinceTimestamp) {
return new BuildOperationsImpl(getContext().withSinceTimestamp(sinceTimestamp), context);
return new BuildOperationsImpl(getContext().withSinceTimestamp(sinceTimestamp), context, version);
}

@Override
public TailPrettyLoggable sinceSeconds(int sinceSeconds) {
return new BuildOperationsImpl(getContext().withSinceSeconds(sinceSeconds), context);
return new BuildOperationsImpl(getContext().withSinceSeconds(sinceSeconds), context, version);
}

@Override
public BytesLimitTerminateTimeTailPrettyLoggable limitBytes(int limitBytes) {
return new BuildOperationsImpl(getContext().withLimitBytes(limitBytes), context);
return new BuildOperationsImpl(getContext().withLimitBytes(limitBytes), context, version);
}

@Override
public TimestampBytesLimitTerminateTimeTailPrettyLoggable withVersion(Integer version) {
return new BuildOperationsImpl(getContext(), context, version);
}

@Override
public BytesLimitTerminateTimeTailPrettyLoggable usingTimestamps() {
return new BuildOperationsImpl(getContext().withTimestamps(true), context);
return new BuildOperationsImpl(getContext().withTimestamps(true), context, version);
}

private void waitUntilBuildPodBecomesReady(Build build) {
List<PodResource> podOps = PodOperationUtil.getPodOperationsForController(context,
buildOperationContext.getPodOperationContext(), build.getMetadata().getUid(),
operationContext, build.getMetadata().getUid(),
getBuildPodLabels(build));

waitForBuildPodToBecomeReady(podOps, podLogWaitTimeout != null ? podLogWaitTimeout : DEFAULT_POD_LOG_WAIT_TIMEOUT);
waitForBuildPodToBecomeReady(podOps, operationContext.getLogWaitTimeout() != null ? operationContext.getLogWaitTimeout() : DEFAULT_POD_LOG_WAIT_TIMEOUT);
}

private static void waitForBuildPodToBecomeReady(List<PodResource> podOps, Integer podLogWaitTimeout) {
Expand Down

0 comments on commit 60fd6c7

Please sign in to comment.