Search in sources :

Example 1 with V1ReplicaSet

use of io.kubernetes.client.openapi.models.V1ReplicaSet in project java by kubernetes-client.

the class DeploymentHelperTest method testGetAllReplicaSetsShouldWork.

@Test
public void testGetAllReplicaSetsShouldWork() throws IOException, ApiException {
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
    AppsV1Api api = new AppsV1Api(this.apiClient);
    V1Deployment deployment = new JSON().deserialize(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))), V1Deployment.class);
    List<V1ReplicaSet> oldRSes = new ArrayList<>();
    List<V1ReplicaSet> allOldRSes = new ArrayList<>();
    V1ReplicaSet newRs = DeploymentHelper.getAllReplicaSets(deployment, api, oldRSes, allOldRSes);
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
    Assert.assertNotNull(newRs);
    Assert.assertEquals(1, oldRSes.size());
    Assert.assertEquals(2, allOldRSes.size());
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) ArrayList(java.util.ArrayList) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) JSON(io.kubernetes.client.openapi.JSON) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) Test(org.junit.Test)

Example 2 with V1ReplicaSet

use of io.kubernetes.client.openapi.models.V1ReplicaSet in project java by kubernetes-client.

the class DeploymentHelperTest method testRevisionShouldWork.

@Test
public void testRevisionShouldWork() throws IOException {
    V1ReplicaSetList replicaSetList = new JSON().deserialize(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))), V1ReplicaSetList.class);
    List<Long> revisions = new ArrayList<>();
    for (V1ReplicaSet rs : replicaSetList.getItems()) {
        revisions.add(DeploymentHelper.revision(rs.getMetadata()));
    }
    revisions.sort(Long::compareTo);
    List<Long> exceptRevisions = Arrays.asList(1L, 2L, 2L, 3L, 4L);
    Assert.assertEquals(exceptRevisions, revisions);
}
Also used : ArrayList(java.util.ArrayList) JSON(io.kubernetes.client.openapi.JSON) V1ReplicaSetList(io.kubernetes.client.openapi.models.V1ReplicaSetList) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) Test(org.junit.Test)

Example 3 with V1ReplicaSet

use of io.kubernetes.client.openapi.models.V1ReplicaSet 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)

Example 4 with V1ReplicaSet

use of io.kubernetes.client.openapi.models.V1ReplicaSet in project java by kubernetes-client.

the class DeploymentHelper method getAllReplicaSets.

/**
 * getAllReplicaSets get the old replica sets and return new replica targeted by the given
 * Deployment. It gets PodList and ReplicaSetList from client interface. Note that the first set
 * of old replica sets doesn't include the ones with no pods, and the second set of old replica
 * sets include all old replica sets. The returned value is the new replica set, and it may be nil
 * if it doesn't exist yet.
 *
 * @param deployment the given deployment
 * @param api the client interface
 * @param oldRSes container of all old ReplicaSet exclude the ones with no pods
 * @param allOldRSes container of all old ReplicaSet
 * @return the new replica set
 */
public static V1ReplicaSet getAllReplicaSets(V1Deployment deployment, AppsV1Api api, List<V1ReplicaSet> oldRSes, List<V1ReplicaSet> allOldRSes) throws ApiException {
    List<V1ReplicaSet> rsList = listReplicaSets(deployment, api);
    V1ReplicaSet newRs = findNewReplicaSet(deployment, rsList);
    findOldReplicaSets(rsList, newRs, oldRSes, allOldRSes);
    return newRs;
}
Also used : V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet)

Example 5 with V1ReplicaSet

use of io.kubernetes.client.openapi.models.V1ReplicaSet in project java by kubernetes-client.

the class KubectlScaleTest method testKubectlScaleReplicaSetShouldWork.

@Test
public void testKubectlScaleReplicaSetShouldWork() throws KubectlException {
    wireMockRule.stubFor(patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets/foo")).willReturn(aResponse().withStatus(200).withBody("{\"metadata\":{\"name\":\"foo\",\"namespace\":\"default\"}}")));
    V1ReplicaSet scaled = Kubectl.scale(V1ReplicaSet.class).apiClient(apiClient).replicas(4).name("foo").namespace("default").execute();
    wireMockRule.verify(1, patchRequestedFor(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets/foo")).withRequestBody(equalToJson("[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":4}]")));
    assertNotNull(scaled);
}
Also used : V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) Test(org.junit.Test)

Aggregations

V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)19 Type (java.lang.reflect.Type)14 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 JSON (io.kubernetes.client.openapi.JSON)2 AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)2 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)2 V1ReplicaSetList (io.kubernetes.client.openapi.models.V1ReplicaSetList)2 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)1 ApiException (io.kubernetes.client.openapi.ApiException)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 Yaml (io.kubernetes.client.util.Yaml)1 LabelSelector (io.kubernetes.client.util.labels.LabelSelector)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1