From fc5e9f5a532c46f1cd4fa18681a4ca2113cd2849 Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Fri, 23 Dec 2022 16:50:41 +0100 Subject: [PATCH] chore: Add simple kubernetes web app construct + test it --- app/main.go | 7 ++++ app/main_test.go | 37 +++++++++++++++++ app/myconstructs/kuberneteswebapp.go | 61 ++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/app/main.go b/app/main.go index 51d2751..a2eb6f7 100644 --- a/app/main.go +++ b/app/main.go @@ -28,6 +28,13 @@ func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack { Environment: jsii.String("dev"), }) + myconstructs.NewKubernetesNodePortService(stack, jsii.String("service"), &myconstructs.KubernetesNodePortServiceConfig{ + Port: 30001, + App: jsii.String("myapp"), + Component: jsii.String("frontend"), + Environment: jsii.String("dev"), + }) + return stack } diff --git a/app/main_test.go b/app/main_test.go index 3b9bbad..ebe7bea 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -38,11 +38,32 @@ func NewTestStack(scope constructs.Construct, id string) cdktf.TerraformStack { return stack } +func NewSimpleKubernetesWebAppTestStack(scope constructs.Construct, id string) cdktf.TerraformStack { + stack := cdktf.NewTerraformStack(scope, &id) + + kubernetesprovider.NewKubernetesProvider(stack, jsii.String("kubernetes"), nil) + + myconstructs.NewSimpleKubernetesWebApp(stack, jsii.String("webapp"), &myconstructs.SimpleKubernetesWebAppConfig{ + Image: jsii.String("nginx:latest"), + Replicas: 4, + App: jsii.String("myapp"), + Component: jsii.String("frontend"), + Environment: jsii.String("dev"), + Port: 30001, + }) + + return stack +} + var run_validations = true var synth = cdktf.Testing_Synth( NewTestStack(cdktf.Testing_App(nil), *jsii.String("testing")), &run_validations, ) +var synthSimpleWebApp = cdktf.Testing_Synth( + NewSimpleKubernetesWebAppTestStack(cdktf.Testing_App(nil), *jsii.String("testing")), + &run_validations, +) func TestShouldContainDeployment(t *testing.T) { assertion := cdktf.Testing_ToHaveResource(synth, deployment.Deployment_TfResourceType()) @@ -59,3 +80,19 @@ func TestShouldContainService(t *testing.T) { t.Error("Expected kubernetes Service construct but found none") } } + +func TestSimpleWebAppShouldContainDeployment(t *testing.T) { + assertion := cdktf.Testing_ToHaveResource(synthSimpleWebApp, deployment.Deployment_TfResourceType()) + + if !*assertion { + t.Error("Expected kubernetes Deployment construct but found none") + } +} + +func TestSimpleWebAppShouldContainService(t *testing.T) { + assertion := cdktf.Testing_ToHaveResource(synthSimpleWebApp, deployment.Deployment_TfResourceType()) + + if !*assertion { + t.Error("Expected kubernetes Service construct but found none") + } +} diff --git a/app/myconstructs/kuberneteswebapp.go b/app/myconstructs/kuberneteswebapp.go index 84ee1e4..f533bfe 100644 --- a/app/myconstructs/kuberneteswebapp.go +++ b/app/myconstructs/kuberneteswebapp.go @@ -7,8 +7,12 @@ import ( "github.com/aws/jsii-runtime-go" "github.com/cdktf/cdktf-provider-kubernetes-go/kubernetes/v4/deployment" "github.com/cdktf/cdktf-provider-kubernetes-go/kubernetes/v4/service" + "github.com/hashicorp/terraform-cdk-go/cdktf" ) +type KubernetesWebAppDeployment struct { +} + type KubernetesWebAppDeploymentConfig struct { Image *string Replicas int @@ -18,7 +22,7 @@ type KubernetesWebAppDeploymentConfig struct { Env *map[string]*string } -func NewKubernetesWebAppDeployment(scope constructs.Construct, name *string, config *KubernetesWebAppDeploymentConfig) constructs.Construct { +func NewKubernetesWebAppDeployment(scope constructs.Construct, name *string, config *KubernetesWebAppDeploymentConfig) KubernetesWebAppDeployment { c := constructs.NewConstruct(scope, name) labels := &map[string]*string{ @@ -64,7 +68,9 @@ func NewKubernetesWebAppDeployment(scope constructs.Construct, name *string, con }, }) - return c + kwad := KubernetesWebAppDeployment{} + + return kwad } type KubernetesNodePortServiceConfig struct { @@ -75,7 +81,7 @@ type KubernetesNodePortServiceConfig struct { } type KubernetesNodePortService struct { - resource service.Service + resource *service.Service } func NewKubernetesNodePortService(scope constructs.Construct, name *string, config *KubernetesNodePortServiceConfig) KubernetesNodePortService { @@ -99,8 +105,55 @@ func NewKubernetesNodePortService(scope constructs.Construct, name *string, conf }) knps := KubernetesNodePortService{ - resource: service, + resource: &service, } return knps } + +type SimpleKubernetesWebApp struct { + deployment *KubernetesWebAppDeployment + service *KubernetesNodePortService + config *SimpleKubernetesWebAppConfig +} + +type SimpleKubernetesWebAppConfig struct { + Port int + Image *string + Replicas int + App *string + Component *string + Environment *string + Env *map[string]*string +} + +func NewSimpleKubernetesWebApp(scope constructs.Construct, name *string, config *SimpleKubernetesWebAppConfig) SimpleKubernetesWebApp { + c := constructs.NewConstruct(scope, name) + + deployment := NewKubernetesWebAppDeployment(c, jsii.String("deployment"), &KubernetesWebAppDeploymentConfig{ + Image: jsii.String("nginx:latest"), + Replicas: 2, + App: jsii.String("myapp"), + Component: jsii.String("frontend"), + Environment: jsii.String("dev"), + }) + + service := NewKubernetesNodePortService(c, jsii.String("service"), &KubernetesNodePortServiceConfig{ + Port: 30001, + App: jsii.String("myapp"), + Component: jsii.String("frontend"), + Environment: jsii.String("dev"), + }) + + skwa := SimpleKubernetesWebApp{ + config: config, + deployment: &deployment, + service: &service, + } + + cdktf.NewTerraformOutput(c, jsii.String("url"), &cdktf.TerraformOutputConfig{ + Value: jsii.String(fmt.Sprintf("http://localhost:%d", config.Port)), + }) + + return skwa +}