Programmatically Import Cluster (No Web UI)

I am able to programmatically install, configure, and create a default cluster. The issue is the final step still requires someone to login into the UI to obtain the import manifest, e.g.

“curl --insecure -sfL https://10.93.97.36/v3/import/x8ktsbm8lcwkh9plbvq8vmgj6zrwwgdchk8l792mxbppddwpjwmbhr.yaml | kubectl apply -f -”

I cant locate this in the API or docs. Is it possible to get this information from any CLI?

Using the CLI:

rancher cluster create test --import
rancher cluster import test

Thanks. Rancher CLI isn’t quite non-interactive (my fault, poorly worded question), but I imagine I can ‘expect’ some of the required prompts. In any case, appreciate the help!

The registration command is in /v3/clusters/<id>/clusterRegistrationTokens. The UI is just an API client; anything it does you can.

After the cluster creation using rke I do rancher clusters create --import
I then do rancher clusters import but it prints a curl command to run how do I avoid this as this is not scriptable

Importing clusters requires creating a cluster (with an empty config so its treated as a imported cluster), then deploying the needed resources into the cluster so Rancher can access it. This is done by providing a YAML file with the needed resources that can be deployed using kubectl (or with curl + kubectl if you are using self signed certificates). All of these steps can be done in an automated way, the command provided for the cluster can be retrieved and be automatically executed after you configured the correct kubeconfig for your cluster for kubectl.

Can you give us some samples please, i’m creating a K8S cluster with terraform inside a gitlab-ci pipeline, but later i wanna administer the cluster via Rancher.

Thanks a lot

use rancher2 provider to create imported i.e. “rancher2_cluster” “foo-imported”
look for the outputs and use the appropriate command (curl for self signed kubectl for signed ssl)
I found out that creating the cluster directly in rancher2 provider works best

For CLI:

# create cluster
rancher cluster create my-imported-cluster --import
# kubectl command
rancher cluster import my-imported-cluster -q | head -1
# kubectl insecure command (self signed certificates on Rancher)
rancher cluster import my-imported-cluster -q | tail -1
# wait til cluster is ready
rancher wait my-imported-cluster

Terraform:

provider "rancher2" {
  api_url    = "https://x.x.x.x"
  access_key = "token-xxxxx"
  secret_key = "x"
  insecure   = "true"
}

resource "rancher2_cluster" "my-imported-cluster-tf" {
  name = "my-imported-cluster-tf"
}

output "kubectl-command" {
  value = ["${rancher2_cluster.my-imported-cluster-tf.cluster_registration_token.0.command}"]
}

output "insecure-kubectl-command" {
  value = ["${rancher2_cluster.my-imported-cluster-tf.cluster_registration_token.0.insecure_command}"]
}

Curl/bash: https://github.com/superseb/katacoda-scenarios/blob/master/rancher2-k3s-import/setup-rancher.sh