Skip to content

Commit 4346457

Browse files
feat: scope resource definitions
1 parent 37d455b commit 4346457

8 files changed

+94
-51
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.github
2+
setup/terraform/.terraform*

1_demo.sh

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
#!/usr/bin/env bash
22
set -eo pipefail
33

4-
if ! humctl get application 5min-idp; then
5-
humctl create application 5min-idp
6-
echo "App created"
7-
fi
8-
94
echo "Deploying workload"
105

11-
humctl score deploy --app 5min-idp --env development -f ./score.yaml
6+
humanitec_app=$(terraform -chdir=setup/terraform output -raw humanitec_app)
7+
8+
humctl score deploy --app "$humanitec_app" --env development -f ./score.yaml
129

1310
echo "Waiting for deployment"
1411

1512
sleep 1
1613

1714
DEPLOYMENT_ID=$(humctl get deployment . -o json \
18-
--app 5min-idp \
15+
--app "$humanitec_app" \
1916
--env development \
2017
| jq -r .metadata.id)
2118

@@ -24,7 +21,7 @@ CURRENT_STATUS=""
2421

2522
while [ "$IS_DONE" = false ]; do
2623
CURRENT_STATUS=$(humctl get deployment "${DEPLOYMENT_ID}" -o json \
27-
--app 5min-idp \
24+
--app "$humanitec_app" \
2825
--env development \
2926
| jq -r .status.status)
3027

@@ -40,17 +37,22 @@ while [ "$IS_DONE" = false ]; do
4037
fi
4138
done
4239
if [ "$CURRENT_STATUS" = "failed" ]; then
43-
humctl get deployment-error --app 5min-idp --env development
40+
humctl get deployment-error --app "$humanitec_app" --env development
4441
exit 1
4542
fi
4643

47-
workload_host=$(humctl get active-resources --app 5min-idp --env development -o yaml | yq '.[] | select(.metadata.type == "route") | .status.resource.host')
44+
workload_host=$(humctl get active-resources --app "$humanitec_app" --env development -o yaml | yq '.[] | select(.metadata.type == "route") | .status.resource.host')
4845

4946
echo "Waiting for workload to be available"
5047

5148
# manually change the host here as the workload host resolves to localhost, which is not reachable from the container
52-
curl -I --retry 20 --retry-delay 3 --retry-all-errors --fail \
49+
if curl -I --retry 20 --retry-delay 3 --retry-all-errors --fail \
5350
--connect-to "$workload_host:30080:5min-idp-control-plane:30080" \
54-
"http://$workload_host:30080"
55-
56-
echo "Workload available at: http://$workload_host:30080"
51+
"http://$workload_host:30080"; then
52+
echo "Workload available at: http://$workload_host:30080"
53+
else
54+
echo "Workload not available"
55+
kubectl get pods --all-namespaces
56+
kubectl -n "$humanitec_app-development" logs deployment/hello-world
57+
exit 1
58+
fi

2_cleanup.sh

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ export HUMANITEC_TOKEN=$humctl_token
1414
export TF_VAR_humanitec_org=$HUMANITEC_ORG
1515
export TF_VAR_kubeconfig=$kubeconfig_docker
1616

17-
18-
if humctl get application 5min-idp; then
19-
humctl delete application 5min-idp
20-
fi
21-
17+
terraform -chdir=setup/terraform init -upgrade
2218
terraform -chdir=setup/terraform destroy -auto-approve
2319

2420
kind delete cluster -n 5min-idp

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ test: build check-image
3434
-v /var/run/docker.sock:/var/run/docker.sock \
3535
--network bridge \
3636
$(IMG) ./image/test.sh
37+
38+
# Run the locally built image
39+
run-local: build
40+
docker run --rm -it -h 5min-idp --name 5min-idp \
41+
-e HUMANITEC_ORG \
42+
-v hum-5min-idp:/state \
43+
-v $(HOME)/.humctl:/root/.humctl \
44+
-v /var/run/docker.sock:/var/run/docker.sock \
45+
--network bridge \
46+
$(IMG)

setup/terraform/idp-base.tf

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
# Ensure we don't have name conflicts
2+
3+
resource "random_string" "install_id" {
4+
length = 4
5+
special = false
6+
upper = false
7+
numeric = false
8+
}
9+
10+
locals {
11+
app = "5min-idp-${random_string.install_id.result}"
12+
prefix = "${local.app}-"
13+
}
14+
15+
resource "humanitec_application" "demo" {
16+
id = local.app
17+
name = local.app
18+
}
19+
120
# Configure k8s namespace naming
221

