1. Deploy the stackdriver-metrics-adapter helm chart on Google Kubernetes Engine (GKE)


    To deploy the Stackdriver Metrics Adapter helm chart on a Google Kubernetes Engine (GKE) cluster, you'll need to do the following:

    1. Create a GKE Cluster: You'll need a running GKE cluster to deploy your helm chart onto. We'll use the google-native.container.v1.Cluster resource for this purpose. This resource allows you to provision a GKE cluster in your GCP project.

    2. Install and Configure Helm: Helm is a package manager for Kubernetes, which you will use to install the Stackdriver Metrics Adapter. Before you can start using Helm with Pulumi, you must ensure Helm is installed on your local machine where Pulumi CLI is being used. You can follow Helm's installation guide to set up Helm.

    3. Deploy the Helm Chart: Once Helm is set up and you have a GKE cluster, you can deploy the Stackdriver Metrics Adapter helm chart. We'll use Pulumi's built-in support for deploying Helm charts with the @pulumi/kubernetes Node.js package.

    Below is a Pulumi program written in TypeScript to accomplish these steps. Before you run the code, ensure you've configured your Pulumi GCP credentials properly.

    import * as gcp from "@pulumi/gcp"; import * as k8s from "@pulumi/kubernetes"; import * as pulumi from "@pulumi/pulumi"; // Step 1: Create a GKE cluster const engineVersion = gcp.container.getEngineVersions({}).then(v => v.latestMasterVersion); const gkeCluster = new gcp.container.Cluster("gke-cluster", { initialNodeCount: 1, minMasterVersion: engineVersion, nodeVersion: engineVersion, nodeConfig: { preemptible: true, machineType: "n1-standard-1", oauthScopes: [ "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring", ], }, }); // Export the Cluster name and Kubeconfig export const clusterName = gkeCluster.name; export const kubeconfig = pulumi. all([ gkeCluster.name, gkeCluster.endpoint, gkeCluster.masterAuth ]). apply(([ name, endpoint, masterAuth ]) => { const context = `${gcp.config.project}_${gcp.config.zone}_${name}`; return `apiVersion: v1 clusters: - cluster: certificate-authority-data: ${masterAuth.clusterCaCertificate} server: https://${endpoint} name: ${context} contexts: - context: cluster: ${context} user: ${context} name: ${context} current-context: ${context} kind: Config preferences: {} users: - name: ${context} user: auth-provider: config: cmd-args: config config-helper --format=json cmd-path: gcloud expiry-key: '{.credential.token_expiry}' token-key: '{.credential.access_token}' name: gcp `; }); // Step 2: Set up Kubernetes provider to deploy Helm chart to GKE const k8sProvider = new k8s.Provider("k8s-provider", { kubeconfig: kubeconfig }); // Step 3: Deploy Stackdriver Metrics Adapter helm chart onto GKE cluster const stackdriverMetricsAdapterChart = new k8s.helm.v3.Chart("stackdriver-metrics-adapter", { chart: "stackdriver-metrics-adapter", version: "0.1.0", // Replace with actual chart version fetchOpts: { repo: "https://kubernetes-charts.storage.googleapis.com", }, }, { provider: k8sProvider }); // Export the Helm chart name export const sdAdapterChartName = stackdriverMetricsAdapterChart.name;

    Here is what each part of the program does:

    1. Create GKE Cluster: We're creating a GKE cluster with a single node to start with. The engineVersion is auto-selected to be the latest available version for the master and the worker nodes. We're also specifying the OAuth scopes for the node configuration to ensure the node has the right permissions for Stackdriver.

    2. Export Cluster Name and Kubeconfig: Once the cluster is created, we export the name and generate the kubeconfig. The kubeconfig is needed to interact with the cluster using the Kubernetes API. This configuration is used by Pulumi when deploying resources to the cluster.

    3. Kubernetes Provider Setup: Before deploying the helm chart, we must configure a Kubernetes provider. The provider uses the kubeconfig generated in the previous step to authenticate against our GKE cluster.

    4. Deploy Helm Chart: With the Kubernetes provider configured, we then deploy the Stackdriver Metrics Adapter helm chart to the cluster. The chart and version specify which helm chart to deploy and which version to use. The fetchOpts specify the repository from where the helm chart should be fetched.

    To run this Pulumi program, you would run pulumi up from the command line in the directory with this code, assuming Pulumi was installed and the GCP provider was configured.

    Remember to replace the helm chart version with the correct one for the Stackdriver Metrics Adapter if it's different from 0.1.0 and ensure that the repository is correct too as it may have changed.