Search in sources :

Example 1 with LabelSelector

use of io.kubernetes.client.util.labels.LabelSelector in project java by kubernetes-client.

the class DeploymentHelper method listReplicaSets.

/**
 * listReplicaSets returns a list of RSes the given deployment targets. Note that this does NOT
 * attempt to reconcile ControllerRef (adopt/orphan), because only the controller itself should do
 * that. However, it does filter out anything whose ControllerRef doesn't match.
 */
private static List<V1ReplicaSet> listReplicaSets(V1Deployment deployment, AppsV1Api api) throws ApiException {
    String namespace = deployment.getMetadata().getNamespace();
    LabelSelector selector = LabelSelector.parse(deployment.getSpec().getSelector());
    List<V1ReplicaSet> all = rsListFromClient(namespace, selector.toString(), api);
    List<V1ReplicaSet> owned = new ArrayList<>(all.size());
    for (V1ReplicaSet rs : all) {
        List<V1OwnerReference> refs = rs.getMetadata().getOwnerReferences();
        Optional<V1OwnerReference> ref = refs.stream().filter(o -> o.getController() != null && o.getController()).findAny();
        // Only include those whose ControllerRef matches the Deployment.
        if (ref.isPresent() && ref.get().getUid().equals(deployment.getMetadata().getUid())) {
            owned.add(rs);
        }
    }
    return owned;
}
Also used : V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) V1PodTemplateSpec(io.kubernetes.client.openapi.models.V1PodTemplateSpec) Yaml(io.kubernetes.client.util.Yaml) LabelSelector(io.kubernetes.client.util.labels.LabelSelector) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) ArrayList(java.util.ArrayList) Objects(java.util.Objects) ApiException(io.kubernetes.client.openapi.ApiException) List(java.util.List) V1ReplicaSetList(io.kubernetes.client.openapi.models.V1ReplicaSetList) V1OwnerReference(io.kubernetes.client.openapi.models.V1OwnerReference) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Optional(java.util.Optional) V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) ArrayList(java.util.ArrayList) LabelSelector(io.kubernetes.client.util.labels.LabelSelector) V1OwnerReference(io.kubernetes.client.openapi.models.V1OwnerReference) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet)

Aggregations

ApiException (io.kubernetes.client.openapi.ApiException)1 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)1 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)1 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)1 V1OwnerReference (io.kubernetes.client.openapi.models.V1OwnerReference)1 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)1 V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)1 V1ReplicaSetList (io.kubernetes.client.openapi.models.V1ReplicaSetList)1 Yaml (io.kubernetes.client.util.Yaml)1 LabelSelector (io.kubernetes.client.util.labels.LabelSelector)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1