Java Code Examples for io.fabric8.kubernetes.api.model.LabelSelector#getMatchLabels()

The following examples show how to use io.fabric8.kubernetes.api.model.LabelSelector#getMatchLabels() . 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: DeploymentConfigEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private DeploymentConfigSpec getDeploymentConfigSpec(Integer replicas, Integer revisionHistoryLimit, LabelSelector selector, PodTemplateSpec podTemplateSpec, String strategyType) {
    DeploymentConfigSpecBuilder specBuilder = new DeploymentConfigSpecBuilder();
    if (replicas != null) {
        specBuilder.withReplicas(replicas);
    }
    if (revisionHistoryLimit != null) {
        specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
    }

    if (selector  != null) {
        Map<String, String> matchLabels = selector.getMatchLabels();
        if (matchLabels != null && !matchLabels.isEmpty()) {
            specBuilder.withSelector(matchLabels);
        }
    }
    if (podTemplateSpec != null) {
        specBuilder.withTemplate(podTemplateSpec);
        PodSpec podSpec = podTemplateSpec.getSpec();
        Objects.requireNonNull(podSpec, "No PodSpec for PodTemplate:" + podTemplateSpec);
        Objects.requireNonNull(podSpec, "No containers for PodTemplate.spec: " + podTemplateSpec);
    }
    DeploymentStrategy deploymentStrategy = getDeploymentStrategy(strategyType);
    if (deploymentStrategy != null) {
        specBuilder.withStrategy(deploymentStrategy);
    }

    if(enableAutomaticTrigger.equals(Boolean.TRUE)) {
        specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
    }

    return specBuilder.build();
}
 
Example 2
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 3
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 4
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;
}