Import MKS Cluster using Terraform
Prerequisite
This script assumes that you have already created the necessary IAM resources to create MKS related resources through Kosmos.
For more details refer:
TF script sample to import existing MKS cluster to Kosmos
Variables
Please note that this script IS NOT A MODULE TO BE IMPORTED, but is meant to be a reference only!, therefore the variables explanation are meant on how this var will be used instead.
mks_cluster_name: The MKS cluster name to be imported towards Kosmosfleet_name: Kosmos fleet where the MKS cluster will be imported towardsmks_cluster_subnets: List of subnets where the actual MKS cluster resides inmks_service_role: Name of the service role to access MKS data (name not ARN!)mks_kosmos_operator_role_arn: ARN of Kosmos' operator role to access MKS datamks_region: Region where the to be imported MKS cluster resides
Introduction
This Terraform script imports an existing MKS cluster into a Kosmos fleet using the Kosmos provider. This is triggered by setting the value of kosmos_mksClusters.this.imported to True.
SPC mimics the EKS API for portability with AWS.
User who runs this script must have required permission on SPC since it will create a new role for Kosmos Operator in SPC to import and configure the MKS cluster into a Kosmos fleet.
Variables and Local
| Name | Explaination | Type |
|---|---|---|
kosmos_access_key (required) | Kosmos Access key | String |
spc_region (required) | Region where MKS cluster is deployed | String |
imported_cluster_name (required) | Name of the MKS cluster | String |
oidc_provider_arn (required) | arn for existing OIDC provider | String |
fleet_name (required) | Kosmos fleet where MKS will be improted into. For now, you must first create the fleet manually | String |
kosmos_role_arn (optional) | Existing Kosmos Service Role ARN, if empty will auto create a new role and policy | String |
Adding delay on the creation of kosmos_mksClusters resource
There’s an issue from AWS provider where the required resource (role_policy_attachment) is created but not yet ready which results in race condtion when creating kosmos_mksClusters.
The error thrown indicated that Kosmos is not authorized to assume the newly created role using the AWS AssumeRoleWithWebIdentity. This issue happens when we are importing existing MKS cluster using kosmos_mksClusters while also creating a new role, policy, and role_policy_attachment.
To overcome this we added a 5s delay in between the creation of role_policy_attachment and kosmos_mksClusters.
How to run
- Run
python scop2aws.pyto generate temporary credentials using SCOP - Run
terraform initto initialize the working directory and download Terraform providers and modules - Run
terraform plan -var="kosmos_access_key=(kosmos_access_key)"to preview the changes to be made by Terraform and read thoroughly on resources to be created - Run
terraform apply -var="kosmos_access_key=(kosmos_access_key)" -var="oidc_provider_arn=(oidc_provider_arn)" -var="fleet_name=(fleet_name)" -var="imported_cluster_name=(imported_cluster_name)" -var="spc_region=(spc_region)"and typeyeswhen prompted - To destroy all the resources, run
terraform destroy -var="kosmos_access_key=(kosmos_access_key)" -var="oidc_provider_arn=(oidc_provider_arn)" -var="fleet_name=(fleet_name)" -var="imported_cluster_name=(imported_cluster_name) -var="spc_region=(spc_region)""
Download Resources
For complete examples and the latest updates, refer to: