Skip to content

Commit

Permalink
Merge pull request fabric8io#2498 from rohanKanojia/pr/issue2460
Browse files Browse the repository at this point in the history
  • Loading branch information
fusesource-ci committed Sep 18, 2020
2 parents 3f4785c + ac65b92 commit 509364b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,7 @@
* Fix #2111: Support automatic refreshing for expired OIDC tokens
* Fix #2314: Fetch logs should wait for the job's associated pod to be ready
* Fix #2043: Support for Tekton Triggers
* Fix #2460: Querying for an event based on InvolvedObject fields

### 4.11.1 (2020-09-02)

Expand Down
Expand Up @@ -16,8 +16,7 @@
package io.fabric8.kubernetes.client.dsl;

import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.api.model.ObjectReference;

import java.util.Map;

Expand Down Expand Up @@ -48,5 +47,12 @@ public interface Filterable<T> {
T withoutField(String key, String value);

T withLabelSelector(LabelSelector selector);

/**
* Filter with the object that this event is about.
* @param objectReference {@link ObjectReference} for providing information of referred object
* @return filtered resource
*/
T withInvolvedObject(ObjectReference objectReference);
}

Expand Up @@ -15,6 +15,7 @@
*/
package io.fabric8.kubernetes.client.dsl.base;

import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -87,6 +88,13 @@ public class BaseOperation<T extends HasMetadata, L extends KubernetesResourceLi
Resource<T, D> {

private static final Logger LOG = LoggerFactory.getLogger(BaseOperation.class);
private static final String INVOLVED_OBJECT_NAME = "involvedObject.name";
private static final String INVOLVED_OBJECT_NAMESPACE = "involvedObject.namespace";
private static final String INVOLVED_OBJECT_KIND = "involvedObject.kind";
private static final String INVOLVED_OBJECT_UID = "involvedObject.uid";
private static final String INVOLVED_OBJECT_RESOURCE_VERSION = "involvedObject.resourceVersion";
private static final String INVOLVED_OBJECT_API_VERSION = "involvedObject.apiVersion";
private static final String INVOLVED_OBJECT_FIELD_PATH = "involvedObject.fieldPath";

private final Boolean cascading;
private final T item;
Expand Down Expand Up @@ -514,6 +522,35 @@ public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withField(Stri
return this;
}

@Override
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withInvolvedObject(ObjectReference objectReference) {
if (objectReference != null) {
if (objectReference.getName() != null) {
fields.put(INVOLVED_OBJECT_NAME, objectReference.getName());
}
if (objectReference.getNamespace() != null) {
fields.put(INVOLVED_OBJECT_NAMESPACE, objectReference.getNamespace());
}
if (objectReference.getKind() != null) {
fields.put(INVOLVED_OBJECT_KIND, objectReference.getKind());
}
if (objectReference.getUid() != null) {
fields.put(INVOLVED_OBJECT_UID, objectReference.getUid());
}
if (objectReference.getResourceVersion() != null) {
fields.put(INVOLVED_OBJECT_RESOURCE_VERSION, objectReference.getResourceVersion());
}
if (objectReference.getApiVersion() != null) {
fields.put(INVOLVED_OBJECT_API_VERSION, objectReference.getApiVersion());
}
if (objectReference.getFieldPath() != null) {
fields.put(INVOLVED_OBJECT_FIELD_PATH, objectReference.getFieldPath());
}
}
return this;
}


/**
* @deprecated as the underlying implementation does not align with the arguments fully.
* Method is created to have a similar API as `withoutLabels`, but should eventually be replaced
Expand Down
Expand Up @@ -18,6 +18,7 @@
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.ListOptions;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
Expand Down Expand Up @@ -180,6 +181,9 @@ private FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> get
@Override
public FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withLabelSelector(LabelSelector selector) { return null; }

@Override
public FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withInvolvedObject(ObjectReference objectReference) { return null; }

@Override
public PodList list() { return getMockPodList(controllerUid); }

Expand Down
Expand Up @@ -17,20 +17,27 @@

import io.fabric8.kubernetes.api.model.Event;
import io.fabric8.kubernetes.api.model.EventBuilder;
import io.fabric8.kubernetes.api.model.EventList;
import io.fabric8.kubernetes.api.model.EventListBuilder;
import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder;
import io.fabric8.kubernetes.api.model.WatchEvent;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.Rule;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.net.HttpURLConnection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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

@EnableRuleMigrationSupport
Expand Down Expand Up @@ -74,4 +81,31 @@ public void onClose(KubernetesClientException cause) {
assertTrue(eventReceivedLatch.await(1, TimeUnit.SECONDS));
watch.close();
}

@Test
void testInvolvedObjectFieldQuery() {
// Given
server.expect().get().withPath("/api/v1/namespaces/ns1/events?fieldSelector="
+ Utils.toUrlEncoded("involvedObject.name=foo," +
"involvedObject.uid=6d71451a-f8df-11ea-a8ac-0e13a02d8ebd," +
"involvedObject.namespace=ns1," +
"involvedObject.kind=Deployment"))
.andReturn(HttpURLConnection.HTTP_OK, new EventListBuilder().withItems(new EventBuilder()
.withNewMetadata().withName("foo-event").endMetadata()
.build())
.build()).once();
KubernetesClient client = server.getClient();

// When
EventList eventList = client.v1().events().inNamespace("ns1").withInvolvedObject(new ObjectReferenceBuilder()
.withName("foo")
.withNamespace("ns1")
.withKind("Deployment")
.withUid("6d71451a-f8df-11ea-a8ac-0e13a02d8ebd")
.build()).list();

// Then
assertNotNull(eventList);
assertEquals(1, eventList.getItems().size());
}
}

0 comments on commit 509364b

Please sign in to comment.