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

io.alauda.devops.client.DefaultAlaudaDevOpsClient Maven / Gradle / Ivy

There is a newer version: 0.2.10
Show newest version
/**
 * Copyright (C) 2018 Alauda
 *
 * 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.alauda.devops.client;

import io.alauda.kubernetes.api.model.*;
import io.alauda.kubernetes.api.model.apiextensions.CustomResourceDefinition;
import io.alauda.kubernetes.api.model.apiextensions.CustomResourceDefinitionList;
import io.alauda.kubernetes.api.model.apiextensions.DoneableCustomResourceDefinition;
import io.alauda.kubernetes.api.model.extensions.DoneablePodSecurityPolicy;
import io.alauda.kubernetes.api.model.extensions.PodSecurityPolicy;
import io.alauda.kubernetes.api.model.extensions.PodSecurityPolicyList;
import io.alauda.devops.client.dsl.*;
import io.alauda.devops.client.dsl.internal.*;
import io.alauda.kubernetes.client.AppsAPIGroupClient;
import io.alauda.kubernetes.client.AutoscalingAPIGroupClient;
import io.alauda.kubernetes.client.RequestConfig;
import io.alauda.kubernetes.client.dsl.AppsAPIGroupDSL;
import io.alauda.kubernetes.client.dsl.AutoscalingAPIGroupDSL;
import io.alauda.kubernetes.client.dsl.FunctionCallable;
import io.alauda.kubernetes.client.dsl.LogWatch;
import io.alauda.kubernetes.client.dsl.ParameterMixedOperation;
import io.alauda.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.alauda.kubernetes.client.WithRequestCallable;
import io.alauda.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.alauda.kubernetes.client.dsl.internal.CustomResourceOperationsImpl;
import io.alauda.kubernetes.client.dsl.internal.PodSecurityPolicyOperationsImpl;
import io.alauda.kubernetes.client.utils.ImpersonatorInterceptor;
import io.alauda.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.*;
import okhttp3.Authenticator;
import okhttp3.OkHttpClient;

import io.alauda.kubernetes.client.BaseClient;
import io.alauda.kubernetes.client.Config;
import io.alauda.kubernetes.client.DefaultKubernetesClient;
import io.alauda.kubernetes.client.ExtensionsAPIGroupClient;
import io.alauda.kubernetes.client.NamespacedKubernetesClient;
import io.alauda.kubernetes.client.KubernetesClientException;
import io.alauda.kubernetes.client.dsl.KubernetesListMixedOperation;
import io.alauda.kubernetes.client.dsl.MixedOperation;
import io.alauda.kubernetes.client.dsl.NonNamespaceOperation;
import io.alauda.kubernetes.client.dsl.PodResource;
import io.alauda.kubernetes.client.dsl.Resource;
import io.alauda.kubernetes.client.dsl.RollableScalableResource;
import io.alauda.kubernetes.client.dsl.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable;
import io.alauda.kubernetes.client.dsl.internal.ComponentStatusOperationsImpl;
import io.alauda.kubernetes.client.dsl.internal.CustomResourceDefinitionOperationsImpl;
import io.alauda.devops.client.internal.AlaudaOAuthInterceptor;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class DefaultAlaudaDevOpsClient extends BaseClient implements NamespacedAlaudaDevOpsClient {

  private URL kubernetesUrl;
  private NamespacedKubernetesClient delegate;

  public DefaultAlaudaDevOpsClient() throws KubernetesClientException {
    this(new AlaudaDevOpsConfigBuilder().build());
  }

  public DefaultAlaudaDevOpsClient(String masterUrl) throws KubernetesClientException {
    this(new AlaudaDevOpsConfigBuilder().withMasterUrl(masterUrl).build());
  }

  public DefaultAlaudaDevOpsClient(final Config config) throws KubernetesClientException {
    this(new AlaudaDevOpsConfig(config));
  }

  public DefaultAlaudaDevOpsClient(final AlaudaDevOpsConfig config) throws KubernetesClientException {
    super(config);
    try {
      this.httpClient = clientWithOpenShiftOAuthInterceptor(this.httpClient);
      this.delegate = new DefaultKubernetesClient(this.httpClient, config);
      this.kubernetesUrl = new URL(config.getKubernetesUrl());
    } catch (MalformedURLException e) {
      throw new KubernetesClientException("Could not create client", e);
    }
  }

  public DefaultAlaudaDevOpsClient(OkHttpClient httpClient, AlaudaDevOpsConfig config) throws KubernetesClientException {
    super(httpClient, config);
    try {
      this.delegate = new DefaultKubernetesClient(clientWithOpenShiftOAuthInterceptor(httpClient), config);
      this.kubernetesUrl = new URL(config.getKubernetesUrl());
    } catch (MalformedURLException e) {
      throw new KubernetesClientException("Could not create client", e);
    }
  }

  public static DefaultAlaudaDevOpsClient fromConfig(String config) {
    return new DefaultAlaudaDevOpsClient(Serialization.unmarshal(config, AlaudaDevOpsConfig.class));
  }

  public static DefaultAlaudaDevOpsClient fromConfig(InputStream is) {
    return new DefaultAlaudaDevOpsClient(Serialization.unmarshal(is, AlaudaDevOpsConfig.class));
  }

  private OkHttpClient clientWithOpenShiftOAuthInterceptor(OkHttpClient httpClient) {
    httpClient = httpClient.newBuilder().authenticator(Authenticator.NONE).build();
    OkHttpClient.Builder builder = httpClient.newBuilder();
    builder.interceptors().clear();
    return builder.addInterceptor(new AlaudaOAuthInterceptor(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration())))
      .addInterceptor(new ImpersonatorInterceptor(getConfiguration()))
      .build();
  }

  @Override
  public URL getKubernetesUrl() {
    return kubernetesUrl;
  }

  @Override
  public MixedOperation> componentstatuses() {
    return new ComponentStatusOperationsImpl(httpClient, getConfiguration());
  }

  @Override
  public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable load(InputStream is) {
    return delegate.load(is);
  }

  @Override
  public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable resource(HasMetadata item) {
    return delegate.resource(item);
  }

  @Override
  public NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable resource(String s) {
    return delegate.resource(s);
  }

  @Override
  public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(KubernetesResourceList is) {
    return delegate.resourceList(is);
  }

  @Override
  public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(HasMetadata... items) {
    return delegate.resourceList(items);
  }

  @Override
  public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(Collection items) {
    return delegate.resourceList(items);
  }

  @Override
  public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList(String s) {
    return delegate.resourceList(s);
  }

  @Override
  public MixedOperation> endpoints() {
    return delegate.endpoints();
  }

  @Override
  public MixedOperation> events() {
    return delegate.events();
  }

  @Override
  public NonNamespaceOperation> namespaces() {
    return delegate.namespaces();
  }

  @Override
  public NonNamespaceOperation> nodes() {
    return delegate.nodes();
  }

  @Override
  public NonNamespaceOperation> persistentVolumes() {
    return delegate.persistentVolumes();
  }

  @Override
  public MixedOperation> persistentVolumeClaims() {
    return delegate.persistentVolumeClaims();
  }

  @Override
  public MixedOperation> pods() {
    return delegate.pods();
  }

  @Override
  public MixedOperation> replicationControllers() {
    return delegate.replicationControllers();
  }

  @Override
  public MixedOperation> resourceQuotas() {
    return delegate.resourceQuotas();
  }

  @Override
  public MixedOperation> secrets() {
    return delegate.secrets();
  }

  @Override
  public MixedOperation> services() {
    return delegate.services();
  }

  @Override
  public MixedOperation> serviceAccounts() {
    return delegate.serviceAccounts();
  }

  @Override
  public KubernetesListMixedOperation lists() {
    return delegate.lists();
  }

  @Override
  public NonNamespaceOperation> securityContextConstraints() {
    return delegate.securityContextConstraints();
  }

  @Override
  public MixedOperation> configMaps() {
    return delegate.configMaps();
  }

  @Override
  public MixedOperation> pipelines() {
    return new PipelineOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> pipelineConfigs() {
    return new PipelineConfigOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> limitRanges() {
    return delegate.limitRanges();
  }

  @Override
  public > MixedOperation> customResources(CustomResourceDefinition crd, Class resourceType, Class listClass, Class doneClass) {
    return new CustomResourceOperationsImpl(httpClient, getConfiguration(), crd, resourceType, listClass, doneClass);
  }

  @Override
  public > MixedOperation> customResource(CustomResourceDefinition crd, Class resourceType, Class listClass, Class doneClass) {
    return customResources(crd, resourceType, listClass, doneClass);
  }

  @Override
  public NonNamespaceOperation> customResourceDefinitions() {
    return new CustomResourceDefinitionOperationsImpl(httpClient, getConfiguration());
  }

  @Override
  public MixedOperation> builds() {
    return new BuildOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> buildConfigs() {
    return new BuildConfigOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> deploymentConfigs() {
    return new DeploymentConfigOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> groups() {
    return new GroupOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> imageStreams() {
    return new ImageStreamOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> imageStreamTags() {
    return new ImageStreamTagOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public NonNamespaceOperation> oAuthAccessTokens() {
    return new OAuthAccessTokenOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public NonNamespaceOperation> oAuthAuthorizeTokens() {
    return new OAuthAuthorizeTokenOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public NonNamespaceOperation> oAuthClients() {
    return new OAuthClientOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public MixedOperation> policies() {
    return new PolicyOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> policyBindings() {
    return new PolicyBindingOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public NonNamespaceOperation> projects() {
    return new ProjectOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public NonNamespaceOperation> jenkins() {
    return new JenkinsOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public MixedOperation> jenkinsBindings() {
    return new JenkinsBindingOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public ProjectRequestOperation projectrequests() {
    return new ProjectRequestsOperationImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()));
  }

  @Override
  public MixedOperation> roles() {
    return new RoleOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> roleBindings() {
    return new RoleBindingOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> routes() {
    return new RouteOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public ParameterMixedOperation> templates() {
    return new TemplateOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> users() {
    return new UserOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public SubjectAccessReviewOperation subjectAccessReviews() {
    return new SubjectAccessReviewOperationImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), null, getNamespace());
  }

  @Override
  public MixedOperation> clusterRoleBindings() {
    return new ClusterRoleBindingOperationsImpl(httpClient, AlaudaDevOpsConfig.wrap(getConfiguration()), getNamespace());
  }

  @Override
  public MixedOperation> storageClasses() {
    return delegate.storageClasses();
  }

  @Override
  public MixedOperation> podSecurityPolicies() {
    return new PodSecurityPolicyOperationsImpl(httpClient, getConfiguration(), getNamespace());
  }

  @Override
  public NamespacedAlaudaDevOpsClient inNamespace(String namespace) {
    AlaudaDevOpsConfig updated = new AlaudaDevOpsConfigBuilder(new AlaudaDevOpsConfig(getConfiguration()))
      .withMasterUrl(kubernetesUrl.toString())
      .withNamespace(namespace)
      .build();
    //      .withOpenShiftUrl(kubernetesUrl.toString())
    return new DefaultAlaudaDevOpsClient(httpClient, updated);
  }

  @Override
  public NamespacedAlaudaDevOpsClient inAnyNamespace() {
    return inNamespace(null);
  }

  @Override
  public ExtensionsAPIGroupClient extensions() {
    return adapt(ExtensionsAPIGroupClient.class);
  }

  @Override
  public AppsAPIGroupDSL apps() {
    return adapt(AppsAPIGroupClient.class);
  }

  @Override
  public AutoscalingAPIGroupDSL autoscaling() {
    return adapt(AutoscalingAPIGroupClient.class);
  }

  @Override
  public FunctionCallable withRequestConfig(RequestConfig requestConfig) {
    return new WithRequestCallable(this, requestConfig);
  }

  @Override
  public User currentUser() {
    return users().withName("~").get();
  }

  @Override
  public boolean supportAlaudaAPIGroup(String apiGroup) {
    String apiGroupPath = "/apis/" + apiGroup;
    RootPaths rootPaths = rootPaths();
    if (rootPaths != null) {
      List paths = rootPaths.getPaths();
      if (paths != null) {
        for (String path : paths) {
          if (Objects.equals(apiGroupPath, path)) {
            return true;
          }
        }
      }
    }
    return false;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy