/* * Copyright © 2020 Atomist, 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. */ import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; import * as pulumi from "@pulumi/pulumi"; /** Arguments to [[clusterProvider]]. */ export interface ClusterProviderArgs { /** GKE cluster to create provider for. */ cluster: gcp.container.Cluster; /** Optional name of resource provider to create. */ name?: string; } /** * Create a Pulumi Kubernetes provider from a GKE cluster. */ export function clusterProvider(opts: ClusterProviderArgs): k8s.Provider { const providerName = opts.name || "k8s-provider"; const clusterIdentifier = opts.cluster.name; const kubeconfig = pulumi .all([ opts.cluster.name, opts.cluster.controlPlaneEndpointsConfig.dnsEndpointConfig.endpoint, opts.cluster.location, opts.cluster.project, ]) .apply(([name, endpoint, location, projectId]) => { const context = `${projectId}_${location}_${name}`; return `apiVersion: v1 clusters: - cluster: server: https://${endpoint} name: ${context} contexts: - context: cluster: ${context} user: ${context} name: ${context} current-context: ${context} kind: Config preferences: {} users: - name: ${context} user: exec: apiVersion: client.authentication.k8s.io/v1beta1 command: gke-gcloud-auth-plugin installHint: Install gke-gcloud-auth-plugin for use with kubectl by following https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#install_plugin provideClusterInfo: true `; }); return new k8s.Provider(providerName, { clusterIdentifier, kubeconfig }); }