terraform: always output node cidr (#2481)

* terraform: always output node cidr
This commit is contained in:
3u13r 2023-10-23 15:06:48 +02:00 committed by GitHub
parent 5d640ff4f9
commit e053d1fa71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 74 additions and 46 deletions

View File

@ -70,9 +70,8 @@ func TestLoadReleases(t *testing.T) {
state.New(). state.New().
SetInfrastructure(state.Infrastructure{ SetInfrastructure(state.Infrastructure{
GCP: &state.GCP{ GCP: &state.GCP{
ProjectID: "test-project-id", ProjectID: "test-project-id",
IPCidrNode: "test-node-cidr", IPCidrPod: "test-pod-cidr",
IPCidrPod: "test-pod-cidr",
}, },
}). }).
SetClusterValues(state.ClusterValues{MeasurementSalt: []byte{0x41}}), SetClusterValues(state.ClusterValues{MeasurementSalt: []byte{0x41}}),

View File

@ -106,6 +106,9 @@ type Infrastructure struct {
// Name used in the cluster's named resources. // Name used in the cluster's named resources.
Name string `yaml:"name"` Name string `yaml:"name"`
// description: | // description: |
// CIDR range of the cluster's nodes.
IPCidrNode string `yaml:"ipCidrNode"`
// description: |
// Values specific to a Constellation cluster running on Azure. // Values specific to a Constellation cluster running on Azure.
Azure *Azure `yaml:"azure,omitempty"` Azure *Azure `yaml:"azure,omitempty"`
// description: | // description: |
@ -119,9 +122,6 @@ type GCP struct {
// Project ID of the GCP project the cluster is running in. // Project ID of the GCP project the cluster is running in.
ProjectID string `yaml:"projectID"` ProjectID string `yaml:"projectID"`
// description: | // description: |
// CIDR range of the cluster's nodes.
IPCidrNode string `yaml:"ipCidrNode"`
// description: |
// CIDR range of the cluster's pods. // CIDR range of the cluster's pods.
IPCidrPod string `yaml:"ipCidrPod"` IPCidrPod string `yaml:"ipCidrPod"`
} }

View File

@ -74,7 +74,7 @@ func init() {
FieldName: "infrastructure", FieldName: "infrastructure",
}, },
} }
InfrastructureDoc.Fields = make([]encoder.Doc, 8) InfrastructureDoc.Fields = make([]encoder.Doc, 9)
InfrastructureDoc.Fields[0].Name = "uid" InfrastructureDoc.Fields[0].Name = "uid"
InfrastructureDoc.Fields[0].Type = "string" InfrastructureDoc.Fields[0].Type = "string"
InfrastructureDoc.Fields[0].Note = "" InfrastructureDoc.Fields[0].Note = ""
@ -105,16 +105,21 @@ func init() {
InfrastructureDoc.Fields[5].Note = "" InfrastructureDoc.Fields[5].Note = ""
InfrastructureDoc.Fields[5].Description = "Name used in the cluster's named resources." InfrastructureDoc.Fields[5].Description = "Name used in the cluster's named resources."
InfrastructureDoc.Fields[5].Comments[encoder.LineComment] = "Name used in the cluster's named resources." InfrastructureDoc.Fields[5].Comments[encoder.LineComment] = "Name used in the cluster's named resources."
InfrastructureDoc.Fields[6].Name = "azure" InfrastructureDoc.Fields[6].Name = "ipCidrNode"
InfrastructureDoc.Fields[6].Type = "Azure" InfrastructureDoc.Fields[6].Type = "string"
InfrastructureDoc.Fields[6].Note = "" InfrastructureDoc.Fields[6].Note = ""
InfrastructureDoc.Fields[6].Description = "Values specific to a Constellation cluster running on Azure." InfrastructureDoc.Fields[6].Description = "CIDR range of the cluster's nodes."
InfrastructureDoc.Fields[6].Comments[encoder.LineComment] = "Values specific to a Constellation cluster running on Azure." InfrastructureDoc.Fields[6].Comments[encoder.LineComment] = "CIDR range of the cluster's nodes."
InfrastructureDoc.Fields[7].Name = "gcp" InfrastructureDoc.Fields[7].Name = "azure"
InfrastructureDoc.Fields[7].Type = "GCP" InfrastructureDoc.Fields[7].Type = "Azure"
InfrastructureDoc.Fields[7].Note = "" InfrastructureDoc.Fields[7].Note = ""
InfrastructureDoc.Fields[7].Description = "Values specific to a Constellation cluster running on GCP." InfrastructureDoc.Fields[7].Description = "Values specific to a Constellation cluster running on Azure."
InfrastructureDoc.Fields[7].Comments[encoder.LineComment] = "Values specific to a Constellation cluster running on GCP." InfrastructureDoc.Fields[7].Comments[encoder.LineComment] = "Values specific to a Constellation cluster running on Azure."
InfrastructureDoc.Fields[8].Name = "gcp"
InfrastructureDoc.Fields[8].Type = "GCP"
InfrastructureDoc.Fields[8].Note = ""
InfrastructureDoc.Fields[8].Description = "Values specific to a Constellation cluster running on GCP."
InfrastructureDoc.Fields[8].Comments[encoder.LineComment] = "Values specific to a Constellation cluster running on GCP."
GCPDoc.Type = "GCP" GCPDoc.Type = "GCP"
GCPDoc.Comments[encoder.LineComment] = "GCP describes the infra state related to GCP." GCPDoc.Comments[encoder.LineComment] = "GCP describes the infra state related to GCP."
@ -125,22 +130,17 @@ func init() {
FieldName: "gcp", FieldName: "gcp",
}, },
} }
GCPDoc.Fields = make([]encoder.Doc, 3) GCPDoc.Fields = make([]encoder.Doc, 2)
GCPDoc.Fields[0].Name = "projectID" GCPDoc.Fields[0].Name = "projectID"
GCPDoc.Fields[0].Type = "string" GCPDoc.Fields[0].Type = "string"
GCPDoc.Fields[0].Note = "" GCPDoc.Fields[0].Note = ""
GCPDoc.Fields[0].Description = "Project ID of the GCP project the cluster is running in." GCPDoc.Fields[0].Description = "Project ID of the GCP project the cluster is running in."
GCPDoc.Fields[0].Comments[encoder.LineComment] = "Project ID of the GCP project the cluster is running in." GCPDoc.Fields[0].Comments[encoder.LineComment] = "Project ID of the GCP project the cluster is running in."
GCPDoc.Fields[1].Name = "ipCidrNode" GCPDoc.Fields[1].Name = "ipCidrPod"
GCPDoc.Fields[1].Type = "string" GCPDoc.Fields[1].Type = "string"
GCPDoc.Fields[1].Note = "" GCPDoc.Fields[1].Note = ""
GCPDoc.Fields[1].Description = "CIDR range of the cluster's nodes." GCPDoc.Fields[1].Description = "CIDR range of the cluster's pods."
GCPDoc.Fields[1].Comments[encoder.LineComment] = "CIDR range of the cluster's nodes." GCPDoc.Fields[1].Comments[encoder.LineComment] = "CIDR range of the cluster's pods."
GCPDoc.Fields[2].Name = "ipCidrPod"
GCPDoc.Fields[2].Type = "string"
GCPDoc.Fields[2].Note = ""
GCPDoc.Fields[2].Description = "CIDR range of the cluster's pods."
GCPDoc.Fields[2].Comments[encoder.LineComment] = "CIDR range of the cluster's pods."
AzureDoc.Type = "Azure" AzureDoc.Type = "Azure"
AzureDoc.Comments[encoder.LineComment] = "Azure describes the infra state related to Azure." AzureDoc.Comments[encoder.LineComment] = "Azure describes the infra state related to Azure."

