Java Code Examples for io.fabric8.kubernetes.api.model.ObjectMeta#getLabels()

The following examples show how to use io.fabric8.kubernetes.api.model.ObjectMeta#getLabels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KubernetesService.java    From vault-crd with Apache License 2.0 6 votes vote down vote up
private ObjectMeta metaData(ObjectMeta resource, String compare) {
    ObjectMeta meta = new ObjectMeta();
    meta.setNamespace(resource.getNamespace());
    meta.setName(resource.getName());
    if (resource.getLabels() != null) {
        meta.setLabels(resource.getLabels());
    }

    HashMap<String, String> annotations = new HashMap<>();
    if (resource.getAnnotations() != null) {
        annotations.putAll(resource.getAnnotations());
    }
    annotations.put(crdName + LAST_UPDATE_ANNOTATION, LocalDateTime.now().toString());
    annotations.put(crdName + COMPARE_ANNOTATION, compare);
    meta.setAnnotations(annotations);
    return meta;
}
 
Example 2
Source File: SelectorFilter.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns true is specified object is matched by specified selector, false otherwise.
 *
 * <p>An empty selector is considered to match anything.
 *
 * @param metadata object metadata to check matching
 * @param selector the selector to match the metadata with
 */
public static boolean test(@Nullable ObjectMeta metadata, Map<String, String> selector) {
  if (selector.isEmpty()) {
    // anything matches if we have nothing to select with
    return true;
  }

  if (metadata == null) {
    return false;
  }

  Map<String, String> labels = metadata.getLabels();
  if (labels == null) {
    return false;
  }

  return labels.entrySet().containsAll(selector.entrySet());
}
 
Example 3
Source File: PVCProvisioner.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Groups list of given PVCs by volume name. The result may be used for easy accessing to PVCs by
 * Che Volume name.
 */
private Map<String, PersistentVolumeClaim> groupByVolumeName(
    Collection<PersistentVolumeClaim> pvcs) {
  final Map<String, PersistentVolumeClaim> grouped = new HashMap<>();
  for (PersistentVolumeClaim pvc : pvcs) {
    final ObjectMeta metadata = pvc.getMetadata();
    final String volumeName;
    if (metadata.getLabels() != null
        && (volumeName = metadata.getLabels().get(CHE_VOLUME_NAME_LABEL)) != null) {
      grouped.put(volumeName, pvc);
    } else {
      grouped.put(metadata.getName(), pvc);
      putLabel(metadata, CHE_VOLUME_NAME_LABEL, metadata.getName());
    }
  }
  return grouped;
}
 
Example 4
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean containsLabelInMetadata(ObjectMeta metadata, String labelKey, String labelValue) {
    if (metadata != null && metadata.getLabels() != null) {
        Map<String, String> labels = metadata.getLabels();
        return labels.containsKey(labelKey) && labelValue.equals(labels.get(labelKey));
    }
    return false;
}
 
Example 5
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static ObjectMeta removeLabel(ObjectMeta metadata, String labelKey, String labelValue) {
    Map<String, String> labels;
    if (metadata != null) {
        labels = metadata.getLabels();
        if (labels != null && labelValue.equals(labels.get(labelKey))) {
            labels.remove(labelKey);
        }
    }
    return metadata;
}
 
Example 6
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static Map<String, String> getOrCreateLabels(HasMetadata entity) {
    ObjectMeta metadata = getOrCreateMetadata(entity);
    Map<String, String> answer = metadata.getLabels();
    if (answer == null) {
        // use linked so the annotations can be in the FIFO order
        answer = new LinkedHashMap<>();
        metadata.setLabels(answer);
    }
    return answer;
}
 
Example 7
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the labels of the given metadata object or an empty map if the metadata or labels are null
 *
 * @param metadata metadata object ObjectMeta
 * @return labels in form of a hashmap
 */
@SuppressWarnings("unchecked")
public static Map<String, String> getLabels(ObjectMeta metadata) {
    if (metadata != null) {
        Map<String, String> labels = metadata.getLabels();
        if (labels != null) {
            return labels;
        }
    }
    return Collections.EMPTY_MAP;
}
 