322
resource "humanitec_resource_definition" "k8s_namespace" {
423
driver_type = "humanitec/echo"
5-
id = "default-namespace"
6-
name = "default-namespace"
24+
id = "${local.prefix}k8s-namespace"
25+
name = "${local.prefix}k8s-namespace"
726
type = "k8s-namespace"
827

928
driver_inputs = {
@@ -15,13 +34,16 @@ resource "humanitec_resource_definition" "k8s_namespace" {
1534

1635
resource "humanitec_resource_definition_criteria" "k8s_namespace" {
1736
resource_definition_id = humanitec_resource_definition.k8s_namespace.id
37+
app_id = humanitec_application.demo.id
38+
39+
force_delete = true
1840
}
1941

2042
# Configure DNS for localhost
2143

22-
resource "humanitec_resource_definition" "localhost_dns" {
23-
id = "localhost-dns"
24-
name = "localhost-dns"
44+
resource "humanitec_resource_definition" "dns_localhost" {
45+
id = "${local.prefix}dns-localhost"
46+
name = "${local.prefix}dns-localhost"
2547
type = "dns"
2648
driver_type = "humanitec/dns-wildcard"
2749

@@ -40,8 +62,11 @@ resource "humanitec_resource_definition" "localhost_dns" {
4062
}
4163
}
4264

43-
resource "humanitec_resource_definition_criteria" "localhost_dns" {
44-
resource_definition_id = humanitec_resource_definition.localhost_dns.id
65+
resource "humanitec_resource_definition_criteria" "dns_localhost" {
66+
resource_definition_id = humanitec_resource_definition.dns_localhost.id
67+
app_id = humanitec_application.demo.id
68+
69+
force_delete = true
4570
}
4671

4772
# Provide postgres resource
@@ -50,11 +75,13 @@ module "postgres_basic" {
5075
# Not pinned as we don't have a release yet
5176
# tflint-ignore: terraform_module_pinned_source
5277
source = "github.com/humanitec-architecture/resource-packs-in-cluster//humanitec-resource-defs/postgres/basic"
53-
prefix = "5min-idp-"
78+
prefix = local.prefix
5479
}
5580

5681
resource "humanitec_resource_definition_criteria" "postgres_basic" {
5782
resource_definition_id = module.postgres_basic.id
5883
class = "default"
59-
force_delete = true
84+
app_id = humanitec_application.demo.id
85+
86+
force_delete = true
6087
}

setup/terraform/idp-cluster.tf

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# Configure k8s cluster by exposing the locally running Kubernetes Cluster to the Humanitec Orchestrator
22
# using the Humanitec Agent
33

4+
resource "tls_private_key" "agent_private_key" {
5+
algorithm = "RSA"
6+
rsa_bits = 4096
7+
}
8+
9+
locals {
10+
agent_id = "${local.prefix}agent"
11+
}
12+
413
resource "humanitec_agent" "agent" {
5-
id = var.agent_id
14+
id = local.agent_id
615
description = "5min-idp"
716
public_keys = [{
817
key = tls_private_key.agent_private_key.public_key_pem
@@ -33,14 +42,14 @@ resource "helm_release" "humanitec_agent" {
3342
}
3443

3544
resource "humanitec_resource_definition" "agent" {
36-
id = var.agent_id
37-
name = var.agent_id
45+
id = local.agent_id
46+
name = local.agent_id
3847
type = "agent"
3948

4049
driver_type = "humanitec/agent"
4150
driver_inputs = {
4251
values_string = jsonencode({
43-
id = var.agent_id
52+
id = local.agent_id
4453
})
4554
}
4655

@@ -52,15 +61,18 @@ resource "humanitec_resource_definition" "agent" {
5261
resource "humanitec_resource_definition_criteria" "agent" {
5362
resource_definition_id = humanitec_resource_definition.agent.id
5463
res_id = "agent"
64+
app_id = humanitec_application.demo.id
65+
66+
force_delete = true
5567
}
5668

5769
locals {
5870
parsed_kubeconfig = yamldecode(file(var.kubeconfig))
5971
}
6072

61-
resource "humanitec_resource_definition" "local_cluster" {
62-
id = "${var.agent_id}-cluster"
63-
name = "${var.agent_id}-cluster"
73+
resource "humanitec_resource_definition" "cluster_local" {
74+
id = "${local.prefix}k8s-cluster"
75+
name = "${local.prefix}k8s-cluster"
6476
type = "k8s-cluster"
6577
driver_type = "humanitec/k8s-cluster"
6678

@@ -74,13 +86,15 @@ resource "humanitec_resource_definition" "local_cluster" {
7486
credentials = local.parsed_kubeconfig["users"][0]["user"]
7587
})
7688
}
89+
}
90+
91+
resource "humanitec_resource_definition_criteria" "cluster_local" {
92+
resource_definition_id = humanitec_resource_definition.cluster_local.id
93+
app_id = humanitec_application.demo.id
94+
95+
force_delete = true
7796

7897
depends_on = [
7998
humanitec_resource_definition_criteria.agent
8099
]
81100
}
82-
83-
84-
resource "humanitec_resource_definition_criteria" "local_cluster" {
85-
resource_definition_id = humanitec_resource_definition.local_cluster.id
86-
}

setup/terraform/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "humanitec_app" {
2+
description = "The ID of the Humanitec application"
3+
value = humanitec_application.demo.id
4+
}

setup/terraform/variables.tf

-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@ variable "humanitec_org" {
44
type = string
55
}
66

7-
variable "agent_id" {
8-
description = "The ID of the agent"
9-
default = "5min-idp"
10-
type = string
11-
}
12-
137
variable "kubeconfig" {
148
description = "Kubeconfig used by the Humanitec Agent / terraform"
159
type = string
1610
default = "/state/kube/config-internal.yaml"
1711
}
18-
19-
resource "tls_private_key" "agent_private_key" {
20-
algorithm = "RSA"
21-
rsa_bits = 4096
22-
}

0 commit comments

Comments
 (0)