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

org.jacpfx.vxms.k8s.client.VxmsDiscoveryK8SImpl Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 * Copyright [2018] [Andy Moncsek]
 *
 * 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 org.jacpfx.vxms.k8s.client;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import org.jacpfx.vxms.common.util.ConfigurationUtil;
import org.jacpfx.vxms.k8s.annotation.K8SDiscovery;
import org.jacpfx.vxms.k8s.api.CustomClientConfig;
import org.jacpfx.vxms.k8s.util.StringUtil;
import org.jacpfx.vxms.k8s.util.TokenUtil;
import org.jacpfx.vxms.spi.ServiceDiscoverySPI;

/**
 *
 * The Kubernetes service discovery implementation for vxms
 * @author Andy Moncsek
 */
public class VxmsDiscoveryK8SImpl implements ServiceDiscoverySPI {

  public static final String USER = "user";
  public static final String PASSWORD = "password";
  public static final String API_TOKEN = "api_token";
  public static final String MASTER_URL = "master_url";
  public static final String NAMESPACE = "namespace";
  public static final String CUSTOM_CLIENT_CONFIGURATION = "customClientConfiguration";

  @Override
  public void initDiscovery(AbstractVerticle service) {
    final JsonObject config = service.config();
    final Vertx vertx = service.getVertx();
    if (!service.getClass().isAnnotationPresent(K8SDiscovery.class))
      return;
    final K8SDiscovery annotation = service.getClass().getAnnotation(K8SDiscovery.class);
    final String customClientClassName =
        ConfigurationUtil.getStringConfiguration(
            config,
            CUSTOM_CLIENT_CONFIGURATION,
            annotation.customClientConfiguration().getCanonicalName());
    final CustomClientConfig custConf = getCustomConfiguration(customClientClassName);
    final Config customConfiguration = custConf.createCustomConfiguration(vertx);
    if (customConfiguration == null) {
      final String master_url =
          ConfigurationUtil.getStringConfiguration(config, MASTER_URL, annotation.master_url());
      final String namespace =
          ConfigurationUtil.getStringConfiguration(config, NAMESPACE, annotation.namespace());
      final Config kubeConfig =
          new ConfigBuilder().withMasterUrl(master_url).withNamespace(namespace).build();

      updateKubeConfig(kubeConfig, config, annotation);
      // 1.) Check from K8SDiscovery Annotation
      // 1.1) read properties and from Annotation or from configuration
      // 2.) init KubernetesClient
      KubeDiscovery.resolveBeanAnnotations(service, kubeConfig);
    } else {
      updateKubeConfig(customConfiguration, config, annotation);
      KubeDiscovery.resolveBeanAnnotations(service, customConfiguration);
    }
  }

  private CustomClientConfig getCustomConfiguration(String customClientClassName) {
    CustomClientConfig custConf = null;
    try {
      final Class optionsClazz =
          (Class) Class.forName(customClientClassName);
      custConf = optionsClazz.newInstance();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    }

    return custConf;
  }

  /**
   * Init discovery with provided Kubernetes Client configuration
   * @param service the service to initialize
   * @param kubeConfig the kubernetes client configuration
   */
  public void initDiscovery(AbstractVerticle service, Config kubeConfig) {
    final JsonObject config = service.config();
    if (!service.getClass().isAnnotationPresent(K8SDiscovery.class))
      return;
    final K8SDiscovery annotation = service.getClass().getAnnotation(K8SDiscovery.class);
    updateKubeConfig(kubeConfig, config, annotation);
    // 1.) Check from K8SDiscovery Annotation
    // 1.1) read properties and from Annotation or from configuration
    // 2.) init KubernetesClient
    KubeDiscovery.resolveBeanAnnotations(service, kubeConfig);
  }

  private void updateKubeConfig(Config kubeConfig, JsonObject config, K8SDiscovery annotation) {
    final String user = ConfigurationUtil.getStringConfiguration(config, USER, annotation.user());
    final String password =
        ConfigurationUtil.getStringConfiguration(config, PASSWORD, annotation.password());
    final String api_token =
        ConfigurationUtil.getStringConfiguration(config, API_TOKEN, annotation.api_token());
    final String master_url =
        ConfigurationUtil.getStringConfiguration(config, MASTER_URL, annotation.master_url());
    final String namespace =
        ConfigurationUtil.getStringConfiguration(config, NAMESPACE, annotation.namespace());
    if (StringUtil.isNullOrEmpty(kubeConfig.getUsername())) kubeConfig.setUsername(user);
    if (StringUtil.isNullOrEmpty(kubeConfig.getPassword())) kubeConfig.setPassword(password);
    if (StringUtil.isNullOrEmpty(kubeConfig.getOauthToken())) kubeConfig.setOauthToken(api_token);
    if (StringUtil.isNullOrEmpty(kubeConfig.getMasterUrl())) kubeConfig.setMasterUrl(master_url);
    if (StringUtil.isNullOrEmpty(kubeConfig.getNamespace())) kubeConfig.setNamespace(namespace);
    // check oauthToken
    if (StringUtil.isNullOrEmpty(kubeConfig.getOauthToken()))
      kubeConfig.setOauthToken(TokenUtil.getAccountToken());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy