gyfora commented on code in PR #213: URL: https://github.com/apache/flink-kubernetes-operator/pull/213#discussion_r873196927
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/EventUtils.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.kubernetes.operator.utils; + +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.HasMetadata; +import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder; +import io.fabric8.kubernetes.client.KubernetesClient; + +import javax.annotation.Nullable; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; + +/** The util to generate an event for the target resource. */ +public class EventUtils { + + private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT; + public static final String TYPE_NORMAL = "Normal"; + public static final String TYPE_WARNING = "Warning"; + + public static EventList getEvents(HasMetadata target, KubernetesClient client) { + var object = + new ObjectReferenceBuilder() + .withKind(target.getKind()) + .withUid(target.getMetadata().getUid()) + .withName(target.getMetadata().getName()) + .withNamespace(target.getMetadata().getNamespace()) + .build(); + return client.v1() + .events() + .inNamespace(target.getMetadata().getNamespace()) + .withInvolvedObject(object) + .list(); + } + + public static Event createOrUpdateEvent( + KubernetesClient client, + HasMetadata target, + String type, + String reason, + String message, + @Nullable Event lastEvent) { + + if (lastEvent == null) { + var event = + new EventBuilder() + .withApiVersion("v1") + .withInvolvedObject( + new ObjectReferenceBuilder() + .withKind(target.getKind()) + .withUid(target.getMetadata().getUid()) + .withName(target.getMetadata().getName()) + .withNamespace(target.getMetadata().getNamespace()) + .build()) + .withType(type) + .withReason(reason) + .withFirstTimestamp(ZonedDateTime.now().format(formatter)) + .withLastTimestamp(ZonedDateTime.now().format(formatter)) Review Comment: I think `Instant.now().toString()` should do the trick also -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
