Skip to content

Commit

Permalink
making topology use config type and adding alb to topology (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsinger-klotho authored Jan 31, 2023
1 parent c9ecd0d commit 91e8f01
Show file tree
Hide file tree
Showing 28 changed files with 86 additions and 80 deletions.
16 changes: 8 additions & 8 deletions pkg/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/klothoplatform/klotho/pkg/config"
"github.com/klothoplatform/klotho/pkg/core"
execunit "github.com/klothoplatform/klotho/pkg/exec_unit"
"github.com/klothoplatform/klotho/pkg/infra/kubernetes"
Expand Down Expand Up @@ -167,24 +168,23 @@ func OutputResources(result *core.CompilationResult, outDir string) (resourceCou
return
}

func GetLanguagesUsed(result *core.CompilationResult) map[core.ExecutableType]bool {
executableLangs := make(map[core.ExecutableType]bool)
func GetLanguagesUsed(result *core.CompilationResult) []core.ExecutableType {
executableLangs := []core.ExecutableType{}
for _, res := range result.Resources() {
switch r := res.(type) {
case *core.ExecutionUnit:
executableLangs[r.Executable.Type] = true
executableLangs = append(executableLangs, r.Executable.Type)
}
}
return executableLangs
}

func GetResourceTypeCount(result *core.CompilationResult) (resourceCounts map[string]map[string]int) {
resourceCounts = make(map[string]map[string]int)
func GetResourceTypeCount(result *core.CompilationResult, cfg *config.Application) (resourceCounts []string) {
for _, res := range result.Resources() {
if _, ok := resourceCounts[res.Key().Kind]; !ok {
resourceCounts[res.Key().Kind] = make(map[string]int)
resType := cfg.GetResourceType(res)
if resType != "" {
resourceCounts = append(resourceCounts, resType)
}
resourceCounts[res.Key().Kind][res.Type()]++
}
return
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cli/klothomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
}
klothoName := "klotho"
if km.VersionQualifier != "" {
klothoName += " " + km.VersionQualifier
analyticsClient.Properties[km.VersionQualifier] = true
}

// if update is specified do the update in place
Expand Down Expand Up @@ -290,7 +290,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
"app": appCfg.AppName,
})

analyticsClient.Info(klothoName + "pre-compile")
analyticsClient.Info(klothoName + " pre-compile")

input, err := input.ReadOSDir(appCfg, cfg.config)
if err != nil {
Expand Down Expand Up @@ -321,7 +321,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
Plugins: plugins.Plugins(),
}

analyticsClient.Info(klothoName + "compiling")
analyticsClient.Info(klothoName + " compiling")

result, err := compiler.Compile(input)
if err != nil || hadErrors.Load() {
Expand All @@ -330,7 +330,7 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
} else {
err = errors.New("Failed run of klotho invocation")
}
analyticsClient.Error(klothoName + "compiling failed")
analyticsClient.Error(klothoName + " compiling failed")

cmd.SilenceErrors = true
cmd.SilenceUsage = true
Expand All @@ -347,10 +347,10 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
}

CloseTreeSitter(result)
analyticsClient.AppendProperties(map[string]interface{}{"resource_types": GetResourceTypeCount(result)})
analyticsClient.AppendProperties(map[string]interface{}{"resource_types": GetResourceTypeCount(result, &appCfg)})
analyticsClient.AppendProperties(map[string]interface{}{"languages": GetLanguagesUsed(result)})
analyticsClient.AppendProperties(map[string]interface{}{"resources": resourceCounts})
analyticsClient.Info(klothoName + "compile complete")
analyticsClient.Info(klothoName + " compile complete")

return nil
}
2 changes: 0 additions & 2 deletions pkg/core/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type (
}

