|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.examples.kubectl.equivalents; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.api.model.Event; |
| 19 | +import io.fabric8.kubernetes.api.model.ObjectReference; |
| 20 | +import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder; |
| 21 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 22 | +import io.fabric8.kubernetes.client.KubernetesClientBuilder; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * This example is Java equivalent to `kubectl events --for job/jobName`, or |
| 28 | + * `kubectl get events --field-selector involvedObject.kind=Job,involvedObject.name=jobName,involvedObject.apiVersion=batch/v1`. |
| 29 | + * It gets events related to a specified Job in a Kubernetes cluster. |
| 30 | + * <p> |
| 31 | + * This example demonstrates how to retrieve events for a specific Kubernetes resource. |
| 32 | + * The same approach can be adapted to fetch events for other resource types, |
| 33 | + * such as Pods, Deployments, or Services, by modifying the {@link ObjectReference} |
| 34 | + * to point to the desired resource. You would need to change the `apiVersion`, |
| 35 | + * `kind`, and `name` fields in the {@link ObjectReferenceBuilder} accordingly. |
| 36 | + * </p> |
| 37 | + */ |
| 38 | +public class EventsGetForJobEquivalent { |
| 39 | + private static final Logger logger = LoggerFactory.getLogger(EventsGetForJobEquivalent.class); |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + try (final KubernetesClient k8s = new KubernetesClientBuilder().build()) { |
| 43 | + ObjectReference objectReference = new ObjectReferenceBuilder().withApiVersion("batch/v1") |
| 44 | + .withKind("Job") |
| 45 | + .withName("jobName") |
| 46 | + .build(); |
| 47 | + |
| 48 | + k8s.v1() |
| 49 | + .events() |
| 50 | + .inNamespace("default") |
| 51 | + .withInvolvedObject(objectReference) |
| 52 | + .list() |
| 53 | + .getItems() |
| 54 | + .stream() |
| 55 | + .map(Event::getMessage) |
| 56 | + .forEach(logger::info); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments