-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (80 loc) · 2.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cdk8ssharedapp
import (
"fmt"
"os"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2"
"github.com/derektamsen/cdk8ssharedapp/imports/k8s"
)
type AppConfig struct {
Name string
Namespace string
}
type ClusterProps struct {
ClusterName string
Image string
}
type K8sClusters struct {
Clusters *[]ClusterProps
}
func NewApp(appConfig *AppConfig, clusters *K8sClusters) error {
// Remove any existing rendered manifests from dist
// This ensures that the rendered output is exactly what we expect.
dir, err := os.Getwd()
if err != nil {
return err
}
rendered_dir := fmt.Sprintf("%s/dist", dir)
fmt.Printf("Removing rendered manifests from \"%s\"\n", rendered_dir)
err = os.RemoveAll(rendered_dir)
if err != nil {
return err
}
// Generate the manifests for all clusters
for _, v := range *clusters.Clusters {
fmt.Printf("Generating manifests for %s\n", v.ClusterName)
appProps := &cdk8s.AppProps{
Outdir: jsii.String(fmt.Sprintf("dist/%s", v.ClusterName)),
OutputFileExtension: jsii.String(".yaml"),
YamlOutputType: cdk8s.YamlOutputType_FOLDER_PER_CHART_FILE_PER_RESOURCE,
}
app := cdk8s.NewApp(appProps)
NewChart(app, appConfig.Name, appConfig.Namespace, appConfig.Name, v.Image)
app.Synth()
}
return nil
}
func NewChart(scope constructs.Construct, id string, ns string, appLabel string, image string) cdk8s.Chart {
chart := cdk8s.NewChart(scope, jsii.String(id), &cdk8s.ChartProps{
Namespace: jsii.String(ns),
})
labels := map[string]*string{
"app": jsii.String(appLabel),
}
k8s.NewKubeDeployment(chart, jsii.String("deployment"), &k8s.KubeDeploymentProps{
Spec: &k8s.DeploymentSpec{
Replicas: jsii.Number(3),
Selector: &k8s.LabelSelector{
MatchLabels: &labels,
},
Template: &k8s.PodTemplateSpec{
Metadata: &k8s.ObjectMeta{
Labels: &labels,
},
Spec: &k8s.PodSpec{
Containers: &[]*k8s.Container{{
Name: jsii.String("app-container"),
Image: jsii.String(image),
Ports: &[]*k8s.ContainerPort{{
ContainerPort: jsii.Number(80),
}},
}},
ServiceAccountName: jsii.String("service-account"),
},
},
},
})
k8s.NewKubeServiceAccount(chart, jsii.String("service-account"), nil)
return chart
}