CloudResource interface {
// Type returns the provider-specific type (implementation) of the resource's kind (`Key().Kind`)
Type() string
// Key returns the unique identifier for the resource.
Key() ResourceKey
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/exec_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

type (
ExecutionUnit struct {
ExecType string
Name string
files ConcurrentMap[string, File]
Executable Executable
Expand Down Expand Up @@ -74,8 +73,6 @@ var (
ExecutableTypeGolang = ExecutableType("Golang")
)

func (unit *ExecutionUnit) Type() string { return unit.ExecType }

func (unit *ExecutionUnit) Key() ResourceKey {
return ResourceKey{
Name: unit.Name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
type (
Gateway struct {
Name string
GWType string
Routes []Route
// Map of gateway targets with the exec unit name as the key
DefinedIn string
Expand Down Expand Up @@ -41,7 +40,6 @@ var (
}
)

func (gw *Gateway) Type() string { return gw.GWType }
func NewGateway(name string) *Gateway {
return &Gateway{
Name: name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/infra_as_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ type InfraFiles struct {

var InfraAsCodeKind = "infra_as_code"

func (*InfraFiles) Type() string { return "" }

func (iac *InfraFiles) Key() ResourceKey {
return ResourceKey{
Name: iac.Name,
Expand Down
2 changes: 0 additions & 2 deletions pkg/core/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ type (

var InputFilesKind = "input_files"

func (*InputFiles) Type() string { return "" }

func (*InputFiles) Key() ResourceKey {
return ResourceKey{
Kind: InputFilesKind,
Expand Down
7 changes: 2 additions & 5 deletions pkg/core/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (

type (
Persist struct {
Name string
PersistType string
Kind PersistKind
Name string
Kind PersistKind
}
Secrets struct {
Persist
Expand All @@ -28,8 +27,6 @@ const (
PersistRedisClusterKind PersistKind = "persist_redis_cluster"
)

func (p *Persist) Type() string { return p.PersistType }

func (p *Persist) Key() ResourceKey {
return ResourceKey{
Name: p.Name,
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type (
PubSub struct {
Path string
Name string
PSType string
Events map[string]*Event
}

Expand All @@ -22,8 +21,6 @@ type (

const PubSubKind = "pubsub"

func (p PubSub) Type() string { return p.PSType }

func (p PubSub) Key() ResourceKey {
return ResourceKey{
Name: p.Name,
Expand Down
3 changes: 0 additions & 3 deletions pkg/core/static_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type (
StaticUnit struct {
StaticType string
Name string
IndexDocument string
StaticFiles ConcurrentMap[string, File]
Expand All @@ -17,8 +16,6 @@ type (

var StaticUnitKind = "static_unit"

func (unit *StaticUnit) Type() string { return unit.StaticType }

func (unit *StaticUnit) Key() ResourceKey {
return ResourceKey{
Name: unit.Name,
Expand Down
7 changes: 5 additions & 2 deletions pkg/core/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var DiagramEntityToImgPath = TopoMap{
{Kind: PubSubKind}: "generic/blank/blank.png",

// Use AWS as the ultimate fallback for the Kind, so don't specify Provider.
{Kind: GatewayKind, Provider: ProviderAWS}: "aws/network/api-gateway.png",
{Kind: GatewayKind, Provider: ProviderAWS, Type: "apigateway"}: "aws/network/api-gateway.png",
{Kind: GatewayKind, Provider: ProviderAWS, Type: "alb"}: "aws/network/elb-application-load-balancer.png",
{Kind: ExecutionUnitKind, Provider: ProviderAWS}: "aws/compute/lambda.png",
{Kind: string(PersistKVKind), Provider: ProviderAWS}: "aws/database/dynamodb.png",
{Kind: string(PersistFileKind), Provider: ProviderAWS}: "aws/compute/simple-storage-service-s3.png",
Expand Down Expand Up @@ -109,7 +110,9 @@ var DiagramEntityToCode = TopoMap{
{Kind: PubSubKind}: `generic_blank.Blank("%s")`,

// Use AWS as the ultimate fallback for the Kind, so don't specify Provider.
{Kind: GatewayKind, Provider: ProviderAWS}: `aws_network.APIGateway("%s")`,
{Kind: GatewayKind, Provider: ProviderAWS, Type: "apigateway"}: `aws_network.APIGateway("%s")`,
{Kind: GatewayKind, Provider: ProviderAWS, Type: "alb"}: `aws_network.ElbNetworkLoadBalancer("%s")`,

{Kind: ExecutionUnitKind, Provider: ProviderAWS}: `aws_compute.Lambda("%s")`,
{Kind: string(PersistKVKind), Provider: ProviderAWS}: `aws_database.Dynamodb("%s")`,
{Kind: string(PersistFileKind), Provider: ProviderAWS}: `aws_storage.S3("%s")`,
Expand Down
1 change: 0 additions & 1 deletion pkg/exec_unit/plugin_exec_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (p ExecUnitPlugin) Transform(result *core.CompilationResult, deps *core.Dep
Executable: core.NewExecutable(),
}
cfg := p.Config.GetExecutionUnit(unit.Name)
unit.ExecType = cfg.Type

for key, value := range cfg.EnvironmentVariables {
unit.EnvironmentVariables = append(unit.EnvironmentVariables, core.EnvironmentVariable{
Expand Down
8 changes: 4 additions & 4 deletions pkg/infra/kubernetes/persist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,19 @@ func Test_generateEnvVarsForPersist(t *testing.T) {
}{
{
name: "Wrong dependencies",
unit: &core.ExecutionUnit{Name: "main", ExecType: "exec_unit"},
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "file", Kind: core.PersistFileKind},
values: []core.EnvironmentVariable{},
},
{
name: "orm dependency",
unit: &core.ExecutionUnit{Name: "main", ExecType: "exec_unit"},
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "orm", Kind: core.PersistORMKind},
values: []core.EnvironmentVariable{{Name: "ORM_PERSIST_ORM_CONNECTION", Kind: "persist_orm", ResourceID: "orm", Value: string(core.CONNECTION_STRING)}},
},
{
name: "redis node dependency",
unit: &core.ExecutionUnit{Name: "main", ExecType: "exec_unit"},
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "redisNode", Kind: core.PersistRedisNodeKind},
values: []core.EnvironmentVariable{
{Name: "REDISNODE_PERSIST_REDIS_HOST", Kind: "persist_redis_node", ResourceID: "redisNode", Value: string(core.HOST)},
Expand All @@ -309,7 +309,7 @@ func Test_generateEnvVarsForPersist(t *testing.T) {
},
{
name: "redis cluster dependency",
unit: &core.ExecutionUnit{Name: "main", ExecType: "exec_unit"},
unit: &core.ExecutionUnit{Name: "main"},
resource: &core.Persist{Name: "redisCluster", Kind: core.PersistRedisClusterKind},
values: []core.EnvironmentVariable{
{Name: "REDISCLUSTER_PERSIST_REDIS_HOST", Kind: "persist_redis_cluster", ResourceID: "redisCluster", Value: string(core.HOST)},
Expand Down
27 changes: 24 additions & 3 deletions pkg/infra/pulumi_aws/iac/load_balancing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export class LoadBalancerPlugin {
})
}

private subPathParametersForWildcard(route: string): string {
const segments = route.split('/')
const newRoute: string[] = []
segments.forEach((seg) => {
if (seg.startsWith(':')) {
newRoute.push('*')
} else {
newRoute.push(seg)
}
})
return newRoute.join('/')
}

private createALBasGateway = (gateway: Gateway): pulumi.Output<string> => {
const albSG = new aws.ec2.SecurityGroup(`${this.lib.name}-${gateway.Name}`, {
name: `${this.lib.name}-${gateway.Name}`,
Expand Down Expand Up @@ -118,14 +131,14 @@ export class LoadBalancerPlugin {
type: 'fixed-response',
fixedResponse: {
contentType: 'application/json',
statusCode: '4XX',
statusCode: '404',
},
},
],
})
}

this.createListenerRule(this.lib.name, route.execUnitName + route.path, {
this.createListenerRule(this.lib.name, route.execUnitName + route.path + route.verb, {
listenerArn: listener!.arn,
actions: [
{
Expand All @@ -136,7 +149,15 @@ export class LoadBalancerPlugin {
conditions: [
{
pathPattern: {
values: [route.path],
values: [
this.subPathParametersForWildcard(route.path),
`${this.subPathParametersForWildcard(route.path)}/`,
],
},
},
{
httpRequestMethod: {
values: [route.verb.toUpperCase()],
},
},
],
Expand Down
5 changes: 3 additions & 2 deletions pkg/lang/golang/aws_runtime/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ var dockerfileLambda []byte

func (r *AwsRuntime) AddExecRuntimeFiles(unit *core.ExecutionUnit, result *core.CompilationResult, deps *core.Dependencies) error {
var DockerFile []byte
switch unit.Type() {
unitType := r.Cfg.GetResourceType(unit)
switch unitType {
case "lambda":
DockerFile = dockerfileLambda
default:
return errors.Errorf("unsupported execution unit type: '%s'", unit.Type())
return errors.Errorf("unsupported execution unit type: '%s'", unitType)
}

templateData := TemplateData{
Expand Down
1 change: 0 additions & 1 deletion pkg/lang/golang/plugin_add_exec_runtime_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (p *AddExecRuntimeFiles) Transform(result *core.CompilationResult, deps *co
continue
}

unit.ExecType = p.cfg.GetExecutionUnit(unit.Name).Type
errs.Append(p.runtime.AddExecRuntimeFiles(unit, result, deps))
}

Expand Down
Loading

0 comments on commit 91e8f01

Please sign in to comment.