import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Setting up GCP and GKE using OpenTofu

This guide walks you through configuring Google Cloud Platform (GCP) and Google Kubernetes Engine (GKE) in nullplatform
using OpenTofu/Terraform®.

## Prerequisites

Before you begin, ensure you have:

1. [OpenTofu](https://opentofu.org/docs/intro/install/) or [Terraform®](https://www.terraform.io/downloads.html)
   installed
2. access to a Google Cloud Platform account
3. [Istio](https://istio.io/latest/docs/setup/getting-started/) installed on your GKE cluster
4. [nullplatform API key](#) for our custom provider

## Configuration

Create a file named `nullplatform_gcp_gke_config.tf` with the following content:

```hcl
# Configure the nullplatform provider
terraform {
  required_providers {
    nullplatform = {
      source = "nullplatform/nullplatform"
    }
    helm = {
      source  = "hashicorp/helm"
      version = "~> 2.5.0"
    }
  }
}

provider "nullplatform" {
  api_key = "your-api-key"
}

# Install nullplatform Helm chart
resource "helm_release" "nullplatform_gke_release" {
  name       = "nullplatform-release"
  repository = "https://nullplatform.github.io/helm-charts/charts"
  chart      = "nullplatform-chart"

  set {
    name  = "global.provider"
    value = "gke"
  }

  set {
    name  = "tls.secretName"
    value = "www-tls"
  }

  set {
    name  = "metrics-server.enabled"
    value = "false"
  }

  set {
    name  = "imagePullSecrets.enabled"
    value = "true"
  }

  set {
    name  = "imagePullSecrets.name"
    value = "image-pull-secret-nullplatform"
  }

  set {
    name  = "imagePullSecrets.registry"
    value = "your-registry-url"
  }

  set {
    name  = "imagePullSecrets.username"
    value = "your-username"
  }

  set {
    name  = "imagePullSecrets.password"
    value = "your-password"
  }

  # Add any other necessary configurations here
}

# Configure Google Cloud Platform (GCP)
resource "nullplatform_provider_config" "gcp-configuration" {
  account = "example-account"
  type    = "gcp-configuration"
  dimensions = { environment = "example-environment" }
  attributes = jsonencode({
    "project": {
      "id": "my-gcp-project"
    },
    "networking": {
      "domain_name": "example.com",
      "public_dns_zone_name": "example-com",
      "private_dns_zone_name": "internal-example"
    },
    "authentication": {
      "credential_base_64": "eyAi... (base64 string) ...IiB9"
    }
  })

  depends_on = [helm_release.nullplatform_gke_release]
}

# Configure Google Kubernetes Engine (GKE)
resource "nullplatform_provider_config" "gke-configuration" {
  account = "example-account"
  type    = "gke-configuration"
  dimensions = {
    environment = "example-environment"
  }
  attributes = jsonencode({
    "cluster": {
      "id": "my-gke-cluster",
      "location": "us-central1",
      "namespace": "my-namespace"
    },
    "gateway": {
      "namespace": "my-namespace",
      "public_name": "public-gateway",
      "private_name": "private-gateway"
    }
  })

  depends_on = [nullplatform_provider_config.gcp-configuration]
}
```

## Deployment steps

1. Replace all placeholder values in the configuration file with your actual details

2. Initialize OpenTofu/Terraform:

    <Tabs>
        <TabItem value="OpenTofu" label="OpenTofu">
            ```bash
            tofu init
            ```
        </TabItem>
        <TabItem value="Terraform" label="Terraform">
            ```bash
            terraform init
            ```
        </TabItem>
    </Tabs>

3. If you're importing existing resources, use the import command:

    <Tabs>
        <TabItem value="OpenTofu" label="OpenTofu">
            ```bash
            tofu import nullplatform_provider_config.gcp-configuration <existing-resource-id>
            tofu import nullplatform_provider_config.gke-configuration <existing-resource-id>
            ```
        </TabItem>
        <TabItem value="Terraform" label="Terraform">
            ```bash
            terraform import nullplatform_provider_config.gcp-configuration <existing-resource-id>
            terraform import nullplatform_provider_config.gke-configuration <existing-resource-id>
            ```
        </TabItem>
    </Tabs>

   Replace `<existing-resource-id>` with the actual resource IDs from your nullplatform account.

4. Review the planned changes:

    <Tabs>
        <TabItem value="OpenTofu" label="OpenTofu">
            ```bash
            tofu plan
            ```
        </TabItem>
        <TabItem value="Terraform" label="Terraform">
            ```bash
            terraform plan
            ```
        </TabItem>
    </Tabs>

5. Apply the configuration:

    <Tabs>
        <TabItem value="OpenTofu" label="OpenTofu">
            ```bash
            tofu apply
            ```
        </TabItem>
        <TabItem value="Terraform" label="Terraform">
            ```bash
            terraform apply
            ```
        </TabItem>
    </Tabs>

6. Confirm the changes by typing `yes` when prompted

## Conclusion

You have now set up your Google Cloud Platform and Google Kubernetes Engine configurations in nullplatform using
OpenTofu/Terraform. This process includes:

1. installing the nullplatform Helm chart with image pull secrets configuration
2. configuring the GCP provider
3. configuring the GKE provider

Remember to keep your `nullplatform_gcp_gke_config.tf` file secure, as it contains sensitive information.

_Terraform® is a registered trademark of HashiCorp, Inc._
