Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster certificate and host outputs to the ske_cluster data #380

Open
tchelovilar opened this issue May 28, 2024 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@tchelovilar
Copy link

Adding the cluster certificate and host outputs to the ske_cluster data will smoothly improve Kubernetes provider setup.

data "stackit_ske_cluster" "main" {
  project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  name       = "example-name"
}

provider "kubernetes" {
  host                   = data.stackit_ske_cluster.main.host
  cluster_ca_certificate = base64decode(data.stackit_ske_cluster.main.cluster_ca_certificate)

  exec {
    api_version = "client.authentication.k8s.io/v1"
    command     = "stackit"
    args        = ["ske", "kubeconfig", "login", "--project=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "--cluster=name"]
  }
}
@vicentepinto98
Copy link
Contributor

Hi @tchelovilar, we are currently checking how to provide this info. I'll get back to you soon

@vicentepinto98
Copy link
Contributor

Like mentioned in stackitcloud/stackit-cli#358, in the meantime, you can manually retrieve the login kubeconfig and provide it to the Kubernetes TF provider:

provider "kubernetes" {
  config_path = "~/.kube/config"
}

@not22day
Copy link

my current solution based on the short lived kubeconfig resource

locals {
  kubeconfig_yaml = stackit_ske_kubeconfig.ske.kube_config
  kubeconfig_hcl  = yamldecode(local.kubeconfig_yaml)
}

provider "kubernetes" {
  host                   = local.kubeconfig_hcl.clusters.0.cluster.server
  cluster_ca_certificate = base64decode(local.kubeconfig_hcl.clusters.0.cluster.certificate-authority-data)
  client_certificate     =  base64decode(local.kubeconfig_hcl.users.0.user.client-certificate-data)
  client_key     =  base64decode(local.kubeconfig_hcl.users.0.user.client-key-data)
}


provider "helm" {
  kubernetes {
    host                   = local.kubeconfig_hcl.clusters.0.cluster.server
    cluster_ca_certificate = base64decode(local.kubeconfig_hcl.clusters.0.cluster.certificate-authority-data)
    client_certificate     =  base64decode(local.kubeconfig_hcl.users.0.user.client-certificate-data)
    client_key     =  base64decode(local.kubeconfig_hcl.users.0.user.client-key-data)
  }
}

@tchelovilar
Copy link
Author

tchelovilar commented Jun 12, 2024

Hi @not22day ,

I have found an interesting workaround, I created the ske_login.sh script to generate the expected values for the Kubernetes provider. That is working well, and will be better once we have the certificate ca and host outputs from the ske cluster resource and data. You just need a service account token and activate the service account before run the terraform stackit auth activate-service-account --service-account-token $STACKIT_SERVICE_ACCOUNT_TOKEN.

ske_login.sh

#!/bin/bash
# Workaround to generate the StackIT kubeconfig for kubernetes terraform provider 

STACKIT_PROJECT_ID=$1
SERVER=$2
CLUSTER_NAME=$3

export KUBERNETES_EXEC_INFO='{"apiVersion": "client.authentication.k8s.io/v1","kind": "ExecCredential", "spec": { "cluster": { "config": { "STACKITProjectID": "'$STACKIT_PROJECT_ID'", "ClusterName": "'$CLUSTER_NAME'"}, "server": "'$SERVER'"}, "interactive": false } }'

stackit ske kubeconfig login

Provider configuration:

provider "kubernetes" {
  host                   = var.cluster_endpoint
  cluster_ca_certificate = base64decode(var.cluster_ca_cert)

  exec {
    api_version = "client.authentication.k8s.io/v1"
    args        = [var.stackit_project_id, var.cluster_endpoint, stackit_ske_cluster.main.name]
    command     = "../ske_login.sh"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants