Skip to content

Commit

Permalink
fix: replaced deprecated method from Kubernetes Client Updatable inte…
Browse files Browse the repository at this point in the history
…rface

From `.replace()` to `.unlock().update()`

Signed-off-by: atsI <[email protected]>
  • Loading branch information
abusk authored Sep 6, 2024
1 parent 892b765 commit 522baa2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,21 @@ private void updateImageName(KubernetesClient kubernetes, HasMetadata entity, St
Deployment resource = (Deployment) entity;
DeploymentSpec spec = resource.getSpec();
if (spec != null && updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.apps().deployments().inNamespace(namespace).resource(resource).replace();
kubernetes.apps().deployments().inNamespace(namespace).resource(resource).unlock().update();
kubernetes.apps().deployments().inNamespace(namespace).withName(name).rolling().restart();
}
} else if (entity instanceof ReplicaSet) {
ReplicaSet resource = (ReplicaSet) entity;
ReplicaSetSpec spec = resource.getSpec();
if (spec != null && updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.apps().replicaSets().inNamespace(namespace).resource(resource).replace();
kubernetes.apps().replicaSets().inNamespace(namespace).resource(resource).unlock().update();
kubernetes.apps().replicaSets().inNamespace(namespace).withName(name).rolling().restart();
}
} else if (entity instanceof ReplicationController) {
ReplicationController resource = (ReplicationController) entity;
ReplicationControllerSpec spec = resource.getSpec();
if (spec != null && updateImageName(entity, spec.getTemplate(), imagePrefix, imageName)) {
kubernetes.replicationControllers().inNamespace(namespace).resource(resource).replace();
kubernetes.replicationControllers().inNamespace(namespace).resource(resource).unlock().update();
kubernetes.replicationControllers().inNamespace(namespace).withName(name).rolling().restart();
}
} else if (entity instanceof DeploymentConfig) {
Expand All @@ -185,7 +185,7 @@ private void updateImageName(KubernetesClient kubernetes, HasMetadata entity, St
if (openshiftClient == null) {
log.warn("Ignoring DeploymentConfig %s as not connected to an OpenShift cluster", name);
} else {
openshiftClient.deploymentConfigs().inNamespace(namespace).resource(resource).replace();
openshiftClient.deploymentConfigs().inNamespace(namespace).resource(resource).unlock().update();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
import io.fabric8.kubernetes.client.dsl.ScalableResource;
import io.fabric8.kubernetes.client.dsl.TimeoutImageEditReplacePatchable;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
Expand Down Expand Up @@ -78,17 +80,19 @@ void restartContainer_whenImageConfigurationAndDeploymentProvided_thenShouldDoRo
// Given
Deployment deployment = createNewDeployment();
RollableScalableResource<Deployment> rollableScalableResource = mockKubernetesClientAppsDeploymentCall();
NonDeletingOperation<Deployment> nonDeletingOperation = mockKubernetesClientAppsNonDeletingOperationCall(rollableScalableResource);
TimeoutImageEditReplacePatchable<Deployment> timeoutImageEditReplacePatchable = mock(TimeoutImageEditReplacePatchable.class);
when(mockedImageWatcher.getImageConfiguration()).thenReturn(createNewImageConfiguration("foo/example-deployment:snapshot-1234"));
when(rollableScalableResource.replace()).thenReturn(deployment);
when(nonDeletingOperation.update()).thenReturn(deployment);
when(rollableScalableResource.rolling()).thenReturn(timeoutImageEditReplacePatchable);
when(timeoutImageEditReplacePatchable.restart()).thenReturn(deployment);

// When
dockerImageWatcher.restartContainer(mockedImageWatcher, Collections.singletonList(deployment));

// Then
verify(rollableScalableResource).replace();
verify(rollableScalableResource).unlock();
verify(nonDeletingOperation).update();
verify(rollableScalableResource).rolling();
verify(timeoutImageEditReplacePatchable).restart();
assertPodTemplateSpecContainsImage(deployment.getSpec().getTemplate(), "foo/example-deployment:snapshot-1234");
Expand All @@ -99,17 +103,19 @@ void restartContainer_whenImageConfigurationAndReplicaSetProvided_thenShouldDoRo
// Given
ReplicaSet replicaSet = createNewReplicaSet();
RollableScalableResource<ReplicaSet> rollableScalableResource = mockKubernetesClientAppsReplicaSetCall();
NonDeletingOperation<ReplicaSet> nonDeletingOperation = mockKubernetesClientAppsNonDeletingOperationCall(rollableScalableResource);
TimeoutImageEditReplacePatchable<ReplicaSet> timeoutImageEditReplacePatchable = mock(TimeoutImageEditReplacePatchable.class);
when(mockedImageWatcher.getImageConfiguration()).thenReturn(createNewImageConfiguration("foo/example-replicaset:snapshot-1234"));
when(rollableScalableResource.replace()).thenReturn(replicaSet);
when(nonDeletingOperation.update()).thenReturn(replicaSet);
when(rollableScalableResource.rolling()).thenReturn(timeoutImageEditReplacePatchable);
when(timeoutImageEditReplacePatchable.restart()).thenReturn(replicaSet);

// When
dockerImageWatcher.restartContainer(mockedImageWatcher, Collections.singletonList(replicaSet));

// Then
verify(rollableScalableResource).replace();
verify(rollableScalableResource).unlock();
verify(nonDeletingOperation).update();
verify(rollableScalableResource).rolling();
verify(timeoutImageEditReplacePatchable).restart();
assertPodTemplateSpecContainsImage(replicaSet.getSpec().getTemplate(), "foo/example-replicaset:snapshot-1234");
Expand All @@ -120,17 +126,19 @@ void restartContainer_whenImageConfigurationAndReplicationControllerProvided_the
// Given
ReplicationController replicationController = createNewReplicationController();
RollableScalableResource<ReplicationController> rollableScalableResource = mockKubernetesClientAppsReplicationControllerCall();
NonDeletingOperation<ReplicationController> nonDeletingOperation = mockKubernetesClientAppsNonDeletingOperationCall(rollableScalableResource);
TimeoutImageEditReplacePatchable<ReplicationController> timeoutImageEditReplacePatchable = mock(TimeoutImageEditReplacePatchable.class);
when(mockedImageWatcher.getImageConfiguration()).thenReturn(createNewImageConfiguration("foo/example-replicationcontroller:snapshot-1234"));
when(rollableScalableResource.replace()).thenReturn(replicationController);
when(nonDeletingOperation.update()).thenReturn(replicationController);
when(rollableScalableResource.rolling()).thenReturn(timeoutImageEditReplacePatchable);
when(timeoutImageEditReplacePatchable.restart()).thenReturn(replicationController);

// When
dockerImageWatcher.restartContainer(mockedImageWatcher, Collections.singletonList(replicationController));

// Then
verify(rollableScalableResource).replace();
verify(rollableScalableResource).unlock();
verify(nonDeletingOperation).update();
verify(rollableScalableResource).rolling();
verify(timeoutImageEditReplacePatchable).restart();
assertPodTemplateSpecContainsImage(replicationController.getSpec().getTemplate(), "foo/example-replicationcontroller:snapshot-1234");
Expand All @@ -145,14 +153,16 @@ void restartContainer_whenImageConfigurationAndDeploymentConfigProvided_thenShou
.thenReturn(mockedOpenShiftClient);
DeploymentConfig deploymentConfig = createNewDeploymentConfig();
DeployableScalableResource<DeploymentConfig> rollableScalableResource = mockOpenShiftClientDeploymentConfigCall(mockedOpenShiftClient);
NonDeletingOperation<DeploymentConfig> nonDeletingOperation = mockKubernetesClientAppsNonDeletingOperationCall(rollableScalableResource);
when(mockedImageWatcher.getImageConfiguration()).thenReturn(createNewImageConfiguration("foo/example-deploymentconfig:snapshot-1234"));
when(rollableScalableResource.replace()).thenReturn(deploymentConfig);
when(nonDeletingOperation.update()).thenReturn(deploymentConfig);

// When
dockerImageWatcher.restartContainer(mockedImageWatcher, Collections.singletonList(deploymentConfig));

// Then
verify(rollableScalableResource).replace();
verify(rollableScalableResource).unlock();
verify(nonDeletingOperation).update();
assertPodTemplateSpecContainsImage(deploymentConfig.getSpec().getTemplate(), "foo/example-deploymentconfig:snapshot-1234");
}
}
Expand Down Expand Up @@ -220,6 +230,12 @@ private DeployableScalableResource<DeploymentConfig> mockOpenShiftClientDeployme
return deployableScalableResource;
}

private <T> NonDeletingOperation<T> mockKubernetesClientAppsNonDeletingOperationCall(ScalableResource<T> scalableResource) {
NonDeletingOperation<T> nonDeletingOperation = mock(NonDeletingOperation.class);
when(scalableResource.unlock()).thenReturn(nonDeletingOperation);
return nonDeletingOperation;
}

private Deployment createNewDeployment() {
return new DeploymentBuilder()
.withNewMetadata()
Expand Down

0 comments on commit 522baa2

Please sign in to comment.