From da65232c2bb5b25e53a72548a22dc43962ebf3c2 Mon Sep 17 00:00:00 2001 From: Nick Nellis Date: Tue, 6 Jul 2021 11:30:57 -0500 Subject: [PATCH] added resources --- README.md | 4 +++ cmd/generate.go | 9 ++++++ docs/generate.md | 4 +++ pkg/generate/assets/app.yaml.tmpl | 31 ++++++++++++++++---- pkg/generate/assets/gateway.yaml.tmpl | 6 ++-- pkg/generate/assets/serviceentry.yaml.tmpl | 4 +-- pkg/generate/assets/virtualservice.yaml.tmpl | 6 ++-- pkg/generate/gen.go | 3 +- pkg/generate/model.go | 7 +++-- 9 files changed, 57 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index bf648b3..0d65fc7 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,12 @@ generate [flags] --chance-version int Percent chance that a given application will have multiple versions v1/v2/v3 (0-100) (default 10) -h, --help help for generate --hostname string Hostname to use for gateway and virtualService (default "*") + --limits-cpu string Kubernetes container CPU limit + --limits-memory string Kubernetes container memory limit -n, --namespaces int Number of namespaces to generate applications for (default 1) -o, --output-dir string Output directory where assets will be generated (default "out") + --requests-cpu string Kubernetes container CPU request (default "100m") + --requests-memory string Kubernetes container memory request (default "100Mi") --seed int Override random seed with static one (for deterministic outputs) -t, --tiers int Length of the application call stack per namespace (how many applications deep) (default 3) ``` diff --git a/cmd/generate.go b/cmd/generate.go index 69b0787..b165673 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -54,4 +54,13 @@ func init() { "Percent chance that a given application will make a call to an external service (0-100)") generateCmd.Flags().StringVarP(&cfg.OutputDir, "output-dir", "o", "out", "Output directory where assets will be generated") + + generateCmd.Flags().StringVar(&cfg.MemoryRequest, "requests-memory", "100Mi", + "Kubernetes container memory request") + generateCmd.Flags().StringVar(&cfg.MemoryLimit, "limits-memory", "", + "Kubernetes container memory limit") + generateCmd.Flags().StringVar(&cfg.CPULimit, "limits-cpu", "", + "Kubernetes container CPU limit") + generateCmd.Flags().StringVar(&cfg.CPURequest, "requests-cpu", "100m", + "Kubernetes container CPU request") } diff --git a/docs/generate.md b/docs/generate.md index 217f779..4ded2ee 100644 --- a/docs/generate.md +++ b/docs/generate.md @@ -21,8 +21,12 @@ generate [flags] --chance-version int Percent chance that a given application will have multiple versions v1/v2/v3 (0-100) (default 10) -h, --help help for generate --hostname string Hostname to use for gateway and virtualService (default "*") + --limits-cpu string Kubernetes container CPU limit + --limits-memory string Kubernetes container memory limit -n, --namespaces int Number of namespaces to generate applications for (default 1) -o, --output-dir string Output directory where assets will be generated (default "out") + --requests-cpu string Kubernetes container CPU request (default "100m") + --requests-memory string Kubernetes container memory request (default "100Mi") --seed int Override random seed with static one (for deterministic outputs) -t, --tiers int Length of the application call stack per namespace (how many applications deep) (default 3) ``` diff --git a/pkg/generate/assets/app.yaml.tmpl b/pkg/generate/assets/app.yaml.tmpl index a9035fb..7b4e4e5 100644 --- a/pkg/generate/assets/app.yaml.tmpl +++ b/pkg/generate/assets/app.yaml.tmpl @@ -1,4 +1,4 @@ -{{ $seed := .Seed }} +{{ $config := .Config }} {{range $namespace,$microservices := .Microservices}} --- apiVersion: v1 @@ -7,7 +7,7 @@ metadata: name: {{$namespace}} labels: istio-injection: enabled - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" --- {{range $microservice := $microservices}} apiVersion: v1 @@ -18,7 +18,7 @@ metadata: labels: app: {{$microservice.Name}} tier: "{{$microservice.Tier}}" - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" --- apiVersion: v1 kind: Service @@ -28,7 +28,7 @@ metadata: labels: app: {{$microservice.Name}} tier: "{{$microservice.Tier}}" - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" spec: selector: app: {{$microservice.Name}} @@ -48,7 +48,7 @@ metadata: app: {{$microservice.Name}} version: {{$version}} tier: "{{$microservice.Tier}}" - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" spec: replicas: 1 selector: @@ -83,6 +83,27 @@ spec: - name: "EXTERNAL_SERVICE_URIS" value: "{{ genExternalServices $microservice.ExternalServices }}" {{- end }} + {{- if or $config.MemoryRequest $config.MemoryLimit $config.CPURequest $config.CPULimit }} + resources: + {{- if or $config.MemoryLimit $config.CPULimit }} + limits: + {{- if $config.MemoryLimit }} + memory: {{ $config.MemoryLimit }} + {{- end }} + {{- if $config.CPULimit }} + cpu: {{ $config.CPULimit }} + {{- end }} + {{- end }} + {{- if or $config.MemoryRequest $config.CPURequest }} + requests: + {{- if $config.MemoryRequest }} + memory: {{ $config.MemoryRequest }} + {{- end }} + {{- if $config.CPURequest }} + cpu: {{ $config.CPURequest }} + {{- end }} + {{- end }} + {{- end }} --- {{- end }} {{- end }} diff --git a/pkg/generate/assets/gateway.yaml.tmpl b/pkg/generate/assets/gateway.yaml.tmpl index 28a3625..1edca1c 100644 --- a/pkg/generate/assets/gateway.yaml.tmpl +++ b/pkg/generate/assets/gateway.yaml.tmpl @@ -1,4 +1,4 @@ -{{ $seed := .Seed }} +{{ $config := .Config }} --- apiVersion: networking.istio.io/v1beta1 kind: Gateway @@ -6,7 +6,7 @@ metadata: name: istio-ingressgateway namespace: istio-system labels: - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" spec: selector: istio: ingressgateway @@ -16,5 +16,5 @@ spec: name: http protocol: HTTP hosts: - - "{{- .Host }}" + - "{{- $config.Hostname }}" --- diff --git a/pkg/generate/assets/serviceentry.yaml.tmpl b/pkg/generate/assets/serviceentry.yaml.tmpl index b38ce89..1138582 100644 --- a/pkg/generate/assets/serviceentry.yaml.tmpl +++ b/pkg/generate/assets/serviceentry.yaml.tmpl @@ -1,4 +1,4 @@ -{{ $seed := .Seed }} +{{ $config := .Config }} {{- range $service := .ExternalServices }} apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry @@ -6,7 +6,7 @@ metadata: name: {{ $service }} namespace: istio-system labels: - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" spec: hosts: - {{ $service }} diff --git a/pkg/generate/assets/virtualservice.yaml.tmpl b/pkg/generate/assets/virtualservice.yaml.tmpl index 614ccde..2b0ead9 100644 --- a/pkg/generate/assets/virtualservice.yaml.tmpl +++ b/pkg/generate/assets/virtualservice.yaml.tmpl @@ -1,4 +1,4 @@ -{{ $seed := .Seed }} +{{ $config := .Config }} --- apiVersion: networking.istio.io/v1beta1 kind: VirtualService @@ -6,10 +6,10 @@ metadata: name: ingress-gw namespace: istio-system labels: - seed: "{{ $seed }}" + seed: "{{ $config.Seed }}" spec: hosts: - - "{{- .Host }}" + - "{{- $config.Hostname }}" gateways: - istio-ingressgateway http: diff --git a/pkg/generate/gen.go b/pkg/generate/gen.go index 82bfdf4..072931d 100644 --- a/pkg/generate/gen.go +++ b/pkg/generate/gen.go @@ -146,8 +146,7 @@ func (a *AppGenerator) render(microservices map[string][]*Microservice) error { templateConfig := &TemplateConfig{ Microservices: microservices, ExternalServices: externalServices, - Host: a.config.Hostname, - Seed: a.config.Seed, + Config: a.config, } gatewayFile, err := os.Create(a.config.OutputDir + "/gateway.yaml") diff --git a/pkg/generate/model.go b/pkg/generate/model.go index ad3fa3c..5ec635b 100644 --- a/pkg/generate/model.go +++ b/pkg/generate/model.go @@ -29,8 +29,7 @@ type Backend struct { type TemplateConfig struct { Microservices map[string][]*Microservice ExternalServices []string - Host string - Seed int64 + Config *Config } type Config struct { @@ -45,4 +44,8 @@ type Config struct { ChanceToCallExternalService int OutputDir string Hostname string + MemoryLimit string + CPULimit string + MemoryRequest string + CPURequest string }