Skip to content

Commit

Permalink
Merge pull request #1466 from wind57/minor-changes-in-event-type
Browse files Browse the repository at this point in the history
minor improvement
  • Loading branch information
k8s-ci-robot committed Jan 4, 2021
2 parents 5c35615 + 7fe4217 commit 2e8d23b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
23 changes: 22 additions & 1 deletion util/src/main/java/io/kubernetes/client/informer/EventType.java
Expand Up @@ -12,6 +12,12 @@
*/
package io.kubernetes.client.informer;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public enum EventType {
ADDED,

Expand All @@ -23,12 +29,17 @@ public enum EventType {

ERROR;

private static final Map<String, EventType> TYPES =
Arrays.stream(EventType.values()).collect(Collectors.toMap(Enum::name, Function.identity()));

/**
* getByType returns the corresponding EventType by type.
* returns the corresponding EventType by type.
*
* @param type specific code
* @return corresponding EventType
* @deprecated will be removed in a future release. use : findByType
*/
@Deprecated
public static EventType getByType(String type) {
if (type != null && type.length() > 0) {
for (EventType eventType : EventType.values()) {
Expand All @@ -39,4 +50,14 @@ public static EventType getByType(String type) {
}
return null;
}

/**
* returns the corresponding EventType by type, wrapped in an {@link Optional}
*
* @param type specific code
* @return an Optional describing the EventType
*/
public static Optional<EventType> findByType(String type) {
return Optional.ofNullable(TYPES.get(String.valueOf(type).toUpperCase()));
}
}
Expand Up @@ -25,6 +25,7 @@
import java.net.ConnectException;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import org.slf4j.Logger;
Expand Down Expand Up @@ -186,12 +187,12 @@ private void watchHandler(Watchable<ApiType> watch) {
while (watch.hasNext()) {
io.kubernetes.client.util.Watch.Response<ApiType> item = watch.next();

EventType eventType = EventType.getByType(item.type);
if (eventType == null) {
Optional<EventType> eventType = EventType.findByType(item.type);
if (!eventType.isPresent()) {
log.error("unrecognized event {}", item);
continue;
}
if (eventType == EventType.ERROR) {
if (eventType.get() == EventType.ERROR) {
String errorMessage =
String.format("got ERROR event and its status: %s", item.status.toString());
log.error(errorMessage);
Expand All @@ -203,7 +204,7 @@ private void watchHandler(Watchable<ApiType> watch) {
V1ObjectMeta meta = obj.getMetadata();

String newResourceVersion = meta.getResourceVersion();
switch (eventType) {
switch (eventType.get()) {
case ADDED:
store.add(obj);
break;
Expand Down

0 comments on commit 2e8d23b

Please sign in to comment.