Skip to content

Commit

Permalink
chore: Add simple kubernetes web app construct + test it
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Dec 23, 2022
1 parent 0d1aba9 commit fc5e9f5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
37 changes: 37 additions & 0 deletions app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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")
}
}
61 changes: 57 additions & 4 deletions app/myconstructs/kuberneteswebapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -64,7 +68,9 @@ func NewKubernetesWebAppDeployment(scope constructs.Construct, name *string, con
},
})

return c
kwad := KubernetesWebAppDeployment{}

return kwad
}

type KubernetesNodePortServiceConfig struct {
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit fc5e9f5

Please sign in to comment.