View File

@ -29,6 +29,7 @@ func defaultState() *State {
"api-server-cert-san-test", "api-server-cert-san-test",
"api-server-cert-san-test-2", "api-server-cert-san-test-2",
}, },
IPCidrNode: "test-cidr-node",
Azure: &Azure{ Azure: &Azure{
ResourceGroup: "test-rg", ResourceGroup: "test-rg",
SubscriptionID: "test-sub", SubscriptionID: "test-sub",
@ -38,9 +39,8 @@ func defaultState() *State {
AttestationURL: "test-maaUrl", AttestationURL: "test-maaUrl",
}, },
GCP: &GCP{ GCP: &GCP{
ProjectID: "test-project", ProjectID: "test-project",
IPCidrNode: "test-cidr-node", IPCidrPod: "test-cidr-pod",
IPCidrPod: "test-cidr-pod",
}, },
}, },
ClusterValues: ClusterValues{ ClusterValues: ClusterValues{

View File

@ -239,6 +239,15 @@ func (c *Client) ShowInfrastructure(ctx context.Context, provider cloudprovider.
return state.Infrastructure{}, errors.New("invalid type in name output: not a string") return state.Infrastructure{}, errors.New("invalid type in name output: not a string")
} }
cidrNodesOutput, ok := tfState.Values.Outputs["ip_cidr_nodes"]
if !ok {
return state.Infrastructure{}, errors.New("no ip_cidr_nodes output found")
}
cidrNodes, ok := cidrNodesOutput.Value.(string)
if !ok {
return state.Infrastructure{}, errors.New("invalid type in ip_cidr_nodes output: not a string")
}
res := state.Infrastructure{ res := state.Infrastructure{
ClusterEndpoint: outOfClusterEndpoint, ClusterEndpoint: outOfClusterEndpoint,
InClusterEndpoint: inClusterEndpoint, InClusterEndpoint: inClusterEndpoint,
@ -246,6 +255,7 @@ func (c *Client) ShowInfrastructure(ctx context.Context, provider cloudprovider.
InitSecret: []byte(secret), InitSecret: []byte(secret),
UID: uid, UID: uid,
Name: name, Name: name,
IPCidrNode: cidrNodes,
} }
switch provider { switch provider {
@ -259,15 +269,6 @@ func (c *Client) ShowInfrastructure(ctx context.Context, provider cloudprovider.
return state.Infrastructure{}, errors.New("invalid type in project output: not a string") return state.Infrastructure{}, errors.New("invalid type in project output: not a string")
} }
cidrNodesOutput, ok := tfState.Values.Outputs["ip_cidr_nodes"]
if !ok {
return state.Infrastructure{}, errors.New("no ip_cidr_nodes output found")
}
cidrNodes, ok := cidrNodesOutput.Value.(string)
if !ok {
return state.Infrastructure{}, errors.New("invalid type in ip_cidr_nodes output: not a string")
}
cidrPodsOutput, ok := tfState.Values.Outputs["ip_cidr_pods"] cidrPodsOutput, ok := tfState.Values.Outputs["ip_cidr_pods"]
if !ok { if !ok {
return state.Infrastructure{}, errors.New("no ip_cidr_pods output found") return state.Infrastructure{}, errors.New("no ip_cidr_pods output found")
@ -278,9 +279,8 @@ func (c *Client) ShowInfrastructure(ctx context.Context, provider cloudprovider.
} }
res.GCP = &state.GCP{ res.GCP = &state.GCP{
ProjectID: gcpProject, ProjectID: gcpProject,
IPCidrNode: cidrNodes, IPCidrPod: cidrPods,
IPCidrPod: cidrPods,
} }
case cloudprovider.Azure: case cloudprovider.Azure:
attestationURLOutput, ok := tfState.Values.Outputs["attestationURL"] attestationURLOutput, ok := tfState.Values.Outputs["attestationURL"]

View File

@ -17,10 +17,11 @@ provider "aws" {
} }
locals { locals {
uid = random_id.uid.hex uid = random_id.uid.hex
name = "${var.name}-${local.uid}" name = "${var.name}-${local.uid}"
initSecretHash = random_password.initSecret.bcrypt_hash initSecretHash = random_password.initSecret.bcrypt_hash
ports_node_range = "30000-32767" cidr_vpc_subnet_nodes = "192.168.176.0/20"
ports_node_range = "30000-32767"
load_balancer_ports = flatten([ load_balancer_ports = flatten([
{ name = "kubernetes", port = "6443", health_check = "HTTPS" }, { name = "kubernetes", port = "6443", health_check = "HTTPS" },
{ name = "bootstrapper", port = "9000", health_check = "TCP" }, { name = "bootstrapper", port = "9000", health_check = "TCP" },
@ -75,7 +76,7 @@ module "public_private_subnet" {
source = "./modules/public_private_subnet" source = "./modules/public_private_subnet"
name = local.name name = local.name
vpc_id = aws_vpc.vpc.id vpc_id = aws_vpc.vpc.id
cidr_vpc_subnet_nodes = "192.168.176.0/20" cidr_vpc_subnet_nodes = local.cidr_vpc_subnet_nodes
cidr_vpc_subnet_internet = "192.168.0.0/20" cidr_vpc_subnet_internet = "192.168.0.0/20"
zone = var.zone zone = var.zone
zones = local.zones zones = local.zones

View File

@ -31,3 +31,7 @@ output "initSecret" {
output "name" { output "name" {
value = local.name value = local.name
} }
output "ip_cidr_nodes" {
value = local.cidr_vpc_subnet_nodes
}

View File

@ -28,7 +28,6 @@ locals {
} }
ports_node_range = "30000-32767" ports_node_range = "30000-32767"
cidr_vpc_subnet_nodes = "192.168.178.0/24" cidr_vpc_subnet_nodes = "192.168.178.0/24"
cidr_vpc_subnet_pods = "10.10.0.0/16"
ports = flatten([ ports = flatten([
{ name = "kubernetes", port = "6443", health_check_protocol = "Https", path = "/readyz", priority = 100 }, { name = "kubernetes", port = "6443", health_check_protocol = "Https", path = "/readyz", priority = 100 },
{ name = "bootstrapper", port = "9000", health_check_protocol = "Tcp", path = null, priority = 101 }, { name = "bootstrapper", port = "9000", health_check_protocol = "Tcp", path = null, priority = 101 },

View File

@ -58,3 +58,7 @@ output "subscription_id" {
output "name" { output "name" {
value = local.name value = local.name
} }
output "ip_cidr_nodes" {
value = local.cidr_vpc_subnet_nodes
}

View File

@ -22,3 +22,7 @@ output "initSecret" {
output "name" { output "name" {
value = local.name value = local.name
} }
output "ip_cidr_nodes" {
value = local.cidr_vpc_subnet_nodes
}

View File

@ -19,6 +19,12 @@ provider "docker" {
host = "unix:///var/run/docker.sock" host = "unix:///var/run/docker.sock"
} }
locals {
cidr_vpc_subnet_nodes = "10.42.0.0/22"
cidr_vpc_subnet_control_planes = "10.42.1.0/24"
cidr_vpc_subnet_worker = "10.42.2.0/24"
}
resource "random_password" "initSecret" { resource "random_password" "initSecret" {
length = 32 length = 32
special = true special = true
@ -61,7 +67,7 @@ module "node_group" {
vcpus = each.value.vcpus vcpus = each.value.vcpus
memory = each.value.memory memory = each.value.memory
machine = var.machine machine = var.machine
cidr = each.value.role == "control-plane" ? "10.42.1.0/24" : "10.42.2.0/24" cidr = each.value.role == "control-plane" ? local.cidr_vpc_subnet_control_planes : local.cidr_vpc_subnet_worker
network_id = libvirt_network.constellation.id network_id = libvirt_network.constellation.id
pool = libvirt_pool.cluster.name pool = libvirt_pool.cluster.name
boot_mode = var.constellation_boot_mode boot_mode = var.constellation_boot_mode

View File

@ -46,3 +46,7 @@ output "validate_constellation_cmdline" {
output "name" { output "name" {
value = "${var.name}-qemu" // placeholder, as per "uid" output value = "${var.name}-qemu" // placeholder, as per "uid" output
} }
output "ip_cidr_nodes" {
value = local.cidr_vpc_subnet_nodes
}

View File

@ -230,6 +230,9 @@ func TestCreateCluster(t *testing.T) {
"name": { "name": {
Value: "constell-12345abc", Value: "constell-12345abc",
}, },
"ip_cidr_nodes": {
Value: "192.0.2.103/32",
},
}, },
}, },
} }
@ -275,6 +278,9 @@ func TestCreateCluster(t *testing.T) {
"name": { "name": {
Value: "constell-12345abc", Value: "constell-12345abc",
}, },
"ip_cidr_nodes": {
Value: "192.0.2.103/32",
},
}, },
}, },
} }
@ -487,6 +493,7 @@ func TestCreateCluster(t *testing.T) {
assert.Equal(state.HexBytes("initSecret"), infraState.InitSecret) assert.Equal(state.HexBytes("initSecret"), infraState.InitSecret)
assert.Equal("12345abc", infraState.UID) assert.Equal("12345abc", infraState.UID)
assert.Equal("192.0.2.101", infraState.InClusterEndpoint) assert.Equal("192.0.2.101", infraState.InClusterEndpoint)
assert.Equal("192.0.2.103/32", infraState.IPCidrNode)
if tc.provider == cloudprovider.Azure { if tc.provider == cloudprovider.Azure {
assert.Equal(tc.expectedAttestationURL, infraState.Azure.AttestationURL) assert.Equal(tc.expectedAttestationURL, infraState.Azure.AttestationURL)
} }