Skip to content

Commit

Permalink
Fix #2342: Fix getLog(), watchLog() methods for Job and Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Jul 14, 2020
1 parent 59dbfcf commit ffdcd46
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Fix KubernetesAttributesExctractor to extract metadata from unregistered custom resources, such when using Raw CustomResource API
* Fix #2296: No adapter available for type:interface io.fabric8.kubernetes.client.dsl.V1APIGroupDSL
* Fix #2269: Setting a grace period when deleting resource using `withPropagationPolicy()`
* Fix #2342: watchLogs for deployment is broken

#### Improvements
* Fix #2233: client.service().getUrl(..) should be able to fetch URL for ClusterIP based services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ private List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> doGetLog(
Deployment deployment = fromServer().get();
String rcUid = deployment.getMetadata().getUid();

ReplicaSetOperationsImpl rsOperations = new ReplicaSetOperationsImpl((RollingOperationContext) context);
ReplicaSetOperationsImpl rsOperations = new ReplicaSetOperationsImpl(
new RollingOperationContext(context.getClient(), context.getConfig(), context.getPlural(), context.getNamespace(),
null, context.getApiGroupName(), context.getApiGroupVersion(), context.getCascading(), null, context.getLabels(),
context.getLabelsNot(), context.getLabelsIn(), context.getLabelsNotIn(), context.getFields(), context.getFieldsNot(),
context.getResourceVersion(), context.getReloadingFromServer(), context.getGracePeriodSeconds(), context.getPropagationPolicy(),
context.getWatchRetryInitialBackoffMillis(), context.getWatchRetryBackoffMultiplier(), false, 0, null
));
ReplicaSetList rcList = rsOperations.withLabels(deployment.getSpec().getTemplate().getMetadata().getLabels()).list();

