All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.openshift.examples.DeploymentConfigScale Maven / Gradle / Ivy

There is a newer version: 6.13.3
Show newest version
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.fabric8.openshift.examples;

import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigList;
import io.fabric8.openshift.api.model.DeploymentConfigSpec;
import io.fabric8.openshift.client.OpenShiftAPIGroups;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.dsl.DeployableScalableResource;

import java.util.List;
import java.util.Objects;

public class DeploymentConfigScale {
  public static void main(String[] args) {
    if (args.length < 2) {
      System.err.println("Usage nameOfDC replicas");
      return;
    }
    String name = args[0];
    String replicaText = args[1];
    int replicas;
    try {
      replicas = Integer.parseInt(replicaText);
    } catch (NumberFormatException e) {
      System.out.println("Could not parse integer " + replicaText + " due to: " + e);
      return;
    }
    try (OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class)) {
      Integer oldReplicas = 0;
      if (!client.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.APPS)) {
        System.out.println("WARNING this cluster does not support the API Group " + OpenShiftAPIGroups.APPS);
        return;
      }
      DeployableScalableResource resource = client.deploymentConfigs().withName(name);
      DeploymentConfig deploymentConfig = resource.get();
      if (deploymentConfig == null) {
        System.out.println("Could not find a DeploymentConfig for name: " + name);
        return;
      }
      try {
        DeploymentConfigSpec spec = deploymentConfig.getSpec();
        oldReplicas = spec.getReplicas();
        spec.setReplicas(replicas);
        DeploymentConfig updated = resource.patch(deploymentConfig);
        System.out.println("Updated the DeploymentConfig " + name + " version: " + deploymentConfig.getApiVersion()
            + " with replicas: " + replicas + " to resourceVersion: " + updated.getMetadata().getResourceVersion());
      } catch (Exception e) {
        System.out.println("Failed to update the DeploymentConfig " + name + " with replicas: " + replicas);
        e.printStackTrace();
      }

      // now lets find the DC via list
      DeploymentConfigList list = Objects.requireNonNull(client.deploymentConfigs().list(),
          "No DeploymentConfigList returned");
      List items = Objects.requireNonNull(list.getItems(),
          "No DeploymentConfigList.getItems() returned");

      DeploymentConfig found = null;
      for (DeploymentConfig item : items) {
        if (name.equals(item.getMetadata().getName())) {
          found = item;
          break;
        }
      }
      Objects.requireNonNull(found, "Could not find DeploymentConfig in list.getItems() for name: " + name);
      found.getSpec().setReplicas(oldReplicas);

      try {
        DeploymentConfig updated = resource.patch(found);
        System.out.println("Updated the list.item DeploymentConfig " + name + " version: " + found.getApiVersion()
            + " with replicas: " + oldReplicas + " to resourceVersion: " + updated.getMetadata().getResourceVersion());
      } catch (Exception e) {
        System.out.println("Failed to update the list.item DeploymentConfig " + name + " with replicas: " + oldReplicas);
        e.printStackTrace();
      }

    } catch (KubernetesClientException e) {
      System.out.println("Failed: " + e);
      e.printStackTrace();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy