io.fabric8.kubernetes.api.model.LabelSelectorRequirement Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.LabelSelectorRequirement. 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: DeploymentRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(Deployment obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #2
Source File: StatefulSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(StatefulSet obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #3
Source File: ReplicaSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(ReplicaSet obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #4
Source File: KubernetesClientUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withSelector(NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods, LabelSelector selector, KitLogger log) {
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> answer = pods;
    Map<String, String> matchLabels = selector.getMatchLabels();
    if (matchLabels != null && !matchLabels.isEmpty()) {
        answer = answer.withLabels(matchLabels);
    }
    List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
    if (matchExpressions != null) {
        for (LabelSelectorRequirement expression : matchExpressions) {
            String key = expression.getKey();
            List<String> values = expression.getValues();
            if (StringUtils.isBlank(key)) {
                log.warn("Ignoring empty key in selector expression %s", expression);
                continue;
            }
            if (values == null || values.isEmpty()) {
                log.warn("Ignoring empty values in selector expression %s", expression);
                continue;
            }
            String[] valuesArray = values.toArray(new String[values.size()]);
            String operator = expression.getOperator();
            switch (operator) {
            case "In":
                answer = answer.withLabelIn(key, valuesArray);
                break;
            case "NotIn":
                answer = answer.withLabelNotIn(key, valuesArray);
                break;
            default:
                log.warn("Ignoring unknown operator %s in selector expression %s", operator, expression);
            }
        }
    }
    return answer;
}
 
Example #5
Source File: KubernetesHelper.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> withSelector(NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods, LabelSelector selector, KitLogger log) {
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> answer = pods;
    Map<String, String> matchLabels = selector.getMatchLabels();
    if (matchLabels != null && !matchLabels.isEmpty()) {
        answer = answer.withLabels(matchLabels);
    }
    List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
    if (matchExpressions != null) {
        for (LabelSelectorRequirement expression : matchExpressions) {
            String key = expression.getKey();
            List<String> values = expression.getValues();
            if (StringUtils.isBlank(key)) {
                log.warn("Ignoring empty key in selector expression %s", expression);
                continue;
            }
            if (values == null || values.isEmpty()) {
                log.warn("Ignoring empty values in selector expression %s", expression);
                continue;
            }
            String[] valuesArray = values.toArray(new String[values.size()]);
            String operator = expression.getOperator();
            switch (operator) {
                case "In":
                    answer = answer.withLabelIn(key, valuesArray);
                    break;
                case "NotIn":
                    answer = answer.withLabelNotIn(key, valuesArray);
                    break;
                default:
                    log.warn("Ignoring unknown operator %s in selector expression %s", operator, expression);
            }
        }
    }
    return answer;
}
 
Example #6
Source File: Pods.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link PodList} that match the specified {@link Deployment}.
 * @param deployment The {@link Deployment}
 */
protected PodList map(Deployment deployment) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = client.pods()
      .inNamespace(deployment.getMetadata().getNamespace());
  if (deployment.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(deployment.getSpec().getSelector().getMatchLabels());
  }
  if (deployment.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : deployment.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
      case "In":
        podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[] {}));
        break;
      case "NotIn":
        podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[] {}));
        break;
      case "DoesNotExist":
        podLister.withoutLabel(req.getKey());
        break;
      case "Exists":
        podLister.withLabel(req.getKey());
        break;
      }
    }
  }
  return podLister.list();
}
 
Example #7
Source File: Pods.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link PodList} that match the specified {@link ReplicaSet}.
 *
 * @param replicaSet The {@link ReplicaSet}
 */
protected PodList map(ReplicaSet replicaSet) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = client.pods()
      .inNamespace(replicaSet.getMetadata().getNamespace());
  if (replicaSet.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(replicaSet.getSpec().getSelector().getMatchLabels());
  }
  if (replicaSet.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : replicaSet.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
      case "In":
        podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[] {}));
        break;
      case "NotIn":
        podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[] {}));
        break;
      case "DoesNotExist":
        podLister.withoutLabel(req.getKey());
        break;
      case "Exists":
        podLister.withLabel(req.getKey());
        break;
      }
    }
  }
  return podLister.list();
}
 
Example #8
Source File: BaseOperation.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withLabelSelector(LabelSelector selector) {
  Map<String, String> matchLabels = selector.getMatchLabels();
  if (matchLabels != null) {
    this.labels.putAll(matchLabels);
  }
  List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
  if (matchExpressions != null) {
    for (LabelSelectorRequirement req : matchExpressions) {
      String operator = req.getOperator();
      String key = req.getKey();
      switch (operator) {
        case "In":
          withLabelIn(key, req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          withLabelNotIn(key, req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          withoutLabel(key);
          break;
        case "Exists":
          withLabel(key);
          break;
        default:
          throw new IllegalArgumentException("Unsupported operator: " + operator);
      }
    }
  }
  return this;
}
 
Example #9
Source File: K8sNetworkPolicyHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private Set<Pod> podsFromPolicyPeer(NetworkPolicyPeer peer, String namespace) {
    Set<Pod> pods = Sets.newConcurrentHashSet();
    if (peer.getPodSelector() != null) {
        Map<String, String> podLabels = peer.getPodSelector().getMatchLabels();
        List<LabelSelectorRequirement> matchExps = peer.getPodSelector().getMatchExpressions();

        if (podLabels == null && matchExps.size() == 0) {
            k8sPodService.pods().stream()
                    .filter(pod -> pod.getMetadata().getNamespace().equals(
                            namespace))
                    .forEach(pods::add);
        } else {
            k8sPodService.pods().stream()
                    .filter(pod -> pod.getMetadata().getNamespace().equals(
                            namespace))
                    .forEach(pod -> {
                        pod.getMetadata().getLabels().forEach((k, v) -> {
                            if (podLabels != null && podLabels.get(k) != null &&
                                    podLabels.get(k).equals(v)) {
                                pods.add(pod);
                            }
                        });
                    });
        }
    }
    return pods;
}