From adca01c1d40f48edfbd77a0ea72481c3adf0ad63 Mon Sep 17 00:00:00 2001 From: Nick Nellis Date: Tue, 6 Jul 2021 09:47:24 -0500 Subject: [PATCH] added seed to labels --- README.md | 17 +++++++++++++++++ pkg/generate/assets/app.yaml.tmpl | 5 +++++ pkg/generate/assets/gateway.yaml.tmpl | 3 +++ pkg/generate/assets/serviceentry.yaml.tmpl | 3 +++ pkg/generate/assets/virtualservice.yaml.tmpl | 3 +++ pkg/generate/const.go | 1 - pkg/generate/gen.go | 8 ++++---- pkg/generate/model.go | 5 +++-- 8 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f6cc3f6..bf648b3 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,23 @@ generate [flags] --app-error-percent 0.25 ``` +* Recreating an output - there are times you might want to recreate a users setup. All assets are labeled with a seed value. simply use the seed in your cmd +```shell +# Example namespace +# apiVersion: v1 +# kind: Namespace +# metadata: +# name: ns-1 +# labels: +# istio-injection: enabled +# seed: "1625582727962871000" + +./istio-app-simulator generate \ + # use the seed above to regenerate the same output + --seed 1625582727962871000 +``` + + ### Fake Service UI You can reach a UI for the fake service call stack by using kubernetes port forwarding to the tier 1 service in the namespace ```shell diff --git a/pkg/generate/assets/app.yaml.tmpl b/pkg/generate/assets/app.yaml.tmpl index bb219ec..a9035fb 100644 --- a/pkg/generate/assets/app.yaml.tmpl +++ b/pkg/generate/assets/app.yaml.tmpl @@ -1,3 +1,4 @@ +{{ $seed := .Seed }} {{range $namespace,$microservices := .Microservices}} --- apiVersion: v1 @@ -6,6 +7,7 @@ metadata: name: {{$namespace}} labels: istio-injection: enabled + seed: "{{ $seed }}" --- {{range $microservice := $microservices}} apiVersion: v1 @@ -16,6 +18,7 @@ metadata: labels: app: {{$microservice.Name}} tier: "{{$microservice.Tier}}" + seed: "{{ $seed }}" --- apiVersion: v1 kind: Service @@ -25,6 +28,7 @@ metadata: labels: app: {{$microservice.Name}} tier: "{{$microservice.Tier}}" + seed: "{{ $seed }}" spec: selector: app: {{$microservice.Name}} @@ -44,6 +48,7 @@ metadata: app: {{$microservice.Name}} version: {{$version}} tier: "{{$microservice.Tier}}" + seed: "{{ $seed }}" spec: replicas: 1 selector: diff --git a/pkg/generate/assets/gateway.yaml.tmpl b/pkg/generate/assets/gateway.yaml.tmpl index 59fb56a..28a3625 100644 --- a/pkg/generate/assets/gateway.yaml.tmpl +++ b/pkg/generate/assets/gateway.yaml.tmpl @@ -1,9 +1,12 @@ +{{ $seed := .Seed }} --- apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: istio-ingressgateway namespace: istio-system + labels: + seed: "{{ $seed }}" spec: selector: istio: ingressgateway diff --git a/pkg/generate/assets/serviceentry.yaml.tmpl b/pkg/generate/assets/serviceentry.yaml.tmpl index 70cd119..b38ce89 100644 --- a/pkg/generate/assets/serviceentry.yaml.tmpl +++ b/pkg/generate/assets/serviceentry.yaml.tmpl @@ -1,9 +1,12 @@ +{{ $seed := .Seed }} {{- range $service := .ExternalServices }} apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: {{ $service }} namespace: istio-system + labels: + seed: "{{ $seed }}" spec: hosts: - {{ $service }} diff --git a/pkg/generate/assets/virtualservice.yaml.tmpl b/pkg/generate/assets/virtualservice.yaml.tmpl index 3efc488..614ccde 100644 --- a/pkg/generate/assets/virtualservice.yaml.tmpl +++ b/pkg/generate/assets/virtualservice.yaml.tmpl @@ -1,9 +1,12 @@ +{{ $seed := .Seed }} --- apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: ingress-gw namespace: istio-system + labels: + seed: "{{ $seed }}" spec: hosts: - "{{- .Host }}" diff --git a/pkg/generate/const.go b/pkg/generate/const.go index 2467a38..e115f2b 100644 --- a/pkg/generate/const.go +++ b/pkg/generate/const.go @@ -1,6 +1,5 @@ package generate - const ( maxAmountOfVersions = 3 ) diff --git a/pkg/generate/gen.go b/pkg/generate/gen.go index ee0d9bb..6b36351 100644 --- a/pkg/generate/gen.go +++ b/pkg/generate/gen.go @@ -27,13 +27,12 @@ func NewAppGenerator(config *Config) *AppGenerator { func (a *AppGenerator) Generate() error { - seed := a.config.Seed // if unset, make random - if seed == 0 { - seed = time.Now().UnixNano() + if a.config.Seed == 0 { + a.config.Seed = time.Now().UnixNano() } - rand.Seed(seed) + rand.Seed(a.config.Seed) // generate namespaces // namespace names will be very simple ns1, ns2 etc var microservices []*Microservice @@ -149,6 +148,7 @@ func (a *AppGenerator) render(microservices map[string][]*Microservice) { Microservices: microservices, ExternalServices: externalServices, Host: a.config.Hostname, + Seed: a.config.Seed, } gatewayFile, err := os.Create(a.config.OutputDir + "/gateway.yaml") diff --git a/pkg/generate/model.go b/pkg/generate/model.go index d5204d7..ad3fa3c 100644 --- a/pkg/generate/model.go +++ b/pkg/generate/model.go @@ -27,9 +27,10 @@ type Backend struct { } type TemplateConfig struct { - Microservices map[string][]*Microservice + Microservices map[string][]*Microservice ExternalServices []string - Host string + Host string + Seed int64 } type Config struct {