/** * Returns the labels from Map. */ public static Labels fromMap(Map<String, String> labels) { if (labels != null) { return new Labels(labels); } return EMPTY; }
/** * A singleton instance with the given {@code cluster} for the {@code strimzi.io/cluster} key. */ public static Labels forCluster(String cluster) { return new Labels(singletonMap(STRIMZI_CLUSTER_LABEL, cluster)); }
/** * A singleton instance with the given {@code kind} for the {@code strimzi.io/kind} key. */ public static Labels forKind(String kind) { return new Labels(singletonMap(STRIMZI_KIND_LABEL, kind)); }
private Labels without(String label) { Map<String, String> newLabels = new HashMap<>(labels); newLabels.remove(label); return new Labels(newLabels); }
public Labels strimziLabels() { Map<String, String> newLabels = new HashMap<>(3); for (Map.Entry<String, String> entry : labels.entrySet()) { if (entry.getKey().startsWith(STRIMZI_DOMAIN)) { newLabels.put(entry.getKey(), entry.getValue()); } } return new Labels(newLabels); }
private Labels with(String label, String value) { Map<String, String> newLabels = new HashMap<>(labels.size() + 1); newLabels.putAll(labels); newLabels.put(label, value); return new Labels(newLabels); }
public static Labels userLabels(Map<String, String> userLabels) { if (userLabels != null) { for (String key : userLabels.keySet()) { if (key.startsWith(STRIMZI_DOMAIN) && !key.equals(STRIMZI_KIND_LABEL)) { throw new IllegalArgumentException("User labels includes a Strimzi label that is not " + STRIMZI_KIND_LABEL + ": " + key); } } return new Labels(userLabels); } return EMPTY; }
public Labels withUserLabels(Map<String, String> userLabels) { Map<String, String> newLabels = new HashMap<>(labels.size()); newLabels.putAll(labels); newLabels.putAll(Labels.userLabels(userLabels).toMap()); return new Labels(newLabels); }
/** * Parse Labels from String into Labels object. The expected format of the String with labels is `key1=value1,key2=value2` * * @param stringLabels String with labels * @return Labels object with parsed labels * @throws IllegalArgumentException */ public static Labels fromString(String stringLabels) throws IllegalArgumentException { Map<String, String> labels = new HashMap<>(); try { if (stringLabels != null && !stringLabels.isEmpty()) { String[] labelsArray = stringLabels.split(","); for (String label : labelsArray) { String[] fields = label.split("="); labels.put(fields[0].trim(), fields[1].trim()); } } } catch (Exception e) { throw new IllegalArgumentException("Failed to parse labels from string " + stringLabels, e); } return new Labels(labels); }
/** * Returns the labels of the given {@code resource}. */ public static Labels fromResource(HasMetadata resource) { return new Labels(resource.getMetadata().getLabels() != null ? resource.getMetadata().getLabels() : emptyMap()); }