Example 8
Source File: K8sTopicWatcher.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Override
public void eventReceived(Action action, KafkaTopic kafkaTopic) {
    ObjectMeta metadata = kafkaTopic.getMetadata();
    Map<String, String> labels = metadata.getLabels();
    if (kafkaTopic.getSpec() != null) {
        LogContext logContext = LogContext.kubeWatch(action, kafkaTopic).withKubeTopic(kafkaTopic);
        String name = metadata.getName();
        String kind = kafkaTopic.getKind();
        if (!initReconcileFuture.isComplete()) {
            LOGGER.debug("Ignoring initial event for {} {} during initial reconcile", kind, name);
            return;
        }
        LOGGER.info("{}: event {} on resource {} generation={}, labels={}", logContext, action, name,
                metadata.getGeneration(), labels);
        Handler<AsyncResult<Void>> resultHandler = ar -> {
            if (ar.succeeded()) {
                LOGGER.info("{}: Success processing event {} on resource {} with labels {}", logContext, action, name, labels);
            } else {
                String message;
                if (ar.cause() instanceof InvalidTopicException) {
                    message = kind + " " + name + " has an invalid spec section: " + ar.cause().getMessage();
                    LOGGER.error("{}", message);

                } else {
                    message = "Failure processing " + kind + " watch event " + action + " on resource " + name + " with labels " + labels + ": " + ar.cause().getMessage();
                    LOGGER.error("{}: {}", logContext, message, ar.cause());
                }
                topicOperator.enqueue(topicOperator.new Event(kafkaTopic, message, TopicOperator.EventType.WARNING, errorResult -> { }));
            }
        };
        if (!action.equals(Action.ERROR)) {
            topicOperator.onResourceEvent(logContext, kafkaTopic, action).onComplete(resultHandler);
        } else {
            LOGGER.error("Watch received action=ERROR for {} {}", kind, name);
        }
    }
}
 
Example 9
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Checks if the specified object has the specified label. */
public static boolean isLabeled(HasMetadata source, String key, String value) {
  ObjectMeta metadata = source.getMetadata();

  if (metadata == null) {
    return false;
  }

  Map<String, String> labels = metadata.getLabels();
  if (labels == null) {
    return false;
  }

  return value.equals(labels.get(key));
}
 
Example 10
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Adds labels to target Kubernetes object. */
public static void putLabels(ObjectMeta metadata, Map<String, String> labels) {
  if (labels == null || labels.isEmpty()) {
    return;
  }

  Map<String, String> metaLabels = metadata.getLabels();
  if (metaLabels == null) {
    metadata.setLabels(new HashMap<>(labels));
  } else {
    metaLabels.putAll(labels);
  }
}
 
Example 11
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Adds label to target Kubernetes object. */
public static void putLabel(ObjectMeta metadata, String key, String value) {
  Map<String, String> labels = metadata.getLabels();
  if (labels == null) {
    metadata.setLabels(labels = new HashMap<>());
  }

  labels.put(key, value);
}
 
Example 12
Source File: KubernetesResourceUtil.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * Null safe get method for getting Labels of a Kubernetes Resource
 *
 * @param entity Kubernetes Resource
 * @return returns a hashmap containing labels
 */
public static Map<String, String> getOrCreateLabels(HasMetadata entity) {
  ObjectMeta metadata = getOrCreateMetadata(entity);
  Map<String, String> answer = metadata.getLabels();
  if (answer == null) {
    // use linked so the annotations can be in the FIFO order
    answer = new LinkedHashMap<>();
    metadata.setLabels(answer);
  }
  return answer;
}
 
Example 13
Source File: KubernetesResourceUtil.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the labels of the given metadata object or an empty map if the metadata or labels are null
 *
 * @param metadata ObjectMeta for resource's metadata
 * @return returns labels as a hashmap
 */

@SuppressWarnings("unchecked")
public static Map<String, String> getLabels(ObjectMeta metadata) {
  if (metadata != null) {
    Map<String, String> labels = metadata.getLabels();
    if (labels != null) {
      return labels;
    }
  }
  return Collections.EMPTY_MAP;
}