for (ReplicaSet rs : rcList.getItems()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private List<PodResource<Pod, DoneablePod>> doGetLog(boolean isPretty) {
String rcUid = replicaSet.getMetadata().getUid();

PodOperationsImpl podOperations = new PodOperationsImpl(new PodOperationContext(context.getClient(),
context.getConfig(), context.getPlural(), context.getNamespace(), context.getName(), null,
context.getConfig(), context.getPlural(), context.getNamespace(), null, null,
"v1", context.getCascading(), context.getItem(), context.getLabels(), context.getLabelsNot(),
context.getLabelsIn(), context.getLabelsNotIn(), context.getFields(), context.getFieldsNot(), context.getResourceVersion(),
context.getReloadingFromServer(), context.getGracePeriodSeconds(), context.getPropagationPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private List<PodResource<Pod, DoneablePod>> doGetLog(boolean isPretty) {
String jobUid = job.getMetadata().getUid();

PodOperationsImpl podOperations = new PodOperationsImpl(new PodOperationContext(context.getClient(),
context.getConfig(), context.getPlural(), context.getNamespace(), context.getName(), null,
context.getConfig(), context.getPlural(), context.getNamespace(), null, null,
"v1", context.getCascading(), context.getItem(), context.getLabels(), context.getLabelsNot(),
context.getLabelsIn(), context.getLabelsNotIn(), context.getFields(), context.getFieldsNot(), context.getResourceVersion(),
context.getReloadingFromServer(), context.getGracePeriodSeconds(), context.getPropagationPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
Expand Down Expand Up @@ -681,10 +685,72 @@ void testRolloutUndo() throws InterruptedException {
assertTrue(recordedRequest.getBody().readUtf8().contains("\"app\":\"rs1\""));
}

@Test
@DisplayName("Should get logs for a Deployment")
void testDeploymentGetLog() {
// Given
ReplicaSet replicaSet = new ReplicaSetBuilder()
.withNewMetadata()
.withOwnerReferences(
new OwnerReferenceBuilder()
.withApiVersion("apps/v1")
.withBlockOwnerDeletion(true)
.withController(true)
.withKind("Deployment")
.withName("deploy1")
.withUid("136581bf-82c2-4675-af05-63c55500b378")
.build()
)
.withName("deploy1-hk9nf")
.addToLabels("app", "nginx")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.endMetadata()
.build();

Pod deployPod = new PodBuilder()
.withNewMetadata()
.withOwnerReferences(new OwnerReferenceBuilder().withApiVersion("apps/v1")
.withBlockOwnerDeletion(true)
.withController(true)
.withKind("ReplicaSet")
.withName("1")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.build())
.withName("deploy1-hk9nf").addToLabels("controller-uid", "3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.endMetadata()
.build();

server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/deployments/deploy1")
.andReturn(HttpURLConnection.HTTP_OK, getDeploymentBuilder().build())
.always();

server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/replicasets?labelSelector=app%3Dnginx")
.andReturn(HttpURLConnection.HTTP_OK, new ReplicaSetListBuilder().withItems(replicaSet).build())
.once();
server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/replicasets/deploy1-hk9nf")
.andReturn(HttpURLConnection.HTTP_OK, replicaSet)
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/pods?labelSelector=app%3Dnginx")
.andReturn(HttpURLConnection.HTTP_OK, new PodListBuilder().withItems(deployPod).build())
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/pods/deploy1-hk9nf/log?pretty=false")
.andReturn(HttpURLConnection.HTTP_OK, "hello")
.once();
KubernetesClient client = server.getClient();

// When
String log = client.apps().deployments().inNamespace("ns1").withName("deploy1").getLog();

// Then
assertNotNull(log);
assertEquals("hello", log);
}

private DeploymentBuilder getDeploymentBuilder() {
return new DeploymentBuilder()
.withNewMetadata()
.withName("deploy1")
.withUid("136581bf-82c2-4675-af05-63c55500b378")
.addToLabels("app", "nginx")
.addToAnnotations("app", "nginx")
.endMetadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.batch.Job;
import io.fabric8.kubernetes.api.model.batch.JobBuilder;
import io.fabric8.kubernetes.api.model.batch.JobList;
Expand All @@ -30,6 +35,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.net.HttpURLConnection;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -292,11 +298,49 @@ public void testCreateOrReplaceWithExistingJob() {
assertEquals("df842342-33bb-4f6c-9707-f76a86748ee6", job.getSpec().getTemplate().getMetadata().getLabels().get("controller-uid"));
}

@Test
@DisplayName("Should get logs for a job")
void testJobGetLog() {
// Given
Pod jobPod = new PodBuilder()
.withNewMetadata()
.withOwnerReferences(new OwnerReferenceBuilder().withApiVersion("batch/v1")
.withBlockOwnerDeletion(true)
.withController(true)
.withKind("Job")
.withName("pi")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.build())
.withName("job1-hk9nf").addToLabels("controller-uid", "3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.endMetadata()
.build();

server.expect().get().withPath("/apis/batch/v1/namespaces/ns1/jobs/job1")
.andReturn(HttpURLConnection.HTTP_OK, getJobBuilder().build())
.always();

server.expect().get().withPath("/api/v1/namespaces/ns1/pods?labelSelector=controller-uid%3D3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.andReturn(HttpURLConnection.HTTP_OK, new PodListBuilder().withItems(jobPod).build())
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/pods/job1-hk9nf/log?pretty=false")
.andReturn(HttpURLConnection.HTTP_OK, "hello")
.once();
KubernetesClient client = server.getClient();

// When
String log = client.batch().jobs().inNamespace("ns1").withName("job1").getLog();

// Then
assertNotNull(log);
assertEquals("hello", log);
}

private JobBuilder getJobBuilder() {
return new JobBuilder()
.withApiVersion("batch/v1")
.withNewMetadata()
.withName("job1")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.withLabels(Collections.singletonMap("label1", "maximum-length-of-63-characters"))
.withAnnotations(Collections.singletonMap("annotation1", "some-very-long-annotation"))
.endMetadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.apps.ControllerRevision;
import io.fabric8.kubernetes.api.model.apps.ControllerRevisionBuilder;
import io.fabric8.kubernetes.api.model.apps.ControllerRevisionListBuilder;
Expand Down Expand Up @@ -452,10 +456,48 @@ void testRolloutUndo() throws InterruptedException {
assertTrue(recordedRequest.getBody().readUtf8().contains("\"app\":\"rs1\""));
}

@Test
@DisplayName("Should test get logs from statefulset")
void testGetLogStatefulSet() {
// Given
Pod jobPod = new PodBuilder()
.withNewMetadata()
.withOwnerReferences(new OwnerReferenceBuilder().withApiVersion("batch/v1")
.withBlockOwnerDeletion(true)
.withController(true)
.withKind("Job")
.withName("pi")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.build())
.withName("job1-hk9nf").addToLabels("controller-uid", "3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.endMetadata()
.build();

server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/statefulsets/statefulset1")
.andReturn(HttpURLConnection.HTTP_OK, getStatefulSetBuilder().build())
.always();

server.expect().get().withPath("/api/v1/namespaces/ns1/pods/statefulset1?labelSelector=app%3Dnginx")
.andReturn(HttpURLConnection.HTTP_OK, new PodListBuilder().withItems(jobPod).build())
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/pods/job1-hk9nf/log?pretty=false")
.andReturn(HttpURLConnection.HTTP_OK, "hello")
.once();
KubernetesClient client = server.getClient();

// When
String log = client.apps().statefulSets().inNamespace("ns1").withName("statefulset1").getLog();

// Then
assertNotNull(log);
assertEquals("hello", log);
}

private StatefulSetBuilder getStatefulSetBuilder() {
return new StatefulSetBuilder()
.withNewMetadata()
.withName("statefulset1")
.withUid("3Dc4c8746c-94fd-47a7-ac01-11047c0323b4")
.addToLabels("app", "nginx")
.addToAnnotations("app", "nginx")
.endMetadata()
Expand Down

0 comments on commit ffdcd46

Please sign in to comment.