Skip to content

Commit

Permalink
eks fixes and sanitize pulumi name
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Singer committed Dec 13, 2023
1 parent 1c0d9a1 commit e96583d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/engine2/testdata/k8s_api.expect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ resources:
TargetType: ip
Vpc: aws:vpc:vpc-0
kubernetes:target_group_binding:restapi4integration0-pod2:
Cluster: aws:eks_cluster:eks_cluster-0
Object:
apiVersion: elbv2.k8s.aws/v1beta1
kind: TargetGroupBinding
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/cli2.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GenerateIac(cmd *cobra.Command, args []string) error {
switch generateIacCfg.provider {
case "pulumi":
pulumiPlugin := iac3.Plugin{
Config: &config.Application{AppName: generateIacCfg.appName},
Config: &iac3.PulumiConfig{AppName: generateIacCfg.appName},
KB: kb,
}
iacFiles, err := pulumiPlugin.Translate(solCtx)
Expand Down
29 changes: 24 additions & 5 deletions pkg/infra/iac3/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"fmt"
"io"
"io/fs"
"regexp"
"strings"
"text/template"

"github.com/klothoplatform/klotho/pkg/config"
construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/engine2/solution_context"
kio "github.com/klothoplatform/klotho/pkg/io"
Expand All @@ -20,10 +20,16 @@ import (
"github.com/klothoplatform/klotho/pkg/templateutils"
)

type Plugin struct {
Config *config.Application
KB *knowledgebase.KnowledgeBase
}
type (
PulumiConfig struct {
AppName string
}

Plugin struct {
Config *PulumiConfig
KB *knowledgebase.KnowledgeBase
}
)

func (p Plugin) Name() string {
return "pulumi3"
Expand All @@ -43,6 +49,10 @@ var (

func (p Plugin) Translate(ctx solution_context.SolutionContext) ([]kio.File, error) {

err := p.sanitizeConfig()
if err != nil {
return nil, err
}
// TODO We'll eventually want to split the output into different files, but we don't know exactly what that looks
// like yet. For now, just write to a single file, "index.ts"
buf := getBuffer()
Expand Down Expand Up @@ -132,6 +142,15 @@ func (p Plugin) Translate(ctx solution_context.SolutionContext) ([]kio.File, err
return files, nil
}

func (p *Plugin) sanitizeConfig() error {
reg, err := regexp.Compile("[^a-zA-Z0-9-_]+")
if err != nil {
return fmt.Errorf("Error compiling regex: %v", err)
}
p.Config.AppName = reg.ReplaceAllString(p.Config.AppName, "")
return nil
}

func renderGlobals(w io.Writer) error {
globalsFile, err := files.Open("templates/globals.ts")
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/knowledge_base2/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (c *ConsumptionObject) Convert(value any, res construct.ResourceId, ctx Dyn
"sub": func(a int, b int) int {
return a - b
},
"add": func(a int, b int) int {
return a + b
},
},
).Parse(c.Converter)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source: kubernetes:storage_class
target: aws:eks_cluster
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/resources/pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ consumption:
{
"name": "{{ $key }}",
"value": "{{ $value }}"
}{{if ne $i (sub (len $) 1)}},{{end}}
}{{if ne $i (sub (len $) 1)}},{{end}}{{ $i = add $i 1 }}
{{ end }}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ sanitize_name:
properties:
Cluster:
type: resource
namespace: true
operational_rule:
step:
direction: downstream
resources:
- classifications:
Expand Down

0 comments on commit e96583d

Please sign in to comment.