Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion terraform/naas/101-naas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a reference design and implementation of an environment template that ca

![Architecture](architecture.png)

[Learn](https://docs.rafay.co/refarch/naas/overview/) more about this template.
[Learn](https://docs.rafay.co/refarch/mt/naas/overview/) more about this template.

## Contributing
This is authored by Rafay Solution Architects. We encourage and welcome contributions from the user community. If you have enhancements that you would like to share, please contact us.
16 changes: 13 additions & 3 deletions terraform/naas/101-naas/terraform/namespace/main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Create a unique random number to be used in the creation of a unique namespace name
resource "random_id" "rnd" {
keepers = {
first = var.cluster_name
}
byte_length = 4
}

# Create a unique namspace name using the username of the user who triggered the environment creation
locals {
# Create a unique namspace name
namespace1 = "${element(split("@",var.username),0)}-${random_id.rnd.dec}"
namespace2 = replace(local.namespace1,"+","-")
namespace3 = replace(local.namespace2,".","-")
namespace4 = replace(local.namespace3,"+","-")
namespace = lower(local.namespace4)
}

# Create the namespace with a resource quota
# Additional details can be found at https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs/resources/namespace
resource "rafay_namespace" "namespace" {
metadata {
name = local.namespace
Expand Down Expand Up @@ -46,20 +49,22 @@ resource "rafay_namespace" "namespace" {
}
}


# Downlod the cluster kubeconfig file which will be used to execute kubectl commands on the cluster
# Additional details can be found at https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs/resources/download_kubeconfig
resource "rafay_download_kubeconfig" "tfkubeconfig" {
cluster = var.cluster_name
output_folder_path = "/tmp"
filename = "kubeconfig-${timestamp()}"
}


# Create a Network Policy spec file from the networkpolicy.yaml file in the TF path and update the namespace name in the spec with the previously created unique namespace name
resource "local_file" "create_network_policy" {
content = templatefile("networkpolicy.yaml", {namespace = local.namespace })
filename = "/tmp/networkpolicy.yaml"
depends_on = [rafay_download_kubeconfig.tfkubeconfig]
}

# Execute commands to download kubectl binary
resource "null_resource" "install_network_policy" {
triggers = { always_run = "${timestamp()}" }
provisioner "local-exec" {
Expand All @@ -68,10 +73,14 @@ resource "null_resource" "install_network_policy" {
depends_on = [local_file.create_network_policy]
}

# Create user group in Rafay
# Additional details can be found at https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs/resources/group
resource "rafay_group" "group" {
name = "${local.namespace}-group"
}

# Associate the user that initiated the environment creation with the the previously created user group. Assign the group namespace admin privledges
# Additional details can be found at https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs/resources/groupassociation
resource "rafay_groupassociation" "groupassociation" {
depends_on = [rafay_group.group]
project = "${var.project}"
Expand All @@ -83,6 +92,7 @@ resource "rafay_groupassociation" "groupassociation" {
}


# If a collaborator user was provided, associate the collaborator user with the user group
resource "rafay_groupassociation" "groupassociation_collaborators" {
count = var.collaborator == "user_email" ? 0 : 1
depends_on = [rafay_groupassociation.groupassociation]
Expand Down
2 changes: 0 additions & 2 deletions terraform/naas/101-naas/terraform/namespace/output.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


output "namespace" {
value =local.namespace
}
Expand Down
3 changes: 3 additions & 0 deletions terraform/naas/101-naas/terraform/namespace/provider.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Please visit the following link for provider details
# https://registry.terraform.io/providers/RafaySystems/rafay/latest/docs

terraform {
backend "local" {}
required_providers {
Expand Down
7 changes: 4 additions & 3 deletions terraform/naas/101-naas/terraform/namespace/variable.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
variable "cluster_name" {
description = "name of the eks cluster"
default = "eks-cluster-1"
description = "name of the cluster that the namespace will be created in"
default = "naas-cluster-1"
}

variable "project" {
description = "name of the project where the cluster resides"
description = "Name of the project where the host cluster resides"
type = string
default = "eaas"
}
Expand Down Expand Up @@ -32,5 +32,6 @@ variable "memory" {
}

variable "collaborator" {
description = "Email address of a user who will be granted access to the environment resources"
type = string
}