Search in sources :

Example 6 with AppsV1Api

use of io.kubernetes.client.openapi.apis.AppsV1Api 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 7 with AppsV1Api

use of io.kubernetes.client.openapi.apis.AppsV1Api 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 8 with AppsV1Api

use of io.kubernetes.client.openapi.apis.AppsV1Api in project java by kubernetes-client.

the class KubectlScale method execute.

@Override
public ApiType execute() throws KubectlException {
    validate();
    String jsonPatchStr = String.format("[{\"op\":\"replace\",\"path\":\"/spec/replicas\",\"value\":%d}]", replicas);
    AppsV1Api api = new AppsV1Api(this.apiClient);
    try {
        if (apiTypeClass.equals(V1Deployment.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedDeploymentCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, // field-manager is optional
            null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else if (apiTypeClass.equals(V1ReplicaSet.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedReplicaSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else if (apiTypeClass.equals(V1StatefulSet.class)) {
            return PatchUtils.patch(apiTypeClass, () -> api.patchNamespacedStatefulSetCall(name, namespace, new V1Patch(jsonPatchStr), null, null, null, null, null, null), V1Patch.PATCH_FORMAT_JSON_PATCH, this.apiClient);
        } else {
            throw new KubectlException("Unsupported class for scale: " + apiTypeClass);
        }
    } catch (ApiException ex) {
        throw new KubectlException(ex);
    }
}
Also used : AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) V1Patch(io.kubernetes.client.custom.V1Patch) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) ApiException(io.kubernetes.client.openapi.ApiException)

Example 9 with AppsV1Api

use of io.kubernetes.client.openapi.apis.AppsV1Api in project twister2 by DSC-SPIDAL.

the class KubernetesController method initApiInstances.

private static void initApiInstances() {
    if (client == null) {
        getApiClient();
    }
    Configuration.setDefaultApiClient(client);
    coreApi = new CoreV1Api();
    appsApi = new AppsV1Api(client);
}
Also used : AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Aggregations

AppsV1Api (io.kubernetes.client.openapi.apis.AppsV1Api)9 V1Deployment (io.kubernetes.client.openapi.models.V1Deployment)7 ApiException (io.kubernetes.client.openapi.ApiException)5 V1Patch (io.kubernetes.client.custom.V1Patch)3 V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)3 V1DeploymentSpec (io.kubernetes.client.openapi.models.V1DeploymentSpec)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2 V1PodTemplateSpec (io.kubernetes.client.openapi.models.V1PodTemplateSpec)2 ArrayList (java.util.ArrayList)2 SneakyThrows (lombok.SneakyThrows)2 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)1 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 JSON (io.kubernetes.client.openapi.JSON)1 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1DeploymentBuilder (io.kubernetes.client.openapi.models.V1DeploymentBuilder)1 V1DeploymentList (io.kubernetes.client.openapi.models.V1DeploymentList)1 V1LabelSelector (io.kubernetes.client.openapi.models.V1LabelSelector)1 V1OwnerReference (io.kubernetes.client.openapi.models.V1OwnerReference)1