Skip to content

Commit

Permalink
fix fabric8io#5236: using scale v1beta1 compatible logic for dc
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jun 13, 2023
1 parent e5d7f64 commit 146ccb0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
* Fix #5218: No export for `io.fabric8.tekton.triggers.internal.knative.pkg.apis.duck.v1beta1` in tekton v1beta1 triggers model
* Fix #5224: Ensuring jetty sets the User-Agent header
* Fix #4225: Enum fields written in generated crd yaml
* Fix #5236: using scale v1beta1 compatible logic for DeploymentConfig

#### Improvements

Expand Down
Expand Up @@ -713,7 +713,7 @@ protected T handlePatch(PatchContext context, T current, T updated) throws Inter
return handlePatch(context, current, updated, getType());
}

protected <S> S handleScale(S scaleParam, Class<S> scaleType) {
public <S> S handleScale(S scaleParam, Class<S> scaleType) {
try {
return handleScale(getCompleteResourceUrl().toString(), scaleParam, scaleType);
} catch (InterruptedException ie) {
Expand Down
Expand Up @@ -71,7 +71,7 @@ public Job scale(int count) {
Job res = accept(b -> b.getSpec().setParallelism(count));
if (context.getTimeout() > 0) {
waitUntilJobIsScaled();
res = getItemOrRequireFromServer();
res = get();
}
return res;
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.autoscaling.v1.Scale;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.internal.OperationContext;
import io.fabric8.kubernetes.client.dsl.internal.PodOperationContext;
import io.fabric8.kubernetes.client.dsl.internal.apps.v1.RollableScalableResourceOperation;
Expand All @@ -29,7 +30,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

abstract class LegacyRollableScalableResourceOperation<T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>>
public abstract class LegacyRollableScalableResourceOperation<T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>>
extends RollableScalableResourceOperation<T, L, R> {

protected LegacyRollableScalableResourceOperation(PodOperationContext context, OperationContext superContext, Class<T> type,
Expand All @@ -39,14 +40,19 @@ protected LegacyRollableScalableResourceOperation(PodOperationContext context, O

@Override
public Scale scale(Scale scaleParam) {
return scale(scaleParam, this);
}

public static Scale scale(Scale scaleParam, HasMetadataOperation<?, ?, ?> operation) {
// handles the conversion back in forth between v1beta1.scale and v1.scale
// the sticking point is mostly the conversion of the selector from a map to a single string
GenericKubernetesResource scale = handleScale(
GenericKubernetesResource scale = operation.handleScale(
Optional.ofNullable(scaleParam)
.map(s -> getKubernetesSerialization().unmarshal(getKubernetesSerialization().asYaml(s),
.map(s -> operation.getKubernetesSerialization().unmarshal(operation.getKubernetesSerialization().asYaml(s),
GenericKubernetesResource.class))
.map(g -> {
g.getAdditionalProperties().put("status", null);
// could explicitly set the apiVersion, but that doesn't seem to be required
return g;
}).orElse(null),
GenericKubernetesResource.class);
Expand All @@ -56,7 +62,8 @@ public Scale scale(Scale scaleParam) {
.ifPresent(selector -> status.put("selector",
selector.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(";")))));
return s;
}).map(s -> getKubernetesSerialization().unmarshal(getKubernetesSerialization().asYaml(s), Scale.class)).orElse(null);
}).map(s -> operation.getKubernetesSerialization().unmarshal(operation.getKubernetesSerialization().asYaml(s), Scale.class))
.orElse(null);
}

}
Expand Up @@ -19,7 +19,6 @@
import io.fabric8.junit.jupiter.api.LoadKubernetesManifests;
import io.fabric8.junit.jupiter.api.RequireK8sSupport;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
import io.fabric8.openshift.api.model.DeploymentConfigList;
import io.fabric8.openshift.client.OpenShiftClient;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -61,9 +60,14 @@ void list() {
@Test
void update() {
DeploymentConfig deploymentConfig1 = client.deploymentConfigs().withName("dc-update")
.edit(d -> new DeploymentConfigBuilder(d)
.editMetadata().withResourceVersion(null).endMetadata()
.editSpec().withReplicas(3).endSpec().build());
.accept(dc -> dc.getMetadata().getAnnotations().put("new", "annotation"));
assertThat(deploymentConfig1).isNotNull();
assertEquals("annotation", deploymentConfig1.getMetadata().getAnnotations().get("new"));
}

@Test
void scale() {
DeploymentConfig deploymentConfig1 = client.deploymentConfigs().withName("dc-update").scale(3);
assertThat(deploymentConfig1).isNotNull();
assertEquals(3, deploymentConfig1.getSpec().getReplicas().intValue());
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.openshift.client.dsl.internal.apps;

import io.fabric8.kubernetes.api.model.autoscaling.v1.Scale;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.BytesLimitTerminateTimeTailPrettyLoggable;
Expand All @@ -29,6 +30,7 @@
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.dsl.internal.extensions.v1beta1.LegacyRollableScalableResourceOperation;
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.kubernetes.client.utils.internal.PodOperationUtil;
import io.fabric8.openshift.api.model.DeploymentConfig;
Expand Down Expand Up @@ -70,6 +72,11 @@ public DeploymentConfigOperationsImpl newInstance(OperationContext context) {
return new DeploymentConfigOperationsImpl(rollingOperationContext, context);
}

@Override
public Scale scale(Scale scaleParam) {
return LegacyRollableScalableResourceOperation.scale(scaleParam, this);
}

@Override
public DeploymentConfig deployLatest(boolean wait) {
DeploymentConfigOperationsImpl deployable = this;
Expand Down

0 comments on commit 146ccb0

Please sign in to comment.