From c3c1bfa7f9fb0227ea2909358b33fdd62d8d927c Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 17 Aug 2022 11:09:26 -0500 Subject: [PATCH 01/98] initial commit before code generation --- .../templates/snapshot_emitter_template.go | 195 ++++++++++++++++-- 1 file changed, 183 insertions(+), 12 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index a86d7adc0..8f28db8ab 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -34,6 +34,11 @@ import ( "github.com/solo-io/go-utils/errutils" "github.com/solo-io/go-utils/contextutils" + // TODO-JAKE ADDED THESE + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + kubewatch "k8s.io/apimachinery/pkg/watch" + // END WHAT JAKE ADDED ) {{ $emitter_prefix := (print (snake .Name) "/emitter") }} @@ -162,19 +167,17 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o var done sync.WaitGroup ctx := opts.Ctx - {{- range .Resources}} /* Create channel for {{ .Name }} */ {{- if (not .ClusterScoped) }} - type {{ lower_camel .Name }}ListWithNamespace struct { - list {{ .ImportPrefix }}{{ .Name }}List - namespace string - } +type {{ lower_camel .Name }}ListWithNamespace struct { + list {{ .ImportPrefix }}{{ .Name }}List + namespace string +} {{ lower_camel .Name }}Chan := make(chan {{ lower_camel .Name }}ListWithNamespace) - - var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List{{- end }} - -{{- end}} + var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List +{{- end }} +{{- end }} currentSnapshot := {{ .GoName }}Snapshot{} @@ -184,7 +187,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} - for _, namespace := range watchNamespaces { + for _, namespace := range watchNamespaces { {{/*BEGIN_RANGE_WATCHNAMESPACES*/}} {{- range .Resources}} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ .Name }} */ @@ -208,7 +211,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }(namespace) {{- end }} -{{- end}} +{{- end }} /* Watch for changes and update snapshot */ go func(namespace string) { @@ -232,7 +235,175 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } } }(namespace) + } {{/*END_RANGE_WATCHNAMESPACES*/}} + +{{- if (not .ClusterScoped) }}{{/*BEGIN_FILTER_NON_WATCHED_NAMESPACES*/}} + if opts.ExpressionSelector != "" { + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + if len(watchNamespaces) == 1 && watchNamespaces[0] == "" { + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + } + + namespacesResources,err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // JAKE-STARTED HERE WITH THE NAMESPACING RESOURCES + for _, namespace := range allOtherNamespaces { + // TODO-JAKE need to do this on all the resources +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + // TODO-JAKE need to ensure that we are using ExpressionSelector here... + /* Setup namespaced watch for {{ upper_camel .Name }} */ + { + // JAKE-TODO we can leave this just how it is? not sure because we might want to have ExpressionSelectors + {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") + } + initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .Name }}s...) + {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting {{ upper_camel .Name }} watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .Name }}s") + }(namespace) + // JAKE END OF THE NEW NAMESPACES ADDED +{{- end }} +{{- end }} + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: + } +{{- end }} +{{- end }} + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch,err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + // TODO-JAKE do somehting about it + } + + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + // TODO something bad happens + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + // TODO something bad happened + } + for _,item := range namespacesResources.Items { +{{- range .Resources }} +{{- if (not .ClusterScoped) } + namespace := item.Namespace + _, hit := {{ lower_camel .Name }}sByNamespace[namespace] + if ! hit { + /* Setup namespaced watch for {{ upper_camel .Name }} */ + { + {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // TODO-JAKE need to add to some other err channell... + errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") + continue + } + // TODO-JAKE not sure if we need to add to the initial{{ upper_camel .Name }}List or not... + // probably do not need to, because this is in an async call. + initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .Name }}s...) + {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + if err != nil { + // TODO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + continue + } + + // TODO-JAKE should I be calling done here? + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .Name }}s") + }(namespace) +{{- end }} +{{- end }} + // if that is the case, return the {{ lower_camel .Name }}NamespacesChan.... + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: + } +{{- end }} +{{- end }} + } + } + }(namespace) + } + } + } + } + } + }() } +{{- end }}{{/*END_FILTER_NON_WATCHED_NAMESPACES*/}} {{- range .Resources}} {{- if .ClusterScoped }} @@ -347,7 +518,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } currentSnapshot.{{ upper_camel .PluralName }} = {{ lower_camel .Name }}List.Sort() {{- end }} -{{- end}} +{{- end }} } } }() From 9d2a486e31b3959b57f91440e5555692e5a6ccc2 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 18 Aug 2022 08:42:34 -0500 Subject: [PATCH 02/98] solving generator problems --- .../templates/snapshot_emitter_template.go | 214 ++++++++++++++++-- 1 file changed, 201 insertions(+), 13 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index a86d7adc0..171d3da1d 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -17,7 +17,7 @@ var ResourceGroupEmitterTemplate = template.Must(template.New("resource_group_em {{- $clients := (join_str_slice $clients ", ") }} import ( - "fmt" + "bytes" "sync" "time" @@ -34,6 +34,9 @@ import ( "github.com/solo-io/go-utils/errutils" "github.com/solo-io/go-utils/contextutils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + kubewatch "k8s.io/apimachinery/pkg/watch" ) {{ $emitter_prefix := (print (snake .Name) "/emitter") }} @@ -159,22 +162,33 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } errs := make(chan error) + hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchNamespacesIsEmpty := ! hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx + // if we are watching namespaces, then we do not want to fitler any of the + // resources in when listing or watching + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} + if watchNamespacesIsEmpty { + // if the namespaces that we are watching is empty, then we want to apply + // the expression Selectors to all the namespaces. + watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector + watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector + } {{- range .Resources}} /* Create channel for {{ .Name }} */ {{- if (not .ClusterScoped) }} - type {{ lower_camel .Name }}ListWithNamespace struct { - list {{ .ImportPrefix }}{{ .Name }}List - namespace string - } +type {{ lower_camel .Name }}ListWithNamespace struct { + list {{ .ImportPrefix }}{{ .Name }}List + namespace string +} {{ lower_camel .Name }}Chan := make(chan {{ lower_camel .Name }}ListWithNamespace) - - var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List{{- end }} - -{{- end}} + var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List +{{- end }} +{{- end }} currentSnapshot := {{ .GoName }}Snapshot{} @@ -184,19 +198,23 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} + // watched namespaces for _, namespace := range watchNamespaces { {{- range .Resources}} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ .Name }} */ { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") } initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .PluralName }}...) {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch") } @@ -208,7 +226,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }(namespace) {{- end }} -{{- end}} +{{- end }} /* Watch for changes and update snapshot */ go func(namespace string) { @@ -233,9 +251,179 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } }(namespace) } +{{- if (not .ClusterScoped) }} + if hasWatchedNamespaces && opts.ExpressionSelector != "" { + // watch resources using non-watched namespaces. With these namespaces we + // will watch only those that are filted using the label selectors defined + // by Expression Selectors + + // first get the renaiming namespaces + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + + namespacesResources,err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // nonWatchedNamespaces + for _, namespace := range allOtherNamespaces { +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + /* Setup namespaced watch for {{ upper_camel .Name }} */ + { + {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") + } + initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .Name }}s...) + {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting {{ upper_camel .Name }} watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .Name }}s") + }(namespace) +{{- end }} +{{- end }} + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: + } +{{- end }} +{{- end }} + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch,err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("receiving namespace event", event) + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + errs <- errors.Wrapf(err, "listing the namespace resources") + } + for _,item := range namespacesResources.Items { +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + namespace := item.Namespace + _, hit := {{ lower_camel .Name }}sByNamespace[namespace] + if ! hit { + /* Setup namespaced watch for {{ upper_camel .Name }} */ + { + {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE pretty sure we want to send + // an error message here, but we might want + // to do something else. + errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") + continue + } + {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .Name }}s") + }(namespace) +{{- end }} +{{- end }} + // if that is the case, return the {{ lower_camel .Name }}NamespacesChan.... + /* Watch for changes and update snapshot */ + // INFO-JAKE I would like to refactor this code + // is there a way we can refactor all this code + // in the go routine once? + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: + } +{{- end }} +{{- end }} + } + } + }(namespace) + } + } + } + } + } + }() + } +{{- end }} {{- range .Resources}} {{- if .ClusterScoped }} + // TODO-JAKE not sure if ther is anything that will need to be done for cluster + // scoped resources. Verify if this is even used in Gloo or not. /* Setup cluster-wide watch for {{ .Name }} */ var err error currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -347,7 +535,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } currentSnapshot.{{ upper_camel .PluralName }} = {{ lower_camel .Name }}List.Sort() {{- end }} -{{- end}} +{{- end }} } } }() From a8a66dd6d8ea09f9b9684bb77211211aae902041 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 18 Aug 2022 12:59:24 -0500 Subject: [PATCH 03/98] updated the snahost emitter template, it now generates code. It will now update all resources if a namespace has been added. --- .../templates/snapshot_emitter_template.go | 87 +-- .../v1/kubeconfigs_snapshot_emitter.sk.go | 187 +++++- test/mocks/v1/testing_snapshot_emitter.sk.go | 549 +++++++++++++++++- .../v1alpha1/testing_snapshot_emitter.sk.go | 187 +++++- .../v2alpha1/testing_snapshot_emitter.sk.go | 331 ++++++++++- 5 files changed, 1265 insertions(+), 76 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 171d3da1d..01dc0af0e 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -169,6 +169,9 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // if we are watching namespaces, then we do not want to fitler any of the // resources in when listing or watching + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} if watchNamespacesIsEmpty { @@ -181,10 +184,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- range .Resources}} /* Create channel for {{ .Name }} */ {{- if (not .ClusterScoped) }} -type {{ lower_camel .Name }}ListWithNamespace struct { - list {{ .ImportPrefix }}{{ .Name }}List - namespace string -} + type {{ lower_camel .Name }}ListWithNamespace struct { + list {{ .ImportPrefix }}{{ .Name }}List + namespace string + } {{ lower_camel .Name }}Chan := make(chan {{ lower_camel .Name }}ListWithNamespace) var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List {{- end }} @@ -192,11 +195,11 @@ type {{ lower_camel .Name }}ListWithNamespace struct { currentSnapshot := {{ .GoName }}Snapshot{} - {{- range .Resources}} - {{- if not .ClusterScoped }} - {{ lower_camel .PluralName }}ByNamespace := make(map[string]{{ .ImportPrefix }}{{ .Name }}List) - {{- end }} - {{- end }} +{{- range .Resources}} +{{- if not .ClusterScoped }} + {{ lower_camel .PluralName }}ByNamespace := make(map[string]{{ .ImportPrefix }}{{ .Name }}List) +{{- end }} +{{- end }} // watched namespaces for _, namespace := range watchNamespaces { @@ -204,9 +207,6 @@ type {{ lower_camel .Name }}ListWithNamespace struct { {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ .Name }} */ { - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") @@ -227,7 +227,6 @@ type {{ lower_camel .Name }}ListWithNamespace struct { {{- end }} {{- end }} - /* Watch for changes and update snapshot */ go func(namespace string) { for { @@ -251,7 +250,6 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } }(namespace) } -{{- if (not .ClusterScoped) }} if hasWatchedNamespaces && opts.ExpressionSelector != "" { // watch resources using non-watched namespaces. With these namespaces we // will watch only those that are filted using the label selectors defined @@ -281,17 +279,18 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } // nonWatchedNamespaces + // REFACTOR for _, namespace := range allOtherNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ upper_camel .Name }} */ { - {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") } - initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .Name }}s...) - {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List,{{ lower_camel .PluralName }}...) + {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} } {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) if err != nil { @@ -301,7 +300,7 @@ type {{ lower_camel .Name }}ListWithNamespace struct { done.Add(1) go func(namespace string) { defer done.Done() - errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .Name }}s") + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .PluralName }}") }(namespace) {{- end }} {{- end }} @@ -347,28 +346,42 @@ type {{ lower_camel .Name }}ListWithNamespace struct { case kubewatch.Error: errs <- errors.Errorf("receiving namespace event", event) default: - // we get an event namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") } + + hit := false + newNamespaces := []string{} + for _,item := range namespacesResources.Items { + namespace := item.Namespace {{- range .Resources }} {{- if (not .ClusterScoped) }} - namespace := item.Namespace - _, hit := {{ lower_camel .Name }}sByNamespace[namespace] + _, hit = {{ lower_camel .PluralName }}ByNamespace[namespace] if ! hit { - /* Setup namespaced watch for {{ upper_camel .Name }} */ + newNamespaces = append(newNamespaces, namespace) + continue + } +{{- end }} +{{- end }} + } + if hit { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + {{- range .Resources }} + {{- if (not .ClusterScoped) }} + /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ { - {{ lower_camel .Name }}s, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE pretty sure we want to send - // an error message here, but we might want - // to do something else. + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") continue } - {{ lower_camel .Name }}sByNamespace[namespace] = {{ lower_camel .Name }}s + {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} } {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) if err != nil { @@ -382,22 +395,19 @@ type {{ lower_camel .Name }}ListWithNamespace struct { done.Add(1) go func(namespace string) { defer done.Done() - errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .Name }}s") + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .PluralName }}") }(namespace) -{{- end }} -{{- end }} - // if that is the case, return the {{ lower_camel .Name }}NamespacesChan.... + {{- end }} + {{- end }} /* Watch for changes and update snapshot */ - // INFO-JAKE I would like to refactor this code - // is there a way we can refactor all this code - // in the go routine once? + // REFACTOR go func(namespace string) { for { select { case <-ctx.Done(): return -{{- range .Resources }} -{{- if (not .ClusterScoped) }} + {{- range .Resources }} + {{- if (not .ClusterScoped) }} case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: if !ok { return @@ -407,8 +417,8 @@ type {{ lower_camel .Name }}ListWithNamespace struct { return case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: } -{{- end }} -{{- end }} + {{- end }} + {{- end }} } } }(namespace) @@ -419,7 +429,6 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } }() } -{{- end }} {{- range .Resources}} {{- if .ClusterScoped }} // TODO-JAKE not sure if ther is anything that will need to be done for cluster diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index f6e29546c..71bb65f75 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -3,6 +3,7 @@ package v1 import ( + "bytes" "sync" "time" @@ -17,6 +18,9 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubewatch "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/kubernetes" ) var ( @@ -124,31 +128,47 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } errs := make(chan error) + hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx + + // if we are watching namespaces, then we do not want to fitler any of the + // resources in when listing or watching + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} + if watchNamespacesIsEmpty { + // if the namespaces that we are watching is empty, then we want to apply + // the expression Selectors to all the namespaces. + watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector + watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector + } /* Create channel for KubeConfig */ type kubeConfigListWithNamespace struct { list KubeConfigList namespace string } kubeConfigChan := make(chan kubeConfigListWithNamespace) - var initialKubeConfigList KubeConfigList currentSnapshot := KubeconfigsSnapshot{} kubeconfigsByNamespace := make(map[string]KubeConfigList) + // watched namespaces for _, namespace := range watchNamespaces { /* Setup namespaced watch for KubeConfig */ { - kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + kubeconfigs, err := c.kubeConfig.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial KubeConfig list") } initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) kubeconfigsByNamespace[namespace] = kubeconfigs } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting KubeConfig watch") } @@ -158,7 +178,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa defer done.Done() errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-kubeconfigs") }(namespace) - /* Watch for changes and update snapshot */ go func(namespace string) { for { @@ -178,6 +197,166 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } }(namespace) } + if hasWatchedNamespaces && opts.ExpressionSelector != "" { + // watch resources using non-watched namespaces. With these namespaces we + // will watch only those that are filted using the label selectors defined + // by Expression Selectors + + // first get the renaiming namespaces + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + + namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // nonWatchedNamespaces + // REFACTOR + for _, namespace := range allOtherNamespaces { + /* Setup namespaced watch for KubeConfig */ + { + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial KubeConfig list") + } + initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) + kubeconfigsByNamespace[namespace] = kubeconfigs + } + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting KubeConfig watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-kubeconfigs") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case kubeConfigList, ok := <-kubeConfigNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: + } + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("receiving namespace event", event) + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + errs <- errors.Wrapf(err, "listing the namespace resources") + } + + hit := false + newNamespaces := []string{} + + for _, item := range namespacesResources.Items { + namespace := item.Namespace + _, hit = kubeconfigsByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + } + if hit { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for KubeConfig for new namespace */ + { + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") + continue + } + kubeconfigsByNamespace[namespace] = kubeconfigs + } + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case kubeConfigList, ok := <-kubeConfigNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: + } + } + } + }(namespace) + } + } + } + } + } + }() + } /* Initialize snapshot for Kubeconfigs */ currentSnapshot.Kubeconfigs = initialKubeConfigList.Sort() diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index ed9b8066c..5f6b6817d 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -3,6 +3,7 @@ package v1 import ( + "bytes" "sync" "time" @@ -19,6 +20,9 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubewatch "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/kubernetes" ) var ( @@ -186,15 +190,30 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } errs := make(chan error) + hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx + + // if we are watching namespaces, then we do not want to fitler any of the + // resources in when listing or watching + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} + if watchNamespacesIsEmpty { + // if the namespaces that we are watching is empty, then we want to apply + // the expression Selectors to all the namespaces. + watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector + watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector + } /* Create channel for SimpleMockResource */ type simpleMockResourceListWithNamespace struct { list SimpleMockResourceList namespace string } simpleMockResourceChan := make(chan simpleMockResourceListWithNamespace) - var initialSimpleMockResourceList SimpleMockResourceList /* Create channel for MockResource */ type mockResourceListWithNamespace struct { @@ -202,7 +221,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } mockResourceChan := make(chan mockResourceListWithNamespace) - var initialMockResourceList MockResourceList /* Create channel for FakeResource */ type fakeResourceListWithNamespace struct { @@ -210,7 +228,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } fakeResourceChan := make(chan fakeResourceListWithNamespace) - var initialFakeResourceList FakeResourceList /* Create channel for AnotherMockResource */ type anotherMockResourceListWithNamespace struct { @@ -218,7 +235,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } anotherMockResourceChan := make(chan anotherMockResourceListWithNamespace) - var initialAnotherMockResourceList AnotherMockResourceList /* Create channel for ClusterResource */ /* Create channel for MockCustomType */ @@ -227,7 +243,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } mockCustomTypeChan := make(chan mockCustomTypeListWithNamespace) - var initialMockCustomTypeList MockCustomTypeList /* Create channel for Pod */ type podListWithNamespace struct { @@ -235,7 +250,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } podChan := make(chan podListWithNamespace) - var initialPodList github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList currentSnapshot := TestingSnapshot{} @@ -246,17 +260,18 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mctsByNamespace := make(map[string]MockCustomTypeList) podsByNamespace := make(map[string]github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) + // watched namespaces for _, namespace := range watchNamespaces { /* Setup namespaced watch for SimpleMockResource */ { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + simplemocks, err := c.simpleMockResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") } initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) simplemocksByNamespace[namespace] = simplemocks } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") } @@ -268,14 +283,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace[namespace] = mocks } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -287,14 +302,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace[namespace] = fakes } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } @@ -306,14 +321,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for AnotherMockResource */ { - anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + anothermockresources, err := c.anotherMockResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") } initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) anothermockresourcesByNamespace[namespace] = anothermockresources } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting AnotherMockResource watch") } @@ -325,14 +340,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockCustomType */ { - mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + mcts, err := c.mockCustomType.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockCustomType list") } initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) mctsByNamespace[namespace] = mcts } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockCustomType watch") } @@ -344,14 +359,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for Pod */ { - pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + pods, err := c.pod.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial Pod list") } initialPodList = append(initialPodList, pods...) podsByNamespace[namespace] = pods } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting Pod watch") } @@ -361,7 +376,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-pods") }(namespace) - /* Watch for changes and update snapshot */ go func(namespace string) { for { @@ -426,6 +440,501 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if hasWatchedNamespaces && opts.ExpressionSelector != "" { + // watch resources using non-watched namespaces. With these namespaces we + // will watch only those that are filted using the label selectors defined + // by Expression Selectors + + // first get the renaiming namespaces + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + + namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // nonWatchedNamespaces + // REFACTOR + for _, namespace := range allOtherNamespaces { + /* Setup namespaced watch for SimpleMockResource */ + { + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") + } + initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) + simplemocksByNamespace[namespace] = simplemocks + } + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-simplemocks") + }(namespace) + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Setup namespaced watch for FakeResource */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FakeResource list") + } + initialFakeResourceList = append(initialFakeResourceList, fakes...) + fakesByNamespace[namespace] = fakes + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting FakeResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") + }(namespace) + /* Setup namespaced watch for AnotherMockResource */ + { + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") + } + initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) + anothermockresourcesByNamespace[namespace] = anothermockresources + } + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting AnotherMockResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-anothermockresources") + }(namespace) + /* Setup namespaced watch for MockCustomType */ + { + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockCustomType list") + } + initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) + mctsByNamespace[namespace] = mcts + } + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockCustomType watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") + }(namespace) + /* Setup namespaced watch for Pod */ + { + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial Pod list") + } + initialPodList = append(initialPodList, pods...) + podsByNamespace[namespace] = pods + } + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting Pod watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-pods") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: + } + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: + } + case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: + } + case podList, ok := <-podNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case podChan <- podListWithNamespace{list: podList, namespace: namespace}: + } + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("receiving namespace event", event) + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + errs <- errors.Wrapf(err, "listing the namespace resources") + } + + hit := false + newNamespaces := []string{} + + for _, item := range namespacesResources.Items { + namespace := item.Namespace + _, hit = simplemocksByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = mocksByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = fakesByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = anothermockresourcesByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = mctsByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = podsByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + } + if hit { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for SimpleMockResource for new namespace */ + { + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") + continue + } + simplemocksByNamespace[namespace] = simplemocks + } + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") + }(namespace) + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") + continue + } + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") + continue + } + fakesByNamespace[namespace] = fakes + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Setup namespaced watch for AnotherMockResource for new namespace */ + { + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") + continue + } + anothermockresourcesByNamespace[namespace] = anothermockresources + } + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") + }(namespace) + /* Setup namespaced watch for MockCustomType for new namespace */ + { + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") + continue + } + mctsByNamespace[namespace] = mcts + } + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") + }(namespace) + /* Setup namespaced watch for Pod for new namespace */ + { + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace Pod list") + continue + } + podsByNamespace[namespace] = pods + } + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace Pod watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: + } + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: + } + case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: + } + case podList, ok := <-podNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case podChan <- podListWithNamespace{list: podList, namespace: namespace}: + } + } + } + }(namespace) + } + } + } + } + } + }() + } /* Initialize snapshot for Simplemocks */ currentSnapshot.Simplemocks = initialSimpleMockResourceList.Sort() /* Initialize snapshot for Mocks */ @@ -434,6 +943,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot.Fakes = initialFakeResourceList.Sort() /* Initialize snapshot for Anothermockresources */ currentSnapshot.Anothermockresources = initialAnotherMockResourceList.Sort() + // TODO-JAKE not sure if ther is anything that will need to be done for cluster + // scoped resources. Verify if this is even used in Gloo or not. /* Setup cluster-wide watch for ClusterResource */ var err error currentSnapshot.Clusterresources, err = c.clusterResource.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 7cc0459ed..4df044c7e 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -3,6 +3,7 @@ package v1alpha1 import ( + "bytes" "sync" "time" @@ -17,6 +18,9 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubewatch "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/kubernetes" ) var ( @@ -124,31 +128,47 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } errs := make(chan error) + hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx + + // if we are watching namespaces, then we do not want to fitler any of the + // resources in when listing or watching + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} + if watchNamespacesIsEmpty { + // if the namespaces that we are watching is empty, then we want to apply + // the expression Selectors to all the namespaces. + watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector + watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector + } /* Create channel for MockResource */ type mockResourceListWithNamespace struct { list MockResourceList namespace string } mockResourceChan := make(chan mockResourceListWithNamespace) - var initialMockResourceList MockResourceList currentSnapshot := TestingSnapshot{} mocksByNamespace := make(map[string]MockResourceList) + // watched namespaces for _, namespace := range watchNamespaces { /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace[namespace] = mocks } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -158,7 +178,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") }(namespace) - /* Watch for changes and update snapshot */ go func(namespace string) { for { @@ -178,6 +197,166 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if hasWatchedNamespaces && opts.ExpressionSelector != "" { + // watch resources using non-watched namespaces. With these namespaces we + // will watch only those that are filted using the label selectors defined + // by Expression Selectors + + // first get the renaiming namespaces + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + + namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // nonWatchedNamespaces + // REFACTOR + for _, namespace := range allOtherNamespaces { + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("receiving namespace event", event) + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + errs <- errors.Wrapf(err, "listing the namespace resources") + } + + hit := false + newNamespaces := []string{} + + for _, item := range namespacesResources.Items { + namespace := item.Namespace + _, hit = mocksByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + } + if hit { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") + continue + } + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + } + } + }(namespace) + } + } + } + } + } + }() + } /* Initialize snapshot for Mocks */ currentSnapshot.Mocks = initialMockResourceList.Sort() diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index fab0272bf..199778583 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -3,6 +3,7 @@ package v2alpha1 import ( + "bytes" "sync" "time" @@ -19,6 +20,9 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubewatch "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/kubernetes" ) var ( @@ -146,15 +150,30 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } errs := make(chan error) + hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx + + // if we are watching namespaces, then we do not want to fitler any of the + // resources in when listing or watching + // TODO-JAKE not sure if we want to get rid of the Selector in the + // ListOpts here. the reason that we might want to is because we no + // longer allow selectors, unless it is on a unwatched namespace. + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} + if watchNamespacesIsEmpty { + // if the namespaces that we are watching is empty, then we want to apply + // the expression Selectors to all the namespaces. + watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector + watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector + } /* Create channel for MockResource */ type mockResourceListWithNamespace struct { list MockResourceList namespace string } mockResourceChan := make(chan mockResourceListWithNamespace) - var initialMockResourceList MockResourceList /* Create channel for FrequentlyChangingAnnotationsResource */ type frequentlyChangingAnnotationsResourceListWithNamespace struct { @@ -162,7 +181,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } frequentlyChangingAnnotationsResourceChan := make(chan frequentlyChangingAnnotationsResourceListWithNamespace) - var initialFrequentlyChangingAnnotationsResourceList FrequentlyChangingAnnotationsResourceList /* Create channel for FakeResource */ type fakeResourceListWithNamespace struct { @@ -170,7 +188,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } fakeResourceChan := make(chan fakeResourceListWithNamespace) - var initialFakeResourceList testing_solo_io.FakeResourceList currentSnapshot := TestingSnapshot{} @@ -178,17 +195,18 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fcarsByNamespace := make(map[string]FrequentlyChangingAnnotationsResourceList) fakesByNamespace := make(map[string]testing_solo_io.FakeResourceList) + // watched namespaces for _, namespace := range watchNamespaces { /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace[namespace] = mocks } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -200,14 +218,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") } initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) fcarsByNamespace[namespace] = fcars } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting FrequentlyChangingAnnotationsResource watch") } @@ -219,14 +237,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace[namespace] = fakes } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } @@ -236,7 +254,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") }(namespace) - /* Watch for changes and update snapshot */ go func(namespace string) { for { @@ -274,6 +291,300 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if hasWatchedNamespaces && opts.ExpressionSelector != "" { + // watch resources using non-watched namespaces. With these namespaces we + // will watch only those that are filted using the label selectors defined + // by Expression Selectors + + // first get the renaiming namespaces + var k kubernetes.Interface + excludeNamespacesFieldDesciptors := "" + + var buffer bytes.Buffer + for i, ns := range watchNamespaces { + buffer.WriteString("metadata.namespace!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } + } + excludeNamespacesFieldDesciptors = buffer.String() + + namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + allOtherNamespaces := make([]string, len(namespacesResources.Items)) + for _, ns := range namespacesResources.Items { + allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + } + + // nonWatchedNamespaces + // REFACTOR + for _, namespace := range allOtherNamespaces { + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ + { + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") + } + initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) + fcarsByNamespace[namespace] = fcars + } + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting FrequentlyChangingAnnotationsResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-fcars") + }(namespace) + /* Setup namespaced watch for FakeResource */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FakeResource list") + } + initialFakeResourceList = append(initialFakeResourceList, fakes...) + fakesByNamespace[namespace] = fakes + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting FakeResource watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + } + } + }(namespace) + } + // create watch on all namespaces, so that we can add resources from new namespaces + namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + return nil, nil, err + } + + go func() { + for { + select { + case <-ctx.Done(): + return + case event, ok := <-namespaceWatch.ResultChan(): + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("receiving namespace event", event) + default: + // we get an event + namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + if err != nil { + errs <- errors.Wrapf(err, "listing the namespace resources") + } + + hit := false + newNamespaces := []string{} + + for _, item := range namespacesResources.Items { + namespace := item.Namespace + _, hit = mocksByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = fcarsByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + _, hit = fakesByNamespace[namespace] + if !hit { + newNamespaces = append(newNamespaces, namespace) + continue + } + } + if hit { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") + continue + } + mocksByNamespace[namespace] = mocks + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ + { + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") + continue + } + fcarsByNamespace[namespace] = fcars + } + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + if err != nil { + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") + continue + } + fakesByNamespace[namespace] = fakes + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } + + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + } + } + }(namespace) + } + } + } + } + } + }() + } /* Initialize snapshot for Mocks */ currentSnapshot.Mocks = initialMockResourceList.Sort() /* Initialize snapshot for Fcars */ From ed99470f2de03bfa746d86f450f0f38cdb974e0e Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 18 Aug 2022 13:06:34 -0500 Subject: [PATCH 04/98] comment with added --- pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go | 1 - test/mocks/v1/testing_snapshot_emitter.sk.go | 1 - test/mocks/v2alpha1/testing_snapshot_emitter.sk.go | 1 - 3 files changed, 3 deletions(-) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 71bb65f75..bc0769dbc 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -285,7 +285,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa case kubewatch.Error: errs <- errors.Errorf("receiving namespace event", event) default: - // we get an event namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 5f6b6817d..59f0b04df 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -668,7 +668,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO case kubewatch.Error: errs <- errors.Errorf("receiving namespace event", event) default: - // we get an event namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 199778583..15d730644 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -435,7 +435,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO case kubewatch.Error: errs <- errors.Errorf("receiving namespace event", event) default: - // we get an event namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") From acebcb8bb09ef1ace1b07492c90843fb878ae444 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 18 Aug 2022 13:11:07 -0500 Subject: [PATCH 05/98] update to testing snapshot emitter --- test/mocks/v1alpha1/testing_snapshot_emitter.sk.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 4df044c7e..2687e5323 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -285,7 +285,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO case kubewatch.Error: errs <- errors.Errorf("receiving namespace event", event) default: - // we get an event namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") From c466f4dc709bd7467f40229cfbf0e75f6148a05d Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 22 Aug 2022 12:11:26 -0500 Subject: [PATCH 06/98] added code for testing snapshot emitter --- .../templates/snapshot_emitter_template.go | 11 +- .../snapshot_emitter_test_template.go | 833 ++++++++++++++---- .../v1/kubeconfigs_snapshot_emitter.sk.go | 2 +- 3 files changed, 692 insertions(+), 154 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 01dc0af0e..f3b968315 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -4,6 +4,14 @@ import ( "text/template" ) +// Snapshot Emitters are used to take snapshots of the current system, using either +// cluster scoped or non namespaced scoped selection. Watches are used to notify +// the snapshot emitter when new resources have been created or updated. +// Snapshot Emitters will delegate to Resource Clients to list and watch defined +// resources. + +// ClusterScoped - without namespacing, get all the resources within the entire cluster. There is one watch per resource. +// Not using ClusterScoped - allows for using namespacing, so that each namespace has it's own watch per resource. var ResourceGroupEmitterTemplate = template.Must(template.New("resource_group_emitter").Funcs(Funcs).Parse( `package {{ .Project.ProjectConfig.Version }} @@ -161,6 +169,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } } + // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := ! hasWatchedNamespaces @@ -344,7 +353,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event", event) + errs <- errors.Errorf("receiving namespace event: %v", event) default: namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 15dfe78c0..16ad47b4f 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -54,9 +54,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } var ( ctx context.Context - namespace1 string - namespace2 string + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string name1, name2 = "angela"+helpers.RandString(3), "bob"+helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labels2 = map[string]string{"env": "testenv", "owner": "foo"} + labelExpression1 = "env in (test)" {{- if $need_kube_config }} cfg *rest.Config clientset *apiext.Clientset @@ -68,6 +73,15 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end}} ) +{{- range .Resources }} + // TODO-JAKE need to make one of these for each type of resource + NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) (*MockResource) { + resource := NewMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } +{{- end }} + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -75,6 +89,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func ctx = context.Background() namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) kube = helpers.MustKubeClient() err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) @@ -114,7 +132,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) + // TODO-JAKE need to make sure that we delete only the used namespaces + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2, namespace3, namespace4, namespace5, namespace6) Expect(err).NotTo(HaveOccurred()) {{- range .Resources }} @@ -125,226 +144,736 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} }) - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + Context("Tracking watched namespaces", func () { + It("tracks snapshots on changes to any resource", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + {{- range .Resources }} + + /* + {{ .Name }} + */ + + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) + {{- if .ClusterScoped }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - var snap *{{ .GoName }}Snapshot -{{- range .Resources }} + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + {{- else }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - /* - {{ .Name }} - */ - - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) + {{- end }} + + {{- if .ClusterScoped }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }, nil) + {{- else }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }, nil) + {{- end }} + + {{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) + {{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} + + {{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + {{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} + {{- end}} + }) + + It("should be able to track resources that are labeled on other namespaces", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + {{- range .Resources }} + + /* + {{ .Name }} + */ + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): + } + + {{- if .ClusterScoped }} - combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) - nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertSnapshot{{ .PluralName }}(watched, nil) {{- end }} - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } -{{- if .ClusterScoped }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) + assertSnapshotMocks(watched, nil) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) + assertSnapshotMocks(watched, nil) +{{- end }} - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }, nil) + err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace3, namespace4) + Expect(err).NotTo(HaveOccurred()) + +{{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace1, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b } + assertNoMessageSent() +{{- end }} - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }, nil) +{{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) + assertSnapshotMocks(watched, notWatched) {{- end }} {{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) + assertNoMessageSent() +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) +{{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) + assertSnapshotMocks(watched, notWatched) +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) +{{- if .ClusterScoped }} + // TODO-JAKE have to correct the ClusterScoped entries as well.. + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) + assertNoMessageSent() {{- end }} + for _, r := range notWatched { + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + {{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5b.GetMetadata().Namespace, {{ lower_camel .Name }}5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7b.GetMetadata().Namespace, {{ lower_camel .Name }}7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b}...) + assertSnapshot{{ .PluralName }}(nil, notWatched) {{- end }} -{{- end}} +{{- end }} + }) }) - It("tracks snapshots on changes to any resource using AllNamespace", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + Context("Tracking empty watched namespaces", func () { + It("tracks snapshots on changes to any resource using AllNamespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + {{- range .Resources }} + + /* + {{ .Name }} + */ + + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + {{- if .ClusterScoped }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + {{- else }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) + {{- end }} + + {{- if .ClusterScoped }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }, nil) + {{- else }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }, nil) + {{- end }} + + {{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) + {{- else }} - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} + + {{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + {{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} + {{- end}} }) - Expect(err).NotTo(HaveOccurred()) - var snap *{{ .GoName }}Snapshot -{{- range .Resources }} + It("should be able to track resources only made with the matching labels", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - /* - {{ .Name }} - */ - - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + assertNoMatchingMocks := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } } - for _, unexpected := range unexpect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } +{{- range .Resources }} + + /* + {{ .Name }} + */ + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): + } + + {{- if .ClusterScoped }} - combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + // TODO-JAKE need to make this work + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) - nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertNoMatchingMocks() {{- end }} - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } -{{- if .ClusterScoped }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + // now add a new namespace + err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace3, namespace4) + Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- if .ClusterScoped }} +// TODO-JAKE need to make this work + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } + assertNoMatchingMocks() {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }, nil) +// TODO-JAKE need to make this work + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }, nil) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b } + assertSnapshotMocks(watched, notWatched) {{- end }} {{- if .ClusterScoped }} +// TODO-JAKE need to make this work + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) + assertSnapshotMocks(watched, notWatched) +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + // create another set of namespaces + err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) +{{- if .ClusterScoped }} +// TODO-JAKE need to make this work + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) + assertNoMessageSent() +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) +{{- if .ClusterScoped }} +// TODO-JAKE need to make this work + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) + assertSnapshotMocks(watched, notWatched) {{- end }} {{- if .ClusterScoped }} +// TODO-JAKE need to make this work + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) +{{- else }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) + assertNoMessageSent() +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4b.GetMetadata().Namespace, {{ lower_camel .Name }}4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } + assertSnapshot{{ .PluralName }}(watched, notWatched) +{{- end }} - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) +{{- if .ClusterScoped }} + // TODO-JAKE need to be able to do this + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6b.GetMetadata().Namespace, {{ lower_camel .Name }}6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) + assertSnapshot{{ .PluralName }}(nil, notWatched) {{- end }} -{{- end}} +{{- end }} + }) }) }) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index bc0769dbc..656b3d842 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -283,7 +283,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event", event) + errs <- errors.Errorf("receiving namespace event: %v", event) default: namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { From 0f6b483d844b09857041ed807fd8d60add9a7ed9 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 22 Aug 2022 14:57:07 -0500 Subject: [PATCH 07/98] updated so that ClusterScoped resource snapshot emitters will work with the selectors --- .../templates/snapshot_emitter_template.go | 5 +- .../snapshot_emitter_test_template.go | 175 ++++++++++-------- 2 files changed, 95 insertions(+), 85 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index f3b968315..345a799dc 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -440,11 +440,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } {{- range .Resources}} {{- if .ClusterScoped }} - // TODO-JAKE not sure if ther is anything that will need to be done for cluster - // scoped resources. Verify if this is even used in Gloo or not. + // TODO-JAKE verify that this is what we should be doing with Cluster Scoped Resources /* Setup cluster-wide watch for {{ .Name }} */ var err error - currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, Selector: opts.Selector}) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") } diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 16ad47b4f..be12e7df1 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -74,19 +74,29 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func ) {{- range .Resources }} - // TODO-JAKE need to make one of these for each type of resource - NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) (*MockResource) { - resource := NewMockResource(namespace, name) + {{ .ImportPrefix }}New{{ .Name }}WithLabels := func(namespace, name string, labels map[string]string) (*{{ .ImportPrefix }}{{ .Name }}) { + resource := {{ .ImportPrefix }}New{{ .Name }}(namespace, name) resource.Metadata.Labels = labels return resource } {{- end }} + createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + Expect(err).NotTo(HaveOccurred()) + for _,ns := range namespaces { + if _,hit := createdNamespaces[ns]; !hit { + createdNamespaces[ns] = true + } + } + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() + createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -94,7 +104,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace5 = helpers.RandString(8) namespace6 = helpers.RandString(8) kube = helpers.MustKubeClient() - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) {{- if $need_kube_config }} cfg, err = kubeutils.GetConfig("", "") @@ -132,8 +142,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - // TODO-JAKE need to make sure that we delete only the used namespaces - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2, namespace3, namespace4, namespace5, namespace6) + namespacesToDelete := []string{} + for namespace,_ := range createdNamespaces { + namespacesToDelete = append(namespacesToDelete, namespace) + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) Expect(err).NotTo(HaveOccurred()) {{- range .Resources }} @@ -324,7 +337,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMessageSent() {{- else }} {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -336,10 +350,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -350,10 +364,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -363,28 +377,27 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshotMocks(watched, nil) {{- end }} - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace3, namespace4) - Expect(err).NotTo(HaveOccurred()) + createNamespaces(ctx, kube, namespace3, namespace4) {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace1, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) + assertNoMessageSent() {{- else }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b } assertNoMessageSent() {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -395,10 +408,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -408,14 +421,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() {{- end }} - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) - Expect(err).NotTo(HaveOccurred()) + createNamespaces(ctx, kube, namespace5, namespace6) {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + assertNoMessageSent() {{- else }} {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -427,10 +439,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO-JAKE have to correct the ClusterScoped entries as well.. - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }WithLabels(namespace3, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -447,10 +459,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + notWatched ={{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -462,10 +475,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -477,10 +491,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -492,10 +507,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -507,10 +523,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + assertSnapshot{{ .PluralName }}(nil, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -718,10 +734,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO-JAKE need to make this work {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMatchingMocks() {{- else }} {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -731,15 +747,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMatchingMocks() {{- end }} - // now add a new namespace - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace3, namespace4) - Expect(err).NotTo(HaveOccurred()) + createNamespaces(ctx, kube, namespace3, namespace4) {{- if .ClusterScoped }} -// TODO-JAKE need to make this work {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }...) + assertNoMatchingMocks() {{- else }} {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -750,10 +764,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} -// TODO-JAKE need to make this work {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } + assertSnapshotMocks(watched, notWatched) {{- else }} {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -764,10 +778,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} -// TODO-JAKE need to make this work - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) + assertSnapshotMocks(watched, notWatched) {{- else }} {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -777,15 +791,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshotMocks(watched, notWatched) {{- end }} - // create another set of namespaces - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) - Expect(err).NotTo(HaveOccurred()) + createNamespaces(ctx, kube, namespace5, namespace6) {{- if .ClusterScoped }} -// TODO-JAKE need to make this work - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + assertNoMatchingMocks() {{- else }} {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -796,10 +808,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} -// TODO-JAKE need to make this work - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) + assertSnapshotMocks(watched, notWatched) {{- else }} {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -810,10 +822,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} -// TODO-JAKE need to make this work - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + assertNoMessageSent() {{- else }} {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -830,10 +842,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}6a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -845,10 +857,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } + assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -860,10 +872,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- if .ClusterScoped }} - // TODO-JAKE need to be able to do this - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + assertSnapshot{{ .PluralName }}(nil, notWatched) {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) From 16b52324a9f25a9b93c94cf6a36acea2d763fb9b Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 22 Aug 2022 15:18:38 -0500 Subject: [PATCH 08/98] update the snapshot_emitter to call out to kube for the namespaces --- .../templates/snapshot_emitter_template.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 345a799dc..23e1dc58d 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -28,6 +28,7 @@ import ( "bytes" "sync" "time" + "os" {{ .Imports }} "go.opencensus.io/stats" @@ -45,6 +46,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" kubewatch "k8s.io/apimachinery/pkg/watch" + "github.com/solo-io/k8s-utils/kubeutils" ) {{ $emitter_prefix := (print (snake .Name) "/emitter") }} @@ -268,9 +270,21 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" + // TODO-JAKE REFACTOR, this must be added another way + // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources + // I do not think this would work in a real scenario + cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) + if err != nil { + return nil, nil, err + } + k, err := kubernetes.NewForConfig(cfg) + if err != nil { + return nil, nil, err + } + var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.namespace!=") + buffer.WriteString("metadata.name!=") buffer.WriteString(ns) if i < len(watchNamespaces)-1 { buffer.WriteByte(',') @@ -365,6 +379,8 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o for _,item := range namespacesResources.Items { namespace := item.Namespace + // TODO-JAKE we might want to add a set of namespaces + // to the struct above, to manage it's list of namespaces {{- range .Resources }} {{- if (not .ClusterScoped) }} _, hit = {{ lower_camel .PluralName }}ByNamespace[namespace] From e64c8f12315e29e24cdcb7019e91a34d9afd3961 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 23 Aug 2022 11:10:17 -0500 Subject: [PATCH 09/98] updates to testing and snapshot emitter --- .../templates/snapshot_emitter_template.go | 14 +- test/mocks/v1/testing_snapshot_emitter.sk.go | 53 +- .../mocks/v1/testing_snapshot_emitter_test.go | 3162 +++++++++++++---- .../v1alpha1/testing_snapshot_emitter.sk.go | 33 +- .../v2alpha1/testing_snapshot_emitter.sk.go | 39 +- .../v2alpha1/testing_snapshot_emitter_test.go | 1488 ++++++-- 6 files changed, 3750 insertions(+), 1039 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 23e1dc58d..2a2e2a43f 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -277,7 +277,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o if err != nil { return nil, nil, err } - k, err := kubernetes.NewForConfig(cfg) + k, err = kubernetes.NewForConfig(cfg) if err != nil { return nil, nil, err } @@ -292,13 +292,14 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } excludeNamespacesFieldDesciptors = buffer.String() + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources,err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { return nil, nil, err } allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for _, ns := range namespacesResources.Items { - allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + for i, ns := range namespacesResources.Items { + allOtherNamespaces[i] = ns.Namespace } // nonWatchedNamespaces @@ -374,7 +375,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o errs <- errors.Wrapf(err, "listing the namespace resources") } - hit := false newNamespaces := []string{} for _,item := range namespacesResources.Items { @@ -383,15 +383,15 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // to the struct above, to manage it's list of namespaces {{- range .Resources }} {{- if (not .ClusterScoped) }} - _, hit = {{ lower_camel .PluralName }}ByNamespace[namespace] - if ! hit { + if _, hit := {{ lower_camel .PluralName }}ByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } {{- end }} {{- end }} } - if hit { + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0{ // add a watch for all the new namespaces // REFACTOR for _, namespace := range newNamespaces { diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 59f0b04df..d828ce5ee 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -4,6 +4,7 @@ package v1 import ( "bytes" + "os" "sync" "time" @@ -20,6 +21,7 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + "github.com/solo-io/k8s-utils/kubeutils" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kubewatch "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" @@ -189,6 +191,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } + // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces @@ -449,9 +452,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" + // TODO-JAKE REFACTOR, this must be added another way + // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources + // I do not think this would work in a real scenario + cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) + if err != nil { + return nil, nil, err + } + k, err = kubernetes.NewForConfig(cfg) + if err != nil { + return nil, nil, err + } + var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.namespace!=") + buffer.WriteString("metadata.name!=") buffer.WriteString(ns) if i < len(watchNamespaces)-1 { buffer.WriteByte(',') @@ -459,13 +474,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { return nil, nil, err } allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for _, ns := range namespacesResources.Items { - allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + for i, ns := range namespacesResources.Items { + allOtherNamespaces[i] = ns.Namespace } // nonWatchedNamespaces @@ -666,50 +682,46 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event", event) + errs <- errors.Errorf("receiving namespace event: %v", event) default: namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") } - hit := false newNamespaces := []string{} for _, item := range namespacesResources.Items { namespace := item.Namespace - _, hit = simplemocksByNamespace[namespace] - if !hit { + // TODO-JAKE we might want to add a set of namespaces + // to the struct above, to manage it's list of namespaces + if _, hit := simplemocksByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = mocksByNamespace[namespace] - if !hit { + if _, hit := mocksByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = fakesByNamespace[namespace] - if !hit { + if _, hit := fakesByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = anothermockresourcesByNamespace[namespace] - if !hit { + if _, hit := anothermockresourcesByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = mctsByNamespace[namespace] - if !hit { + if _, hit := mctsByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = podsByNamespace[namespace] - if !hit { + if _, hit := podsByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } } - if hit { + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { // add a watch for all the new namespaces // REFACTOR for _, namespace := range newNamespaces { @@ -942,11 +954,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot.Fakes = initialFakeResourceList.Sort() /* Initialize snapshot for Anothermockresources */ currentSnapshot.Anothermockresources = initialAnotherMockResourceList.Sort() - // TODO-JAKE not sure if ther is anything that will need to be done for cluster - // scoped resources. Verify if this is even used in Gloo or not. + // TODO-JAKE verify that this is what we should be doing with Cluster Scoped Resources /* Setup cluster-wide watch for ClusterResource */ var err error - currentSnapshot.Clusterresources, err = c.clusterResource.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + currentSnapshot.Clusterresources, err = c.clusterResource.List(clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, Selector: opts.Selector}) if err != nil { return nil, nil, errors.Wrapf(err, "initial ClusterResource list") } diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 1ba24474f..de9b595c8 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 @@ -40,9 +39,15 @@ var _ = Describe("V1Emitter", func() { } var ( ctx context.Context - namespace1 string - namespace2 string + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string + createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labels2 = map[string]string{"env": "testenv", "owner": "foo"} + labelExpression1 = "env in (test)" cfg *rest.Config clientset *apiext.Clientset kube kubernetes.Interface @@ -55,16 +60,66 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeClient MockCustomTypeClient podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient ) + NewSimpleMockResourceWithLabels := func(namespace, name string, labels map[string]string) *SimpleMockResource { + resource := NewSimpleMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { + resource := NewMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewFakeResourceWithLabels := func(namespace, name string, labels map[string]string) *FakeResource { + resource := NewFakeResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewAnotherMockResourceWithLabels := func(namespace, name string, labels map[string]string) *AnotherMockResource { + resource := NewAnotherMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewClusterResourceWithLabels := func(namespace, name string, labels map[string]string) *ClusterResource { + resource := NewClusterResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewMockCustomTypeWithLabels := func(namespace, name string, labels map[string]string) *MockCustomType { + resource := NewMockCustomType(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewPodWithLabels := func(namespace, name string, labels map[string]string) *github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.Pod { + resource := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace, name) + resource.Metadata.Labels = labels + return resource + } + + createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + Expect(err).NotTo(HaveOccurred()) + for _, ns := range namespaces { + if _, hit := createdNamespaces[ns]; !hit { + createdNamespaces[ns] = true + } + } + } BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() + createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) kube = helpers.MustKubeClient() - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") Expect(err).NotTo(HaveOccurred()) @@ -141,815 +196,2570 @@ var _ = Describe("V1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) + namespacesToDelete := []string{} + for namespace, _ := range createdNamespaces { + namespacesToDelete = append(namespacesToDelete, namespace) + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) Expect(err).NotTo(HaveOccurred()) clusterResourceClient.Delete(name1, clients.DeleteOpts{}) clusterResourceClient.Delete(name2, clients.DeleteOpts{}) }) - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - - /* - SimpleMockResource - */ - - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectSimplemocks { - if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + SimpleMockResource + */ + + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectSimplemocks { - if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) - simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) - - err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) - - err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) - - /* - MockResource - */ - - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) + + err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) + + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + + /* + FakeResource + */ + + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) - - /* - FakeResource - */ - - assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) + + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + + /* + AnotherMockResource + */ + + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) + + err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) + + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) + + /* + ClusterResource + */ + + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) - /* - AnotherMockResource - */ + /* + MockCustomType + */ - assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectAnothermockresources { - if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectAnothermockresources { - if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) + + err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) + + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + + /* + Pod + */ + + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) - anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) - err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) - err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) + assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) + }) - /* - ClusterResource - */ + It("should be able to track resources that are labeled on other namespaces", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectClusterresources { - if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + /* + SimpleMockResource + */ + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectClusterresources { - if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - combined, _ := clusterResourceClient.List(clients.ListOpts{}) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) - - err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) - - err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) - - /* - MockCustomType - */ - - assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectmcts { - if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(watched, nil) + + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + assertSnapshotMocks(watched, nil) + + simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} + assertNoMessageSent() + + simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + assertSnapshotMocks(watched, notWatched) + + simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + assertSnapshotMocks(watched, notWatched) + + simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) + watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + watched = SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource5b.GetMetadata().Namespace, simpleMockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + watched = SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource7b.GetMetadata().Namespace, simpleMockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + assertSnapshotSimplemocks(nil, notWatched) + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectmcts { - if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) - mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) - err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) - - err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) - - /* - Pod - */ - - assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectpods { - if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource4a, mockResource4b} + assertNoMessageSent() + + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectpods { - if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) - pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) - - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(watched, nil) + + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(watched, nil) + + fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + fakeResource4a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FakeResourceList{fakeResource4a, fakeResource4b} + assertNoMessageSent() + + fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource5a, fakeResource5b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource7a, fakeResource7b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) + watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = FakeResourceList{fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource5b.GetMetadata().Namespace, fakeResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = FakeResourceList{fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource7b.GetMetadata().Namespace, fakeResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) + assertSnapshotFakes(nil, notWatched) + + /* + AnotherMockResource + */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(watched, nil) + + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + assertSnapshotMocks(watched, nil) + + anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} + assertNoMessageSent() + + anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + assertSnapshotMocks(watched, notWatched) + + anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + assertSnapshotMocks(watched, notWatched) + + anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) + watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + watched = AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource5b.GetMetadata().Namespace, anotherMockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + watched = AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource7b.GetMetadata().Namespace, anotherMockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + assertSnapshotAnothermockresources(nil, notWatched) + + /* + ClusterResource + */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) - }) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMessageSent() + + clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := ClusterResourceList{clusterResource2a} + assertSnapshotClusterresources(watched, notWatched) + + clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource3a}...) + assertSnapshotClusterresources(watched, notWatched) + + createNamespaces(ctx, kube, namespace3, namespace4) + + clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource4a}...) + assertNoMessageSent() + + clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource5a}...) + assertSnapshotClusterresources(watched, notWatched) + + clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource6a}...) + assertSnapshotClusterresources(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + assertNoMessageSent() + + clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource8a}...) + assertSnapshotClusterresources(watched, notWatched) + + for _, r := range notWatched { + err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = ClusterResourceList{clusterResource2a} + watched = ClusterResourceList{clusterResource3a, clusterResource5a, clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource3a}...) + watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) + watched = ClusterResourceList{clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) + watched = ClusterResourceList{clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + assertSnapshotClusterresources(nil, notWatched) + + /* + MockCustomType + */ + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - It("tracks snapshots on changes to any resource using AllNamespace", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(watched, nil) + + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + assertSnapshotMocks(watched, nil) + + mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockCustomTypeList{mockCustomType4a, mockCustomType4b} + assertNoMessageSent() + + mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + assertSnapshotMocks(watched, notWatched) + + mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + assertSnapshotMocks(watched, notWatched) + + mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) + watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + watched = MockCustomTypeList{mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType5b.GetMetadata().Namespace, mockCustomType5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + watched = MockCustomTypeList{mockCustomType7a, mockCustomType7b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType7b.GetMetadata().Namespace, mockCustomType7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + assertSnapshotmcts(nil, notWatched) + + /* + Pod + */ + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(watched, nil) + + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + assertSnapshotMocks(watched, nil) + + pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} + assertNoMessageSent() + + pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + assertSnapshotMocks(watched, notWatched) + + pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + assertSnapshotMocks(watched, notWatched) + + pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod5a, pod5b, pod7a, pod7b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod3a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod5a, pod5b, pod7a, pod7b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b, pod7a, pod7b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod5b.GetMetadata().Namespace, pod5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod7b.GetMetadata().Namespace, pod7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + assertSnapshotpods(nil, notWatched) }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - - /* - SimpleMockResource - */ + }) - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectSimplemocks { - if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + Context("Tracking empty watched namespaces", func() { + It("tracks snapshots on changes to any resource using AllNamespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + SimpleMockResource + */ + + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectSimplemocks { - if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) - simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) - - err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) - - err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) - - /* - MockResource - */ - - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) + + err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) + + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + + /* + FakeResource + */ + + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) - - /* - FakeResource - */ - - assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) + + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + + /* + AnotherMockResource + */ + + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) + + err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) + + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) + + /* + ClusterResource + */ + + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) - /* - AnotherMockResource - */ + /* + MockCustomType + */ - assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectAnothermockresources { - if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectAnothermockresources { - if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) + + err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) + + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + + /* + Pod + */ + + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) - anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) - err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) - err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) + assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) + }) - /* - ClusterResource - */ + It("should be able to track resources only made with the matching labels", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectClusterresources { - if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + assertNoMatchingMocks := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { continue drain } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectClusterresources { - if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + SimpleMockResource + */ + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - combined, _ := clusterResourceClient.List(clients.ListOpts{}) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) - - err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) - - err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} + assertNoMatchingMocks() + + simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b} + assertSnapshotMocks(watched, notWatched) + + simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + assertNoMessageSent() + + simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) + assertSnapshotMocks(watched, notWatched) + + simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b, simpleMockResource6a, simpleMockResource6b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource4b.GetMetadata().Namespace, simpleMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) + watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b} + assertSnapshotSimplemocks(watched, notWatched) + + err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource6b.GetMetadata().Namespace, simpleMockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) + assertSnapshotSimplemocks(nil, notWatched) + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - /* - MockCustomType - */ + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = MockResourceList{mockResource2a, mockResource2b} + assertNoMatchingMocks() + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource3a, mockResource3b} + assertSnapshotMocks(watched, notWatched) + + mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + assertNoMessageSent() + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) + watched = MockResourceList{mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectmcts { - if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = FakeResourceList{fakeResource2a, fakeResource2b} + assertNoMatchingMocks() + + fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FakeResourceList{fakeResource3a, fakeResource3b} + assertSnapshotMocks(watched, notWatched) + + fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + fakeResource5a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + assertNoMessageSent() + + fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource6a, fakeResource6b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = FakeResourceList{fakeResource4a, fakeResource4b, fakeResource6a, fakeResource6b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) + watched = FakeResourceList{fakeResource6a, fakeResource6b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource6b.GetMetadata().Namespace, fakeResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) + assertSnapshotFakes(nil, notWatched) + + /* + AnotherMockResource + */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectmcts { - if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} + assertNoMatchingMocks() + + anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b} + assertSnapshotMocks(watched, notWatched) + + anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + assertNoMessageSent() + + anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) + assertSnapshotMocks(watched, notWatched) + + anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b, anotherMockResource6a, anotherMockResource6b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource4b.GetMetadata().Namespace, anotherMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) + watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource6b.GetMetadata().Namespace, anotherMockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) + assertSnapshotAnothermockresources(nil, notWatched) + + /* + ClusterResource + */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) - mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMatchingMocks() - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) + createNamespaces(ctx, kube, namespace3, namespace4) - err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource2a}...) + assertNoMatchingMocks() - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) + clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := ClusterResourceList{clusterResource3a} + assertSnapshotMocks(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource4a}...) + assertSnapshotMocks(watched, notWatched) - assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + createNamespaces(ctx, kube, namespace5, namespace6) - /* - Pod - */ + clusterResource6a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) + assertNoMatchingMocks() - assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectpods { - if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource5a}...) + assertSnapshotMocks(watched, notWatched) + + clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource4a, clusterResource6a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource6a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotClusterresources(nil, notWatched) + + /* + MockCustomType + */ + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectpods { - if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) - pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) - - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) - err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = MockCustomTypeList{mockCustomType2a, mockCustomType2b} + assertNoMatchingMocks() + + mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockCustomTypeList{mockCustomType3a, mockCustomType3b} + assertSnapshotMocks(watched, notWatched) + + mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + assertNoMessageSent() + + mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) + assertSnapshotMocks(watched, notWatched) + + mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + watched = MockCustomTypeList{mockCustomType4a, mockCustomType4b, mockCustomType6a, mockCustomType6b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType4b.GetMetadata().Namespace, mockCustomType4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) + watched = MockCustomTypeList{mockCustomType6a, mockCustomType6b} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType6b.GetMetadata().Namespace, mockCustomType6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) + assertSnapshotmcts(nil, notWatched) + + /* + Pod + */ + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} + assertNoMatchingMocks() + + pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b} + assertSnapshotMocks(watched, notWatched) + + pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + assertNoMessageSent() + + pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) + assertSnapshotMocks(watched, notWatched) + + pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b, pod6a, pod6b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod4b.GetMetadata().Namespace, pod4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod6b.GetMetadata().Namespace, pod6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) + assertSnapshotpods(nil, notWatched) + }) }) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 2687e5323..2fada145d 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -4,6 +4,7 @@ package v1alpha1 import ( "bytes" + "os" "sync" "time" @@ -18,6 +19,7 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + "github.com/solo-io/k8s-utils/kubeutils" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kubewatch "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" @@ -127,6 +129,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } + // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces @@ -206,9 +209,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" + // TODO-JAKE REFACTOR, this must be added another way + // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources + // I do not think this would work in a real scenario + cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) + if err != nil { + return nil, nil, err + } + k, err = kubernetes.NewForConfig(cfg) + if err != nil { + return nil, nil, err + } + var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.namespace!=") + buffer.WriteString("metadata.name!=") buffer.WriteString(ns) if i < len(watchNamespaces)-1 { buffer.WriteByte(',') @@ -216,13 +231,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { return nil, nil, err } allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for _, ns := range namespacesResources.Items { - allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + for i, ns := range namespacesResources.Items { + allOtherNamespaces[i] = ns.Namespace } // nonWatchedNamespaces @@ -283,25 +299,26 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event", event) + errs <- errors.Errorf("receiving namespace event: %v", event) default: namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") } - hit := false newNamespaces := []string{} for _, item := range namespacesResources.Items { namespace := item.Namespace - _, hit = mocksByNamespace[namespace] - if !hit { + // TODO-JAKE we might want to add a set of namespaces + // to the struct above, to manage it's list of namespaces + if _, hit := mocksByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } } - if hit { + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { // add a watch for all the new namespaces // REFACTOR for _, namespace := range newNamespaces { diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 15d730644..334fd7e8d 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -4,6 +4,7 @@ package v2alpha1 import ( "bytes" + "os" "sync" "time" @@ -20,6 +21,7 @@ import ( "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" + "github.com/solo-io/k8s-utils/kubeutils" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kubewatch "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" @@ -149,6 +151,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } + // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces @@ -300,9 +303,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" + // TODO-JAKE REFACTOR, this must be added another way + // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources + // I do not think this would work in a real scenario + cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) + if err != nil { + return nil, nil, err + } + k, err = kubernetes.NewForConfig(cfg) + if err != nil { + return nil, nil, err + } + var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.namespace!=") + buffer.WriteString("metadata.name!=") buffer.WriteString(ns) if i < len(watchNamespaces)-1 { buffer.WriteByte(',') @@ -310,13 +325,14 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { return nil, nil, err } allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for _, ns := range namespacesResources.Items { - allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + for i, ns := range namespacesResources.Items { + allOtherNamespaces[i] = ns.Namespace } // nonWatchedNamespaces @@ -433,35 +449,34 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event", event) + errs <- errors.Errorf("receiving namespace event: %v", event) default: namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) if err != nil { errs <- errors.Wrapf(err, "listing the namespace resources") } - hit := false newNamespaces := []string{} for _, item := range namespacesResources.Items { namespace := item.Namespace - _, hit = mocksByNamespace[namespace] - if !hit { + // TODO-JAKE we might want to add a set of namespaces + // to the struct above, to manage it's list of namespaces + if _, hit := mocksByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = fcarsByNamespace[namespace] - if !hit { + if _, hit := fcarsByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } - _, hit = fakesByNamespace[namespace] - if !hit { + if _, hit := fakesByNamespace[namespace]; !hit { newNamespaces = append(newNamespaces, namespace) continue } } - if hit { + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { // add a watch for all the new namespaces // REFACTOR for _, namespace := range newNamespaces { diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 175bf4e3e..7829ab485 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v2alpha1 @@ -40,9 +39,15 @@ var _ = Describe("V2Alpha1Emitter", func() { } var ( ctx context.Context - namespace1 string - namespace2 string + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string + createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labels2 = map[string]string{"env": "testenv", "owner": "foo"} + labelExpression1 = "env in (test)" cfg *rest.Config clientset *apiext.Clientset kube kubernetes.Interface @@ -51,16 +56,46 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient fakeResourceClient testing_solo_io.FakeResourceClient ) + NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { + resource := NewMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewFrequentlyChangingAnnotationsResourceWithLabels := func(namespace, name string, labels map[string]string) *FrequentlyChangingAnnotationsResource { + resource := NewFrequentlyChangingAnnotationsResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + NewFakeResourceWithLabels := func(namespace, name string, labels map[string]string) *testing_solo_io.FakeResource { + resource := testing_solo_io.NewFakeResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + + createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + Expect(err).NotTo(HaveOccurred()) + for _, ns := range namespaces { + if _, hit := createdNamespaces[ns]; !hit { + createdNamespaces[ns] = true + } + } + } BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() + createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) kube = helpers.MustKubeClient() - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") Expect(err).NotTo(HaveOccurred()) @@ -99,377 +134,1200 @@ var _ = Describe("V2Alpha1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) + namespacesToDelete := []string{} + for namespace, _ := range createdNamespaces { + namespacesToDelete = append(namespacesToDelete, namespace) + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) Expect(err).NotTo(HaveOccurred()) }) - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - - /* - MockResource - */ - - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) - - /* - FrequentlyChangingAnnotationsResource - */ - - assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFcars { - if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + + /* + FrequentlyChangingAnnotationsResource + */ + + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectFcars { - if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + + /* + FakeResource + */ + + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) - frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + }) - /* - FakeResource - */ + It("should be able to track resources that are labeled on other namespaces", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) - - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) - - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) - }) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource4a, mockResource4b} + assertNoMessageSent() + + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(nil, notWatched) + + /* + FrequentlyChangingAnnotationsResource + */ + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - It("tracks snapshots on changes to any resource using AllNamespace", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(watched, nil) + + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + assertSnapshotMocks(watched, nil) + + frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} + assertNoMessageSent() + + frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + assertSnapshotFcars(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(watched, nil) + + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(watched, nil) + + fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} + assertNoMessageSent() + + fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) + watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource5b.GetMetadata().Namespace, fakeResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource7b.GetMetadata().Namespace, fakeResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + assertSnapshotFakes(nil, notWatched) }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - - /* - MockResource - */ + }) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + Context("Tracking empty watched namespaces", func() { + It("tracks snapshots on changes to any resource using AllNamespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } - } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) - - /* - FrequentlyChangingAnnotationsResource - */ - - assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFcars { - if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + + /* + FrequentlyChangingAnnotationsResource + */ + + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - for _, unexpected := range unexpectFcars { - if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + + /* + FakeResource + */ + + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) - frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + }) - /* - FakeResource - */ + It("should be able to track resources only made with the matching labels", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + assertNoMatchingMocks := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { continue drain } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) - - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = MockResourceList{mockResource2a, mockResource2b} + assertNoMatchingMocks() + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource3a, mockResource3b} + assertSnapshotMocks(watched, notWatched) + + mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + assertNoMessageSent() + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) + watched = MockResourceList{mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(nil, notWatched) + + /* + FrequentlyChangingAnnotationsResource + */ + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} + assertNoMatchingMocks() + + frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b} + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + assertNoMessageSent() + + frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) + assertSnapshotFcars(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } - assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} + assertNoMatchingMocks() + + fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b} + assertSnapshotMocks(watched, notWatched) + + fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + assertNoMessageSent() + + fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) + assertSnapshotMocks(watched, notWatched) + + fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b, fakeResource6a, fakeResource6b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) + watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource6b.GetMetadata().Namespace, fakeResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) + assertSnapshotFakes(nil, notWatched) + }) }) }) From 670cf7e65d1c18c7dc8866304f68ab2bc26c3ae6 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 25 Aug 2022 15:46:50 -0500 Subject: [PATCH 10/98] update for interface for ResourceNamespaces --- README.md | 4 + .../namespace/resource_namespace.go | 73 ++ pkg/api/v1/resources/resource_interface.go | 41 ++ .../templates/snapshot_emitter_template.go | 209 +++--- .../snapshot_emitter_test_template.go | 130 +++- .../v1/kubeconfigs_snapshot_emitter.sk.go | 191 ++--- .../v1/kubeconfigs_snapshot_emitter_test.go | 615 ++++++++++++---- test/mocks/v1/testing_snapshot_emitter.sk.go | 665 ++++++++++-------- .../mocks/v1/testing_snapshot_emitter_test.go | 17 +- .../v1alpha1/testing_snapshot_emitter.sk.go | 198 +++--- .../v1alpha1/testing_snapshot_emitter_test.go | 619 ++++++++++++---- .../v2alpha1/testing_snapshot_emitter.sk.go | 366 +++++----- .../v2alpha1/testing_snapshot_emitter_test.go | 17 +- 13 files changed, 2113 insertions(+), 1032 deletions(-) create mode 100644 pkg/api/external/kubernetes/namespace/resource_namespace.go diff --git a/README.md b/README.md index df29233fe..a210c99b5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # solo-kit A collection of code generation and libraries to for API development. +### Testing +To generate tests set `SkipGeneratedTests` to false in the `generate.go` file. This will use the test templates +to generate the code for the tests. Such as files like `pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go` + ### Description: - Define your declarative API in `.proto` files - APIs are defined by top-level protobuf messages in `.proto` files diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go new file mode 100644 index 000000000..0b2252986 --- /dev/null +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -0,0 +1,73 @@ +package namespace + +import ( + "context" + + "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" + skkube "github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes" + "k8s.io/client-go/kubernetes" +) + +var _ resources.ResourceNamespaceLister = &kubeResourceNamespaceLister{} + +// if we use the list thingy I guess we could try it out, if it does not work, then lets move forward with the base client +// solution, that uses the kubernetes interface... +func NewKubeResourceNamespaceLister(kube kubernetes.Interface, cache cache.KubeCoreCache) resources.ResourceNamespaceLister { + return &kubeResourceNamespaceLister{ + namespace: NewNamespaceClient(kube, cache), + } +} + +type kubeResourceNamespaceLister struct { + namespace skkube.KubeNamespaceClient +} + +// GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces +func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(ctx context.Context, opts resources.ResourceNamespaceListOptions) (resources.ResourceNamespaceList, error) { + clopts := clients.ListOpts{FieldSelectors: opts.FieldSelectors} + namespaces, err := kns.namespace.List(clopts) + if err != nil { + return nil, err + } + // convert + + return convertNamespaceListToResourceNamespace(namespaces), nil +} + +func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(ctx context.Context, opts resources.ResourceNamespaceWatchOptions) (chan resources.ResourceNamespaceList, <-chan error, error) { + wopts := clients.WatchOpts{FieldSelectors: opts.FieldSelectors} + // todo look that the namespace implementation to know exacactly what the channel of errors is returning. + namespaceChan, errorChan, err := kns.namespace.Watch(wopts) + if err != nil { + return nil, nil, err + } + + resourceNamespaceChan := make(chan resources.ResourceNamespaceList) + go func() { + for { + select { + case namespaceList := <-namespaceChan: + select { + case resourceNamespaceChan <- convertNamespaceListToResourceNamespace(namespaceList): + case <-ctx.Done(): + close(resourceNamespaceChan) + return + } + case <-ctx.Done(): + close(resourceNamespaceChan) + return + } + } + }() + return resourceNamespaceChan, errorChan, nil +} + +func convertNamespaceListToResourceNamespace(namespaces skkube.KubeNamespaceList) resources.ResourceNamespaceList { + l := resources.ResourceNamespaceList{} + for _, ns := range namespaces { + l = append(l, resources.ResourceNamespace{Name: ns.ObjectMeta.Name}) + } + return l +} diff --git a/pkg/api/v1/resources/resource_interface.go b/pkg/api/v1/resources/resource_interface.go index ba2b7b7ee..e5f76c081 100644 --- a/pkg/api/v1/resources/resource_interface.go +++ b/pkg/api/v1/resources/resource_interface.go @@ -1,6 +1,7 @@ package resources import ( + "context" "fmt" "reflect" "sort" @@ -80,6 +81,46 @@ type CustomInputResource interface { MarshalStatus() (v1.Status, error) } +// ResourceNamespaceListOptions provides the options for listing Resoiurce Namespaces +type ResourceNamespaceListOptions struct { + // FieldSelectors are used to filter out specific fields that are associated with + // the Namespace. IE if using Kubernetes you can filter namespaces by the name + // of the namespace by using metadata.name!= + // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ + FieldSelectors string +} + +// ResourceNamespaceWatchOptions provides the options for watching Resource Namespaces +type ResourceNamespaceWatchOptions struct { + // FieldSelectors are used to filter out specific fields that are associated with + // the Namespace. IE if using Kubernetes you can filter namespaces by the name + // of the namespace by using metadata.name!= + // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ + FieldSelectors string +} + +// ResoruceNamespace is the namespaces that resources can be found. ResourceNamespaces are +// anything that contains resources independent of other resoruces. They provide sections +// independent infrastructure or regions. IE kubernetes namespaces +type ResourceNamespace struct { + // Name the name of the namespace + Name string +} + +// ResourceNamespaceList contains a list of ResourceNamespaces +type ResourceNamespaceList []ResourceNamespace + +// ResourceNamespaceLister is anything that can list and watch namespaces that +// resources can be found. +type ResourceNamespaceLister interface { + // GetNamespaceResourceList returns the list of the namespaces that resources + // can be found. + GetNamespaceResourceList(ctx context.Context, opts ResourceNamespaceListOptions) (ResourceNamespaceList, error) + // GetNamespaceResourceWatch returns a watch that receives events when namespaces + // are updated or created. + GetNamespaceResourceWatch(ctx context.Context, opts ResourceNamespaceWatchOptions) (chan ResourceNamespaceList, <-chan error, error) +} + type ResourceList []Resource type ResourcesById map[string]Resource type ResourcesByKind map[string]ResourceList diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 2a2e2a43f..1b2b6e973 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -28,7 +28,6 @@ import ( "bytes" "sync" "time" - "os" {{ .Imports }} "go.opencensus.io/stats" @@ -39,14 +38,11 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/errors" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" skstats "github.com/solo-io/solo-kit/pkg/stats" "github.com/solo-io/go-utils/errutils" "github.com/solo-io/go-utils/contextutils" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/kubernetes" - kubewatch "k8s.io/apimachinery/pkg/watch" - "github.com/solo-io/k8s-utils/kubeutils" ) {{ $emitter_prefix := (print (snake .Name) "/emitter") }} @@ -122,15 +118,16 @@ type {{ .GoName }}Emitter interface { {{- end}} } -func New{{ .GoName }}Emitter({{ $client_declarations }}) {{ .GoName }}Emitter { - return New{{ .GoName }}EmitterWithEmit({{ $clients }}, make(chan struct{})) +func New{{ .GoName }}Emitter({{ $client_declarations }}, resourceNamespaceLister resources.ResourceNamespaceLister) {{ .GoName }}Emitter { + return New{{ .GoName }}EmitterWithEmit({{ $clients }}, resourceNamespaceLister, make(chan struct{})) } -func New{{ .GoName }}EmitterWithEmit({{ $client_declarations }}, emit <-chan struct{}) {{ .GoName }}Emitter { +func New{{ .GoName }}EmitterWithEmit({{ $client_declarations }}, resourceNamespaceLister resources.ResourceNamespaceLister, emit <-chan struct{}) {{ .GoName }}Emitter { return &{{ lower_camel .GoName }}Emitter{ {{- range .Resources}} {{ lower_camel .Name }}:{{ lower_camel .Name }}Client, {{- end}} + resourceNamespaceLister: resourceNamespaceLister, forceEmit: emit, } } @@ -140,6 +137,7 @@ type {{ lower_camel .GoName }}Emitter struct { {{- range .Resources}} {{ lower_camel .Name }} {{ .ImportPrefix }}{{ .Name }}Client {{- end}} + resourceNamespaceLister resources.ResourceNamespaceLister } func (c *{{ lower_camel .GoName }}Emitter) Register() error { @@ -208,7 +206,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- range .Resources}} {{- if not .ClusterScoped }} - {{ lower_camel .PluralName }}ByNamespace := make(map[string]{{ .ImportPrefix }}{{ .Name }}List) + {{ lower_camel .PluralName }}ByNamespace := sync.Map{} {{- end }} {{- end }} @@ -223,7 +221,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") } initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .PluralName }}...) - {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) } {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -267,21 +265,15 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // by Expression Selectors // first get the renaiming namespaces - var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" - // TODO-JAKE REFACTOR, this must be added another way - // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources - // I do not think this would work in a real scenario - cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) - if err != nil { - return nil, nil, err - } - k, err = kubernetes.NewForConfig(cfg) - if err != nil { - return nil, nil, err - } + // TODO-JAKE may want to add some comments around how the snapshot_emitter + // event_loop and resource clients -> resource client implementations work in a README.md + // this would be helpful for documentation purposes + + // TODO implement how we will be able to delete resources from namespaces that are deleted + // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { buffer.WriteString("metadata.name!=") @@ -292,14 +284,29 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } excludeNamespacesFieldDesciptors = buffer.String() + // we should only be watching namespaces that have the selectors that we want to be watching + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources,err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) + if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for i, ns := range namespacesResources.Items { - allOtherNamespaces[i] = ns.Namespace + allOtherNamespaces := make([]string, 0) + for _, ns := range namespacesResources { + // TODO-JAKE get the filters on the namespacing working + add := true + // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister + for _,wns := range watchNamespaces { + if ns.Name == wns { + add = false + } + } + if add { + allOtherNamespaces = append(allOtherNamespaces, ns.Name) + } } // nonWatchedNamespaces @@ -314,7 +321,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") } initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List,{{ lower_camel .PluralName }}...) - {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) } {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) if err != nil { @@ -352,7 +359,11 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }(namespace) } // create watch on all namespaces, so that we can add resources from new namespaces - namespaceWatch,err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. + // we will need a way to deal with DELETES and CREATES and updates seperately + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) if err != nil { return nil, nil, err } @@ -362,92 +373,86 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o select { case <-ctx.Done(): return - case event, ok := <-namespaceWatch.ResultChan(): + case resourceNamespaces, ok := <-namespaceWatch: if !ok { return } - switch event.Type { - case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event: %v", event) - default: - namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) - if err != nil { - errs <- errors.Wrapf(err, "listing the namespace resources") - } - - newNamespaces := []string{} + newNamespaces := []string{} - for _,item := range namespacesResources.Items { - namespace := item.Namespace - // TODO-JAKE we might want to add a set of namespaces - // to the struct above, to manage it's list of namespaces + for _, ns := range resourceNamespaces { + // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here + {{- range .Resources }} {{- if (not .ClusterScoped) }} - if _, hit := {{ lower_camel .PluralName }}ByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := {{ lower_camel .PluralName }}ByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } {{- end }} {{- end }} - } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0{ - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - {{- range .Resources }} - {{- if (not .ClusterScoped) }} - /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ - { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") - continue - } - {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }} - } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + } + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0{ + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ + { + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") continue } + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .PluralName }}") - }(namespace) - {{- end }} - {{- end }} - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .PluralName }}") + }(namespace) +{{- end }} +{{- end }} + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - {{- range .Resources }} - {{- if (not .ClusterScoped) }} - case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: - } - {{- end }} - {{- end }} + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: } +{{- end }} +{{- end }} } - }(namespace) - } + } + }(namespace) } } } @@ -561,11 +566,13 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o ) // merge lists by namespace - {{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .Name }}NamespacedList.list + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .Name }}NamespacedList.list) var {{ lower_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List - for _, {{ lower_camel .PluralName }} := range {{ lower_camel .PluralName }}ByNamespace { - {{ lower_camel .Name }}List = append({{ lower_camel .Name }}List, {{ lower_camel .PluralName }}...) - } + {{ lower_camel .PluralName }}ByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.({{ .ImportPrefix }}{{ .Name }}List) + {{ lower_camel .Name }}List = append({{ lower_camel .Name }}List, mocks...) + return true + }) currentSnapshot.{{ upper_camel .PluralName }} = {{ lower_camel .Name }}List.Sort() {{- end }} {{- end }} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index be12e7df1..f4c8916aa 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -32,6 +32,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/test/helpers" "github.com/solo-io/solo-kit/test/setup" "github.com/solo-io/k8s-utils/kubeutils" @@ -57,6 +60,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string + createdNamespaces map[string]bool name1, name2 = "angela"+helpers.RandString(3), "bob"+helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -71,10 +75,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- range .Resources }} {{ lower_camel .Name }}Client {{ .ImportPrefix }}{{ .Name }}Client {{- end}} + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache + ) + const ( + TIME_BETWEEN_MESSAGES = 5 ) - {{- range .Resources }} - {{ .ImportPrefix }}New{{ .Name }}WithLabels := func(namespace, name string, labels map[string]string) (*{{ .ImportPrefix }}{{ .Name }}) { + New{{ .Name }}WithLabels := func(namespace, name string, labels map[string]string) (*{{ .ImportPrefix }}{{ .Name }}) { resource := {{ .ImportPrefix }}New{{ .Name }}(namespace, name) resource.Metadata.Labels = labels return resource @@ -82,7 +90,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { - err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) for _,ns := range namespaces { if _,hit := createdNamespaces[ns]; !hit { @@ -103,7 +111,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace4 = helpers.RandString(8) namespace5 = helpers.RandString(8) namespace6 = helpers.RandString(8) + kube = helpers.MustKubeClient() + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) {{- if $need_kube_config }} @@ -136,7 +149,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}Client, err = {{ .ImportPrefix }}New{{ .Name }}Client(ctx, {{ lower_camel .Name }}ClientFactory) Expect(err).NotTo(HaveOccurred()) {{- end}} - emitter = New{{ .GoName }}Emitter({{ $clients }}) + emitter = New{{ .GoName }}Emitter({{ $clients }}, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) @@ -335,55 +348,68 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertNoMessageSent() + {{- else }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } assertSnapshot{{ .PluralName }}(watched, nil) -{{- end }} +{{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) assertSnapshotMocks(watched, nil) + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) assertSnapshotMocks(watched, nil) + {{- end }} createNamespaces(ctx, kube, namespace3, namespace4) {{- if .ClusterScoped }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) assertNoMessageSent() + {{- else }} {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -391,65 +417,82 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b } assertNoMessageSent() + {{- end }} + {{- if .ClusterScoped }} + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) assertSnapshotMocks(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) assertNoMessageSent() + {{- end }} createNamespaces(ctx, kube, namespace5, namespace6) {{- if .ClusterScoped }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) assertNoMessageSent() + {{- else }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) assertSnapshotMocks(watched, notWatched) -{{- end }} +{{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) assertNoMessageSent() + {{- end }} for _, r := range notWatched { @@ -459,81 +502,101 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched ={{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5b.GetMetadata().Namespace, {{ lower_camel .Name }}5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) assertSnapshot{{ .PluralName }}(nil, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7b.GetMetadata().Namespace, {{ lower_camel .Name }}7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b}...) assertSnapshot{{ .PluralName }}(nil, notWatched) + {{- end }} {{- end }} }) @@ -734,105 +797,134 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertNoMatchingMocks() + {{- else }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } assertNoMatchingMocks() + {{- end }} createNamespaces(ctx, kube, namespace3, namespace4) {{- if .ClusterScoped }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }...) assertNoMatchingMocks() + {{- else }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } assertNoMatchingMocks() + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } assertSnapshotMocks(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b } assertSnapshotMocks(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) assertSnapshotMocks(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) assertSnapshotMocks(watched, notWatched) + {{- end }} createNamespaces(ctx, kube, namespace5, namespace6) {{- if .ClusterScoped }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) assertNoMatchingMocks() + {{- else }} + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) assertNoMessageSent() + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) assertSnapshotMocks(watched, notWatched) + {{- else }} + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) assertSnapshotMocks(watched, notWatched) + {{- end }} + {{- if .ClusterScoped }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) assertNoMessageSent() + {{- else }} + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) assertNoMessageSent() + {{- end }} for _, r := range notWatched { @@ -842,45 +934,55 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}6a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4b.GetMetadata().Namespace, {{ lower_camel .Name }}4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } assertSnapshot{{ .PluralName }}(watched, notWatched) + {{- end }} {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}(nil, notWatched) {{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6b.GetMetadata().Namespace, {{ lower_camel .Name }}6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notwatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) assertSnapshot{{ .PluralName }}(nil, notWatched) {{- end }} {{- end }} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 656b3d842..c0e090bea 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -13,14 +13,12 @@ import ( "go.uber.org/zap" "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/errors" skstats "github.com/solo-io/solo-kit/pkg/stats" "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kubewatch "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" ) var ( @@ -87,20 +85,22 @@ type KubeconfigsEmitter interface { KubeConfig() KubeConfigClient } -func NewKubeconfigsEmitter(kubeConfigClient KubeConfigClient) KubeconfigsEmitter { - return NewKubeconfigsEmitterWithEmit(kubeConfigClient, make(chan struct{})) +func NewKubeconfigsEmitter(kubeConfigClient KubeConfigClient, resourceNamespaceLister resources.ResourceNamespaceLister) KubeconfigsEmitter { + return NewKubeconfigsEmitterWithEmit(kubeConfigClient, resourceNamespaceLister, make(chan struct{})) } -func NewKubeconfigsEmitterWithEmit(kubeConfigClient KubeConfigClient, emit <-chan struct{}) KubeconfigsEmitter { +func NewKubeconfigsEmitterWithEmit(kubeConfigClient KubeConfigClient, resourceNamespaceLister resources.ResourceNamespaceLister, emit <-chan struct{}) KubeconfigsEmitter { return &kubeconfigsEmitter{ - kubeConfig: kubeConfigClient, - forceEmit: emit, + kubeConfig: kubeConfigClient, + resourceNamespaceLister: resourceNamespaceLister, + forceEmit: emit, } } type kubeconfigsEmitter struct { - forceEmit <-chan struct{} - kubeConfig KubeConfigClient + forceEmit <-chan struct{} + kubeConfig KubeConfigClient + resourceNamespaceLister resources.ResourceNamespaceLister } func (c *kubeconfigsEmitter) Register() error { @@ -127,6 +127,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } } + // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces @@ -155,7 +156,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa var initialKubeConfigList KubeConfigList currentSnapshot := KubeconfigsSnapshot{} - kubeconfigsByNamespace := make(map[string]KubeConfigList) + kubeconfigsByNamespace := sync.Map{} // watched namespaces for _, namespace := range watchNamespaces { @@ -166,7 +167,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa return nil, nil, errors.Wrapf(err, "initial KubeConfig list") } initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) - kubeconfigsByNamespace[namespace] = kubeconfigs + kubeconfigsByNamespace.Store(namespace, kubeconfigs) } kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -203,12 +204,18 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // by Expression Selectors // first get the renaiming namespaces - var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" + // TODO-JAKE may want to add some comments around how the snapshot_emitter + // event_loop and resource clients -> resource client implementations work in a README.md + // this would be helpful for documentation purposes + + // TODO implement how we will be able to delete resources from namespaces that are deleted + + // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.namespace!=") + buffer.WriteString("metadata.name!=") buffer.WriteString(ns) if i < len(watchNamespaces)-1 { buffer.WriteByte(',') @@ -216,13 +223,29 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } excludeNamespacesFieldDesciptors = buffer.String() - namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // we should only be watching namespaces that have the selectors that we want to be watching + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + // TODO-JAKE test that we can create a huge field selector of massive size + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) + if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for _, ns := range namespacesResources.Items { - allOtherNamespaces = append(allOtherNamespaces, ns.Namespace) + allOtherNamespaces := make([]string, 0) + for _, ns := range namespacesResources { + // TODO-JAKE get the filters on the namespacing working + add := true + // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister + for _, wns := range watchNamespaces { + if ns.Name == wns { + add = false + } + } + if add { + allOtherNamespaces = append(allOtherNamespaces, ns.Name) + } } // nonWatchedNamespaces @@ -235,7 +258,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa return nil, nil, errors.Wrapf(err, "initial KubeConfig list") } initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) - kubeconfigsByNamespace[namespace] = kubeconfigs + kubeconfigsByNamespace.Store(namespace, kubeconfigs) } kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) if err != nil { @@ -267,7 +290,11 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }(namespace) } // create watch on all namespaces, so that we can add resources from new namespaces - namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. + // we will need a way to deal with DELETES and CREATES and updates seperately + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) if err != nil { return nil, nil, err } @@ -277,79 +304,73 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa select { case <-ctx.Done(): return - case event, ok := <-namespaceWatch.ResultChan(): + case resourceNamespaces, ok := <-namespaceWatch: if !ok { return } - switch event.Type { - case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event: %v", event) - default: - namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) - if err != nil { - errs <- errors.Wrapf(err, "listing the namespace resources") + newNamespaces := []string{} + + for _, ns := range resourceNamespaces { + // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := kubeconfigsByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - - hit := false - newNamespaces := []string{} - - for _, item := range namespacesResources.Items { - namespace := item.Namespace - _, hit = kubeconfigsByNamespace[namespace] - if !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - } - if hit { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for KubeConfig for new namespace */ - { - kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") - continue - } - kubeconfigsByNamespace[namespace] = kubeconfigs - } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + } + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for KubeConfig for new namespace */ + { + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") continue } + kubeconfigsByNamespace.Store(namespace, kubeconfigs) + } + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case kubeConfigList, ok := <-kubeConfigNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case kubeConfigList, ok := <-kubeConfigNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: - } + case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } @@ -424,11 +445,13 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa ) // merge lists by namespace - kubeconfigsByNamespace[namespace] = kubeConfigNamespacedList.list + kubeconfigsByNamespace.Store(namespace, kubeConfigNamespacedList.list) var kubeConfigList KubeConfigList - for _, kubeconfigs := range kubeconfigsByNamespace { - kubeConfigList = append(kubeConfigList, kubeconfigs...) - } + kubeconfigsByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(KubeConfigList) + kubeConfigList = append(kubeConfigList, mocks...) + return true + }) currentSnapshot.Kubeconfigs = kubeConfigList.Sort() } } diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 113dd484d..96dac19d6 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 @@ -14,9 +13,12 @@ import ( . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" "github.com/solo-io/k8s-utils/kubeutils" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" "k8s.io/client-go/kubernetes" @@ -34,24 +36,60 @@ var _ = Describe("V1Emitter", func() { return } var ( - ctx context.Context - namespace1 string - namespace2 string - name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) - kube kubernetes.Interface - emitter KubeconfigsEmitter - kubeConfigClient KubeConfigClient + ctx context.Context + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string + createdNamespaces map[string]bool + name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labels2 = map[string]string{"env": "testenv", "owner": "foo"} + labelExpression1 = "env in (test)" + kube kubernetes.Interface + emitter KubeconfigsEmitter + kubeConfigClient KubeConfigClient + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache ) + const ( + TIME_BETWEEN_MESSAGES = 5 + ) + NewKubeConfigWithLabels := func(namespace, name string, labels map[string]string) *KubeConfig { + resource := NewKubeConfig(namespace, name) + resource.Metadata.Labels = labels + return resource + } + + createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + for _, ns := range namespaces { + if _, hit := createdNamespaces[ns]; !hit { + createdNamespaces[ns] = true + } + } + } BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() + createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + kube = helpers.MustKubeClient() - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) // KubeConfig Constructor kubeConfigClientFactory := &factory.MemoryResourceClientFactory{ @@ -60,155 +98,486 @@ var _ = Describe("V1Emitter", func() { kubeConfigClient, err = NewKubeConfigClient(ctx, kubeConfigClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewKubeconfigsEmitter(kubeConfigClient) + emitter = NewKubeconfigsEmitter(kubeConfigClient, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) + namespacesToDelete := []string{} + for namespace, _ := range createdNamespaces { + namespacesToDelete = append(namespacesToDelete, namespace) + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) Expect(err).NotTo(HaveOccurred()) }) - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + /* + KubeConfig + */ + + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - var snap *KubeconfigsSnapshot + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}, nil) - /* - KubeConfig - */ + err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, KubeConfigList{kubeConfig2a, kubeConfig2b}) + + err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) + }) + + It("should be able to track resources that are labeled on other namespaces", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + KubeConfig + */ + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) - kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(watched, nil) + + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + assertSnapshotMocks(watched, nil) + + kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := KubeConfigList{kubeConfig4a, kubeConfig4b} + assertNoMessageSent() + + kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + assertSnapshotMocks(watched, notWatched) + + kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + assertSnapshotMocks(watched, notWatched) + + kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) + watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + watched = KubeConfigList{kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig5b.GetMetadata().Namespace, kubeConfig5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + watched = KubeConfigList{kubeConfig7a, kubeConfig7b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig7b.GetMetadata().Namespace, kubeConfig7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + assertSnapshotkubeconfigs(nil, notWatched) + }) + }) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}, nil) + Context("Tracking empty watched namespaces", func() { + It("tracks snapshots on changes to any resource using AllNamespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + /* + KubeConfig + */ + + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, KubeConfigList{kubeConfig2a, kubeConfig2b}) + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}, nil) - err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) - }) + assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, KubeConfigList{kubeConfig2a, kubeConfig2b}) - It("tracks snapshots on changes to any resource using AllNamespace", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, + assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) }) - Expect(err).NotTo(HaveOccurred()) - - var snap *KubeconfigsSnapshot - /* - KubeConfig - */ + It("should be able to track resources only made with the matching labels", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + assertNoMatchingMocks := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { continue drain } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + KubeConfig + */ + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) - kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}, nil) - err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, KubeConfigList{kubeConfig2a, kubeConfig2b}) - - err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = KubeConfigList{kubeConfig2a, kubeConfig2b} + assertNoMatchingMocks() + + kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := KubeConfigList{kubeConfig3a, kubeConfig3b} + assertSnapshotMocks(watched, notWatched) + + kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + assertNoMessageSent() + + kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) + assertSnapshotMocks(watched, notWatched) + + kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + watched = KubeConfigList{kubeConfig4a, kubeConfig4b, kubeConfig6a, kubeConfig6b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig4b.GetMetadata().Namespace, kubeConfig4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) + watched = KubeConfigList{kubeConfig6a, kubeConfig6b} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig6b.GetMetadata().Namespace, kubeConfig6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) + assertSnapshotkubeconfigs(nil, notWatched) + }) }) }) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index d828ce5ee..b5f0a7eff 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -4,7 +4,6 @@ package v1 import ( "bytes" - "os" "sync" "time" @@ -16,15 +15,12 @@ import ( "go.uber.org/zap" "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/errors" skstats "github.com/solo-io/solo-kit/pkg/stats" "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" - "github.com/solo-io/k8s-utils/kubeutils" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kubewatch "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" ) var ( @@ -97,32 +93,34 @@ type TestingEmitter interface { Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient } -func NewTestingEmitter(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient) TestingEmitter { - return NewTestingEmitterWithEmit(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, make(chan struct{})) +func NewTestingEmitter(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, resourceNamespaceLister resources.ResourceNamespaceLister) TestingEmitter { + return NewTestingEmitterWithEmit(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister, make(chan struct{})) } -func NewTestingEmitterWithEmit(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, emit <-chan struct{}) TestingEmitter { +func NewTestingEmitterWithEmit(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, resourceNamespaceLister resources.ResourceNamespaceLister, emit <-chan struct{}) TestingEmitter { return &testingEmitter{ - simpleMockResource: simpleMockResourceClient, - mockResource: mockResourceClient, - fakeResource: fakeResourceClient, - anotherMockResource: anotherMockResourceClient, - clusterResource: clusterResourceClient, - mockCustomType: mockCustomTypeClient, - pod: podClient, - forceEmit: emit, + simpleMockResource: simpleMockResourceClient, + mockResource: mockResourceClient, + fakeResource: fakeResourceClient, + anotherMockResource: anotherMockResourceClient, + clusterResource: clusterResourceClient, + mockCustomType: mockCustomTypeClient, + pod: podClient, + resourceNamespaceLister: resourceNamespaceLister, + forceEmit: emit, } } type testingEmitter struct { - forceEmit <-chan struct{} - simpleMockResource SimpleMockResourceClient - mockResource MockResourceClient - fakeResource FakeResourceClient - anotherMockResource AnotherMockResourceClient - clusterResource ClusterResourceClient - mockCustomType MockCustomTypeClient - pod github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + forceEmit <-chan struct{} + simpleMockResource SimpleMockResourceClient + mockResource MockResourceClient + fakeResource FakeResourceClient + anotherMockResource AnotherMockResourceClient + clusterResource ClusterResourceClient + mockCustomType MockCustomTypeClient + pod github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + resourceNamespaceLister resources.ResourceNamespaceLister } func (c *testingEmitter) Register() error { @@ -256,12 +254,12 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var initialPodList github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList currentSnapshot := TestingSnapshot{} - simplemocksByNamespace := make(map[string]SimpleMockResourceList) - mocksByNamespace := make(map[string]MockResourceList) - fakesByNamespace := make(map[string]FakeResourceList) - anothermockresourcesByNamespace := make(map[string]AnotherMockResourceList) - mctsByNamespace := make(map[string]MockCustomTypeList) - podsByNamespace := make(map[string]github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) + simplemocksByNamespace := sync.Map{} + mocksByNamespace := sync.Map{} + fakesByNamespace := sync.Map{} + anothermockresourcesByNamespace := sync.Map{} + mctsByNamespace := sync.Map{} + podsByNamespace := sync.Map{} // watched namespaces for _, namespace := range watchNamespaces { @@ -272,7 +270,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") } initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) - simplemocksByNamespace[namespace] = simplemocks + simplemocksByNamespace.Store(namespace, simplemocks) } simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -291,7 +289,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -310,7 +308,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace[namespace] = fakes + fakesByNamespace.Store(namespace, fakes) } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -329,7 +327,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") } initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) - anothermockresourcesByNamespace[namespace] = anothermockresources + anothermockresourcesByNamespace.Store(namespace, anothermockresources) } anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -348,7 +346,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockCustomType list") } initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) - mctsByNamespace[namespace] = mcts + mctsByNamespace.Store(namespace, mcts) } mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -367,7 +365,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial Pod list") } initialPodList = append(initialPodList, pods...) - podsByNamespace[namespace] = pods + podsByNamespace.Store(namespace, pods) } podNamespacesChan, podErrs, err := c.pod.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -449,21 +447,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // by Expression Selectors // first get the renaiming namespaces - var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" - // TODO-JAKE REFACTOR, this must be added another way - // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources - // I do not think this would work in a real scenario - cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) - if err != nil { - return nil, nil, err - } - k, err = kubernetes.NewForConfig(cfg) - if err != nil { - return nil, nil, err - } + // TODO-JAKE may want to add some comments around how the snapshot_emitter + // event_loop and resource clients -> resource client implementations work in a README.md + // this would be helpful for documentation purposes + + // TODO implement how we will be able to delete resources from namespaces that are deleted + // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { buffer.WriteString("metadata.name!=") @@ -474,14 +466,29 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // we should only be watching namespaces that have the selectors that we want to be watching + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) + if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for i, ns := range namespacesResources.Items { - allOtherNamespaces[i] = ns.Namespace + allOtherNamespaces := make([]string, 0) + for _, ns := range namespacesResources { + // TODO-JAKE get the filters on the namespacing working + add := true + // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister + for _, wns := range watchNamespaces { + if ns.Name == wns { + add = false + } + } + if add { + allOtherNamespaces = append(allOtherNamespaces, ns.Name) + } } // nonWatchedNamespaces @@ -494,7 +501,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") } initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) - simplemocksByNamespace[namespace] = simplemocks + simplemocksByNamespace.Store(namespace, simplemocks) } simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) if err != nil { @@ -513,7 +520,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) if err != nil { @@ -532,7 +539,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace[namespace] = fakes + fakesByNamespace.Store(namespace, fakes) } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) if err != nil { @@ -551,7 +558,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") } initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) - anothermockresourcesByNamespace[namespace] = anothermockresources + anothermockresourcesByNamespace.Store(namespace, anothermockresources) } anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) if err != nil { @@ -570,7 +577,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockCustomType list") } initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) - mctsByNamespace[namespace] = mcts + mctsByNamespace.Store(namespace, mcts) } mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) if err != nil { @@ -589,7 +596,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial Pod list") } initialPodList = append(initialPodList, pods...) - podsByNamespace[namespace] = pods + podsByNamespace.Store(namespace, pods) } podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) if err != nil { @@ -666,7 +673,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } // create watch on all namespaces, so that we can add resources from new namespaces - namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. + // we will need a way to deal with DELETES and CREATES and updates seperately + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) if err != nil { return nil, nil, err } @@ -676,270 +687,288 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO select { case <-ctx.Done(): return - case event, ok := <-namespaceWatch.ResultChan(): + case resourceNamespaces, ok := <-namespaceWatch: if !ok { return } - switch event.Type { - case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event: %v", event) - default: - namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) - if err != nil { - errs <- errors.Wrapf(err, "listing the namespace resources") + newNamespaces := []string{} + + for _, ns := range resourceNamespaces { + // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := simplemocksByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - - newNamespaces := []string{} - - for _, item := range namespacesResources.Items { - namespace := item.Namespace - // TODO-JAKE we might want to add a set of namespaces - // to the struct above, to manage it's list of namespaces - if _, hit := simplemocksByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := mocksByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := fakesByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := anothermockresourcesByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := mctsByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := podsByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := mocksByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for SimpleMockResource for new namespace */ - { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") - continue - } - simplemocksByNamespace[namespace] = simplemocks - } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := fakesByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := anothermockresourcesByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := mctsByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := podsByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } + } + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for SimpleMockResource for new namespace */ + { + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") continue } + simplemocksByNamespace.Store(namespace, simplemocks) + } + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") - }(namespace) - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace[namespace] = mocks - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") + }(namespace) + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Setup namespaced watch for FakeResource for new namespace */ - { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") - continue - } - fakesByNamespace[namespace] = fakes - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") - }(namespace) - /* Setup namespaced watch for AnotherMockResource for new namespace */ - { - anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") - continue - } - anothermockresourcesByNamespace[namespace] = anothermockresources - } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Setup namespaced watch for AnotherMockResource for new namespace */ + { + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") continue } + anothermockresourcesByNamespace.Store(namespace, anothermockresources) + } + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") - }(namespace) - /* Setup namespaced watch for MockCustomType for new namespace */ - { - mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") - continue - } - mctsByNamespace[namespace] = mcts - } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") + }(namespace) + /* Setup namespaced watch for MockCustomType for new namespace */ + { + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") continue } + mctsByNamespace.Store(namespace, mcts) + } + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") - }(namespace) - /* Setup namespaced watch for Pod for new namespace */ - { - pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace Pod list") - continue - } - podsByNamespace[namespace] = pods - } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") + }(namespace) + /* Setup namespaced watch for Pod for new namespace */ + { + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace Pod watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace Pod list") continue } + podsByNamespace.Store(namespace, pods) + } + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace Pod watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: - } - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: - } - case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: - } - case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: - } - case podList, ok := <-podNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case podChan <- podListWithNamespace{list: podList, namespace: namespace}: - } + case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: + } + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: + } + case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: + } + case podList, ok := <-podNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case podChan <- podListWithNamespace{list: podList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } @@ -1040,11 +1069,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - simplemocksByNamespace[namespace] = simpleMockResourceNamespacedList.list + simplemocksByNamespace.Store(namespace, simpleMockResourceNamespacedList.list) var simpleMockResourceList SimpleMockResourceList - for _, simplemocks := range simplemocksByNamespace { - simpleMockResourceList = append(simpleMockResourceList, simplemocks...) - } + simplemocksByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(SimpleMockResourceList) + simpleMockResourceList = append(simpleMockResourceList, mocks...) + return true + }) currentSnapshot.Simplemocks = simpleMockResourceList.Sort() case mockResourceNamespacedList, ok := <-mockResourceChan: if !ok { @@ -1062,11 +1093,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - mocksByNamespace[namespace] = mockResourceNamespacedList.list + mocksByNamespace.Store(namespace, mockResourceNamespacedList.list) var mockResourceList MockResourceList - for _, mocks := range mocksByNamespace { + mocksByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(MockResourceList) mockResourceList = append(mockResourceList, mocks...) - } + return true + }) currentSnapshot.Mocks = mockResourceList.Sort() case fakeResourceNamespacedList, ok := <-fakeResourceChan: if !ok { @@ -1084,11 +1117,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - fakesByNamespace[namespace] = fakeResourceNamespacedList.list + fakesByNamespace.Store(namespace, fakeResourceNamespacedList.list) var fakeResourceList FakeResourceList - for _, fakes := range fakesByNamespace { - fakeResourceList = append(fakeResourceList, fakes...) - } + fakesByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(FakeResourceList) + fakeResourceList = append(fakeResourceList, mocks...) + return true + }) currentSnapshot.Fakes = fakeResourceList.Sort() case anotherMockResourceNamespacedList, ok := <-anotherMockResourceChan: if !ok { @@ -1106,11 +1141,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - anothermockresourcesByNamespace[namespace] = anotherMockResourceNamespacedList.list + anothermockresourcesByNamespace.Store(namespace, anotherMockResourceNamespacedList.list) var anotherMockResourceList AnotherMockResourceList - for _, anothermockresources := range anothermockresourcesByNamespace { - anotherMockResourceList = append(anotherMockResourceList, anothermockresources...) - } + anothermockresourcesByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(AnotherMockResourceList) + anotherMockResourceList = append(anotherMockResourceList, mocks...) + return true + }) currentSnapshot.Anothermockresources = anotherMockResourceList.Sort() case clusterResourceList, ok := <-clusterResourceChan: if !ok { @@ -1142,11 +1179,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - mctsByNamespace[namespace] = mockCustomTypeNamespacedList.list + mctsByNamespace.Store(namespace, mockCustomTypeNamespacedList.list) var mockCustomTypeList MockCustomTypeList - for _, mcts := range mctsByNamespace { - mockCustomTypeList = append(mockCustomTypeList, mcts...) - } + mctsByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(MockCustomTypeList) + mockCustomTypeList = append(mockCustomTypeList, mocks...) + return true + }) currentSnapshot.Mcts = mockCustomTypeList.Sort() case podNamespacedList, ok := <-podChan: if !ok { @@ -1164,11 +1203,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - podsByNamespace[namespace] = podNamespacedList.list + podsByNamespace.Store(namespace, podNamespacedList.list) var podList github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList - for _, pods := range podsByNamespace { - podList = append(podList, pods...) - } + podsByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) + podList = append(podList, mocks...) + return true + }) currentSnapshot.Pods = podList.Sort() } } diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index de9b595c8..4034e2a75 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -16,10 +16,13 @@ import ( . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" "github.com/solo-io/k8s-utils/kubeutils" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/test/helpers" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/client-go/kubernetes" @@ -59,6 +62,11 @@ var _ = Describe("V1Emitter", func() { clusterResourceClient ClusterResourceClient mockCustomTypeClient MockCustomTypeClient podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache + ) + const ( + TIME_BETWEEN_MESSAGES = 5 ) NewSimpleMockResourceWithLabels := func(namespace, name string, labels map[string]string) *SimpleMockResource { resource := NewSimpleMockResource(namespace, name) @@ -97,7 +105,7 @@ var _ = Describe("V1Emitter", func() { } createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { - err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { @@ -118,7 +126,12 @@ var _ = Describe("V1Emitter", func() { namespace4 = helpers.RandString(8) namespace5 = helpers.RandString(8) namespace6 = helpers.RandString(8) + kube = helpers.MustKubeClient() + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") @@ -190,7 +203,7 @@ var _ = Describe("V1Emitter", func() { podClient, err = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 2fada145d..d9b898672 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -4,7 +4,6 @@ package v1alpha1 import ( "bytes" - "os" "sync" "time" @@ -14,15 +13,12 @@ import ( "go.uber.org/zap" "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/errors" skstats "github.com/solo-io/solo-kit/pkg/stats" "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" - "github.com/solo-io/k8s-utils/kubeutils" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kubewatch "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" ) var ( @@ -89,20 +85,22 @@ type TestingEmitter interface { MockResource() MockResourceClient } -func NewTestingEmitter(mockResourceClient MockResourceClient) TestingEmitter { - return NewTestingEmitterWithEmit(mockResourceClient, make(chan struct{})) +func NewTestingEmitter(mockResourceClient MockResourceClient, resourceNamespaceLister resources.ResourceNamespaceLister) TestingEmitter { + return NewTestingEmitterWithEmit(mockResourceClient, resourceNamespaceLister, make(chan struct{})) } -func NewTestingEmitterWithEmit(mockResourceClient MockResourceClient, emit <-chan struct{}) TestingEmitter { +func NewTestingEmitterWithEmit(mockResourceClient MockResourceClient, resourceNamespaceLister resources.ResourceNamespaceLister, emit <-chan struct{}) TestingEmitter { return &testingEmitter{ - mockResource: mockResourceClient, - forceEmit: emit, + mockResource: mockResourceClient, + resourceNamespaceLister: resourceNamespaceLister, + forceEmit: emit, } } type testingEmitter struct { - forceEmit <-chan struct{} - mockResource MockResourceClient + forceEmit <-chan struct{} + mockResource MockResourceClient + resourceNamespaceLister resources.ResourceNamespaceLister } func (c *testingEmitter) Register() error { @@ -158,7 +156,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var initialMockResourceList MockResourceList currentSnapshot := TestingSnapshot{} - mocksByNamespace := make(map[string]MockResourceList) + mocksByNamespace := sync.Map{} // watched namespaces for _, namespace := range watchNamespaces { @@ -169,7 +167,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -206,21 +204,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // by Expression Selectors // first get the renaiming namespaces - var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" - // TODO-JAKE REFACTOR, this must be added another way - // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources - // I do not think this would work in a real scenario - cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) - if err != nil { - return nil, nil, err - } - k, err = kubernetes.NewForConfig(cfg) - if err != nil { - return nil, nil, err - } + // TODO-JAKE may want to add some comments around how the snapshot_emitter + // event_loop and resource clients -> resource client implementations work in a README.md + // this would be helpful for documentation purposes + // TODO implement how we will be able to delete resources from namespaces that are deleted + + // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { buffer.WriteString("metadata.name!=") @@ -231,14 +223,29 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // we should only be watching namespaces that have the selectors that we want to be watching + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) + if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for i, ns := range namespacesResources.Items { - allOtherNamespaces[i] = ns.Namespace + allOtherNamespaces := make([]string, 0) + for _, ns := range namespacesResources { + // TODO-JAKE get the filters on the namespacing working + add := true + // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister + for _, wns := range watchNamespaces { + if ns.Name == wns { + add = false + } + } + if add { + allOtherNamespaces = append(allOtherNamespaces, ns.Name) + } } // nonWatchedNamespaces @@ -251,7 +258,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) if err != nil { @@ -283,7 +290,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } // create watch on all namespaces, so that we can add resources from new namespaces - namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. + // we will need a way to deal with DELETES and CREATES and updates seperately + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) if err != nil { return nil, nil, err } @@ -293,80 +304,73 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO select { case <-ctx.Done(): return - case event, ok := <-namespaceWatch.ResultChan(): + case resourceNamespaces, ok := <-namespaceWatch: if !ok { return } - switch event.Type { - case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event: %v", event) - default: - namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) - if err != nil { - errs <- errors.Wrapf(err, "listing the namespace resources") + newNamespaces := []string{} + + for _, ns := range resourceNamespaces { + // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := mocksByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - - newNamespaces := []string{} - - for _, item := range namespacesResources.Items { - namespace := item.Namespace - // TODO-JAKE we might want to add a set of namespaces - // to the struct above, to manage it's list of namespaces - if _, hit := mocksByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace[namespace] = mocks - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + } + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } @@ -441,11 +445,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - mocksByNamespace[namespace] = mockResourceNamespacedList.list + mocksByNamespace.Store(namespace, mockResourceNamespacedList.list) var mockResourceList MockResourceList - for _, mocks := range mocksByNamespace { + mocksByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(MockResourceList) mockResourceList = append(mockResourceList, mocks...) - } + return true + }) currentSnapshot.Mocks = mockResourceList.Sort() } } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 4ac4c6719..60588905b 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1alpha1 @@ -14,9 +13,12 @@ import ( . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" "github.com/solo-io/k8s-utils/kubeutils" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" @@ -36,26 +38,62 @@ var _ = Describe("V1Alpha1Emitter", func() { return } var ( - ctx context.Context - namespace1 string - namespace2 string - name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) - cfg *rest.Config - clientset *apiext.Clientset - kube kubernetes.Interface - emitter TestingEmitter - mockResourceClient MockResourceClient + ctx context.Context + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string + createdNamespaces map[string]bool + name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labels2 = map[string]string{"env": "testenv", "owner": "foo"} + labelExpression1 = "env in (test)" + cfg *rest.Config + clientset *apiext.Clientset + kube kubernetes.Interface + emitter TestingEmitter + mockResourceClient MockResourceClient + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache ) + const ( + TIME_BETWEEN_MESSAGES = 5 + ) + NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { + resource := NewMockResource(namespace, name) + resource.Metadata.Labels = labels + return resource + } + + createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + for _, ns := range namespaces { + if _, hit := createdNamespaces[ns]; !hit { + createdNamespaces[ns] = true + } + } + } BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() + createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + kube = helpers.MustKubeClient() - err = kubeutils.CreateNamespacesInParallel(ctx, kube, namespace1, namespace2) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") Expect(err).NotTo(HaveOccurred()) @@ -74,155 +112,486 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceClient, err = NewMockResourceClient(ctx, mockResourceClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(mockResourceClient) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) + namespacesToDelete := []string{} + for namespace, _ := range createdNamespaces { + namespacesToDelete = append(namespacesToDelete, namespace) + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) Expect(err).NotTo(HaveOccurred()) }) - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - var snap *TestingSnapshot + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - /* - MockResource - */ + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + }) + + It("should be able to track resources that are labeled on other namespaces", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaces(ctx, kube, namespace3, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource4a, mockResource4b} + assertNoMessageSent() + + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource7a, mockResource7b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertSnapshotMocks(nil, notWatched) + }) + }) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + Context("Tracking empty watched namespaces", func() { + It("tracks snapshots on changes to any resource using AllNamespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) - }) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - It("tracks snapshots on changes to any resource using AllNamespace", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - /* - MockResource - */ + It("should be able to track resources only made with the matching labels", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + assertNoMatchingMocks := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { continue drain } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain + } + } + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMatchingMocks() + + createNamespaces(ctx, kube, namespace3, namespace4) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = MockResourceList{mockResource2a, mockResource2b} + assertNoMatchingMocks() + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource3a, mockResource3b} + assertSnapshotMocks(watched, notWatched) + + mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(watched, notWatched) + + createNamespaces(ctx, kube, namespace5, namespace6) + + mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + assertNoMessageSent() + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(watched, notWatched) + + mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) + watched = MockResourceList{mockResource6a, mockResource6b} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertSnapshotMocks(nil, notWatched) + }) }) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 334fd7e8d..d8f9bf056 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -4,7 +4,6 @@ package v2alpha1 import ( "bytes" - "os" "sync" "time" @@ -16,15 +15,12 @@ import ( "go.uber.org/zap" "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/errors" skstats "github.com/solo-io/solo-kit/pkg/stats" "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/errutils" - "github.com/solo-io/k8s-utils/kubeutils" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - kubewatch "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" ) var ( @@ -93,15 +89,16 @@ type TestingEmitter interface { FakeResource() testing_solo_io.FakeResourceClient } -func NewTestingEmitter(mockResourceClient MockResourceClient, frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient, fakeResourceClient testing_solo_io.FakeResourceClient) TestingEmitter { - return NewTestingEmitterWithEmit(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, make(chan struct{})) +func NewTestingEmitter(mockResourceClient MockResourceClient, frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient, fakeResourceClient testing_solo_io.FakeResourceClient, resourceNamespaceLister resources.ResourceNamespaceLister) TestingEmitter { + return NewTestingEmitterWithEmit(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister, make(chan struct{})) } -func NewTestingEmitterWithEmit(mockResourceClient MockResourceClient, frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient, fakeResourceClient testing_solo_io.FakeResourceClient, emit <-chan struct{}) TestingEmitter { +func NewTestingEmitterWithEmit(mockResourceClient MockResourceClient, frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient, fakeResourceClient testing_solo_io.FakeResourceClient, resourceNamespaceLister resources.ResourceNamespaceLister, emit <-chan struct{}) TestingEmitter { return &testingEmitter{ mockResource: mockResourceClient, frequentlyChangingAnnotationsResource: frequentlyChangingAnnotationsResourceClient, fakeResource: fakeResourceClient, + resourceNamespaceLister: resourceNamespaceLister, forceEmit: emit, } } @@ -111,6 +108,7 @@ type testingEmitter struct { mockResource MockResourceClient frequentlyChangingAnnotationsResource FrequentlyChangingAnnotationsResourceClient fakeResource testing_solo_io.FakeResourceClient + resourceNamespaceLister resources.ResourceNamespaceLister } func (c *testingEmitter) Register() error { @@ -194,9 +192,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO var initialFakeResourceList testing_solo_io.FakeResourceList currentSnapshot := TestingSnapshot{} - mocksByNamespace := make(map[string]MockResourceList) - fcarsByNamespace := make(map[string]FrequentlyChangingAnnotationsResourceList) - fakesByNamespace := make(map[string]testing_solo_io.FakeResourceList) + mocksByNamespace := sync.Map{} + fcarsByNamespace := sync.Map{} + fakesByNamespace := sync.Map{} // watched namespaces for _, namespace := range watchNamespaces { @@ -207,7 +205,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -226,7 +224,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") } initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) - fcarsByNamespace[namespace] = fcars + fcarsByNamespace.Store(namespace, fcars) } frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -245,7 +243,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace[namespace] = fakes + fakesByNamespace.Store(namespace, fakes) } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { @@ -300,21 +298,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // by Expression Selectors // first get the renaiming namespaces - var k kubernetes.Interface excludeNamespacesFieldDesciptors := "" - // TODO-JAKE REFACTOR, this must be added another way - // TODO-JAKE should not be from KUBECONFIG, might need to use the abstraction for namespace Resources - // I do not think this would work in a real scenario - cfg, err := kubeutils.GetConfig("", os.Getenv("KUBECONFIG")) - if err != nil { - return nil, nil, err - } - k, err = kubernetes.NewForConfig(cfg) - if err != nil { - return nil, nil, err - } + // TODO-JAKE may want to add some comments around how the snapshot_emitter + // event_loop and resource clients -> resource client implementations work in a README.md + // this would be helpful for documentation purposes + // TODO implement how we will be able to delete resources from namespaces that are deleted + + // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { buffer.WriteString("metadata.name!=") @@ -325,14 +317,29 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } excludeNamespacesFieldDesciptors = buffer.String() + // we should only be watching namespaces that have the selectors that we want to be watching + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := k.CoreV1().Namespaces().List(ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) + if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, len(namespacesResources.Items)) - for i, ns := range namespacesResources.Items { - allOtherNamespaces[i] = ns.Namespace + allOtherNamespaces := make([]string, 0) + for _, ns := range namespacesResources { + // TODO-JAKE get the filters on the namespacing working + add := true + // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister + for _, wns := range watchNamespaces { + if ns.Name == wns { + add = false + } + } + if add { + allOtherNamespaces = append(allOtherNamespaces, ns.Name) + } } // nonWatchedNamespaces @@ -345,7 +352,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace[namespace] = mocks + mocksByNamespace.Store(namespace, mocks) } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) if err != nil { @@ -364,7 +371,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") } initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) - fcarsByNamespace[namespace] = fcars + fcarsByNamespace.Store(namespace, fcars) } frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) if err != nil { @@ -383,7 +390,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace[namespace] = fakes + fakesByNamespace.Store(namespace, fakes) } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) if err != nil { @@ -433,7 +440,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } // create watch on all namespaces, so that we can add resources from new namespaces - namespaceWatch, err := k.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. + // we will need a way to deal with DELETES and CREATES and updates seperately + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ + FieldSelectors: excludeNamespacesFieldDesciptors, + }) if err != nil { return nil, nil, err } @@ -443,156 +454,159 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO select { case <-ctx.Done(): return - case event, ok := <-namespaceWatch.ResultChan(): + case resourceNamespaces, ok := <-namespaceWatch: if !ok { return } - switch event.Type { - case kubewatch.Error: - errs <- errors.Errorf("receiving namespace event: %v", event) - default: - namespacesResources, err := k.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespacesFieldDesciptors}) - if err != nil { - errs <- errors.Wrapf(err, "listing the namespace resources") + newNamespaces := []string{} + + for _, ns := range resourceNamespaces { + // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := mocksByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - - newNamespaces := []string{} - - for _, item := range namespacesResources.Items { - namespace := item.Namespace - // TODO-JAKE we might want to add a set of namespaces - // to the struct above, to manage it's list of namespaces - if _, hit := mocksByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := fcarsByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } - if _, hit := fakesByNamespace[namespace]; !hit { - newNamespaces = append(newNamespaces, namespace) - continue - } + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := fcarsByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace[namespace] = mocks - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current + // namespaces that are used + if _, hit := fakesByNamespace.Load(ns.Name); !hit { + newNamespaces = append(newNamespaces, ns.Name) + continue + } + } + // TODO-JAKE I think we could get rid of this if statement if needed. + if len(newNamespaces) > 0 { + // add a watch for all the new namespaces + // REFACTOR + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ - { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") - continue - } - fcarsByNamespace[namespace] = fcars - } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ + { + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") continue } + fcarsByNamespace.Store(namespace, fcars) + } + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") - }(namespace) - /* Setup namespaced watch for FakeResource for new namespace */ - { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") - continue - } - fakesByNamespace[namespace] = fakes - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) if err != nil { - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: - } + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } @@ -671,11 +685,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - mocksByNamespace[namespace] = mockResourceNamespacedList.list + mocksByNamespace.Store(namespace, mockResourceNamespacedList.list) var mockResourceList MockResourceList - for _, mocks := range mocksByNamespace { + mocksByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(MockResourceList) mockResourceList = append(mockResourceList, mocks...) - } + return true + }) currentSnapshot.Mocks = mockResourceList.Sort() case frequentlyChangingAnnotationsResourceNamespacedList, ok := <-frequentlyChangingAnnotationsResourceChan: if !ok { @@ -693,11 +709,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - fcarsByNamespace[namespace] = frequentlyChangingAnnotationsResourceNamespacedList.list + fcarsByNamespace.Store(namespace, frequentlyChangingAnnotationsResourceNamespacedList.list) var frequentlyChangingAnnotationsResourceList FrequentlyChangingAnnotationsResourceList - for _, fcars := range fcarsByNamespace { - frequentlyChangingAnnotationsResourceList = append(frequentlyChangingAnnotationsResourceList, fcars...) - } + fcarsByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(FrequentlyChangingAnnotationsResourceList) + frequentlyChangingAnnotationsResourceList = append(frequentlyChangingAnnotationsResourceList, mocks...) + return true + }) currentSnapshot.Fcars = frequentlyChangingAnnotationsResourceList.Sort() case fakeResourceNamespacedList, ok := <-fakeResourceChan: if !ok { @@ -715,11 +733,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - fakesByNamespace[namespace] = fakeResourceNamespacedList.list + fakesByNamespace.Store(namespace, fakeResourceNamespacedList.list) var fakeResourceList testing_solo_io.FakeResourceList - for _, fakes := range fakesByNamespace { - fakeResourceList = append(fakeResourceList, fakes...) - } + fakesByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(testing_solo_io.FakeResourceList) + fakeResourceList = append(fakeResourceList, mocks...) + return true + }) currentSnapshot.Fakes = fakeResourceList.Sort() } } diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 7829ab485..ccbc442a5 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -15,10 +15,13 @@ import ( . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" "github.com/solo-io/k8s-utils/kubeutils" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" @@ -55,6 +58,11 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceClient MockResourceClient frequentlyChangingAnnotationsResourceClient FrequentlyChangingAnnotationsResourceClient fakeResourceClient testing_solo_io.FakeResourceClient + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache + ) + const ( + TIME_BETWEEN_MESSAGES = 5 ) NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { resource := NewMockResource(namespace, name) @@ -73,7 +81,7 @@ var _ = Describe("V2Alpha1Emitter", func() { } createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { - err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace5, namespace6) + err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { @@ -94,7 +102,12 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace4 = helpers.RandString(8) namespace5 = helpers.RandString(8) namespace6 = helpers.RandString(8) + kube = helpers.MustKubeClient() + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") @@ -128,7 +141,7 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceClient, err = testing_solo_io.NewFakeResourceClient(ctx, fakeResourceClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient) + emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) From 95d854817ac592987cacd062121235d4df0b83d0 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 25 Aug 2022 16:24:19 -0500 Subject: [PATCH 11/98] updated snapshot_emitter to filter namespaces by labels, and not resources --- .../templates/snapshot_emitter_template.go | 210 +++++---- .../v1/kubeconfigs_snapshot_emitter.sk.go | 191 ++++---- .../v1alpha1/testing_snapshot_emitter.sk.go | 191 ++++---- .../v2alpha1/testing_snapshot_emitter.sk.go | 433 +++++++++--------- 4 files changed, 531 insertions(+), 494 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 1b2b6e973..1f1d36a8f 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -156,6 +156,10 @@ func (c *{{ lower_camel $.GoName }}Emitter) {{ .Name }}() {{ .ImportPrefix }}{{ } {{- end}} +// TODO-JAKE may want to add some comments around how the snapshot_emitter +// event_loop and resource clients -> resource client implementations work in a README.md +// this would be helpful for documentation purposes + func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -210,69 +214,67 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} - // watched namespaces - for _, namespace := range watchNamespaces { -{{- range .Resources}} -{{- if (not .ClusterScoped) }} - /* Setup namespaced watch for {{ .Name }} */ - { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, watchedNamespacesListOptions) + if ! watchNamespacesIsEmpty { + // then watch all resources on watch Namespaces + + // watched namespaces + for _, namespace := range watchNamespaces { + {{- range .Resources}} + {{- if (not .ClusterScoped) }} + /* Setup namespaced watch for {{ .Name }} */ + { + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") + } + initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .PluralName }}...) + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") + return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch") } - initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .PluralName }}...) - {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) - } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .PluralName }}") - }(namespace) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .PluralName }}") + }(namespace) -{{- end }} -{{- end }} - /* Watch for changes and update snapshot */ - go func(namespace string) { - for { - select { - case <-ctx.Done(): - return -{{- range .Resources}} -{{- if (not .ClusterScoped) }} - case {{ lower_camel .Name }}List, ok := <- {{ lower_camel .Name }}NamespacesChan: - if !ok { - return - } + {{- end }} + {{- end }} + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { select { case <-ctx.Done(): return - case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list:{{ lower_camel .Name }}List, namespace:namespace}: + {{- range .Resources}} + {{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <- {{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list:{{ lower_camel .Name }}List, namespace:namespace}: + } + {{- end }} + {{- end}} } -{{- end }} -{{- end}} } - } - }(namespace) + }(namespace) + } } - if hasWatchedNamespaces && opts.ExpressionSelector != "" { - // watch resources using non-watched namespaces. With these namespaces we - // will watch only those that are filted using the label selectors defined - // by Expression Selectors + // watch all other namespaces that fit the Expression Selectors + if opts.ExpressionSelector != "" { + // watch resources of non-watched namespaces that fit the Expression + // Selector filters. // first get the renaiming namespaces excludeNamespacesFieldDesciptors := "" - // TODO-JAKE may want to add some comments around how the snapshot_emitter - // event_loop and resource clients -> resource client implementations work in a README.md - // this would be helpful for documentation purposes - - // TODO implement how we will be able to delete resources from namespaces that are deleted - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { @@ -285,10 +287,15 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o excludeNamespacesFieldDesciptors = buffer.String() // we should only be watching namespaces that have the selectors that we want to be watching - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. + // this could be built dyynamically + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ + // TODO-JAKE field selectors are not working FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { @@ -302,6 +309,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o for _,wns := range watchNamespaces { if ns.Name == wns { add = false + break } } if add { @@ -309,21 +317,21 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } } - // nonWatchedNamespaces + // non Watched Namespaces // REFACTOR for _, namespace := range allOtherNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ upper_camel .Name }} */ - { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + {clien + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") } initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List,{{ lower_camel .PluralName }}...) {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting {{ upper_camel .Name }} watch") } @@ -363,6 +371,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // we will need a way to deal with DELETES and CREATES and updates seperately namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { return nil, nil, err @@ -377,6 +386,9 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o if !ok { return } + // TODO-JAKE with the interface, we have lost the ability to know the event type. + // so the interface must be able to identify the type of event that occured as well + // not just return the list of namespaces newNamespaces := []string{} for _, ns := range resourceNamespaces { @@ -393,67 +405,63 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0{ - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { + // add a watch for all the new namespaces + for _, namespace := range newNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} - /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ - { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") - continue - } - {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) - } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts) + /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ + { + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") continue } + {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) + } + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .PluralName }}") - }(namespace) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-new-namespace-{{ lower_camel .PluralName }}") + }(namespace) {{- end }} {{- end }} - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return +{{- range .Resources }} +{{- if (not .ClusterScoped) }} + case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return -{{- range .Resources }} -{{- if (not .ClusterScoped) }} - case {{ lower_camel .Name }}List, ok := <-{{ lower_camel .Name }}NamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: - } + case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ lower_camel .Name }}List, namespace: namespace}: + } {{- end }} {{- end }} - } } - }(namespace) - } + } + }(namespace) } } } diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index c0e090bea..a3008051e 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -114,6 +114,10 @@ func (c *kubeconfigsEmitter) KubeConfig() KubeConfigClient { return c.kubeConfig } +// TODO-JAKE may want to add some comments around how the snapshot_emitter +// event_loop and resource clients -> resource client implementations work in a README.md +// this would be helpful for documentation purposes + func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *KubeconfigsSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -158,60 +162,58 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa currentSnapshot := KubeconfigsSnapshot{} kubeconfigsByNamespace := sync.Map{} - // watched namespaces - for _, namespace := range watchNamespaces { - /* Setup namespaced watch for KubeConfig */ - { - kubeconfigs, err := c.kubeConfig.List(namespace, watchedNamespacesListOptions) + if !watchNamespacesIsEmpty { + // then watch all resources on watch Namespaces + + // watched namespaces + for _, namespace := range watchNamespaces { + /* Setup namespaced watch for KubeConfig */ + { + kubeconfigs, err := c.kubeConfig.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial KubeConfig list") + } + initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) + kubeconfigsByNamespace.Store(namespace, kubeconfigs) + } + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial KubeConfig list") + return nil, nil, errors.Wrapf(err, "starting KubeConfig watch") } - initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) - kubeconfigsByNamespace.Store(namespace, kubeconfigs) - } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting KubeConfig watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-kubeconfigs") - }(namespace) - /* Watch for changes and update snapshot */ - go func(namespace string) { - for { - select { - case <-ctx.Done(): - return - case kubeConfigList, ok := <-kubeConfigNamespacesChan: - if !ok { - return - } + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-kubeconfigs") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { select { case <-ctx.Done(): return - case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: + case kubeConfigList, ok := <-kubeConfigNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: + } } } - } - }(namespace) + }(namespace) + } } - if hasWatchedNamespaces && opts.ExpressionSelector != "" { - // watch resources using non-watched namespaces. With these namespaces we - // will watch only those that are filted using the label selectors defined - // by Expression Selectors + // watch all other namespaces that fit the Expression Selectors + if opts.ExpressionSelector != "" { + // watch resources of non-watched namespaces that fit the Expression + // Selector filters. // first get the renaiming namespaces excludeNamespacesFieldDesciptors := "" - // TODO-JAKE may want to add some comments around how the snapshot_emitter - // event_loop and resource clients -> resource client implementations work in a README.md - // this would be helpful for documentation purposes - - // TODO implement how we will be able to delete resources from namespaces that are deleted - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { @@ -224,10 +226,15 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa excludeNamespacesFieldDesciptors = buffer.String() // we should only be watching namespaces that have the selectors that we want to be watching - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. + // this could be built dyynamically + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + // TODO-JAKE field selectors are not working + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { @@ -241,6 +248,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa for _, wns := range watchNamespaces { if ns.Name == wns { add = false + break } } if add { @@ -248,19 +256,20 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } } - // nonWatchedNamespaces + // non Watched Namespaces // REFACTOR for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for KubeConfig */ { - kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial KubeConfig list") } initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) kubeconfigsByNamespace.Store(namespace, kubeconfigs) } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting KubeConfig watch") } @@ -293,7 +302,8 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { return nil, nil, err @@ -308,6 +318,9 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa if !ok { return } + // TODO-JAKE with the interface, we have lost the ability to know the event type. + // so the interface must be able to identify the type of event that occured as well + // not just return the list of namespaces newNamespaces := []string{} for _, ns := range resourceNamespaces { @@ -319,59 +332,55 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa continue } } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for KubeConfig for new namespace */ - { - kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") - continue - } - kubeconfigsByNamespace.Store(namespace, kubeconfigs) - } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, opts) + // add a watch for all the new namespaces + for _, namespace := range newNamespaces { + /* Setup namespaced watch for KubeConfig for new namespace */ + { + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") continue } + kubeconfigsByNamespace.Store(namespace, kubeconfigs) + } + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case kubeConfigList, ok := <-kubeConfigNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case kubeConfigList, ok := <-kubeConfigNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: - } + case kubeConfigChan <- kubeConfigListWithNamespace{list: kubeConfigList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index d9b898672..5b7018c08 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -114,6 +114,10 @@ func (c *testingEmitter) MockResource() MockResourceClient { return c.mockResource } +// TODO-JAKE may want to add some comments around how the snapshot_emitter +// event_loop and resource clients -> resource client implementations work in a README.md +// this would be helpful for documentation purposes + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -158,60 +162,58 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot := TestingSnapshot{} mocksByNamespace := sync.Map{} - // watched namespaces - for _, namespace := range watchNamespaces { - /* Setup namespaced watch for MockResource */ - { - mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + if !watchNamespacesIsEmpty { + // then watch all resources on watch Namespaces + + // watched namespaces + for _, namespace := range watchNamespaces { + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "starting MockResource watch") } - initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting MockResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") - }(namespace) - /* Watch for changes and update snapshot */ - go func(namespace string) { - for { - select { - case <-ctx.Done(): - return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { select { case <-ctx.Done(): return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } } } - } - }(namespace) + }(namespace) + } } - if hasWatchedNamespaces && opts.ExpressionSelector != "" { - // watch resources using non-watched namespaces. With these namespaces we - // will watch only those that are filted using the label selectors defined - // by Expression Selectors + // watch all other namespaces that fit the Expression Selectors + if opts.ExpressionSelector != "" { + // watch resources of non-watched namespaces that fit the Expression + // Selector filters. // first get the renaiming namespaces excludeNamespacesFieldDesciptors := "" - // TODO-JAKE may want to add some comments around how the snapshot_emitter - // event_loop and resource clients -> resource client implementations work in a README.md - // this would be helpful for documentation purposes - - // TODO implement how we will be able to delete resources from namespaces that are deleted - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { @@ -224,10 +226,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO excludeNamespacesFieldDesciptors = buffer.String() // we should only be watching namespaces that have the selectors that we want to be watching - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. + // this could be built dyynamically + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + // TODO-JAKE field selectors are not working + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { @@ -241,6 +248,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, wns := range watchNamespaces { if ns.Name == wns { add = false + break } } if add { @@ -248,19 +256,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // nonWatchedNamespaces + // non Watched Namespaces // REFACTOR for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -293,7 +302,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { return nil, nil, err @@ -308,6 +318,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } + // TODO-JAKE with the interface, we have lost the ability to know the event type. + // so the interface must be able to identify the type of event that occured as well + // not just return the list of namespaces newNamespaces := []string{} for _, ns := range resourceNamespaces { @@ -319,59 +332,55 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + // add a watch for all the new namespaces + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index d8f9bf056..b710dcb91 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -136,6 +136,10 @@ func (c *testingEmitter) FakeResource() testing_solo_io.FakeResourceClient { return c.fakeResource } +// TODO-JAKE may want to add some comments around how the snapshot_emitter +// event_loop and resource clients -> resource client implementations work in a README.md +// this would be helpful for documentation purposes + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -196,116 +200,114 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fcarsByNamespace := sync.Map{} fakesByNamespace := sync.Map{} - // watched namespaces - for _, namespace := range watchNamespaces { - /* Setup namespaced watch for MockResource */ - { - mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + if !watchNamespacesIsEmpty { + // then watch all resources on watch Namespaces + + // watched namespaces + for _, namespace := range watchNamespaces { + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "starting MockResource watch") } - initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting MockResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") - }(namespace) - /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ - { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ + { + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") + } + initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) + fcarsByNamespace.Store(namespace, fcars) + } + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") + return nil, nil, errors.Wrapf(err, "starting FrequentlyChangingAnnotationsResource watch") } - initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) - fcarsByNamespace.Store(namespace, fcars) - } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting FrequentlyChangingAnnotationsResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-fcars") - }(namespace) - /* Setup namespaced watch for FakeResource */ - { - fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-fcars") + }(namespace) + /* Setup namespaced watch for FakeResource */ + { + fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FakeResource list") + } + initialFakeResourceList = append(initialFakeResourceList, fakes...) + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FakeResource list") + return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } - initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace.Store(namespace, fakes) - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting FakeResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") - }(namespace) - /* Watch for changes and update snapshot */ - go func(namespace string) { - for { - select { - case <-ctx.Done(): - return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { select { case <-ctx.Done(): return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } } } - } - }(namespace) + }(namespace) + } } - if hasWatchedNamespaces && opts.ExpressionSelector != "" { - // watch resources using non-watched namespaces. With these namespaces we - // will watch only those that are filted using the label selectors defined - // by Expression Selectors + // watch all other namespaces that fit the Expression Selectors + if opts.ExpressionSelector != "" { + // watch resources of non-watched namespaces that fit the Expression + // Selector filters. // first get the renaiming namespaces excludeNamespacesFieldDesciptors := "" - // TODO-JAKE may want to add some comments around how the snapshot_emitter - // event_loop and resource clients -> resource client implementations work in a README.md - // this would be helpful for documentation purposes - - // TODO implement how we will be able to delete resources from namespaces that are deleted - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { @@ -318,10 +320,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO excludeNamespacesFieldDesciptors = buffer.String() // we should only be watching namespaces that have the selectors that we want to be watching - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. + // this could be built dyynamically + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + // TODO-JAKE field selectors are not working + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { @@ -335,6 +342,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, wns := range watchNamespaces { if ns.Name == wns { add = false + break } } if add { @@ -342,19 +350,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // nonWatchedNamespaces + // non Watched Namespaces // REFACTOR for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -366,14 +375,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") } initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) fcarsByNamespace.Store(namespace, fcars) } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting FrequentlyChangingAnnotationsResource watch") } @@ -385,14 +395,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace.Store(namespace, fakes) } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } @@ -443,7 +454,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { return nil, nil, err @@ -458,6 +470,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } + // TODO-JAKE with the interface, we have lost the ability to know the event type. + // so the interface must be able to identify the type of event that occured as well + // not just return the list of namespaces newNamespaces := []string{} for _, ns := range resourceNamespaces { @@ -481,133 +496,129 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + // add a watch for all the new namespaces + for _, namespace := range newNamespaces { + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ - { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") - continue - } - fcarsByNamespace.Store(namespace, fcars) - } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ + { + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") continue } + fcarsByNamespace.Store(namespace, fcars) + } + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") - }(namespace) - /* Setup namespaced watch for FakeResource for new namespace */ - { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") - continue - } - fakesByNamespace.Store(namespace, fakes) - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case frequentlyChangingAnnotationsResourceList, ok := <-frequentlyChangingAnnotationsResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: - } + case frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: frequentlyChangingAnnotationsResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } From 86bca001945675abbb871fc86dcbcf6867f00237 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 25 Aug 2022 16:25:22 -0500 Subject: [PATCH 12/98] added client interface --- pkg/api/v1/clients/client_interface.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 36db15e00..4dbdb1f19 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -128,6 +128,8 @@ type ListOpts struct { // (2) the label key equal to version and the value equal to v1 // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string + // TODO-JAKE add in Field Selectors + FieldSelectors string } func (o ListOpts) WithDefaults() ListOpts { @@ -164,7 +166,9 @@ type WatchOpts struct { // (2) the label key equal to version and the value equal to v1 // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string - RefreshRate time.Duration + // JAKE-TODO + FieldSelectors string + RefreshRate time.Duration // Cluster is ignored by aggregated watches, but is respected by multi cluster clients. Cluster string } From 90b86d2602d0ee17489a6a6d72e536c6dc51534f Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 25 Aug 2022 16:26:12 -0500 Subject: [PATCH 13/98] left out some things --- pkg/api/v1/resources/resource_interface.go | 4 + test/mocks/v1/testing_snapshot_emitter.sk.go | 796 ++++++++++--------- 2 files changed, 409 insertions(+), 391 deletions(-) diff --git a/pkg/api/v1/resources/resource_interface.go b/pkg/api/v1/resources/resource_interface.go index e5f76c081..79084482a 100644 --- a/pkg/api/v1/resources/resource_interface.go +++ b/pkg/api/v1/resources/resource_interface.go @@ -88,6 +88,8 @@ type ResourceNamespaceListOptions struct { // of the namespace by using metadata.name!= // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ FieldSelectors string + // TODO-JAKE add description + ExpressionSelectors string } // ResourceNamespaceWatchOptions provides the options for watching Resource Namespaces @@ -97,6 +99,8 @@ type ResourceNamespaceWatchOptions struct { // of the namespace by using metadata.name!= // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ FieldSelectors string + // TODO-JAKE add description + ExpressionSelectors string } // ResoruceNamespace is the namespaces that resources can be found. ResourceNamespaces are diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index b5f0a7eff..ae6530da4 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -176,6 +176,10 @@ func (c *testingEmitter) Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_ return c.pod } +// TODO-JAKE may want to add some comments around how the snapshot_emitter +// event_loop and resource clients -> resource client implementations work in a README.md +// this would be helpful for documentation purposes + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -261,200 +265,198 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mctsByNamespace := sync.Map{} podsByNamespace := sync.Map{} - // watched namespaces - for _, namespace := range watchNamespaces { - /* Setup namespaced watch for SimpleMockResource */ - { - simplemocks, err := c.simpleMockResource.List(namespace, watchedNamespacesListOptions) + if !watchNamespacesIsEmpty { + // then watch all resources on watch Namespaces + + // watched namespaces + for _, namespace := range watchNamespaces { + /* Setup namespaced watch for SimpleMockResource */ + { + simplemocks, err := c.simpleMockResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") + } + initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) + simplemocksByNamespace.Store(namespace, simplemocks) + } + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") + return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") } - initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) - simplemocksByNamespace.Store(namespace, simplemocks) - } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-simplemocks") - }(namespace) - /* Setup namespaced watch for MockResource */ - { - mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-simplemocks") + }(namespace) + /* Setup namespaced watch for MockResource */ + { + mocks, err := c.mockResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockResource list") + } + initialMockResourceList = append(initialMockResourceList, mocks...) + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "starting MockResource watch") } - initialMockResourceList = append(initialMockResourceList, mocks...) - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting MockResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") - }(namespace) - /* Setup namespaced watch for FakeResource */ - { - fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") + }(namespace) + /* Setup namespaced watch for FakeResource */ + { + fakes, err := c.fakeResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial FakeResource list") + } + initialFakeResourceList = append(initialFakeResourceList, fakes...) + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FakeResource list") + return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } - initialFakeResourceList = append(initialFakeResourceList, fakes...) - fakesByNamespace.Store(namespace, fakes) - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting FakeResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") - }(namespace) - /* Setup namespaced watch for AnotherMockResource */ - { - anothermockresources, err := c.anotherMockResource.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") + }(namespace) + /* Setup namespaced watch for AnotherMockResource */ + { + anothermockresources, err := c.anotherMockResource.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") + } + initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) + anothermockresourcesByNamespace.Store(namespace, anothermockresources) + } + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") + return nil, nil, errors.Wrapf(err, "starting AnotherMockResource watch") } - initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) - anothermockresourcesByNamespace.Store(namespace, anothermockresources) - } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting AnotherMockResource watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-anothermockresources") - }(namespace) - /* Setup namespaced watch for MockCustomType */ - { - mcts, err := c.mockCustomType.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-anothermockresources") + }(namespace) + /* Setup namespaced watch for MockCustomType */ + { + mcts, err := c.mockCustomType.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockCustomType list") + } + initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) + mctsByNamespace.Store(namespace, mcts) + } + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockCustomType list") + return nil, nil, errors.Wrapf(err, "starting MockCustomType watch") } - initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) - mctsByNamespace.Store(namespace, mcts) - } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting MockCustomType watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") - }(namespace) - /* Setup namespaced watch for Pod */ - { - pods, err := c.pod.List(namespace, watchedNamespacesListOptions) + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") + }(namespace) + /* Setup namespaced watch for Pod */ + { + pods, err := c.pod.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial Pod list") + } + initialPodList = append(initialPodList, pods...) + podsByNamespace.Store(namespace, pods) + } + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, watchedNamespacesWatchOptions) if err != nil { - return nil, nil, errors.Wrapf(err, "initial Pod list") + return nil, nil, errors.Wrapf(err, "starting Pod watch") } - initialPodList = append(initialPodList, pods...) - podsByNamespace.Store(namespace, pods) - } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, watchedNamespacesWatchOptions) - if err != nil { - return nil, nil, errors.Wrapf(err, "starting Pod watch") - } - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-pods") - }(namespace) - /* Watch for changes and update snapshot */ - go func(namespace string) { - for { - select { - case <-ctx.Done(): - return - case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: - } - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: - } - case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: - } - case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: - } - case podList, ok := <-podNamespacesChan: - if !ok { - return - } + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-pods") + }(namespace) + /* Watch for changes and update snapshot */ + go func(namespace string) { + for { select { case <-ctx.Done(): return - case podChan <- podListWithNamespace{list: podList, namespace: namespace}: + case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: + } + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: + } + case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: + } + case podList, ok := <-podNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case podChan <- podListWithNamespace{list: podList, namespace: namespace}: + } } } - } - }(namespace) + }(namespace) + } } - if hasWatchedNamespaces && opts.ExpressionSelector != "" { - // watch resources using non-watched namespaces. With these namespaces we - // will watch only those that are filted using the label selectors defined - // by Expression Selectors + // watch all other namespaces that fit the Expression Selectors + if opts.ExpressionSelector != "" { + // watch resources of non-watched namespaces that fit the Expression + // Selector filters. // first get the renaiming namespaces excludeNamespacesFieldDesciptors := "" - // TODO-JAKE may want to add some comments around how the snapshot_emitter - // event_loop and resource clients -> resource client implementations work in a README.md - // this would be helpful for documentation purposes - - // TODO implement how we will be able to delete resources from namespaces that are deleted - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { @@ -467,10 +469,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO excludeNamespacesFieldDesciptors = buffer.String() // we should only be watching namespaces that have the selectors that we want to be watching - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list + + // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. + // this could be built dyynamically + // TODO-JAKE test that we can create a huge field selector of massive size namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + // TODO-JAKE field selectors are not working + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { @@ -484,6 +491,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, wns := range watchNamespaces { if ns.Name == wns { add = false + break } } if add { @@ -491,19 +499,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // nonWatchedNamespaces + // non Watched Namespaces // REFACTOR for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for SimpleMockResource */ { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") } initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) simplemocksByNamespace.Store(namespace, simplemocks) } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") } @@ -515,14 +524,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockResource */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockResource watch") } @@ -534,14 +544,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace.Store(namespace, fakes) } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting FakeResource watch") } @@ -553,14 +564,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for AnotherMockResource */ { - anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") } initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) anothermockresourcesByNamespace.Store(namespace, anothermockresources) } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting AnotherMockResource watch") } @@ -572,14 +584,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockCustomType */ { - mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockCustomType list") } initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) mctsByNamespace.Store(namespace, mcts) } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting MockCustomType watch") } @@ -591,14 +604,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for Pod */ { - pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) + clien + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial Pod list") } initialPodList = append(initialPodList, pods...) podsByNamespace.Store(namespace, pods) } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "starting Pod watch") } @@ -676,7 +690,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, + FieldSelectors: excludeNamespacesFieldDesciptors, + ExpressionSelectors: opts.ExpressionSelector, }) if err != nil { return nil, nil, err @@ -691,6 +706,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } + // TODO-JAKE with the interface, we have lost the ability to know the event type. + // so the interface must be able to identify the type of event that occured as well + // not just return the list of namespaces newNamespaces := []string{} for _, ns := range resourceNamespaces { @@ -732,244 +750,240 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } } - // TODO-JAKE I think we could get rid of this if statement if needed. - if len(newNamespaces) > 0 { - // add a watch for all the new namespaces - // REFACTOR - for _, namespace := range newNamespaces { - /* Setup namespaced watch for SimpleMockResource for new namespace */ - { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") - continue - } - simplemocksByNamespace.Store(namespace, simplemocks) - } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, opts) + // add a watch for all the new namespaces + for _, namespace := range newNamespaces { + /* Setup namespaced watch for SimpleMockResource for new namespace */ + { + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") continue } + simplemocksByNamespace.Store(namespace, simplemocks) + } + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") - }(namespace) - /* Setup namespaced watch for MockResource for new namespace */ - { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockResource list") - continue - } - mocksByNamespace.Store(namespace, mocks) - } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") + }(namespace) + /* Setup namespaced watch for MockResource for new namespace */ + { + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } + mocksByNamespace.Store(namespace, mocks) + } + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") - }(namespace) - /* Setup namespaced watch for FakeResource for new namespace */ - { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") - continue - } - fakesByNamespace.Store(namespace, fakes) - } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") + }(namespace) + /* Setup namespaced watch for FakeResource for new namespace */ + { + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } + fakesByNamespace.Store(namespace, fakes) + } + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") - }(namespace) - /* Setup namespaced watch for AnotherMockResource for new namespace */ - { - anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") - continue - } - anothermockresourcesByNamespace.Store(namespace, anothermockresources) - } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") + }(namespace) + /* Setup namespaced watch for AnotherMockResource for new namespace */ + { + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") continue } + anothermockresourcesByNamespace.Store(namespace, anothermockresources) + } + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") - }(namespace) - /* Setup namespaced watch for MockCustomType for new namespace */ - { - mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") - continue - } - mctsByNamespace.Store(namespace, mcts) - } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") + }(namespace) + /* Setup namespaced watch for MockCustomType for new namespace */ + { + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") continue } + mctsByNamespace.Store(namespace, mcts) + } + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") - }(namespace) - /* Setup namespaced watch for Pod for new namespace */ - { - pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector}) - if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine - errs <- errors.Wrapf(err, "initial new namespace Pod list") - continue - } - podsByNamespace.Store(namespace, pods) - } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts) + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") + }(namespace) + /* Setup namespaced watch for Pod for new namespace */ + { + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - errs <- errors.Wrapf(err, "starting new namespace Pod watch") + // INFO-JAKE not sure if we want to do something else + // but since this is occuring in async I think it should be fine + errs <- errors.Wrapf(err, "initial new namespace Pod list") continue } + podsByNamespace.Store(namespace, pods) + } + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality + // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the + // ResourceNamespaceLister currently + // INFO-JAKE is this what we really want to do when there is an error? + errs <- errors.Wrapf(err, "starting new namespace Pod watch") + continue + } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace - done.Add(1) - go func(namespace string) { - defer done.Done() - errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") - }(namespace) - /* Watch for changes and update snapshot */ - // REFACTOR - go func(namespace string) { - for { + // INFO-JAKE I think this is appropriate, becasue + // we want to watch the errors coming off the namespace + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") + }(namespace) + /* Watch for changes and update snapshot */ + // REFACTOR + go func(namespace string) { + for { + select { + case <-ctx.Done(): + return + case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: + } + case mockResourceList, ok := <-mockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: + } + case fakeResourceList, ok := <-fakeResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: + } + case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: + } + case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: + if !ok { + return + } select { case <-ctx.Done(): return - case simpleMockResourceList, ok := <-simpleMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: simpleMockResourceList, namespace: namespace}: - } - case mockResourceList, ok := <-mockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockResourceChan <- mockResourceListWithNamespace{list: mockResourceList, namespace: namespace}: - } - case fakeResourceList, ok := <-fakeResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case fakeResourceChan <- fakeResourceListWithNamespace{list: fakeResourceList, namespace: namespace}: - } - case anotherMockResourceList, ok := <-anotherMockResourceNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: anotherMockResourceList, namespace: namespace}: - } - case mockCustomTypeList, ok := <-mockCustomTypeNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: - } - case podList, ok := <-podNamespacesChan: - if !ok { - return - } - select { - case <-ctx.Done(): - return - case podChan <- podListWithNamespace{list: podList, namespace: namespace}: - } + case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: + } + case podList, ok := <-podNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case podChan <- podListWithNamespace{list: podList, namespace: namespace}: } } - }(namespace) - } + } + }(namespace) } } } From 26216b5ca567b1308fa432709d69d8f57f67fccf Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 25 Aug 2022 17:34:49 -0500 Subject: [PATCH 14/98] get interface to work with expression selectors --- .../kubernetes/namespace/resource_client.go | 7 +++++-- .../kubernetes/namespace/resource_namespace.go | 2 +- pkg/api/v1/clients/common/common.go | 5 +++-- pkg/api/v1/clients/kube/resource_client.go | 13 +------------ pkg/api/v1/clients/options.go | 15 +++++++++++++++ .../templates/snapshot_emitter_template.go | 2 +- 6 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 pkg/api/v1/clients/options.go diff --git a/pkg/api/external/kubernetes/namespace/resource_client.go b/pkg/api/external/kubernetes/namespace/resource_client.go index 1ed549aa6..c6204066d 100644 --- a/pkg/api/external/kubernetes/namespace/resource_client.go +++ b/pkg/api/external/kubernetes/namespace/resource_client.go @@ -16,7 +16,6 @@ import ( kubev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/kubernetes" ) @@ -144,7 +143,11 @@ func (rc *namespaceResourceClient) List(namespace string, opts clients.ListOpts) return nil, eris.New("to list namespaces you must watch all namespaces") } - namespaceObjList, err := rc.cache.NamespaceLister().List(labels.SelectorFromSet(opts.Selector)) + listOptions, err := clients.GetLabelSelector(opts) + if err != nil { + return nil, err + } + namespaceObjList, err := rc.cache.NamespaceLister().List(listOptions) if err != nil { return nil, eris.Wrapf(err, "listing namespaces level") } diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 0b2252986..41f49941f 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -37,7 +37,7 @@ func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(ctx context.Con } func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(ctx context.Context, opts resources.ResourceNamespaceWatchOptions) (chan resources.ResourceNamespaceList, <-chan error, error) { - wopts := clients.WatchOpts{FieldSelectors: opts.FieldSelectors} + wopts := clients.WatchOpts{FieldSelectors: opts.FieldSelectors, ExpressionSelector: opts.ExpressionSelectors} // todo look that the namespace implementation to know exacactly what the channel of errors is returning. namespaceChan, errorChan, err := kns.namespace.Watch(wopts) if err != nil { diff --git a/pkg/api/v1/clients/common/common.go b/pkg/api/v1/clients/common/common.go index ab4eab816..0729b2f82 100644 --- a/pkg/api/v1/clients/common/common.go +++ b/pkg/api/v1/clients/common/common.go @@ -38,8 +38,9 @@ func KubeResourceWatch(cache cache.Cache, listFunc ResourceListFunc, namespace s var previous *resources.ResourceList updateResourceList := func() { list, err := listFunc(namespace, clients.ListOpts{ - Ctx: opts.Ctx, - Selector: opts.Selector, + Ctx: opts.Ctx, + Selector: opts.Selector, + ExpressionSelector: opts.ExpressionSelector, }) if err != nil { errs <- err diff --git a/pkg/api/v1/clients/kube/resource_client.go b/pkg/api/v1/clients/kube/resource_client.go index 3b1d85c51..6cdccfed4 100644 --- a/pkg/api/v1/clients/kube/resource_client.go +++ b/pkg/api/v1/clients/kube/resource_client.go @@ -23,7 +23,6 @@ import ( "go.opencensus.io/tag" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" ) var ( @@ -296,7 +295,7 @@ func (rc *ResourceClient) List(namespace string, opts clients.ListOpts) (resourc return nil, err } - labelSelector, err := rc.getLabelSelector(opts) + labelSelector, err := clients.GetLabelSelector(opts) if err != nil { return nil, errors.Wrapf(err, "parsing label selector") } @@ -414,16 +413,6 @@ func (rc *ResourceClient) Watch(namespace string, opts clients.WatchOpts) (<-cha return resourcesChan, errs, nil } -func (rc *ResourceClient) getLabelSelector(listOpts clients.ListOpts) (labels.Selector, error) { - // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement - if listOpts.ExpressionSelector != "" { - return labels.Parse(listOpts.ExpressionSelector) - } - - // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement - return labels.SelectorFromSet(listOpts.Selector), nil -} - // Checks whether the group version kind of the given resource matches that of the client's underlying CRD: func (rc *ResourceClient) matchesClientGVK(resource v1.Resource) bool { return resource.GroupVersionKind().String() == rc.crd.GroupVersionKind().String() diff --git a/pkg/api/v1/clients/options.go b/pkg/api/v1/clients/options.go new file mode 100644 index 000000000..c48312f41 --- /dev/null +++ b/pkg/api/v1/clients/options.go @@ -0,0 +1,15 @@ +package clients + +import ( + "k8s.io/apimachinery/pkg/labels" +) + +func GetLabelSelector(listOpts ListOpts) (labels.Selector, error) { + // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement + if listOpts.ExpressionSelector != "" { + return labels.Parse(listOpts.ExpressionSelector) + } + + // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement + return labels.SelectorFromSet(listOpts.Selector), nil +} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 1f1d36a8f..570c23227 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -323,7 +323,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- range .Resources }} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ upper_camel .Name }} */ - {clien + { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") From c82925416be0db292b244acb9c5cd4b7ec7c4569 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Fri, 26 Aug 2022 11:04:31 -0500 Subject: [PATCH 15/98] got it passing tests for namespaced labels --- .../namespace/resource_namespace.go | 5 +- pkg/api/v1/clients/client_interface.go | 7 + .../templates/snapshot_emitter_template.go | 2 +- .../snapshot_emitter_test_template.go | 130 +++-- .../v1/kubeconfigs_snapshot_emitter_test.go | 116 ++-- test/mocks/v1/testing_snapshot_emitter.sk.go | 8 +- .../mocks/v1/testing_snapshot_emitter_test.go | 518 +++++++++--------- .../v1alpha1/testing_snapshot_emitter.sk.go | 13 +- .../v1alpha1/testing_snapshot_emitter_test.go | 114 ++-- .../v2alpha1/testing_snapshot_emitter.sk.go | 5 +- .../v2alpha1/testing_snapshot_emitter_test.go | 268 +++++---- 11 files changed, 671 insertions(+), 515 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 41f49941f..9da4d1156 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -26,13 +26,10 @@ type kubeResourceNamespaceLister struct { // GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(ctx context.Context, opts resources.ResourceNamespaceListOptions) (resources.ResourceNamespaceList, error) { - clopts := clients.ListOpts{FieldSelectors: opts.FieldSelectors} - namespaces, err := kns.namespace.List(clopts) + namespaces, err := kns.namespace.List(clients.TranslateResourceNamespaceListToListOptions(opts)) if err != nil { return nil, err } - // convert - return convertNamespaceListToResourceNamespace(namespaces), nil } diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 4dbdb1f19..0bea11de8 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -182,3 +182,10 @@ func (o WatchOpts) WithDefaults() WatchOpts { } return o } + +// TODO-JAKE maybe they should be the same type of options? +// TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options +func TranslateResourceNamespaceListToListOptions(rnlo resources.ResourceNamespaceListOptions) ListOpts { + clopts := ListOpts{FieldSelectors: rnlo.FieldSelectors, ExpressionSelector: rnlo.ExpressionSelectors} + return clopts +} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 570c23227..e40f667da 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -214,7 +214,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} - if ! watchNamespacesIsEmpty { + if ! watchNamespacesIsEmpty || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index f4c8916aa..61f16ecf8 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -42,6 +42,8 @@ import ( kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" "k8s.io/client-go/rest" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // Needed to run tests in GKE _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" @@ -92,6 +94,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) + // add namespaces to created list for clean up for _,ns := range namespaces { if _,hit := createdNamespaces[ns]; !hit { createdNamespaces[ns] = true @@ -99,6 +102,35 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } + createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { + _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: labels, + }, + }, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + // add namespace to created list for clean up + if _, hit := createdNamespaces[namespace]; !hit { + createdNamespaces[namespace] = true + } + } + + deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { + // clean up your local environment + namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + Expect(err).ToNot(HaveOccurred()) + defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} + var namespacesToDelete []string + for _,ns := range namespaces.Items { + if _, hit := defaultNamespaces[ns.Name]; ! hit{ + namespacesToDelete = append(namespacesToDelete, ns.Name) + } + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) + Expect(err).ToNot(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -117,6 +149,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) {{- if $need_kube_config }} @@ -282,7 +316,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end}} }) - It("should be able to track resources that are labeled on other namespaces", func() { + It("should be able to track all resources that are on labeled namespaces", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) @@ -401,10 +435,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) {{- if .ClusterScoped }} + // TODO need to fix clusterScoped {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) @@ -415,8 +451,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b } - assertNoMessageSent() + watched = append(watched, {{ lower_camel .Name }}4a) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } + assertSnapshotMocks(watched, notWatched) {{- end }} @@ -434,7 +471,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) + watched = append(watched, {{ lower_camel .Name }}5a) + notWatched = append(notWatched, {{ lower_camel .Name }}5b) assertSnapshotMocks(watched, notWatched) {{- end }} @@ -452,12 +490,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) - assertNoMessageSent() + watched = append(watched, {{ lower_camel .Name }}6a) + notWatched = append(notWatched, {{ lower_camel .Name }}6b) + assertSnapshotMocks(watched, notWatched) {{- end }} - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) {{- if .ClusterScoped }} @@ -472,8 +512,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) - assertSnapshotMocks(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}7a) + notWatched = append(notWatched, {{ lower_camel .Name }}7b) + assertSnapshotMocks(watched, notWatched) {{- end }} @@ -486,9 +527,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) assertNoMessageSent() @@ -516,7 +557,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- end }} @@ -531,12 +572,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- end }} @@ -556,7 +597,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- end }} @@ -571,12 +612,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5b.GetMetadata().Namespace, {{ lower_camel .Name }}5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- end }} @@ -590,11 +631,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7b.GetMetadata().Namespace, {{ lower_camel .Name }}7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b}...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) assertSnapshot{{ .PluralName }}(nil, notWatched) {{- end }} @@ -798,6 +839,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} + // TODO need to add in ClusterScoped {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } @@ -814,7 +856,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) {{- if .ClusterScoped }} @@ -829,8 +872,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } - assertNoMatchingMocks() + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b} + assertSnapshotMocks(watched, notWatched) {{- end }} @@ -847,8 +890,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b } - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + assertNoMatchingMocks() {{- end }} @@ -874,9 +917,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) assertNoMatchingMocks() {{- else }} @@ -903,8 +946,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b }...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) + assertNoMessageSent() {{- end }} @@ -942,12 +985,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- end }} @@ -966,28 +1009,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4b.GetMetadata().Namespace, {{ lower_camel .Name }}4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}(nil, notWatched) -{{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6b.GetMetadata().Namespace, {{ lower_camel .Name }}6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) - assertSnapshot{{ .PluralName }}(nil, notWatched) {{- end }} {{- end }} }) }) + // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot }) `)) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 96dac19d6..c4419e207 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -9,6 +9,8 @@ import ( "os" "time" + "github.com/solo-io/solo-kit/api/multicluster/v1" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" @@ -21,6 +23,8 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" // Needed to run tests in GKE @@ -64,6 +68,7 @@ var _ = Describe("V1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) + // add namespaces to created list for clean up for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { createdNamespaces[ns] = true @@ -71,6 +76,35 @@ var _ = Describe("V1Emitter", func() { } } + createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { + _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: labels, + }, + }, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + // add namespace to created list for clean up + if _, hit := createdNamespaces[namespace]; !hit { + createdNamespaces[namespace] = true + } + } + + deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { + // clean up your local environment + namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + Expect(err).ToNot(HaveOccurred()) + defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} + var namespacesToDelete []string + for _, ns := range namespaces.Items { + if _, hit := defaultNamespaces[ns.Name]; !hit { + namespacesToDelete = append(namespacesToDelete, ns.Name) + } + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) + Expect(err).ToNot(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -89,6 +123,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) // KubeConfig Constructor @@ -184,7 +220,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) }) - It("should be able to track resources that are labeled on other namespaces", func() { + It("should be able to track all resources that are on labeled namespaces", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) @@ -264,40 +300,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := KubeConfigList{kubeConfig4a, kubeConfig4b} - assertNoMessageSent() + watched = append(watched, kubeConfig4a) + notWatched := KubeConfigList{kubeConfig4b} + assertSnapshotMocks(watched, notWatched) kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + watched = append(watched, kubeConfig5a) + notWatched = append(notWatched, kubeConfig5b) assertSnapshotMocks(watched, notWatched) kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) - assertNoMessageSent() + watched = append(watched, kubeConfig6a) + notWatched = append(notWatched, kubeConfig6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + watched = append(watched, kubeConfig7a) + notWatched = append(notWatched, kubeConfig7b) assertSnapshotMocks(watched, notWatched) - kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) assertNoMessageSent() @@ -313,15 +355,15 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) - watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} assertSnapshotkubeconfigs(watched, notWatched) - err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} assertSnapshotkubeconfigs(watched, notWatched) err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -329,22 +371,22 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - watched = KubeConfigList{kubeConfig5a, kubeConfig5b, kubeConfig7a, kubeConfig7b} + watched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} assertSnapshotkubeconfigs(watched, notWatched) - err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig5b.GetMetadata().Namespace, kubeConfig5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) - watched = KubeConfigList{kubeConfig7a, kubeConfig7b} + watched = KubeConfigList{kubeConfig6a, kubeConfig7a} assertSnapshotkubeconfigs(watched, notWatched) - err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig7b.GetMetadata().Namespace, kubeConfig7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) assertSnapshotkubeconfigs(nil, notWatched) }) }) @@ -504,21 +546,22 @@ var _ = Describe("V1Emitter", func() { notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = KubeConfigList{kubeConfig2a, kubeConfig2b} - assertNoMatchingMocks() + watched := KubeConfigList{kubeConfig2a, kubeConfig2b} + assertSnapshotMocks(watched, notWatched) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := KubeConfigList{kubeConfig3a, kubeConfig3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + assertNoMatchingMocks() kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -540,8 +583,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) + assertNoMessageSent() kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -556,12 +599,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - watched = KubeConfigList{kubeConfig4a, kubeConfig4b, kubeConfig6a, kubeConfig6b} + notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + watched = KubeConfigList{kubeConfig4a, kubeConfig4b} assertSnapshotkubeconfigs(watched, notWatched) err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -569,15 +612,8 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig4b.GetMetadata().Namespace, kubeConfig4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) - watched = KubeConfigList{kubeConfig6a, kubeConfig6b} - assertSnapshotkubeconfigs(watched, notWatched) - - err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig6b.GetMetadata().Namespace, kubeConfig6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) assertSnapshotkubeconfigs(nil, notWatched) }) }) + // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot }) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index ae6530da4..0bc0d2dec 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -265,7 +265,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mctsByNamespace := sync.Map{} podsByNamespace := sync.Map{} - if !watchNamespacesIsEmpty { + if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces @@ -504,7 +504,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for SimpleMockResource */ { - clien simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") @@ -524,7 +523,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockResource */ { - clien mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") @@ -544,7 +542,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - clien fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") @@ -564,7 +561,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for AnotherMockResource */ { - clien anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") @@ -584,7 +580,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockCustomType */ { - clien mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockCustomType list") @@ -604,7 +599,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for Pod */ { - clien pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial Pod list") diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 4034e2a75..755a9c80f 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -24,7 +24,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/test/helpers" + v1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -107,6 +109,7 @@ var _ = Describe("V1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) + // add namespaces to created list for clean up for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { createdNamespaces[ns] = true @@ -114,6 +117,35 @@ var _ = Describe("V1Emitter", func() { } } + createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { + _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: labels, + }, + }, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + // add namespace to created list for clean up + if _, hit := createdNamespaces[namespace]; !hit { + createdNamespaces[namespace] = true + } + } + + deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { + // clean up your local environment + namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + Expect(err).ToNot(HaveOccurred()) + defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} + var namespacesToDelete []string + for _, ns := range namespaces.Items { + if _, hit := defaultNamespaces[ns.Name]; !hit { + namespacesToDelete = append(namespacesToDelete, ns.Name) + } + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) + Expect(err).ToNot(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -132,6 +164,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") @@ -623,7 +657,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) }) - It("should be able to track resources that are labeled on other namespaces", func() { + It("should be able to track all resources that are on labeled namespaces", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) @@ -703,40 +737,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} - assertNoMessageSent() + watched = append(watched, simpleMockResource4a) + notWatched := SimpleMockResourceList{simpleMockResource4b} + assertSnapshotMocks(watched, notWatched) simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + watched = append(watched, simpleMockResource5a) + notWatched = append(notWatched, simpleMockResource5b) assertSnapshotMocks(watched, notWatched) simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) - assertNoMessageSent() + watched = append(watched, simpleMockResource6a) + notWatched = append(notWatched, simpleMockResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + watched = append(watched, simpleMockResource7a) + notWatched = append(notWatched, simpleMockResource7b) assertSnapshotMocks(watched, notWatched) - simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) assertNoMessageSent() @@ -752,15 +792,15 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) - watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} assertSnapshotSimplemocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} assertSnapshotSimplemocks(watched, notWatched) err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -768,22 +808,22 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - watched = SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b, simpleMockResource7a, simpleMockResource7b} + watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} assertSnapshotSimplemocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource5b.GetMetadata().Namespace, simpleMockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) - watched = SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b} + watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} assertSnapshotSimplemocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource7b.GetMetadata().Namespace, simpleMockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) assertSnapshotSimplemocks(nil, notWatched) /* @@ -837,40 +877,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource4a, mockResource4b} - assertNoMessageSent() + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) assertSnapshotMocks(watched, notWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) assertSnapshotMocks(watched, notWatched) - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() @@ -886,15 +932,15 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -902,22 +948,22 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource7a, mockResource7b} + watched = MockResourceList{mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, notWatched) /* @@ -971,40 +1017,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, FakeResourceList{fakeResource3a, fakeResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) fakeResource4a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FakeResourceList{fakeResource4a, fakeResource4b} - assertNoMessageSent() + watched = append(watched, fakeResource4a) + notWatched := FakeResourceList{fakeResource4b} + assertSnapshotMocks(watched, notWatched) fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = append(watched, fakeResource5a) + notWatched = append(notWatched, fakeResource5b) assertSnapshotMocks(watched, notWatched) fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) - assertNoMessageSent() + watched = append(watched, fakeResource6a) + notWatched = append(notWatched, fakeResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource7a, fakeResource7b}...) + watched = append(watched, fakeResource7a) + notWatched = append(notWatched, fakeResource7b) assertSnapshotMocks(watched, notWatched) - fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) assertNoMessageSent() @@ -1020,15 +1072,15 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1036,22 +1088,22 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = FakeResourceList{fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource5b.GetMetadata().Namespace, fakeResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = FakeResourceList{fakeResource7a, fakeResource7b} + watched = FakeResourceList{fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7b.GetMetadata().Namespace, fakeResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) + notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, notWatched) /* @@ -1105,40 +1157,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} - assertNoMessageSent() + watched = append(watched, anotherMockResource4a) + notWatched := AnotherMockResourceList{anotherMockResource4b} + assertSnapshotMocks(watched, notWatched) anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + watched = append(watched, anotherMockResource5a) + notWatched = append(notWatched, anotherMockResource5b) assertSnapshotMocks(watched, notWatched) anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) - assertNoMessageSent() + watched = append(watched, anotherMockResource6a) + notWatched = append(notWatched, anotherMockResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + watched = append(watched, anotherMockResource7a) + notWatched = append(notWatched, anotherMockResource7b) assertSnapshotMocks(watched, notWatched) - anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) assertNoMessageSent() @@ -1154,15 +1212,15 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) - watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} assertSnapshotAnothermockresources(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} assertSnapshotAnothermockresources(watched, notWatched) err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1170,22 +1228,22 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - watched = AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b, anotherMockResource7a, anotherMockResource7b} + watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} assertSnapshotAnothermockresources(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource5b.GetMetadata().Namespace, anotherMockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) - watched = AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b} + watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} assertSnapshotAnothermockresources(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource7b.GetMetadata().Namespace, anotherMockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) assertSnapshotAnothermockresources(nil, notWatched) /* @@ -1231,8 +1289,10 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, ClusterResourceList{clusterResource3a}...) assertSnapshotClusterresources(watched, notWatched) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + // TODO need to fix clusterScoped clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, ClusterResourceList{clusterResource4a}...) @@ -1248,7 +1308,8 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, ClusterResourceList{clusterResource6a}...) assertSnapshotClusterresources(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1346,40 +1407,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockCustomTypeList{mockCustomType4a, mockCustomType4b} - assertNoMessageSent() + watched = append(watched, mockCustomType4a) + notWatched := MockCustomTypeList{mockCustomType4b} + assertSnapshotMocks(watched, notWatched) mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + watched = append(watched, mockCustomType5a) + notWatched = append(notWatched, mockCustomType5b) assertSnapshotMocks(watched, notWatched) mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) - assertNoMessageSent() + watched = append(watched, mockCustomType6a) + notWatched = append(notWatched, mockCustomType6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + watched = append(watched, mockCustomType7a) + notWatched = append(notWatched, mockCustomType7b) assertSnapshotMocks(watched, notWatched) - mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) assertNoMessageSent() @@ -1395,15 +1462,15 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) - watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} assertSnapshotmcts(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} assertSnapshotmcts(watched, notWatched) err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1411,22 +1478,22 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - watched = MockCustomTypeList{mockCustomType5a, mockCustomType5b, mockCustomType7a, mockCustomType7b} + watched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} assertSnapshotmcts(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType5b.GetMetadata().Namespace, mockCustomType5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) - watched = MockCustomTypeList{mockCustomType7a, mockCustomType7b} + watched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} assertSnapshotmcts(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType7b.GetMetadata().Namespace, mockCustomType7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) assertSnapshotmcts(nil, notWatched) /* @@ -1480,40 +1547,46 @@ var _ = Describe("V1Emitter", func() { watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} - assertNoMessageSent() + watched = append(watched, pod4a) + notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} + assertSnapshotMocks(watched, notWatched) pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + watched = append(watched, pod5a) + notWatched = append(notWatched, pod5b) assertSnapshotMocks(watched, notWatched) pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) - assertNoMessageSent() + watched = append(watched, pod6a) + notWatched = append(notWatched, pod6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + watched = append(watched, pod7a) + notWatched = append(notWatched, pod7b) assertSnapshotMocks(watched, notWatched) - pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) assertNoMessageSent() @@ -1529,15 +1602,15 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod5a, pod5b, pod7a, pod7b} + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} assertSnapshotpods(watched, notWatched) - err = podClient.Delete(pod3a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod5a, pod5b, pod7a, pod7b} + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} assertSnapshotpods(watched, notWatched) err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1545,22 +1618,22 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b, pod7a, pod7b} + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} assertSnapshotpods(watched, notWatched) - err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod5b.GetMetadata().Namespace, pod5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b} + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} assertSnapshotpods(watched, notWatched) - err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod7b.GetMetadata().Namespace, pod7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) assertSnapshotpods(nil, notWatched) }) }) @@ -2052,21 +2125,22 @@ var _ = Describe("V1Emitter", func() { notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} - assertNoMatchingMocks() + watched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} + assertSnapshotMocks(watched, notWatched) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + assertNoMatchingMocks() simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2088,8 +2162,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) + assertNoMessageSent() simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2104,12 +2178,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b, simpleMockResource6a, simpleMockResource6b} + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} assertSnapshotSimplemocks(watched, notWatched) err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2117,14 +2191,6 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource4b.GetMetadata().Namespace, simpleMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) - watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b} - assertSnapshotSimplemocks(watched, notWatched) - - err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource6b.GetMetadata().Namespace, simpleMockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) assertSnapshotSimplemocks(nil, notWatched) /* @@ -2164,21 +2230,22 @@ var _ = Describe("V1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = MockResourceList{mockResource2a, mockResource2b} - assertNoMatchingMocks() + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource3a, mockResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertNoMatchingMocks() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2200,8 +2267,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2216,12 +2283,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource4a, mockResource4b} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2229,14 +2296,6 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - watched = MockResourceList{mockResource6a, mockResource6b} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) assertSnapshotMocks(nil, notWatched) /* @@ -2276,21 +2335,22 @@ var _ = Describe("V1Emitter", func() { notWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = FakeResourceList{fakeResource2a, fakeResource2b} - assertNoMatchingMocks() + watched := FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(watched, notWatched) fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FakeResourceList{fakeResource3a, fakeResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + assertNoMatchingMocks() fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2312,8 +2372,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource6a, fakeResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) + assertNoMessageSent() fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2328,12 +2388,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = FakeResourceList{fakeResource4a, fakeResource4b, fakeResource6a, fakeResource6b} + notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = FakeResourceList{fakeResource4a, fakeResource4b} assertSnapshotFakes(watched, notWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2341,14 +2401,6 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) - watched = FakeResourceList{fakeResource6a, fakeResource6b} - assertSnapshotFakes(watched, notWatched) - - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource6b.GetMetadata().Namespace, fakeResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) assertSnapshotFakes(nil, notWatched) /* @@ -2388,21 +2440,22 @@ var _ = Describe("V1Emitter", func() { notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} - assertNoMatchingMocks() + watched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} + assertSnapshotMocks(watched, notWatched) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + assertNoMatchingMocks() anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2424,8 +2477,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) + assertNoMessageSent() anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2440,12 +2493,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b, anotherMockResource6a, anotherMockResource6b} + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} assertSnapshotAnothermockresources(watched, notWatched) err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2453,14 +2506,6 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource4b.GetMetadata().Namespace, anotherMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) - watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b} - assertSnapshotAnothermockresources(watched, notWatched) - - err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource6b.GetMetadata().Namespace, anotherMockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) assertSnapshotAnothermockresources(nil, notWatched) /* @@ -2491,12 +2536,14 @@ var _ = Describe("V1Emitter", func() { } } + // TODO need to add in ClusterScoped clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := ClusterResourceList{clusterResource1a} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2515,9 +2562,9 @@ var _ = Describe("V1Emitter", func() { createNamespaces(ctx, kube, namespace5, namespace6) - clusterResource6a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) + notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) assertNoMatchingMocks() clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) @@ -2546,10 +2593,6 @@ var _ = Describe("V1Emitter", func() { watched = ClusterResourceList{clusterResource6a} assertSnapshotClusterresources(watched, notWatched) - err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(nil, notWatched) - /* MockCustomType */ @@ -2587,21 +2630,22 @@ var _ = Describe("V1Emitter", func() { notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = MockCustomTypeList{mockCustomType2a, mockCustomType2b} - assertNoMatchingMocks() + watched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} + assertSnapshotMocks(watched, notWatched) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockCustomTypeList{mockCustomType3a, mockCustomType3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + assertNoMatchingMocks() mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2623,8 +2667,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) + assertNoMessageSent() mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2639,12 +2683,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - watched = MockCustomTypeList{mockCustomType4a, mockCustomType4b, mockCustomType6a, mockCustomType6b} + notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + watched = MockCustomTypeList{mockCustomType4a, mockCustomType4b} assertSnapshotmcts(watched, notWatched) err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2652,14 +2696,6 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType4b.GetMetadata().Namespace, mockCustomType4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) - watched = MockCustomTypeList{mockCustomType6a, mockCustomType6b} - assertSnapshotmcts(watched, notWatched) - - err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType6b.GetMetadata().Namespace, mockCustomType6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) assertSnapshotmcts(nil, notWatched) /* @@ -2699,21 +2735,22 @@ var _ = Describe("V1Emitter", func() { notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} - assertNoMatchingMocks() + watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} + assertSnapshotMocks(watched, notWatched) pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + assertNoMatchingMocks() pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2735,8 +2772,8 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) + assertNoMessageSent() pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2751,12 +2788,12 @@ var _ = Describe("V1Emitter", func() { } assertNoMessageSent() - err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b, pod6a, pod6b} + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} assertSnapshotpods(watched, notWatched) err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -2764,15 +2801,8 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod4b.GetMetadata().Namespace, pod4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b} - assertSnapshotpods(watched, notWatched) - - err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod6b.GetMetadata().Namespace, pod6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) assertSnapshotpods(nil, notWatched) }) }) + // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 5b7018c08..fe842db94 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -162,7 +162,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot := TestingSnapshot{} mocksByNamespace := sync.Map{} - if !watchNamespacesIsEmpty { + if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces @@ -217,10 +217,12 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') + if ns != "" { + buffer.WriteString("metadata.name!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } } } excludeNamespacesFieldDesciptors = buffer.String() @@ -261,7 +263,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for MockResource */ { - clien mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 60588905b..9cd34970a 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -21,7 +21,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" + v1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -68,6 +70,7 @@ var _ = Describe("V1Alpha1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) + // add namespaces to created list for clean up for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { createdNamespaces[ns] = true @@ -75,6 +78,35 @@ var _ = Describe("V1Alpha1Emitter", func() { } } + createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { + _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: labels, + }, + }, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + // add namespace to created list for clean up + if _, hit := createdNamespaces[namespace]; !hit { + createdNamespaces[namespace] = true + } + } + + deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { + // clean up your local environment + namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + Expect(err).ToNot(HaveOccurred()) + defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} + var namespacesToDelete []string + for _, ns := range namespaces.Items { + if _, hit := defaultNamespaces[ns.Name]; !hit { + namespacesToDelete = append(namespacesToDelete, ns.Name) + } + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) + Expect(err).ToNot(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -93,6 +125,8 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") @@ -198,7 +232,7 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) }) - It("should be able to track resources that are labeled on other namespaces", func() { + It("should be able to track all resources that are on labeled namespaces", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) @@ -278,40 +312,46 @@ var _ = Describe("V1Alpha1Emitter", func() { watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource4a, mockResource4b} - assertNoMessageSent() + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) assertSnapshotMocks(watched, notWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) assertSnapshotMocks(watched, notWatched) - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() @@ -327,15 +367,15 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -343,22 +383,22 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource7a, mockResource7b} + watched = MockResourceList{mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, notWatched) }) }) @@ -518,21 +558,22 @@ var _ = Describe("V1Alpha1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = MockResourceList{mockResource2a, mockResource2b} - assertNoMatchingMocks() + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource3a, mockResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertNoMatchingMocks() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -554,8 +595,8 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -570,12 +611,12 @@ var _ = Describe("V1Alpha1Emitter", func() { } assertNoMessageSent() - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource4a, mockResource4b} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -583,15 +624,8 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - watched = MockResourceList{mockResource6a, mockResource6b} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) assertSnapshotMocks(nil, notWatched) }) }) + // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index b710dcb91..65f985b3b 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -200,7 +200,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fcarsByNamespace := sync.Map{} fakesByNamespace := sync.Map{} - if !watchNamespacesIsEmpty { + if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces @@ -355,7 +355,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for MockResource */ { - clien mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial MockResource list") @@ -375,7 +374,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ { - clien fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") @@ -395,7 +393,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource */ { - clien fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial FakeResource list") diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index ccbc442a5..caa24b01d 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -24,7 +24,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" + v1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -83,6 +85,7 @@ var _ = Describe("V2Alpha1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) + // add namespaces to created list for clean up for _, ns := range namespaces { if _, hit := createdNamespaces[ns]; !hit { createdNamespaces[ns] = true @@ -90,6 +93,35 @@ var _ = Describe("V2Alpha1Emitter", func() { } } + createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { + _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + Labels: labels, + }, + }, metav1.CreateOptions{}) + Expect(err).ToNot(HaveOccurred()) + // add namespace to created list for clean up + if _, hit := createdNamespaces[namespace]; !hit { + createdNamespaces[namespace] = true + } + } + + deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { + // clean up your local environment + namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + Expect(err).ToNot(HaveOccurred()) + defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} + var namespacesToDelete []string + for _, ns := range namespaces.Items { + if _, hit := defaultNamespaces[ns.Name]; !hit { + namespacesToDelete = append(namespacesToDelete, ns.Name) + } + } + err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) + Expect(err).ToNot(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -108,6 +140,8 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) Expect(err).NotTo(HaveOccurred()) cfg, err = kubeutils.GetConfig("", "") @@ -341,7 +375,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) }) - It("should be able to track resources that are labeled on other namespaces", func() { + It("should be able to track all resources that are on labeled namespaces", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) @@ -421,40 +455,46 @@ var _ = Describe("V2Alpha1Emitter", func() { watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource4a, mockResource4b} - assertNoMessageSent() + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource5a, mockResource5b}...) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) assertSnapshotMocks(watched, notWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource7a, mockResource7b}...) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) assertSnapshotMocks(watched, notWatched) - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() @@ -470,15 +510,15 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -486,22 +526,22 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource5a, mockResource5b, mockResource7a, mockResource7b} + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5b.GetMetadata().Namespace, mockResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource7a, mockResource7b} + watched = MockResourceList{mockResource6a, mockResource7a} assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7b.GetMetadata().Namespace, mockResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, notWatched) /* @@ -555,40 +595,46 @@ var _ = Describe("V2Alpha1Emitter", func() { watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} - assertNoMessageSent() + watched = append(watched, frequentlyChangingAnnotationsResource4a) + notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} + assertSnapshotMocks(watched, notWatched) frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + watched = append(watched, frequentlyChangingAnnotationsResource5a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource5b) assertSnapshotMocks(watched, notWatched) frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) - assertNoMessageSent() + watched = append(watched, frequentlyChangingAnnotationsResource6a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + watched = append(watched, frequentlyChangingAnnotationsResource7a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource7b) assertSnapshotMocks(watched, notWatched) - frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) assertNoMessageSent() @@ -604,15 +650,15 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} assertSnapshotFcars(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} assertSnapshotFcars(watched, notWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -620,22 +666,22 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b, frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} assertSnapshotFcars(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b} + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} assertSnapshotFcars(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) assertSnapshotFcars(nil, notWatched) /* @@ -689,40 +735,46 @@ var _ = Describe("V2Alpha1Emitter", func() { watched = append(watched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} - assertNoMessageSent() + watched = append(watched, fakeResource4a) + notWatched := testing_solo_io.FakeResourceList{fakeResource4b} + assertSnapshotMocks(watched, notWatched) fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = append(watched, fakeResource5a) + notWatched = append(notWatched, fakeResource5b) assertSnapshotMocks(watched, notWatched) fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) - assertNoMessageSent() + watched = append(watched, fakeResource6a) + notWatched = append(notWatched, fakeResource6b) + assertSnapshotMocks(watched, notWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + watched = append(watched, fakeResource7a) + notWatched = append(notWatched, fakeResource7b) assertSnapshotMocks(watched, notWatched) - fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) assertNoMessageSent() @@ -738,15 +790,15 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -754,22 +806,22 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b, fakeResource7a, fakeResource7b} + watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource5b.GetMetadata().Namespace, fakeResource5b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b} + watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7b.GetMetadata().Namespace, fakeResource7b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, notWatched) }) }) @@ -1043,21 +1095,22 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = MockResourceList{mockResource2a, mockResource2b} - assertNoMatchingMocks() + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource3a, mockResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertNoMatchingMocks() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1079,8 +1132,8 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource6a, mockResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1095,12 +1148,12 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMessageSent() - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource4b, mockResource6a, mockResource6b} + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource4a, mockResource4b} assertSnapshotMocks(watched, notWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1108,14 +1161,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - watched = MockResourceList{mockResource6a, mockResource6b} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource6b.GetMetadata().Namespace, mockResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) assertSnapshotMocks(nil, notWatched) /* @@ -1155,21 +1200,22 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} - assertNoMatchingMocks() + watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(watched, notWatched) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + assertNoMatchingMocks() frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1191,8 +1237,8 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) + assertNoMessageSent() frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1207,12 +1253,12 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMessageSent() - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b} + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} assertSnapshotFcars(watched, notWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1220,14 +1266,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b} - assertSnapshotFcars(watched, notWatched) - - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) assertSnapshotFcars(nil, notWatched) /* @@ -1267,21 +1305,22 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertNoMatchingMocks() - createNamespaces(ctx, kube, namespace3, namespace4) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} - assertNoMatchingMocks() + watched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(watched, notWatched) fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b} - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + assertNoMatchingMocks() fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1303,8 +1342,8 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) - assertSnapshotMocks(watched, notWatched) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) + assertNoMessageSent() fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1319,12 +1358,12 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMessageSent() - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b, fakeResource6a, fakeResource6b} + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} assertSnapshotFakes(watched, notWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1332,15 +1371,8 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) - watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b} - assertSnapshotFakes(watched, notWatched) - - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource6b.GetMetadata().Namespace, fakeResource6b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) assertSnapshotFakes(nil, notWatched) }) }) + // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot }) From b2ce0cfab7cfb0111b73f0adbe70a7668bf4a0ad Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Fri, 26 Aug 2022 12:09:26 -0500 Subject: [PATCH 16/98] snapshot does not contain deleted namespace resources --- .../templates/snapshot_emitter_template.go | 10 +- .../snapshot_emitter_test_template.go | 245 ++++++- .../v1/kubeconfigs_snapshot_emitter.sk.go | 3 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 197 ++++- .../mocks/v1/testing_snapshot_emitter_test.go | 670 +++++++++++++++++- .../v1alpha1/testing_snapshot_emitter_test.go | 197 ++++- .../v2alpha1/testing_snapshot_emitter_test.go | 355 +++++++++- 7 files changed, 1541 insertions(+), 136 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index e40f667da..aedd6b74c 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -278,10 +278,12 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields var buffer bytes.Buffer for i, ns := range watchNamespaces { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') + if ns != "" { + buffer.WriteString("metadata.name!=") + buffer.WriteString(ns) + if i < len(watchNamespaces)-1 { + buffer.WriteByte(',') + } } } excludeNamespacesFieldDesciptors = buffer.String() diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 61f16ecf8..ba7086dee 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -62,7 +62,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string - createdNamespaces map[string]bool name1, name2 = "angela"+helpers.RandString(3), "bob"+helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -94,12 +93,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) - // add namespaces to created list for clean up - for _,ns := range namespaces { - if _,hit := createdNamespaces[ns]; !hit { - createdNamespaces[ns] = true - } - } } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { @@ -110,10 +103,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func }, }, metav1.CreateOptions{}) Expect(err).ToNot(HaveOccurred()) - // add namespace to created list for clean up - if _, hit := createdNamespaces[namespace]; !hit { - createdNamespaces[namespace] = true - } } deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { @@ -131,12 +120,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).ToNot(HaveOccurred()) } + deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() - createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -189,12 +182,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - namespacesToDelete := []string{} - for namespace,_ := range createdNamespaces { - namespacesToDelete = append(namespacesToDelete, namespace) - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).NotTo(HaveOccurred()) + deleteNonDefaultKubeNamespaces(ctx, kube) {{- range .Resources }} {{- if .ClusterScoped }} @@ -783,7 +771,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } - assertNoMatchingMocks := func() { + assertNoMocksSent := func() { drain: for { select { @@ -843,7 +831,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMatchingMocks() + assertNoMocksSent() {{- else }} @@ -852,7 +840,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertNoMatchingMocks() + assertNoMocksSent() {{- end }} @@ -864,7 +852,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }...) - assertNoMatchingMocks() + assertNoMocksSent() {{- else }} @@ -891,7 +879,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - assertNoMatchingMocks() + assertNoMocksSent() {{- end }} @@ -920,7 +908,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - assertNoMatchingMocks() + assertNoMocksSent() {{- else }} @@ -1015,7 +1003,214 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} }) }) - // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot + + Context("Tracking resources on namespaces that are deleted", func () { + It("Should not contain resources from a deleted namespace", func () { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, notWatched) + }) + + It("Should not contain resources from a deleted namespace, that is filtered", func () { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } +{{- range .Resources }} + + /* + {{ .Name }} + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + +{{- if .ClusterScoped }} + + // TODO need to add in ClusterScoped + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMocksSent() + +{{- else }} + + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertNoMocksSent() + +{{- end }} + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + +{{- if .ClusterScoped }} + + // TODO need to add in ClusterScoped + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMocksSent() + +{{- else }} + + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } + assertSnapshotMocks(watched, notWatched) + +{{- end }} + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, {{ lower_camel .Name }}2a) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + +{{- if .ClusterScoped }} + + // TODO need to add in ClusterScoped + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMocksSent() + +{{- else }} + + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, {{ lower_camel .Name }}3a) + assertSnapshotMocks(watched, notWatched) + +{{- end }} + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, {{ lower_camel .Name }}2b) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} + assertSnapshotMocks(watched, notWatched) +{{- end }} + }) + }) + }) `)) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index a3008051e..4c07bbf1b 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -162,7 +162,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa currentSnapshot := KubeconfigsSnapshot{} kubeconfigsByNamespace := sync.Map{} - if !watchNamespacesIsEmpty { + if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces @@ -261,7 +261,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa for _, namespace := range allOtherNamespaces { /* Setup namespaced watch for KubeConfig */ { - clien kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { return nil, nil, errors.Wrapf(err, "initial KubeConfig list") diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index c4419e207..11cfc139c 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -44,7 +44,6 @@ var _ = Describe("V1Emitter", func() { namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string - createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -68,12 +67,6 @@ var _ = Describe("V1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) - // add namespaces to created list for clean up - for _, ns := range namespaces { - if _, hit := createdNamespaces[ns]; !hit { - createdNamespaces[ns] = true - } - } } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { @@ -84,10 +77,6 @@ var _ = Describe("V1Emitter", func() { }, }, metav1.CreateOptions{}) Expect(err).ToNot(HaveOccurred()) - // add namespace to created list for clean up - if _, hit := createdNamespaces[namespace]; !hit { - createdNamespaces[namespace] = true - } } deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { @@ -105,12 +94,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } + deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() - createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -140,12 +133,7 @@ var _ = Describe("V1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - namespacesToDelete := []string{} - for namespace, _ := range createdNamespaces { - namespacesToDelete = append(namespacesToDelete, namespace) - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).NotTo(HaveOccurred()) + deleteNonDefaultKubeNamespaces(ctx, kube) }) Context("Tracking watched namespaces", func() { @@ -491,7 +479,7 @@ var _ = Describe("V1Emitter", func() { } } - assertNoMatchingMocks := func() { + assertNoMocksSent := func() { drain: for { select { @@ -544,7 +532,7 @@ var _ = Describe("V1Emitter", func() { kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -561,7 +549,7 @@ var _ = Describe("V1Emitter", func() { kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertNoMatchingMocks() + assertNoMocksSent() kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -615,5 +603,170 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(nil, notWatched) }) }) - // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot + + Context("Tracking resources on namespaces that are deleted", func() { + It("Should not contain resources from a deleted namespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, notWatched) + }) + + It("Should not contain resources from a deleted namespace, that is filtered", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + KubeConfig + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := KubeConfigList{kubeConfig2a, kubeConfig2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, kubeConfig2a) + watched = KubeConfigList{kubeConfig2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, kubeConfig3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, kubeConfig2b) + watched = KubeConfigList{kubeConfig3a} + assertSnapshotMocks(watched, notWatched) + }) + }) + }) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 755a9c80f..83a96d58d 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -47,7 +47,6 @@ var _ = Describe("V1Emitter", func() { namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string - createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -109,12 +108,6 @@ var _ = Describe("V1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) - // add namespaces to created list for clean up - for _, ns := range namespaces { - if _, hit := createdNamespaces[ns]; !hit { - createdNamespaces[ns] = true - } - } } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { @@ -125,10 +118,6 @@ var _ = Describe("V1Emitter", func() { }, }, metav1.CreateOptions{}) Expect(err).ToNot(HaveOccurred()) - // add namespace to created list for clean up - if _, hit := createdNamespaces[namespace]; !hit { - createdNamespaces[namespace] = true - } } deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { @@ -146,12 +135,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } + deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() - createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -243,12 +236,7 @@ var _ = Describe("V1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - namespacesToDelete := []string{} - for namespace, _ := range createdNamespaces { - namespacesToDelete = append(namespacesToDelete, namespace) - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).NotTo(HaveOccurred()) + deleteNonDefaultKubeNamespaces(ctx, kube) clusterResourceClient.Delete(name1, clients.DeleteOpts{}) clusterResourceClient.Delete(name2, clients.DeleteOpts{}) }) @@ -2070,7 +2058,7 @@ var _ = Describe("V1Emitter", func() { } } - assertNoMatchingMocks := func() { + assertNoMocksSent := func() { drain: for { select { @@ -2123,7 +2111,7 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2140,7 +2128,7 @@ var _ = Describe("V1Emitter", func() { simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2228,7 +2216,7 @@ var _ = Describe("V1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2245,7 +2233,7 @@ var _ = Describe("V1Emitter", func() { mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2333,7 +2321,7 @@ var _ = Describe("V1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2350,7 +2338,7 @@ var _ = Describe("V1Emitter", func() { fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2438,7 +2426,7 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2455,7 +2443,7 @@ var _ = Describe("V1Emitter", func() { anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2540,7 +2528,7 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := ClusterResourceList{clusterResource1a} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2548,7 +2536,7 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, ClusterResourceList{clusterResource2a}...) - assertNoMatchingMocks() + assertNoMocksSent() clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2565,7 +2553,7 @@ var _ = Describe("V1Emitter", func() { clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) - assertNoMatchingMocks() + assertNoMocksSent() clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2628,7 +2616,7 @@ var _ = Describe("V1Emitter", func() { mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2645,7 +2633,7 @@ var _ = Describe("V1Emitter", func() { mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertNoMatchingMocks() + assertNoMocksSent() mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2733,7 +2721,7 @@ var _ = Describe("V1Emitter", func() { pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2750,7 +2738,7 @@ var _ = Describe("V1Emitter", func() { pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertNoMatchingMocks() + assertNoMocksSent() pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2804,5 +2792,617 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(nil, notWatched) }) }) - // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot + + Context("Tracking resources on namespaces that are deleted", func() { + It("Should not contain resources from a deleted namespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, notWatched) + }) + + It("Should not contain resources from a deleted namespace, that is filtered", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + SimpleMockResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, simpleMockResource2a) + watched = SimpleMockResourceList{simpleMockResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, simpleMockResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, simpleMockResource2b) + watched = SimpleMockResourceList{simpleMockResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + MockResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, mockResource2a) + watched = MockResourceList{mockResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, mockResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, mockResource2b) + watched = MockResourceList{mockResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + FakeResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, fakeResource2a) + watched = FakeResourceList{fakeResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + fakeResource3a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, fakeResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, fakeResource2b) + watched = FakeResourceList{fakeResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + AnotherMockResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, anotherMockResource2a) + watched = AnotherMockResourceList{anotherMockResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, anotherMockResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, anotherMockResource2b) + watched = AnotherMockResourceList{anotherMockResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + ClusterResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + // TODO need to add in ClusterScoped + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + // TODO need to add in ClusterScoped + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMocksSent() + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, clusterResource2a) + watched = ClusterResourceList{clusterResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + // TODO need to add in ClusterScoped + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMocksSent() + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, clusterResource2b) + watched = ClusterResourceList{clusterResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + MockCustomType + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, mockCustomType2a) + watched = MockCustomTypeList{mockCustomType2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, mockCustomType3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, mockCustomType2b) + watched = MockCustomTypeList{mockCustomType3a} + assertSnapshotMocks(watched, notWatched) + + /* + Pod + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, pod2a) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, pod3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, pod2b) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} + assertSnapshotMocks(watched, notWatched) + }) + }) + }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 9cd34970a..f223841f0 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -44,7 +44,6 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string - createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -70,12 +69,6 @@ var _ = Describe("V1Alpha1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) - // add namespaces to created list for clean up - for _, ns := range namespaces { - if _, hit := createdNamespaces[ns]; !hit { - createdNamespaces[ns] = true - } - } } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { @@ -86,10 +79,6 @@ var _ = Describe("V1Alpha1Emitter", func() { }, }, metav1.CreateOptions{}) Expect(err).ToNot(HaveOccurred()) - // add namespace to created list for clean up - if _, hit := createdNamespaces[namespace]; !hit { - createdNamespaces[namespace] = true - } } deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { @@ -107,12 +96,16 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } + deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() - createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -152,12 +145,7 @@ var _ = Describe("V1Alpha1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - namespacesToDelete := []string{} - for namespace, _ := range createdNamespaces { - namespacesToDelete = append(namespacesToDelete, namespace) - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).NotTo(HaveOccurred()) + deleteNonDefaultKubeNamespaces(ctx, kube) }) Context("Tracking watched namespaces", func() { @@ -503,7 +491,7 @@ var _ = Describe("V1Alpha1Emitter", func() { } } - assertNoMatchingMocks := func() { + assertNoMocksSent := func() { drain: for { select { @@ -556,7 +544,7 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -573,7 +561,7 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -627,5 +615,170 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(nil, notWatched) }) }) - // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot + + Context("Tracking resources on namespaces that are deleted", func() { + It("Should not contain resources from a deleted namespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, notWatched) + }) + + It("Should not contain resources from a deleted namespace, that is filtered", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + MockResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, mockResource2a) + watched = MockResourceList{mockResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, mockResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, mockResource2b) + watched = MockResourceList{mockResource3a} + assertSnapshotMocks(watched, notWatched) + }) + }) + }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index caa24b01d..19068fbe1 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -47,7 +47,6 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace1, namespace2 string namespace3, namespace4 string namespace5, namespace6 string - createdNamespaces map[string]bool name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} @@ -85,12 +84,6 @@ var _ = Describe("V2Alpha1Emitter", func() { createNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) - // add namespaces to created list for clean up - for _, ns := range namespaces { - if _, hit := createdNamespaces[ns]; !hit { - createdNamespaces[ns] = true - } - } } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { @@ -101,10 +94,6 @@ var _ = Describe("V2Alpha1Emitter", func() { }, }, metav1.CreateOptions{}) Expect(err).ToNot(HaveOccurred()) - // add namespace to created list for clean up - if _, hit := createdNamespaces[namespace]; !hit { - createdNamespaces[namespace] = true - } } deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { @@ -122,12 +111,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } + deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { + err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) + Expect(err).NotTo(HaveOccurred()) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) ctx = context.Background() - createdNamespaces = make(map[string]bool) namespace1 = helpers.RandString(8) namespace2 = helpers.RandString(8) namespace3 = helpers.RandString(8) @@ -181,12 +174,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - namespacesToDelete := []string{} - for namespace, _ := range createdNamespaces { - namespacesToDelete = append(namespacesToDelete, namespace) - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).NotTo(HaveOccurred()) + deleteNonDefaultKubeNamespaces(ctx, kube) }) Context("Tracking watched namespaces", func() { @@ -1040,7 +1028,7 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - assertNoMatchingMocks := func() { + assertNoMocksSent := func() { drain: for { select { @@ -1093,7 +1081,7 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1110,7 +1098,7 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1198,7 +1186,7 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1215,7 +1203,7 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1303,7 +1291,7 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMatchingMocks() + assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1320,7 +1308,7 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoMatchingMocks() + assertNoMocksSent() fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1374,5 +1362,320 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(nil, notWatched) }) }) - // TODO-JAKE need to write a test that deletes a namespace, see if it gets rid of all resources on that namespace from the snapshot + + Context("Tracking resources on namespaces that are deleted", func() { + It("Should not contain resources from a deleted namespace", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, notWatched) + }) + + It("Should not contain resources from a deleted namespace, that is filtered", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + MockResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := MockResourceList{mockResource1a, mockResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, mockResource2a) + watched = MockResourceList{mockResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, mockResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, mockResource2b) + watched = MockResourceList{mockResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + FrequentlyChangingAnnotationsResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource2a) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, frequentlyChangingAnnotationsResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource2b) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} + assertSnapshotMocks(watched, notWatched) + + /* + FakeResource + */ + + // clean up the namespaces and set back to default namespaces + deleteNonDefaultKubeNamespaces(ctx, kube) + createNamespaces(ctx, kube, namespace1, namespace2) + + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertNoMocksSent() + + // TODO-JAKE we need to create namespaces at the end so that the other resources work too. + deleteNamespaces(ctx, kube, namespace1, namespace2) + assertNoMocksSent() + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(watched, notWatched) + + // TODO-JAKE need to ensure that this will work for each resource + deleteNamespaces(ctx, kube, namespace3) + notWatched = append(notWatched, fakeResource2a) + watched = testing_solo_io.FakeResourceList{fakeResource2b} + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + + fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := append(watched, fakeResource3a) + assertSnapshotMocks(watched, notWatched) + + deleteNamespaces(ctx, kube, namespace4) + notWatched = append(notWatched, fakeResource2b) + watched = testing_solo_io.FakeResourceList{fakeResource3a} + assertSnapshotMocks(watched, notWatched) + }) + }) + }) From 4cf797d07689a9996f8ca0510fc6cf284b1d99cb Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Fri, 26 Aug 2022 16:04:35 -0500 Subject: [PATCH 17/98] pass CI for non ClusterScoped --- .../snapshot_emitter_test_template.go | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index ba7086dee..4f34027a3 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -142,16 +142,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - Expect(err).NotTo(HaveOccurred()) + {{- if $need_kube_config }} + cfg, err = kubeutils.GetConfig("", "") Expect(err).NotTo(HaveOccurred()) clientset, err = apiext.NewForConfig(cfg) Expect(err).NotTo(HaveOccurred()) + {{- end}} {{- range .Resources }} @@ -1091,16 +1091,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } } -{{- range .Resources }} - +{{- $length := len .Resources }} +{{- range $i, $r := .Resources }} +{{ with $r }} /* {{ .Name }} */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: for { @@ -1198,7 +1195,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, {{ lower_camel .Name }}3a) + watched = append(watched, {{ lower_camel .Name }}3a) assertSnapshotMocks(watched, notWatched) {{- end }} @@ -1207,7 +1204,20 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func notWatched = append(notWatched, {{ lower_camel .Name }}2b) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} assertSnapshotMocks(watched, notWatched) + + for _, r := range watched { + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + +{{/* after each resource we have to create the namespaces that were just deleted */}} +{{if ne $length $i }} + createNamespaces(ctx, kube, namespace1, namespace2) {{- end }} + +{{- end }}{{/* end of with */}} +{{- end }}{{/* end of range */}} }) }) From 765bfc5d2e725b24d77aa06b91a6bea35115d400 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 29 Aug 2022 15:44:15 -0500 Subject: [PATCH 18/98] adding kube client resource namespace lister --- .../namespace/resource_namespace.go | 123 +- pkg/api/v1/clients/client_interface.go | 16 +- pkg/api/v1/clients/common/common.go | 7 +- pkg/api/v1/resources/resource_interface.go | 17 +- .../templates/snapshot_emitter_template.go | 102 +- .../snapshot_emitter_test_template.go | 662 ++--- .../v1/kubeconfigs_snapshot_emitter.sk.go | 102 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 366 +-- test/mocks/v1/testing_snapshot_emitter.sk.go | 142 +- .../mocks/v1/testing_snapshot_emitter_test.go | 2129 +++++++++-------- .../v1alpha1/testing_snapshot_emitter.sk.go | 102 +- .../v1alpha1/testing_snapshot_emitter_test.go | 367 +-- .../v2alpha1/testing_snapshot_emitter.sk.go | 118 +- .../v2alpha1/testing_snapshot_emitter_test.go | 1005 ++++---- 14 files changed, 2637 insertions(+), 2621 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 9da4d1156..5fb473ce6 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -1,16 +1,21 @@ package namespace import ( - "context" + "bytes" + "github.com/pkg/errors" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/resources" skkube "github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes" + kubev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubewatch "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" ) var _ resources.ResourceNamespaceLister = &kubeResourceNamespaceLister{} +var _ resources.ResourceNamespaceLister = &kubeResourceNamespaceClient{} // if we use the list thingy I guess we could try it out, if it does not work, then lets move forward with the base client // solution, that uses the kubernetes interface... @@ -20,22 +25,30 @@ func NewKubeResourceNamespaceLister(kube kubernetes.Interface, cache cache.KubeC } } +func NewKubeClientResourceNamespaceLister(kube kubernetes.Interface) resources.ResourceNamespaceLister { + return &kubeResourceNamespaceClient{ + kube: kube, + } +} + type kubeResourceNamespaceLister struct { namespace skkube.KubeNamespaceClient } // GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces -func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(ctx context.Context, opts resources.ResourceNamespaceListOptions) (resources.ResourceNamespaceList, error) { +func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { namespaces, err := kns.namespace.List(clients.TranslateResourceNamespaceListToListOptions(opts)) if err != nil { return nil, err } - return convertNamespaceListToResourceNamespace(namespaces), nil + converted := convertNamespaceListToResourceNamespace(namespaces) + return kns.filter(converted, filtered), nil } -func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(ctx context.Context, opts resources.ResourceNamespaceWatchOptions) (chan resources.ResourceNamespaceList, <-chan error, error) { - wopts := clients.WatchOpts{FieldSelectors: opts.FieldSelectors, ExpressionSelector: opts.ExpressionSelectors} - // todo look that the namespace implementation to know exacactly what the channel of errors is returning. +// GetNamespaceResourceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces +func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList, errs chan error) (chan resources.ResourceNamespaceList, <-chan error, error) { + ctx := opts.Ctx + wopts := clients.TranslateResourceNamespaceListToWatchOptions(opts) namespaceChan, errorChan, err := kns.namespace.Watch(wopts) if err != nil { return nil, nil, err @@ -47,7 +60,7 @@ func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(ctx context.Co select { case namespaceList := <-namespaceChan: select { - case resourceNamespaceChan <- convertNamespaceListToResourceNamespace(namespaceList): + case resourceNamespaceChan <- kns.filter(convertNamespaceListToResourceNamespace(namespaceList), filtered): case <-ctx.Done(): close(resourceNamespaceChan) return @@ -61,6 +74,23 @@ func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(ctx context.Co return resourceNamespaceChan, errorChan, nil } +func (kns *kubeResourceNamespaceLister) filter(namespaces resources.ResourceNamespaceList, filter resources.ResourceNamespaceList) resources.ResourceNamespaceList { + filteredList := resources.ResourceNamespaceList{} + for _, ns := range namespaces { + add := true + for _, wns := range filter { + if ns.Name == wns.Name { + add = false + break + } + } + if add { + filteredList = append(filteredList, ns) + } + } + return filteredList +} + func convertNamespaceListToResourceNamespace(namespaces skkube.KubeNamespaceList) resources.ResourceNamespaceList { l := resources.ResourceNamespaceList{} for _, ns := range namespaces { @@ -68,3 +98,82 @@ func convertNamespaceListToResourceNamespace(namespaces skkube.KubeNamespaceList } return l } + +type kubeResourceNamespaceClient struct { + kube kubernetes.Interface +} + +// GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces +func (client *kubeResourceNamespaceClient) GetNamespaceResourceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { + excludeNamespaces := client.getExcludeFieldSelector(filtered) + namespaceList, err := client.kube.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) + if err != nil { + return nil, err + } + return convertNamespaceListToResourceNamespaceList(namespaceList), nil +} + +// GetNamespaceResourceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces +func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList, errs chan error) (chan resources.ResourceNamespaceList, <-chan error, error) { + excludeNamespaces := client.getExcludeFieldSelector(filtered) + namespaceWatcher, err := client.kube.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) + if err != nil { + return nil, nil, err + } + namespaceChan := namespaceWatcher.ResultChan() + resourceNamespaceChan := make(chan resources.ResourceNamespaceList) + go func() { + for { + select { + case <-opts.Ctx.Done(): + return + case event, ok := <-namespaceChan: + if !ok { + return + } + switch event.Type { + case kubewatch.Error: + errs <- errors.Errorf("error with the event from watching namespaces: %v", event) + return + default: + resourceNamespaceList, err := client.GetNamespaceResourceList(resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + FieldSelectors: excludeNamespaces, + ExpressionSelector: opts.ExpressionSelector, + }, filtered) + if err != nil { + errs <- errors.Wrap(err, "error getting the list of namespaces while watching") + return + } + resourceNamespaceChan <- resourceNamespaceList + } + } + } + }() + // TODO-JAKE do we need a <- chan error + return resourceNamespaceChan, nil, nil +} + +func (client *kubeResourceNamespaceClient) getExcludeFieldSelector(filtered resources.ResourceNamespaceList) string { + var buffer bytes.Buffer + for i, rns := range filtered { + ns := rns.Name + if ns != "" { + buffer.WriteString("metadata.name!=") + buffer.WriteString(ns) + if i < len(filtered)-1 { + buffer.WriteByte(',') + } + } + } + return buffer.String() +} + +func convertNamespaceListToResourceNamespaceList(namespaceList *kubev1.NamespaceList) resources.ResourceNamespaceList { + resourceNamespaces := resources.ResourceNamespaceList{} + for _, item := range namespaceList.Items { + ns := item.Name + resourceNamespaces = append(resourceNamespaces, resources.ResourceNamespace{Name: ns}) + } + return resourceNamespaces +} diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 0bea11de8..23025d2e6 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -139,6 +139,8 @@ func (o ListOpts) WithDefaults() ListOpts { return o } +// TODO-JAKE do we want to combine the WatchOpts, ListOpts, and ResourceNamespaceOpts??? + // RefreshRate is currently ignored by the Kubernetes ResourceClient implementation. // To achieve a similar behavior you can use the KubeResourceClientFactory.ResyncPeriod field. The difference is that it // will apply to all the watches started by clients built with the factory. @@ -183,9 +185,19 @@ func (o WatchOpts) WithDefaults() WatchOpts { return o } +func TranslateWatchOptsIntoListOpts(wopts WatchOpts) ListOpts { + clopts := ListOpts{Ctx: wopts.Ctx, FieldSelectors: wopts.FieldSelectors, ExpressionSelector: wopts.ExpressionSelector, Selector: wopts.Selector} + return clopts +} + // TODO-JAKE maybe they should be the same type of options? // TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options -func TranslateResourceNamespaceListToListOptions(rnlo resources.ResourceNamespaceListOptions) ListOpts { - clopts := ListOpts{FieldSelectors: rnlo.FieldSelectors, ExpressionSelector: rnlo.ExpressionSelectors} +func TranslateResourceNamespaceListToListOptions(lopts resources.ResourceNamespaceListOptions) ListOpts { + clopts := ListOpts{Ctx: lopts.Ctx, FieldSelectors: lopts.FieldSelectors, ExpressionSelector: lopts.ExpressionSelector} + return clopts +} + +func TranslateResourceNamespaceListToWatchOptions(wopts resources.ResourceNamespaceWatchOptions) WatchOpts { + clopts := WatchOpts{Ctx: wopts.Ctx, FieldSelectors: wopts.FieldSelectors, ExpressionSelector: wopts.ExpressionSelector} return clopts } diff --git a/pkg/api/v1/clients/common/common.go b/pkg/api/v1/clients/common/common.go index 0729b2f82..50f07f4f6 100644 --- a/pkg/api/v1/clients/common/common.go +++ b/pkg/api/v1/clients/common/common.go @@ -37,11 +37,8 @@ func KubeResourceWatch(cache cache.Cache, listFunc ResourceListFunc, namespace s // prevent flooding the channel with duplicates var previous *resources.ResourceList updateResourceList := func() { - list, err := listFunc(namespace, clients.ListOpts{ - Ctx: opts.Ctx, - Selector: opts.Selector, - ExpressionSelector: opts.ExpressionSelector, - }) + lopts := clients.TranslateWatchOptsIntoListOpts(opts) + list, err := listFunc(namespace, lopts) if err != nil { errs <- err return diff --git a/pkg/api/v1/resources/resource_interface.go b/pkg/api/v1/resources/resource_interface.go index 79084482a..9f6bc1c30 100644 --- a/pkg/api/v1/resources/resource_interface.go +++ b/pkg/api/v1/resources/resource_interface.go @@ -83,24 +83,28 @@ type CustomInputResource interface { // ResourceNamespaceListOptions provides the options for listing Resoiurce Namespaces type ResourceNamespaceListOptions struct { + // Ctx is the context + Ctx context.Context // FieldSelectors are used to filter out specific fields that are associated with // the Namespace. IE if using Kubernetes you can filter namespaces by the name // of the namespace by using metadata.name!= // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ FieldSelectors string // TODO-JAKE add description - ExpressionSelectors string + ExpressionSelector string } // ResourceNamespaceWatchOptions provides the options for watching Resource Namespaces type ResourceNamespaceWatchOptions struct { + // Ctx is the context + Ctx context.Context // FieldSelectors are used to filter out specific fields that are associated with // the Namespace. IE if using Kubernetes you can filter namespaces by the name // of the namespace by using metadata.name!= // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ FieldSelectors string // TODO-JAKE add description - ExpressionSelectors string + ExpressionSelector string } // ResoruceNamespace is the namespaces that resources can be found. ResourceNamespaces are @@ -118,11 +122,12 @@ type ResourceNamespaceList []ResourceNamespace // resources can be found. type ResourceNamespaceLister interface { // GetNamespaceResourceList returns the list of the namespaces that resources - // can be found. - GetNamespaceResourceList(ctx context.Context, opts ResourceNamespaceListOptions) (ResourceNamespaceList, error) + // can be found. The list returned will not contain namespacesToFilter. + GetNamespaceResourceList(opts ResourceNamespaceListOptions, namespacesToFilter ResourceNamespaceList) (ResourceNamespaceList, error) // GetNamespaceResourceWatch returns a watch that receives events when namespaces - // are updated or created. - GetNamespaceResourceWatch(ctx context.Context, opts ResourceNamespaceWatchOptions) (chan ResourceNamespaceList, <-chan error, error) + // are updated or created. The channel will not return namespacesToFilter. Use the errs for when + // errors are async. + GetNamespaceResourceWatch(opts ResourceNamespaceWatchOptions, namespacesToFilter ResourceNamespaceList, errs chan error) (chan ResourceNamespaceList, <-chan error, error) } type ResourceList []Resource diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index aedd6b74c..7930c644b 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -173,26 +173,15 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } } - // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := ! hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx - // if we are watching namespaces, then we do not want to fitler any of the - // resources in when listing or watching - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. - watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} - watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} - if watchNamespacesIsEmpty { - // if the namespaces that we are watching is empty, then we want to apply - // the expression Selectors to all the namespaces. - watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector - watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector - } + // setting up the options for both listing and watching resources in namespaces + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector } + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector } {{- range .Resources}} /* Create channel for {{ .Name }} */ @@ -270,58 +259,31 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // Selector filters. - - // first get the renaiming namespaces - excludeNamespacesFieldDesciptors := "" + // TODO-JAKE might want to get rid of the FieldSelectors + //setting up the options for both Listing and Watching namespaces + namespaceListOptions := resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } + namespaceWatchOptions := resources.ResourceNamespaceWatchOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields - var buffer bytes.Buffer - for i, ns := range watchNamespaces { + // TODO-JAKE test that we can create a huge field selector of massive size + filterNamespaces := resources.ResourceNamespaceList{} + for _, ns := range watchNamespaces { if ns != "" { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') - } + filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - excludeNamespacesFieldDesciptors = buffer.String() - - // we should only be watching namespaces that have the selectors that we want to be watching - - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. - // this could be built dyynamically - - // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - // TODO-JAKE field selectors are not working - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) - + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, 0) - for _, ns := range namespacesResources { - // TODO-JAKE get the filters on the namespacing working - add := true - // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister - for _,wns := range watchNamespaces { - if ns.Name == wns { - add = false - break - } - } - if add { - allOtherNamespaces = append(allOtherNamespaces, ns.Name) - } - } - // non Watched Namespaces - // REFACTOR - for _, namespace := range allOtherNamespaces { + for _, resourceNamespace := range namespacesResources { + namespace := resourceNamespace.Name {{- range .Resources }} {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ upper_camel .Name }} */ @@ -368,13 +330,16 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } }(namespace) } - // create watch on all namespaces, so that we can add resources from new namespaces + // create watch on all namespaces, so that we can add all resources from new namespaces + // we will be watching namespaces that meet the Expression Selector filter + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) + // I believe this is delt with in the last tests, but I want to check the snapshots once more. + + // watch for new namespaces + // TODO-JAKE not sure if I need to watch the <- chan error here... or not + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) if err != nil { return nil, nil, err } @@ -393,13 +358,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // not just return the list of namespaces newNamespaces := []string{} + // TODO-JAKE get a map of the namespaces that are currently being watched for _, ns := range resourceNamespaces { - // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here - {{- range .Resources }} {{- if (not .ClusterScoped) }} - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := {{ lower_camel .PluralName }}ByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue @@ -413,7 +375,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- if (not .ClusterScoped) }} /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ { - {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -422,7 +384,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) } - {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -432,8 +394,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 4f34027a3..aba4fc44c 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -125,6 +125,333 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) } + runNamespacedSelectorsWithWatchNamespaces := func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *{{ .GoName }}Snapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } +{{- range .Resources }} + + /* + {{ .Name }} + */ + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + {{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + {{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + {{- end }} + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertNoMessageSent() + +{{- else }} + + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertSnapshot{{ .PluralName }}(watched, nil) + +{{- end }} + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) + assertSnapshotMocks(watched, nil) + +{{- end }} + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) + assertSnapshotMocks(watched, nil) + +{{- end }} + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + +{{- if .ClusterScoped }} + + + // TODO need to fix clusterScoped + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) + assertNoMessageSent() + +{{- else }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ lower_camel .Name }}4a) + notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } + assertSnapshotMocks(watched, notWatched) + +{{- end }} + + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ lower_camel .Name }}5a) + notWatched = append(notWatched, {{ lower_camel .Name }}5b) + assertSnapshotMocks(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ lower_camel .Name }}6a) + notWatched = append(notWatched, {{ lower_camel .Name }}6b) + assertSnapshotMocks(watched, notWatched) + +{{- end }} + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + assertNoMessageSent() + +{{- else }} + + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ lower_camel .Name }}7a) + notWatched = append(notWatched, {{ lower_camel .Name }}7b) + assertSnapshotMocks(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) + assertNoMessageSent() + +{{- end }} + + for _, r := range notWatched { + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + +{{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched ={{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}(watched, notWatched) + +{{- end }} + +{{- if .ClusterScoped }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + assertSnapshot{{ .PluralName }}(nil, notWatched) + +{{- else }} + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) + assertSnapshot{{ .PluralName }}(nil, notWatched) + +{{- end }} +{{- end }} + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -305,329 +632,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func }) It("should be able to track all resources that are on labeled namespaces", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - // There is an error here in the code. - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *{{ .GoName }}Snapshot - - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - {{- range .Resources }} - - /* - {{ .Name }} - */ - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpect{{ .PluralName }} { - if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - {{- if .ClusterScoped }} - combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) - {{- else }} - nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) - nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - {{- end }} - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMessageSent() - -{{- else }} - - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertSnapshot{{ .PluralName }}(watched, nil) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) - assertSnapshotMocks(watched, nil) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) - assertSnapshotMocks(watched, nil) - -{{- end }} - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - -{{- if .ClusterScoped }} - - // TODO need to fix clusterScoped - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) - assertNoMessageSent() - -{{- else }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}4a) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } - assertSnapshotMocks(watched, notWatched) - -{{- end }} - - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}5a) - notWatched = append(notWatched, {{ lower_camel .Name }}5b) - assertSnapshotMocks(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}6a) - notWatched = append(notWatched, {{ lower_camel .Name }}6b) - assertSnapshotMocks(watched, notWatched) - -{{- end }} - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - assertNoMessageSent() - -{{- else }} - - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}7a) - notWatched = append(notWatched, {{ lower_camel .Name }}7b) - assertSnapshotMocks(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) - assertNoMessageSent() - -{{- end }} - - for _, r := range notWatched { - err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched ={{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - assertSnapshot{{ .PluralName }}(nil, notWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) - assertSnapshot{{ .PluralName }}(nil, notWatched) - -{{- end }} -{{- end }} + runNamespacedSelectorsWithWatchNamespaces() }) }) @@ -1221,6 +1226,17 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func }) }) + Context("use different resource namespace listers", func() { + BeforeEach(func () { + resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + }) + + It("Should work with the Kube Client Namespace Lister", func () { + runNamespacedSelectorsWithWatchNamespaces() + }) + }) + }) `)) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 4c07bbf1b..8e0f30083 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -3,7 +3,6 @@ package v1 import ( - "bytes" "sync" "time" @@ -131,26 +130,15 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } } - // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx - // if we are watching namespaces, then we do not want to fitler any of the - // resources in when listing or watching - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. - watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} - watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} - if watchNamespacesIsEmpty { - // if the namespaces that we are watching is empty, then we want to apply - // the expression Selectors to all the namespaces. - watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector - watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector - } + // setting up the options for both listing and watching resources in namespaces + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector} /* Create channel for KubeConfig */ type kubeConfigListWithNamespace struct { list KubeConfigList @@ -209,56 +197,31 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // Selector filters. - - // first get the renaiming namespaces - excludeNamespacesFieldDesciptors := "" - - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields - var buffer bytes.Buffer - for i, ns := range watchNamespaces { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') - } + // TODO-JAKE might want to get rid of the FieldSelectors + //setting up the options for both Listing and Watching namespaces + namespaceListOptions := resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } + namespaceWatchOptions := resources.ResourceNamespaceWatchOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, } - excludeNamespacesFieldDesciptors = buffer.String() - - // we should only be watching namespaces that have the selectors that we want to be watching - - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. - // this could be built dyynamically // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - // TODO-JAKE field selectors are not working - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) - + filterNamespaces := resources.ResourceNamespaceList{} + for _, ns := range watchNamespaces { + if ns != "" { + filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) + } + } + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, 0) - for _, ns := range namespacesResources { - // TODO-JAKE get the filters on the namespacing working - add := true - // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister - for _, wns := range watchNamespaces { - if ns.Name == wns { - add = false - break - } - } - if add { - allOtherNamespaces = append(allOtherNamespaces, ns.Name) - } - } - // non Watched Namespaces - // REFACTOR - for _, namespace := range allOtherNamespaces { + for _, resourceNamespace := range namespacesResources { + namespace := resourceNamespace.Name /* Setup namespaced watch for KubeConfig */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -297,13 +260,16 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } }(namespace) } - // create watch on all namespaces, so that we can add resources from new namespaces + // create watch on all namespaces, so that we can add all resources from new namespaces + // we will be watching namespaces that meet the Expression Selector filter + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) + // I believe this is delt with in the last tests, but I want to check the snapshots once more. + + // watch for new namespaces + // TODO-JAKE not sure if I need to watch the <- chan error here... or not + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) if err != nil { return nil, nil, err } @@ -322,10 +288,8 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // not just return the list of namespaces newNamespaces := []string{} + // TODO-JAKE get a map of the namespaces that are currently being watched for _, ns := range resourceNamespaces { - // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := kubeconfigsByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue @@ -335,7 +299,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa for _, namespace := range newNamespaces { /* Setup namespaced watch for KubeConfig for new namespace */ { - kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -344,7 +308,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } kubeconfigsByNamespace.Store(namespace, kubeconfigs) } - kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -354,8 +318,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 11cfc139c..422feee7f 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -99,6 +99,176 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + runNamespacedSelectorsWithWatchNamespaces := func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *KubeconfigsSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + KubeConfig + */ + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(watched, nil) + + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + assertSnapshotMocks(watched, nil) + + kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, kubeConfig4a) + notWatched := KubeConfigList{kubeConfig4b} + assertSnapshotMocks(watched, notWatched) + + kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, kubeConfig5a) + notWatched = append(notWatched, kubeConfig5b) + assertSnapshotMocks(watched, notWatched) + + kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, kubeConfig6a) + notWatched = append(notWatched, kubeConfig6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, kubeConfig7a) + notWatched = append(notWatched, kubeConfig7b) + assertSnapshotMocks(watched, notWatched) + + kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) + watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + watched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + watched = KubeConfigList{kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(watched, notWatched) + + err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) + assertSnapshotkubeconfigs(nil, notWatched) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -116,10 +286,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - Expect(err).NotTo(HaveOccurred()) // KubeConfig Constructor kubeConfigClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), @@ -209,173 +376,7 @@ var _ = Describe("V1Emitter", func() { }) It("should be able to track all resources that are on labeled namespaces", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - // There is an error here in the code. - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *KubeconfigsSnapshot - - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - - /* - KubeConfig - */ - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpectkubeconfigs { - if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertSnapshotkubeconfigs(watched, nil) - - kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - assertSnapshotMocks(watched, nil) - - kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig4a) - notWatched := KubeConfigList{kubeConfig4b} - assertSnapshotMocks(watched, notWatched) - - kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig5a) - notWatched = append(notWatched, kubeConfig5b) - assertSnapshotMocks(watched, notWatched) - - kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig6a) - notWatched = append(notWatched, kubeConfig6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig7a) - notWatched = append(notWatched, kubeConfig7b) - assertSnapshotMocks(watched, notWatched) - - kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - - err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) - watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) - - err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) - - err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - watched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) - - err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) - watched = KubeConfigList{kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) - - err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) - assertSnapshotkubeconfigs(nil, notWatched) + runNamespacedSelectorsWithWatchNamespaces() }) }) @@ -696,10 +697,6 @@ var _ = Describe("V1Emitter", func() { KubeConfig */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: for { @@ -759,13 +756,32 @@ var _ = Describe("V1Emitter", func() { kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, kubeConfig3a) + watched = append(watched, kubeConfig3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) notWatched = append(notWatched, kubeConfig2b) watched = KubeConfigList{kubeConfig3a} assertSnapshotMocks(watched, notWatched) + + for _, r := range watched { + err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + }) + }) + + Context("use different resource namespace listers", func() { + BeforeEach(func() { + resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + }) + + It("Should work with the Kube Client Namespace Lister", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 0bc0d2dec..20e857065 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -3,7 +3,6 @@ package v1 import ( - "bytes" "sync" "time" @@ -193,26 +192,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx - // if we are watching namespaces, then we do not want to fitler any of the - // resources in when listing or watching - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. - watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} - watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} - if watchNamespacesIsEmpty { - // if the namespaces that we are watching is empty, then we want to apply - // the expression Selectors to all the namespaces. - watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector - watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector - } + // setting up the options for both listing and watching resources in namespaces + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector} /* Create channel for SimpleMockResource */ type simpleMockResourceListWithNamespace struct { list SimpleMockResourceList @@ -452,56 +440,31 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // Selector filters. - - // first get the renaiming namespaces - excludeNamespacesFieldDesciptors := "" - - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields - var buffer bytes.Buffer - for i, ns := range watchNamespaces { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') - } + // TODO-JAKE might want to get rid of the FieldSelectors + //setting up the options for both Listing and Watching namespaces + namespaceListOptions := resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } + namespaceWatchOptions := resources.ResourceNamespaceWatchOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, } - excludeNamespacesFieldDesciptors = buffer.String() - - // we should only be watching namespaces that have the selectors that we want to be watching - - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. - // this could be built dyynamically // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - // TODO-JAKE field selectors are not working - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) - + filterNamespaces := resources.ResourceNamespaceList{} + for _, ns := range watchNamespaces { + if ns != "" { + filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) + } + } + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, 0) - for _, ns := range namespacesResources { - // TODO-JAKE get the filters on the namespacing working - add := true - // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister - for _, wns := range watchNamespaces { - if ns.Name == wns { - add = false - break - } - } - if add { - allOtherNamespaces = append(allOtherNamespaces, ns.Name) - } - } - // non Watched Namespaces - // REFACTOR - for _, namespace := range allOtherNamespaces { + for _, resourceNamespace := range namespacesResources { + namespace := resourceNamespace.Name /* Setup namespaced watch for SimpleMockResource */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -680,13 +643,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - // create watch on all namespaces, so that we can add resources from new namespaces + // create watch on all namespaces, so that we can add all resources from new namespaces + // we will be watching namespaces that meet the Expression Selector filter + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) + // I believe this is delt with in the last tests, but I want to check the snapshots once more. + + // watch for new namespaces + // TODO-JAKE not sure if I need to watch the <- chan error here... or not + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) if err != nil { return nil, nil, err } @@ -705,40 +671,28 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // not just return the list of namespaces newNamespaces := []string{} + // TODO-JAKE get a map of the namespaces that are currently being watched for _, ns := range resourceNamespaces { - // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := simplemocksByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := mocksByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := fakesByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := anothermockresourcesByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := mctsByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := podsByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue @@ -748,7 +702,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range newNamespaces { /* Setup namespaced watch for SimpleMockResource for new namespace */ { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -757,7 +711,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } simplemocksByNamespace.Store(namespace, simplemocks) } - simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -767,8 +721,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -776,7 +728,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockResource for new namespace */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -785,7 +737,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -795,8 +747,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -804,7 +754,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource for new namespace */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -813,7 +763,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } fakesByNamespace.Store(namespace, fakes) } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -823,8 +773,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -832,7 +780,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for AnotherMockResource for new namespace */ { - anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -841,7 +789,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } anothermockresourcesByNamespace.Store(namespace, anothermockresources) } - anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -851,8 +799,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -860,7 +806,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for MockCustomType for new namespace */ { - mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -869,7 +815,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mctsByNamespace.Store(namespace, mcts) } - mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -879,8 +825,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -888,7 +832,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for Pod for new namespace */ { - pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -897,7 +841,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } podsByNamespace.Store(namespace, pods) } - podNamespacesChan, podErrs, err := c.pod.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + podNamespacesChan, podErrs, err := c.pod.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -907,8 +851,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 83a96d58d..ebd4608b8 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -140,543 +140,1103 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - BeforeEach(func() { - err := os.Setenv(statusutils.PodNamespaceEnvName, "default") + runNamespacedSelectorsWithWatchNamespaces := func() { + ctx := context.Background() + err := emitter.Register() Expect(err).NotTo(HaveOccurred()) - ctx = context.Background() - namespace1 = helpers.RandString(8) - namespace2 = helpers.RandString(8) - namespace3 = helpers.RandString(8) - namespace4 = helpers.RandString(8) - namespace5 = helpers.RandString(8) - namespace6 = helpers.RandString(8) + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) - kube = helpers.MustKubeClient() - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + SimpleMockResource + */ + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(watched, nil) - deleteNonDefaultKubeNamespaces(ctx, kube) + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + assertSnapshotMocks(watched, nil) - createNamespaces(ctx, kube, namespace1, namespace2) + simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - cfg, err = kubeutils.GetConfig("", "") + simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched = append(watched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + assertSnapshotMocks(watched, nil) - clientset, err = apiext.NewForConfig(cfg) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // SimpleMockResource Constructor - simpleMockResourceClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), - } + simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, simpleMockResource4a) + notWatched := SimpleMockResourceList{simpleMockResource4b} + assertSnapshotMocks(watched, notWatched) - simpleMockResourceClient, err = NewSimpleMockResourceClient(ctx, simpleMockResourceClientFactory) + simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // MockResource Constructor - mockResourceClientFactory := &factory.KubeResourceClientFactory{ - Crd: MockResourceCrd, - Cfg: cfg, - SharedCache: kuberc.NewKubeCache(context.TODO()), - } + simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, simpleMockResource5a) + notWatched = append(notWatched, simpleMockResource5b) + assertSnapshotMocks(watched, notWatched) - err = helpers.AddAndRegisterCrd(ctx, MockResourceCrd, clientset) + simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched = append(watched, simpleMockResource6a) + notWatched = append(notWatched, simpleMockResource6b) + assertSnapshotMocks(watched, notWatched) - mockResourceClient, err = NewMockResourceClient(ctx, mockResourceClientFactory) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // FakeResource Constructor - fakeResourceClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), - } + simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, simpleMockResource7a) + notWatched = append(notWatched, simpleMockResource7b) + assertSnapshotMocks(watched, notWatched) - fakeResourceClient, err = NewFakeResourceClient(ctx, fakeResourceClientFactory) + simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // AnotherMockResource Constructor - anotherMockResourceClientFactory := &factory.KubeResourceClientFactory{ - Crd: AnotherMockResourceCrd, - Cfg: cfg, - SharedCache: kuberc.NewKubeCache(context.TODO()), + simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) } + assertNoMessageSent() - err = helpers.AddAndRegisterCrd(ctx, AnotherMockResourceCrd, clientset) + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) + watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(watched, notWatched) - anotherMockResourceClient, err = NewAnotherMockResourceClient(ctx, anotherMockResourceClientFactory) + err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // ClusterResource Constructor - clusterResourceClientFactory := &factory.KubeResourceClientFactory{ - Crd: ClusterResourceCrd, - Cfg: cfg, - SharedCache: kuberc.NewKubeCache(context.TODO()), - } + err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(watched, notWatched) - err = helpers.AddAndRegisterCrd(ctx, ClusterResourceCrd, clientset) + err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(watched, notWatched) - clusterResourceClient, err = NewClusterResourceClient(ctx, clusterResourceClientFactory) + err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // MockCustomType Constructor - mockCustomTypeClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), - } + err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(watched, notWatched) - mockCustomTypeClient, err = NewMockCustomTypeClient(ctx, mockCustomTypeClientFactory) + err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // Pod Constructor - podClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), + err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) + assertSnapshotSimplemocks(nil, notWatched) + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } } - podClient, err = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) - }) - AfterEach(func() { - err := os.Unsetenv(statusutils.PodNamespaceEnvName) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) - deleteNonDefaultKubeNamespaces(ctx, kube) - clusterResourceClient.Delete(name1, clients.DeleteOpts{}) - clusterResourceClient.Delete(name2, clients.DeleteOpts{}) - }) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) - Context("Tracking watched namespaces", func() { - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) - Expect(err).NotTo(HaveOccurred()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) - var snap *TestingSnapshot + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) + assertSnapshotMocks(watched, notWatched) - /* - SimpleMockResource - */ + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectSimplemocks { - if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpectSimplemocks { - if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) - simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() - err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - /* - MockResource - */ + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(watched, nil) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(watched, nil) - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + fakeResource4a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource4a) + notWatched := FakeResourceList{fakeResource4b} + assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource5a) + notWatched = append(notWatched, fakeResource5b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource6a) + notWatched = append(notWatched, fakeResource6b) + assertSnapshotMocks(watched, notWatched) - /* - FakeResource - */ + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) - assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource7a) + notWatched = append(notWatched, fakeResource7b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) + assertNoMessageSent() - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + for _, r := range notWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) + watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) - assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) - /* - AnotherMockResource - */ + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = FakeResourceList{fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) - assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectAnothermockresources { - if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) + assertSnapshotFakes(nil, notWatched) + + /* + AnotherMockResource + */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectAnothermockresources { - if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) - anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(watched, nil) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + assertSnapshotMocks(watched, nil) - err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, anotherMockResource4a) + notWatched := AnotherMockResourceList{anotherMockResource4b} + assertSnapshotMocks(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, anotherMockResource5a) + notWatched = append(notWatched, anotherMockResource5b) + assertSnapshotMocks(watched, notWatched) + + anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, anotherMockResource6a) + notWatched = append(notWatched, anotherMockResource6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, anotherMockResource7a) + notWatched = append(notWatched, anotherMockResource7b) + assertSnapshotMocks(watched, notWatched) + + anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) + watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(watched, notWatched) - /* - ClusterResource - */ + err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(watched, notWatched) - assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectClusterresources { - if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(watched, notWatched) + + err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) + assertSnapshotAnothermockresources(nil, notWatched) + + /* + ClusterResource + */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectClusterresources { - if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - combined, _ := clusterResourceClient.List(clients.ListOpts{}) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched := ClusterResourceList{clusterResource1a} + assertNoMessageSent() - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := ClusterResourceList{clusterResource2a} + assertSnapshotClusterresources(watched, notWatched) - err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource3a}...) + assertSnapshotClusterresources(watched, notWatched) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) - err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + // TODO need to fix clusterScoped + clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource4a}...) + assertNoMessageSent() + + clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource5a}...) + assertSnapshotClusterresources(watched, notWatched) + + clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource6a}...) + assertSnapshotClusterresources(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + assertNoMessageSent() + + clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, ClusterResourceList{clusterResource8a}...) + assertSnapshotClusterresources(watched, notWatched) + + for _, r := range notWatched { + err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = ClusterResourceList{clusterResource2a} + watched = ClusterResourceList{clusterResource3a, clusterResource5a, clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) - /* - MockCustomType - */ + err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource3a}...) + watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) - assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectmcts { - if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) + watched = ClusterResourceList{clusterResource6a, clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) + watched = ClusterResourceList{clusterResource7a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + assertSnapshotClusterresources(nil, notWatched) + + /* + MockCustomType + */ + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectmcts { - if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) - mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(watched, nil) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + assertSnapshotMocks(watched, nil) - err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockCustomType4a) + notWatched := MockCustomTypeList{mockCustomType4b} + assertSnapshotMocks(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockCustomType5a) + notWatched = append(notWatched, mockCustomType5b) + assertSnapshotMocks(watched, notWatched) + + mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockCustomType6a) + notWatched = append(notWatched, mockCustomType6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockCustomType7a) + notWatched = append(notWatched, mockCustomType7b) + assertSnapshotMocks(watched, notWatched) + + mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) + watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(watched, notWatched) - /* - Pod - */ + err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(watched, notWatched) - assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectpods { - if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + watched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + watched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(watched, notWatched) + + err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) + assertSnapshotmcts(nil, notWatched) + + /* + Pod + */ + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectpods { - if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) - pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(watched, nil) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + assertSnapshotMocks(watched, nil) - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, pod4a) + notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} + assertSnapshotMocks(watched, notWatched) - err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, pod5a) + notWatched = append(notWatched, pod5b) + assertSnapshotMocks(watched, notWatched) + + pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, pod6a) + notWatched = append(notWatched, pod6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, pod7a) + notWatched = append(notWatched, pod7b) + assertSnapshotMocks(watched, notWatched) + + pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} + assertSnapshotpods(watched, notWatched) + + err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) + assertSnapshotpods(nil, notWatched) + } + + BeforeEach(func() { + err := os.Setenv(statusutils.PodNamespaceEnvName, "default") + Expect(err).NotTo(HaveOccurred()) + + ctx = context.Background() + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + + kube = helpers.MustKubeClient() + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + + createNamespaces(ctx, kube, namespace1, namespace2) + + cfg, err = kubeutils.GetConfig("", "") + Expect(err).NotTo(HaveOccurred()) + + clientset, err = apiext.NewForConfig(cfg) + Expect(err).NotTo(HaveOccurred()) + // SimpleMockResource Constructor + simpleMockResourceClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } + + simpleMockResourceClient, err = NewSimpleMockResourceClient(ctx, simpleMockResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // MockResource Constructor + mockResourceClientFactory := &factory.KubeResourceClientFactory{ + Crd: MockResourceCrd, + Cfg: cfg, + SharedCache: kuberc.NewKubeCache(context.TODO()), + } + + err = helpers.AddAndRegisterCrd(ctx, MockResourceCrd, clientset) + Expect(err).NotTo(HaveOccurred()) + + mockResourceClient, err = NewMockResourceClient(ctx, mockResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // FakeResource Constructor + fakeResourceClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } + + fakeResourceClient, err = NewFakeResourceClient(ctx, fakeResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // AnotherMockResource Constructor + anotherMockResourceClientFactory := &factory.KubeResourceClientFactory{ + Crd: AnotherMockResourceCrd, + Cfg: cfg, + SharedCache: kuberc.NewKubeCache(context.TODO()), + } + + err = helpers.AddAndRegisterCrd(ctx, AnotherMockResourceCrd, clientset) + Expect(err).NotTo(HaveOccurred()) + + anotherMockResourceClient, err = NewAnotherMockResourceClient(ctx, anotherMockResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // ClusterResource Constructor + clusterResourceClientFactory := &factory.KubeResourceClientFactory{ + Crd: ClusterResourceCrd, + Cfg: cfg, + SharedCache: kuberc.NewKubeCache(context.TODO()), + } + + err = helpers.AddAndRegisterCrd(ctx, ClusterResourceCrd, clientset) + Expect(err).NotTo(HaveOccurred()) + + clusterResourceClient, err = NewClusterResourceClient(ctx, clusterResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // MockCustomType Constructor + mockCustomTypeClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } + + mockCustomTypeClient, err = NewMockCustomTypeClient(ctx, mockCustomTypeClientFactory) + Expect(err).NotTo(HaveOccurred()) + // Pod Constructor + podClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } + + podClient, err = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) + Expect(err).NotTo(HaveOccurred()) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) + }) + AfterEach(func() { + err := os.Unsetenv(statusutils.PodNamespaceEnvName) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) - }) + deleteNonDefaultKubeNamespaces(ctx, kube) + clusterResourceClient.Delete(name1, clients.DeleteOpts{}) + clusterResourceClient.Delete(name2, clients.DeleteOpts{}) + }) - It("should be able to track all resources that are on labeled namespaces", func() { + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) - // There is an error here in the code. snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, + Ctx: ctx, + RefreshRate: time.Second, }) Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* SimpleMockResource */ + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: for { @@ -703,120 +1263,37 @@ var _ = Describe("V1Emitter", func() { } } } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotSimplemocks(watched, nil) + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - assertSnapshotMocks(watched, nil) - - simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource4a) - notWatched := SimpleMockResourceList{simpleMockResource4b} - assertSnapshotMocks(watched, notWatched) - - simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource5a) - notWatched = append(notWatched, simpleMockResource5b) - assertSnapshotMocks(watched, notWatched) - - simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource6a) - notWatched = append(notWatched, simpleMockResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource7a) - notWatched = append(notWatched, simpleMockResource7b) - assertSnapshotMocks(watched, notWatched) - - simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) - watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) - err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) - watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) - err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) - assertSnapshotSimplemocks(nil, notWatched) + assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) /* MockResource */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -843,120 +1320,37 @@ var _ = Describe("V1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) - - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) - - mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) - - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) /* FakeResource */ + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: for { @@ -983,120 +1377,37 @@ var _ = Describe("V1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(watched, nil) + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(watched, nil) - - fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - fakeResource4a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource4a) - notWatched := FakeResourceList{fakeResource4b} - assertSnapshotMocks(watched, notWatched) - - fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource5a) - notWatched = append(notWatched, fakeResource5b) - assertSnapshotMocks(watched, notWatched) - - fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource6a) - notWatched = append(notWatched, fakeResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource7a) - notWatched = append(notWatched, fakeResource7b) - assertSnapshotMocks(watched, notWatched) - - fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) - err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) - assertSnapshotFakes(nil, notWatched) + assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) /* AnotherMockResource */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: for { @@ -1123,120 +1434,37 @@ var _ = Describe("V1Emitter", func() { } } } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotAnothermockresources(watched, nil) + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - assertSnapshotMocks(watched, nil) - - anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource4a) - notWatched := AnotherMockResourceList{anotherMockResource4b} - assertSnapshotMocks(watched, notWatched) - - anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource5a) - notWatched = append(notWatched, anotherMockResource5b) - assertSnapshotMocks(watched, notWatched) - - anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource6a) - notWatched = append(notWatched, anotherMockResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource7a) - notWatched = append(notWatched, anotherMockResource7b) - assertSnapshotMocks(watched, notWatched) - - anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - - err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) - watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) - err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) - watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) - err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) - assertSnapshotAnothermockresources(nil, notWatched) + assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) /* ClusterResource */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { drain: for { @@ -1261,92 +1489,29 @@ var _ = Describe("V1Emitter", func() { } } } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMessageSent() - - clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource2a} - assertSnapshotClusterresources(watched, notWatched) - - clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource3a}...) - assertSnapshotClusterresources(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - - // TODO need to fix clusterScoped - clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource4a}...) - assertNoMessageSent() - - clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource5a}...) - assertSnapshotClusterresources(watched, notWatched) - - clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource6a}...) - assertSnapshotClusterresources(watched, notWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) - assertNoMessageSent() - - clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource8a}...) - assertSnapshotClusterresources(watched, notWatched) - for _, r := range notWatched { - err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = ClusterResourceList{clusterResource2a} - watched = ClusterResourceList{clusterResource3a, clusterResource5a, clusterResource6a, clusterResource7a} - assertSnapshotClusterresources(watched, notWatched) - - err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource3a}...) - watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} - assertSnapshotClusterresources(watched, notWatched) - err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) - watched = ClusterResourceList{clusterResource6a, clusterResource7a} - assertSnapshotClusterresources(watched, notWatched) + assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) - err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) - watched = ClusterResourceList{clusterResource7a} - assertSnapshotClusterresources(watched, notWatched) - err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) - assertSnapshotClusterresources(nil, notWatched) + assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) /* MockCustomType */ + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: for { @@ -1373,120 +1538,37 @@ var _ = Describe("V1Emitter", func() { } } } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertSnapshotmcts(watched, nil) + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - assertSnapshotMocks(watched, nil) - - mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType4a) - notWatched := MockCustomTypeList{mockCustomType4b} - assertSnapshotMocks(watched, notWatched) - - mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType5a) - notWatched = append(notWatched, mockCustomType5b) - assertSnapshotMocks(watched, notWatched) - - mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType6a) - notWatched = append(notWatched, mockCustomType6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType7a) - notWatched = append(notWatched, mockCustomType7b) - assertSnapshotMocks(watched, notWatched) - - mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) - watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - watched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) - err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) - watched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) - err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) - assertSnapshotmcts(nil, notWatched) + assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) /* Pod */ + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: for { @@ -1513,116 +1595,36 @@ var _ = Describe("V1Emitter", func() { } } } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertSnapshotpods(watched, nil) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - assertSnapshotMocks(watched, nil) - - pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod4a) - notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} - assertSnapshotMocks(watched, notWatched) - - pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod5a) - notWatched = append(notWatched, pod5b) - assertSnapshotMocks(watched, notWatched) - - pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod6a) - notWatched = append(notWatched, pod6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod7a) - notWatched = append(notWatched, pod7b) - assertSnapshotMocks(watched, notWatched) + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) - pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) - assertNoMessageSent() - for _, r := range notWatched { - err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() + assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) - - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) - - err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) - err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} - assertSnapshotpods(watched, notWatched) + assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) + }) - err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) - assertSnapshotpods(nil, notWatched) + It("should be able to track all resources that are on labeled namespaces", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) @@ -2885,10 +2887,6 @@ var _ = Describe("V1Emitter", func() { SimpleMockResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: for { @@ -2948,7 +2946,7 @@ var _ = Describe("V1Emitter", func() { simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, simpleMockResource3a) + watched = append(watched, simpleMockResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -2956,14 +2954,18 @@ var _ = Describe("V1Emitter", func() { watched = SimpleMockResourceList{simpleMockResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* MockResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -3023,7 +3025,7 @@ var _ = Describe("V1Emitter", func() { mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, mockResource3a) + watched = append(watched, mockResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -3031,14 +3033,18 @@ var _ = Describe("V1Emitter", func() { watched = MockResourceList{mockResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* FakeResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: for { @@ -3098,7 +3104,7 @@ var _ = Describe("V1Emitter", func() { fakeResource3a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, fakeResource3a) + watched = append(watched, fakeResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -3106,14 +3112,18 @@ var _ = Describe("V1Emitter", func() { watched = FakeResourceList{fakeResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* AnotherMockResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: for { @@ -3173,7 +3183,7 @@ var _ = Describe("V1Emitter", func() { anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, anotherMockResource3a) + watched = append(watched, anotherMockResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -3181,14 +3191,18 @@ var _ = Describe("V1Emitter", func() { watched = AnotherMockResourceList{anotherMockResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* ClusterResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { drain: for { @@ -3253,14 +3267,18 @@ var _ = Describe("V1Emitter", func() { watched = ClusterResourceList{clusterResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* MockCustomType */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: for { @@ -3320,7 +3338,7 @@ var _ = Describe("V1Emitter", func() { mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, mockCustomType3a) + watched = append(watched, mockCustomType3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -3328,14 +3346,18 @@ var _ = Describe("V1Emitter", func() { watched = MockCustomTypeList{mockCustomType3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* Pod */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: for { @@ -3395,13 +3417,32 @@ var _ = Describe("V1Emitter", func() { pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, pod3a) + watched = append(watched, pod3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) notWatched = append(notWatched, pod2b) watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} assertSnapshotMocks(watched, notWatched) + + for _, r := range watched { + err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + }) + }) + + Context("use different resource namespace listers", func() { + BeforeEach(func() { + resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + }) + + It("Should work with the Kube Client Namespace Lister", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index fe842db94..48c28f3f8 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -3,7 +3,6 @@ package v1alpha1 import ( - "bytes" "sync" "time" @@ -131,26 +130,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx - // if we are watching namespaces, then we do not want to fitler any of the - // resources in when listing or watching - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. - watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} - watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} - if watchNamespacesIsEmpty { - // if the namespaces that we are watching is empty, then we want to apply - // the expression Selectors to all the namespaces. - watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector - watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector - } + // setting up the options for both listing and watching resources in namespaces + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector} /* Create channel for MockResource */ type mockResourceListWithNamespace struct { list MockResourceList @@ -209,58 +197,31 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // Selector filters. - - // first get the renaiming namespaces - excludeNamespacesFieldDesciptors := "" + // TODO-JAKE might want to get rid of the FieldSelectors + //setting up the options for both Listing and Watching namespaces + namespaceListOptions := resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } + namespaceWatchOptions := resources.ResourceNamespaceWatchOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields - var buffer bytes.Buffer - for i, ns := range watchNamespaces { + // TODO-JAKE test that we can create a huge field selector of massive size + filterNamespaces := resources.ResourceNamespaceList{} + for _, ns := range watchNamespaces { if ns != "" { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') - } + filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - excludeNamespacesFieldDesciptors = buffer.String() - - // we should only be watching namespaces that have the selectors that we want to be watching - - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. - // this could be built dyynamically - - // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - // TODO-JAKE field selectors are not working - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) - + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, 0) - for _, ns := range namespacesResources { - // TODO-JAKE get the filters on the namespacing working - add := true - // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister - for _, wns := range watchNamespaces { - if ns.Name == wns { - add = false - break - } - } - if add { - allOtherNamespaces = append(allOtherNamespaces, ns.Name) - } - } - // non Watched Namespaces - // REFACTOR - for _, namespace := range allOtherNamespaces { + for _, resourceNamespace := range namespacesResources { + namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -299,13 +260,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - // create watch on all namespaces, so that we can add resources from new namespaces + // create watch on all namespaces, so that we can add all resources from new namespaces + // we will be watching namespaces that meet the Expression Selector filter + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) + // I believe this is delt with in the last tests, but I want to check the snapshots once more. + + // watch for new namespaces + // TODO-JAKE not sure if I need to watch the <- chan error here... or not + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) if err != nil { return nil, nil, err } @@ -324,10 +288,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // not just return the list of namespaces newNamespaces := []string{} + // TODO-JAKE get a map of the namespaces that are currently being watched for _, ns := range resourceNamespaces { - // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := mocksByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue @@ -337,7 +299,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range newNamespaces { /* Setup namespaced watch for MockResource for new namespace */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -346,7 +308,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -356,8 +318,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index f223841f0..9316a5163 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -101,6 +101,176 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + runNamespacedSelectorsWithWatchNamespaces := func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) + + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) + + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) + assertSnapshotMocks(watched, notWatched) + + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) + assertSnapshotMocks(watched, notWatched) + + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() + + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, notWatched) + } + BeforeEach(func() { err := os.Setenv(statusutils.PodNamespaceEnvName, "default") Expect(err).NotTo(HaveOccurred()) @@ -118,10 +288,8 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - Expect(err).NotTo(HaveOccurred()) + cfg, err = kubeutils.GetConfig("", "") Expect(err).NotTo(HaveOccurred()) @@ -221,173 +389,7 @@ var _ = Describe("V1Alpha1Emitter", func() { }) It("should be able to track all resources that are on labeled namespaces", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - // There is an error here in the code. - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - - /* - MockResource - */ - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) - - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) - - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) - - mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) - - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + runNamespacedSelectorsWithWatchNamespaces() }) }) @@ -708,10 +710,6 @@ var _ = Describe("V1Alpha1Emitter", func() { MockResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -771,13 +769,32 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, mockResource3a) + watched = append(watched, mockResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) notWatched = append(notWatched, mockResource2b) watched = MockResourceList{mockResource3a} assertSnapshotMocks(watched, notWatched) + + for _, r := range watched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + }) + }) + + Context("use different resource namespace listers", func() { + BeforeEach(func() { + resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + }) + + It("Should work with the Kube Client Namespace Lister", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 65f985b3b..3baa5f209 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -3,7 +3,6 @@ package v2alpha1 import ( - "bytes" "sync" "time" @@ -153,26 +152,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } - // TODO-JAKE some of this should only be present if scoped by namespace errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx - // if we are watching namespaces, then we do not want to fitler any of the - // resources in when listing or watching - // TODO-JAKE not sure if we want to get rid of the Selector in the - // ListOpts here. the reason that we might want to is because we no - // longer allow selectors, unless it is on a unwatched namespace. - watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx} - watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx} - if watchNamespacesIsEmpty { - // if the namespaces that we are watching is empty, then we want to apply - // the expression Selectors to all the namespaces. - watchedNamespacesListOptions.ExpressionSelector = opts.ExpressionSelector - watchedNamespacesWatchOptions.ExpressionSelector = opts.ExpressionSelector - } + // setting up the options for both listing and watching resources in namespaces + watchedNamespacesListOptions := clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector} + watchedNamespacesWatchOptions := clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector} /* Create channel for MockResource */ type mockResourceListWithNamespace struct { list MockResourceList @@ -303,56 +291,31 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // Selector filters. - - // first get the renaiming namespaces - excludeNamespacesFieldDesciptors := "" - - // TODO-JAKE REFACTOR, we can refactor how the watched namespaces are added up to make a exclusion namespaced fields - var buffer bytes.Buffer - for i, ns := range watchNamespaces { - buffer.WriteString("metadata.name!=") - buffer.WriteString(ns) - if i < len(watchNamespaces)-1 { - buffer.WriteByte(',') - } + // TODO-JAKE might want to get rid of the FieldSelectors + //setting up the options for both Listing and Watching namespaces + namespaceListOptions := resources.ResourceNamespaceListOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, + } + namespaceWatchOptions := resources.ResourceNamespaceWatchOptions{ + Ctx: opts.Ctx, + ExpressionSelector: opts.ExpressionSelector, } - excludeNamespacesFieldDesciptors = buffer.String() - - // we should only be watching namespaces that have the selectors that we want to be watching - - // TODO-JAKE need to add in the other namespaces that will not be allowed, IE the exclusion list. - // this could be built dyynamically // TODO-JAKE test that we can create a huge field selector of massive size - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(ctx, resources.ResourceNamespaceListOptions{ - // TODO-JAKE field selectors are not working - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) - + filterNamespaces := resources.ResourceNamespaceList{} + for _, ns := range watchNamespaces { + if ns != "" { + filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) + } + } + namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - allOtherNamespaces := make([]string, 0) - for _, ns := range namespacesResources { - // TODO-JAKE get the filters on the namespacing working - add := true - // TODO-JAKE need to implement the filtering of the field selectors in the resourceNamespaceLister - for _, wns := range watchNamespaces { - if ns.Name == wns { - add = false - break - } - } - if add { - allOtherNamespaces = append(allOtherNamespaces, ns.Name) - } - } - // non Watched Namespaces - // REFACTOR - for _, namespace := range allOtherNamespaces { + for _, resourceNamespace := range namespacesResources { + namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -447,13 +410,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - // create watch on all namespaces, so that we can add resources from new namespaces + // create watch on all namespaces, so that we can add all resources from new namespaces + // we will be watching namespaces that meet the Expression Selector filter + // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. // we will need a way to deal with DELETES and CREATES and updates seperately - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(ctx, resources.ResourceNamespaceWatchOptions{ - FieldSelectors: excludeNamespacesFieldDesciptors, - ExpressionSelectors: opts.ExpressionSelector, - }) + // I believe this is delt with in the last tests, but I want to check the snapshots once more. + + // watch for new namespaces + // TODO-JAKE not sure if I need to watch the <- chan error here... or not + namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) if err != nil { return nil, nil, err } @@ -472,22 +438,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // not just return the list of namespaces newNamespaces := []string{} + // TODO-JAKE get a map of the namespaces that are currently being watched for _, ns := range resourceNamespaces { - // TODO-JAKE are we sure we need this. Looks like there is a cocurrent map read and map write here - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := mocksByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := fcarsByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - // TODO-JAKE we willl only need to do this once, I might be best to keep a set/map of the current - // namespaces that are used if _, hit := fakesByNamespace.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue @@ -497,7 +457,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO for _, namespace := range newNamespaces { /* Setup namespaced watch for MockResource for new namespace */ { - mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -506,7 +466,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mocksByNamespace.Store(namespace, mocks) } - mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -516,8 +476,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -525,7 +483,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ { - fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -534,7 +492,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } fcarsByNamespace.Store(namespace, fcars) } - frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -544,8 +502,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() @@ -553,7 +509,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Setup namespaced watch for FakeResource for new namespace */ { - fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // INFO-JAKE not sure if we want to do something else // but since this is occuring in async I think it should be fine @@ -562,7 +518,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } fakesByNamespace.Store(namespace, fakes) } - fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the @@ -572,8 +528,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO continue } - // INFO-JAKE I think this is appropriate, becasue - // we want to watch the errors coming off the namespace done.Add(1) go func(namespace string) { defer done.Done() diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 19068fbe1..2b62d87a3 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -116,285 +116,533 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - BeforeEach(func() { - err := os.Setenv(statusutils.PodNamespaceEnvName, "default") + runNamespacedSelectorsWithWatchNamespaces := func() { + ctx := context.Background() + err := emitter.Register() Expect(err).NotTo(HaveOccurred()) - ctx = context.Background() - namespace1 = helpers.RandString(8) - namespace2 = helpers.RandString(8) - namespace3 = helpers.RandString(8) - namespace4 = helpers.RandString(8) - namespace5 = helpers.RandString(8) - namespace6 = helpers.RandString(8) - - kube = helpers.MustKubeClient() - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + // There is an error here in the code. + snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) - deleteNonDefaultKubeNamespaces(ctx, kube) + var snap *TestingSnapshot + + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } - createNamespaces(ctx, kube, namespace1, namespace2) + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - cfg, err = kubeutils.GetConfig("", "") + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(watched, nil) - clientset, err = apiext.NewForConfig(cfg) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // MockResource Constructor - mockResourceClientFactory := &factory.KubeResourceClientFactory{ - Crd: MockResourceCrd, - Cfg: cfg, - SharedCache: kuberc.NewKubeCache(context.TODO()), - } + mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(watched, nil) - err = helpers.AddAndRegisterCrd(ctx, MockResourceCrd, clientset) + mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(watched, nil) - mockResourceClient, err = NewMockResourceClient(ctx, mockResourceClientFactory) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // FrequentlyChangingAnnotationsResource Constructor - frequentlyChangingAnnotationsResourceClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), - } + mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource4a) + notWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(watched, notWatched) - frequentlyChangingAnnotationsResourceClient, err = NewFrequentlyChangingAnnotationsResourceClient(ctx, frequentlyChangingAnnotationsResourceClientFactory) + mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - // FakeResource Constructor - fakeResourceClientFactory := &factory.MemoryResourceClientFactory{ - Cache: memory.NewInMemoryResourceCache(), - } + mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource5a) + notWatched = append(notWatched, mockResource5b) + assertSnapshotMocks(watched, notWatched) - fakeResourceClient, err = testing_solo_io.NewFakeResourceClient(ctx, fakeResourceClientFactory) + mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister) - }) - AfterEach(func() { - err := os.Unsetenv(statusutils.PodNamespaceEnvName) + mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource6a) + notWatched = append(notWatched, mockResource6b) + assertSnapshotMocks(watched, notWatched) - deleteNonDefaultKubeNamespaces(ctx, kube) - }) + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) - Context("Tracking watched namespaces", func() { - It("tracks snapshots on changes to any resource", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) + mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, mockResource7a) + notWatched = append(notWatched, mockResource7b) + assertSnapshotMocks(watched, notWatched) - snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - }) + mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - var snap *TestingSnapshot + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) + watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - /* - MockResource - */ + err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) + watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + watched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(watched, notWatched) + + err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, notWatched) + + /* + FrequentlyChangingAnnotationsResource + */ + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(watched, nil) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + assertSnapshotMocks(watched, nil) - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, frequentlyChangingAnnotationsResource4a) + notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} + assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, frequentlyChangingAnnotationsResource5a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource5b) + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, frequentlyChangingAnnotationsResource6a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource6b) + assertSnapshotMocks(watched, notWatched) + + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) + + frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, frequentlyChangingAnnotationsResource7a) + notWatched = append(notWatched, frequentlyChangingAnnotationsResource7b) + assertSnapshotMocks(watched, notWatched) + + frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(watched, notWatched) - /* - FrequentlyChangingAnnotationsResource - */ + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(watched, notWatched) - assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFcars { - if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(watched, notWatched) + + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) + assertSnapshotFcars(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain } - for _, unexpected := range unexpectFcars { - if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + } - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) - frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(watched, nil) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(watched, nil) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(watched, nil) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource4a) + notWatched := testing_solo_io.FakeResourceList{fakeResource4b} + assertSnapshotMocks(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource5a) + notWatched = append(notWatched, fakeResource5b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) + fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource6a) + notWatched = append(notWatched, fakeResource6b) + assertSnapshotMocks(watched, notWatched) - /* - FakeResource - */ + createNamespaceWithLabel(ctx, kube, namespace5, labels1) + createNamespaces(ctx, kube, namespace6) - assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { - drain: - for { - select { - case snap = <-snapshots: - for _, expected := range expectFakes { - if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { - continue drain - } - } - for _, unexpected := range unexpectFakes { - if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { - continue drain - } - } - break drain - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) - } - } - } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = append(watched, fakeResource7a) + notWatched = append(notWatched, fakeResource7b) + assertSnapshotMocks(watched, notWatched) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) - fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) + assertNoMessageSent() + + for _, r := range notWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + } + assertNoMessageSent() - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) + watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} + assertSnapshotFakes(watched, notWatched) + + err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) + assertSnapshotFakes(nil, notWatched) + } + + BeforeEach(func() { + err := os.Setenv(statusutils.PodNamespaceEnvName, "default") + Expect(err).NotTo(HaveOccurred()) + + ctx = context.Background() + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + + kube = helpers.MustKubeClient() + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + + createNamespaces(ctx, kube, namespace1, namespace2) + + cfg, err = kubeutils.GetConfig("", "") + Expect(err).NotTo(HaveOccurred()) + + clientset, err = apiext.NewForConfig(cfg) + Expect(err).NotTo(HaveOccurred()) + // MockResource Constructor + mockResourceClientFactory := &factory.KubeResourceClientFactory{ + Crd: MockResourceCrd, + Cfg: cfg, + SharedCache: kuberc.NewKubeCache(context.TODO()), + } + + err = helpers.AddAndRegisterCrd(ctx, MockResourceCrd, clientset) + Expect(err).NotTo(HaveOccurred()) + + mockResourceClient, err = NewMockResourceClient(ctx, mockResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // FrequentlyChangingAnnotationsResource Constructor + frequentlyChangingAnnotationsResourceClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) + frequentlyChangingAnnotationsResourceClient, err = NewFrequentlyChangingAnnotationsResourceClient(ctx, frequentlyChangingAnnotationsResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + // FakeResource Constructor + fakeResourceClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } - err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) + fakeResourceClient, err = testing_solo_io.NewFakeResourceClient(ctx, fakeResourceClientFactory) + Expect(err).NotTo(HaveOccurred()) + emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister) + }) + AfterEach(func() { + err := os.Unsetenv(statusutils.PodNamespaceEnvName) + Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) - }) + deleteNonDefaultKubeNamespaces(ctx, kube) + }) - It("should be able to track all resources that are on labeled namespaces", func() { + Context("Tracking watched namespaces", func() { + It("tracks snapshots on changes to any resource", func() { ctx := context.Background() err := emitter.Register() Expect(err).NotTo(HaveOccurred()) - // There is an error here in the code. snapshots, errs, err := emitter.Snapshots([]string{namespace1, namespace2}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, + Ctx: ctx, + RefreshRate: time.Second, }) Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -421,120 +669,37 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) - - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) - - mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) - - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - - for _, r := range notWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) /* FrequentlyChangingAnnotationsResource */ + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: for { @@ -561,120 +726,37 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotFcars(watched, nil) + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - assertSnapshotMocks(watched, nil) - - frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource4a) - notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} - assertSnapshotMocks(watched, notWatched) - - frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource5a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource5b) - assertSnapshotMocks(watched, notWatched) - - frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource6a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource7a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource7b) - assertSnapshotMocks(watched, notWatched) - - frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) - assertNoMessageSent() - for _, r := range notWatched { - err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() - - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) - assertSnapshotFcars(nil, notWatched) + assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) /* FakeResource */ + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: for { @@ -701,116 +783,36 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(watched, nil) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(watched, nil) - - fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(watched, nil) - - createNamespaceWithLabel(ctx, kube, namespace3, labels1) - createNamespaces(ctx, kube, namespace4) - fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource4a) - notWatched := testing_solo_io.FakeResourceList{fakeResource4b} - assertSnapshotMocks(watched, notWatched) - - fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource5a) - notWatched = append(notWatched, fakeResource5b) - assertSnapshotMocks(watched, notWatched) - - fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource6a) - notWatched = append(notWatched, fakeResource6b) - assertSnapshotMocks(watched, notWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource7a) - notWatched = append(notWatched, fakeResource7b) - assertSnapshotMocks(watched, notWatched) + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) - fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) - assertNoMessageSent() - for _, r := range notWatched { - err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() + assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) - - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) - - err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) - err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) + }) - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) - assertSnapshotFakes(nil, notWatched) + It("should be able to track all resources that are on labeled namespaces", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) @@ -1455,10 +1457,6 @@ var _ = Describe("V2Alpha1Emitter", func() { MockResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -1518,7 +1516,7 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, mockResource3a) + watched = append(watched, mockResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -1526,14 +1524,18 @@ var _ = Describe("V2Alpha1Emitter", func() { watched = MockResourceList{mockResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* FrequentlyChangingAnnotationsResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: for { @@ -1593,7 +1595,7 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, frequentlyChangingAnnotationsResource3a) + watched = append(watched, frequentlyChangingAnnotationsResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) @@ -1601,14 +1603,18 @@ var _ = Describe("V2Alpha1Emitter", func() { watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} assertSnapshotMocks(watched, notWatched) + for _, r := range watched { + err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + /* FakeResource */ - // clean up the namespaces and set back to default namespaces - deleteNonDefaultKubeNamespaces(ctx, kube) - createNamespaces(ctx, kube, namespace1, namespace2) - assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: for { @@ -1668,13 +1674,32 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := append(watched, fakeResource3a) + watched = append(watched, fakeResource3a) assertSnapshotMocks(watched, notWatched) deleteNamespaces(ctx, kube, namespace4) notWatched = append(notWatched, fakeResource2b) watched = testing_solo_io.FakeResourceList{fakeResource3a} assertSnapshotMocks(watched, notWatched) + + for _, r := range watched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + assertNoMocksSent() + + createNamespaces(ctx, kube, namespace1, namespace2) + }) + }) + + Context("use different resource namespace listers", func() { + BeforeEach(func() { + resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + }) + + It("Should work with the Kube Client Namespace Lister", func() { + runNamespacedSelectorsWithWatchNamespaces() }) }) From 36bccb7a26643e9ce01655ee105265deedcec1ea Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 29 Aug 2022 16:31:56 -0500 Subject: [PATCH 19/98] added in map for watched namespaces --- .../namespace/resource_namespace.go | 1 - pkg/api/v1/clients/client_interface.go | 4 +- .../templates/snapshot_emitter_template.go | 48 +++++++++++-------- .../v1alpha1/testing_snapshot_emitter.sk.go | 46 +++++++++++------- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 5fb473ce6..d1d6b0d78 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -138,7 +138,6 @@ func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resour default: resourceNamespaceList, err := client.GetNamespaceResourceList(resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, - FieldSelectors: excludeNamespaces, ExpressionSelector: opts.ExpressionSelector, }, filtered) if err != nil { diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 23025d2e6..02945ee5f 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -193,11 +193,11 @@ func TranslateWatchOptsIntoListOpts(wopts WatchOpts) ListOpts { // TODO-JAKE maybe they should be the same type of options? // TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options func TranslateResourceNamespaceListToListOptions(lopts resources.ResourceNamespaceListOptions) ListOpts { - clopts := ListOpts{Ctx: lopts.Ctx, FieldSelectors: lopts.FieldSelectors, ExpressionSelector: lopts.ExpressionSelector} + clopts := ListOpts{Ctx: lopts.Ctx, ExpressionSelector: lopts.ExpressionSelector} return clopts } func TranslateResourceNamespaceListToWatchOptions(wopts resources.ResourceNamespaceWatchOptions) WatchOpts { - clopts := WatchOpts{Ctx: wopts.Ctx, FieldSelectors: wopts.FieldSelectors, ExpressionSelector: wopts.ExpressionSelector} + clopts := WatchOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector} return clopts } diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 7930c644b..85f12ff55 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -137,7 +137,12 @@ type {{ lower_camel .GoName }}Emitter struct { {{- range .Resources}} {{ lower_camel .Name }} {{ .ImportPrefix }}{{ .Name }}Client {{- end}} + // resourceNamespaceLister is used to watch for new namespaces when they are created. + // It is used when Expression Selector is in the Watch Opts set in Snapshot(). resourceNamespaceLister resources.ResourceNamespaceLister + // namespacesWatching is the set of namespaces that we are watching. This is helpful + // when Expression Selector is set on the Watch Opts in Snapshot(). + namespacesWatching sync.Map } func (c *{{ lower_camel .GoName }}Emitter) Register() error { @@ -160,6 +165,15 @@ func (c *{{ lower_camel $.GoName }}Emitter) {{ .Name }}() {{ .ImportPrefix }}{{ // event_loop and resource clients -> resource client implementations work in a README.md // this would be helpful for documentation purposes +// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. +// we will need a way to deal with DELETES and CREATES and updates seperately +// I believe this is delt with in the last tests, but I want to check the snapshots once more. +// with the interface, we have lost the ability to know the event type. +// so the interface must be able to identify the type of event that occured as well +// not just return the list of namespaces + +// TODO-JAKE test that we can create a huge field selector of massive size + func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -234,6 +248,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} /* Watch for changes and update snapshot */ go func(namespace string) { + defer func () { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -259,8 +277,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the Expression - // TODO-JAKE might want to get rid of the FieldSelectors - //setting up the options for both Listing and Watching namespaces namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -270,7 +286,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o ExpressionSelector: opts.ExpressionSelector, } - // TODO-JAKE test that we can create a huge field selector of massive size filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { if ns != "" { @@ -333,10 +348,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. - // we will need a way to deal with DELETES and CREATES and updates seperately - // I believe this is delt with in the last tests, but I want to check the snapshots once more. - // watch for new namespaces // TODO-JAKE not sure if I need to watch the <- chan error here... or not namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) @@ -353,23 +364,18 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o if !ok { return } - // TODO-JAKE with the interface, we have lost the ability to know the event type. - // so the interface must be able to identify the type of event that occured as well - // not just return the list of namespaces - newNamespaces := []string{} + // get the list of new namespaces, if there is a new namespace + // get the list of resources from that namespace, and add + // a watch for new resources created/deleted on that namespace - // TODO-JAKE get a map of the namespaces that are currently being watched + newNamespaces := []string{} for _, ns := range resourceNamespaces { -{{- range .Resources }} -{{- if (not .ClusterScoped) }} - if _, hit := {{ lower_camel .PluralName }}ByNamespace.Load(ns.Name); !hit { + if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } -{{- end }} -{{- end }} } - // add a watch for all the new namespaces + for _, namespace := range newNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} @@ -377,8 +383,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") continue } @@ -404,6 +408,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o /* Watch for changes and update snapshot */ // REFACTOR go func(namespace string) { + defer func () { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 48c28f3f8..c581a53ed 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -100,6 +100,9 @@ type testingEmitter struct { forceEmit <-chan struct{} mockResource MockResourceClient resourceNamespaceLister resources.ResourceNamespaceLister + // namespacesWatching is the set of namespaces that we are watching. This is helpful + // when Expression Selector is set on the Watch Opts in Snapshot(). + namespacesWatching sync.Map } func (c *testingEmitter) Register() error { @@ -117,6 +120,15 @@ func (c *testingEmitter) MockResource() MockResourceClient { // event_loop and resource clients -> resource client implementations work in a README.md // this would be helpful for documentation purposes +// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. +// we will need a way to deal with DELETES and CREATES and updates seperately +// I believe this is delt with in the last tests, but I want to check the snapshots once more. +// with the interface, we have lost the ability to know the event type. +// so the interface must be able to identify the type of event that occured as well +// not just return the list of namespaces + +// TODO-JAKE test that we can create a huge field selector of massive size + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -176,6 +188,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { + defer func () { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -196,9 +212,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression - // TODO-JAKE might want to get rid of the FieldSelectors - //setting up the options for both Listing and Watching namespaces + // watch resources of non-watched namespaces that fit the Expression Selector namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -208,7 +222,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ExpressionSelector: opts.ExpressionSelector, } - // TODO-JAKE test that we can create a huge field selector of massive size filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { if ns != "" { @@ -263,10 +276,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. - // we will need a way to deal with DELETES and CREATES and updates seperately - // I believe this is delt with in the last tests, but I want to check the snapshots once more. - // watch for new namespaces // TODO-JAKE not sure if I need to watch the <- chan error here... or not namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) @@ -283,26 +292,23 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } - // TODO-JAKE with the interface, we have lost the ability to know the event type. - // so the interface must be able to identify the type of event that occured as well - // not just return the list of namespaces - newNamespaces := []string{} + // get the list of new namespaces, if there is a new namespace + // get the list of resources from that namespace, and add + // a watch for new resources created/deleted on that namespace - // TODO-JAKE get a map of the namespaces that are currently being watched + newNamespaces := []string{} for _, ns := range resourceNamespaces { - if _, hit := mocksByNamespace.Load(ns.Name); !hit { + if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } } - // add a watch for all the new namespaces + for _, namespace := range newNamespaces { /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } @@ -314,6 +320,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the // ResourceNamespaceLister currently // INFO-JAKE is this what we really want to do when there is an error? + // TODO-JAKE ensure that the MockResource is set to the correct format errs <- errors.Wrapf(err, "starting new namespace MockResource watch") continue } @@ -324,8 +331,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") }(namespace) /* Watch for changes and update snapshot */ - // REFACTOR go func(namespace string) { + defer func () { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): From af6eb5d64b268a4337ec85ff26db64cf146367e1 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 11:09:20 -0500 Subject: [PATCH 20/98] updated snapshot emitter, delt with err channels and with deleting namespace resources on deleted/missing namespaces when watching namespaces from Expression Selector --- .../namespace/resource_namespace.go | 26 +- pkg/api/v1/clients/client_interface.go | 12 +- pkg/api/v1/clients/mocks/client_interface.go | 3 +- pkg/api/v1/resources/resource_interface.go | 41 +- pkg/code-generator/codegen/templates/funcs.go | 3 + .../templates/snapshot_emitter_template.go | 60 ++- .../snapshot_emitter_test_template.go | 206 +++++---- .../v1/kubeconfigs_snapshot_emitter.sk.go | 95 ++-- .../v1/kubeconfigs_snapshot_emitter_test.go | 30 +- test/mocks/v1/testing_snapshot_emitter.sk.go | 163 ++++--- .../mocks/v1/testing_snapshot_emitter_test.go | 434 ++++++++++++++---- .../v1alpha1/testing_snapshot_emitter.sk.go | 57 ++- .../v1alpha1/testing_snapshot_emitter_test.go | 6 +- .../v2alpha1/testing_snapshot_emitter.sk.go | 113 +++-- .../v2alpha1/testing_snapshot_emitter_test.go | 98 +++- 15 files changed, 920 insertions(+), 427 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index d1d6b0d78..2e2942022 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -35,8 +35,8 @@ type kubeResourceNamespaceLister struct { namespace skkube.KubeNamespaceClient } -// GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces -func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { +// GetResourceNamespaceList is the kubernetes implementation that returns the list of namespaces +func (kns *kubeResourceNamespaceLister) GetResourceNamespaceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { namespaces, err := kns.namespace.List(clients.TranslateResourceNamespaceListToListOptions(opts)) if err != nil { return nil, err @@ -45,8 +45,8 @@ func (kns *kubeResourceNamespaceLister) GetNamespaceResourceList(opts resources. return kns.filter(converted, filtered), nil } -// GetNamespaceResourceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces -func (kns *kubeResourceNamespaceLister) GetNamespaceResourceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList, errs chan error) (chan resources.ResourceNamespaceList, <-chan error, error) { +// GetResourceNamespaceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces +func (kns *kubeResourceNamespaceLister) GetResourceNamespaceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList) (chan resources.ResourceNamespaceList, <-chan error, error) { ctx := opts.Ctx wopts := clients.TranslateResourceNamespaceListToWatchOptions(opts) namespaceChan, errorChan, err := kns.namespace.Watch(wopts) @@ -103,8 +103,8 @@ type kubeResourceNamespaceClient struct { kube kubernetes.Interface } -// GetNamespaceResourceList is the kubernetes implementation that returns the list of namespaces -func (client *kubeResourceNamespaceClient) GetNamespaceResourceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { +// GetResourceNamespaceList is the kubernetes implementation that returns the list of namespaces +func (client *kubeResourceNamespaceClient) GetResourceNamespaceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { excludeNamespaces := client.getExcludeFieldSelector(filtered) namespaceList, err := client.kube.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) if err != nil { @@ -113,8 +113,8 @@ func (client *kubeResourceNamespaceClient) GetNamespaceResourceList(opts resourc return convertNamespaceListToResourceNamespaceList(namespaceList), nil } -// GetNamespaceResourceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces -func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList, errs chan error) (chan resources.ResourceNamespaceList, <-chan error, error) { +// GetResourceNamespaceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces +func (client *kubeResourceNamespaceClient) GetResourceNamespaceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList) (chan resources.ResourceNamespaceList, <-chan error, error) { excludeNamespaces := client.getExcludeFieldSelector(filtered) namespaceWatcher, err := client.kube.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) if err != nil { @@ -122,6 +122,7 @@ func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resour } namespaceChan := namespaceWatcher.ResultChan() resourceNamespaceChan := make(chan resources.ResourceNamespaceList) + errorChannel := make(chan error) go func() { for { select { @@ -133,15 +134,15 @@ func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resour } switch event.Type { case kubewatch.Error: - errs <- errors.Errorf("error with the event from watching namespaces: %v", event) + errorChannel <- errors.Errorf("error with the event from watching namespaces: %v", event) return default: - resourceNamespaceList, err := client.GetNamespaceResourceList(resources.ResourceNamespaceListOptions{ + resourceNamespaceList, err := client.GetResourceNamespaceList(resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, }, filtered) if err != nil { - errs <- errors.Wrap(err, "error getting the list of namespaces while watching") + errorChannel <- errors.Wrap(err, "error getting the list of resource namespaces while watching") return } resourceNamespaceChan <- resourceNamespaceList @@ -149,8 +150,7 @@ func (client *kubeResourceNamespaceClient) GetNamespaceResourceWatch(opts resour } } }() - // TODO-JAKE do we need a <- chan error - return resourceNamespaceChan, nil, nil + return resourceNamespaceChan, errorChannel, nil } func (client *kubeResourceNamespaceClient) getExcludeFieldSelector(filtered resources.ResourceNamespaceList) string { diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 02945ee5f..169787baf 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -128,8 +128,6 @@ type ListOpts struct { // (2) the label key equal to version and the value equal to v1 // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string - // TODO-JAKE add in Field Selectors - FieldSelectors string } func (o ListOpts) WithDefaults() ListOpts { @@ -139,8 +137,6 @@ func (o ListOpts) WithDefaults() ListOpts { return o } -// TODO-JAKE do we want to combine the WatchOpts, ListOpts, and ResourceNamespaceOpts??? - // RefreshRate is currently ignored by the Kubernetes ResourceClient implementation. // To achieve a similar behavior you can use the KubeResourceClientFactory.ResyncPeriod field. The difference is that it // will apply to all the watches started by clients built with the factory. @@ -168,9 +164,7 @@ type WatchOpts struct { // (2) the label key equal to version and the value equal to v1 // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string - // JAKE-TODO - FieldSelectors string - RefreshRate time.Duration + RefreshRate time.Duration // Cluster is ignored by aggregated watches, but is respected by multi cluster clients. Cluster string } @@ -186,17 +180,17 @@ func (o WatchOpts) WithDefaults() WatchOpts { } func TranslateWatchOptsIntoListOpts(wopts WatchOpts) ListOpts { - clopts := ListOpts{Ctx: wopts.Ctx, FieldSelectors: wopts.FieldSelectors, ExpressionSelector: wopts.ExpressionSelector, Selector: wopts.Selector} + clopts := ListOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector, Selector: wopts.Selector} return clopts } -// TODO-JAKE maybe they should be the same type of options? // TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options func TranslateResourceNamespaceListToListOptions(lopts resources.ResourceNamespaceListOptions) ListOpts { clopts := ListOpts{Ctx: lopts.Ctx, ExpressionSelector: lopts.ExpressionSelector} return clopts } +// TranslateResourceNamespaceListToWatchOptions translates the resource namespace watch options to Watch Options func TranslateResourceNamespaceListToWatchOptions(wopts resources.ResourceNamespaceWatchOptions) WatchOpts { clopts := WatchOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector} return clopts diff --git a/pkg/api/v1/clients/mocks/client_interface.go b/pkg/api/v1/clients/mocks/client_interface.go index 132839c31..bc3832ab1 100644 --- a/pkg/api/v1/clients/mocks/client_interface.go +++ b/pkg/api/v1/clients/mocks/client_interface.go @@ -5,11 +5,10 @@ package mocks import ( - reflect "reflect" - gomock "github.com/golang/mock/gomock" clients "github.com/solo-io/solo-kit/pkg/api/v1/clients" resources "github.com/solo-io/solo-kit/pkg/api/v1/resources" + reflect "reflect" ) // MockResourceWatcher is a mock of ResourceWatcher interface diff --git a/pkg/api/v1/resources/resource_interface.go b/pkg/api/v1/resources/resource_interface.go index 9f6bc1c30..9d3ca6d55 100644 --- a/pkg/api/v1/resources/resource_interface.go +++ b/pkg/api/v1/resources/resource_interface.go @@ -81,16 +81,20 @@ type CustomInputResource interface { MarshalStatus() (v1.Status, error) } -// ResourceNamespaceListOptions provides the options for listing Resoiurce Namespaces +// ResourceNamespaceListOptions provides the options for listing Resource Namespaces type ResourceNamespaceListOptions struct { // Ctx is the context Ctx context.Context - // FieldSelectors are used to filter out specific fields that are associated with - // the Namespace. IE if using Kubernetes you can filter namespaces by the name - // of the namespace by using metadata.name!= - // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ - FieldSelectors string - // TODO-JAKE add description + + // Equality-based label requirements + // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement + // Equality-based requirements allow filtering by label keys and values. + // Matching objects must satisfy all of the specified label constraints, + // though they may have additional labels as well. + // Example: + // {product: edge} would return all objects with a label key equal to + // product and label value equal to edge + // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string } @@ -98,12 +102,15 @@ type ResourceNamespaceListOptions struct { type ResourceNamespaceWatchOptions struct { // Ctx is the context Ctx context.Context - // FieldSelectors are used to filter out specific fields that are associated with - // the Namespace. IE if using Kubernetes you can filter namespaces by the name - // of the namespace by using metadata.name!= - // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ - FieldSelectors string - // TODO-JAKE add description + // Equality-based label requirements + // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement + // Equality-based requirements allow filtering by label keys and values. + // Matching objects must satisfy all of the specified label constraints, + // though they may have additional labels as well. + // Example: + // {product: edge} would return all objects with a label key equal to + // product and label value equal to edge + // If both ExpressionSelector and Selector are defined, ExpressionSelector is preferred ExpressionSelector string } @@ -121,13 +128,13 @@ type ResourceNamespaceList []ResourceNamespace // ResourceNamespaceLister is anything that can list and watch namespaces that // resources can be found. type ResourceNamespaceLister interface { - // GetNamespaceResourceList returns the list of the namespaces that resources + // GetResourceNamespaceList returns the list of the namespaces that resources // can be found. The list returned will not contain namespacesToFilter. - GetNamespaceResourceList(opts ResourceNamespaceListOptions, namespacesToFilter ResourceNamespaceList) (ResourceNamespaceList, error) - // GetNamespaceResourceWatch returns a watch that receives events when namespaces + GetResourceNamespaceList(opts ResourceNamespaceListOptions, namespacesToFilter ResourceNamespaceList) (ResourceNamespaceList, error) + // GetResourceNamespaceWatch returns a watch that receives events when namespaces // are updated or created. The channel will not return namespacesToFilter. Use the errs for when // errors are async. - GetNamespaceResourceWatch(opts ResourceNamespaceWatchOptions, namespacesToFilter ResourceNamespaceList, errs chan error) (chan ResourceNamespaceList, <-chan error, error) + GetResourceNamespaceWatch(opts ResourceNamespaceWatchOptions, namespacesToFilter ResourceNamespaceList) (chan ResourceNamespaceList, <-chan error, error) } type ResourceList []Resource diff --git a/pkg/code-generator/codegen/templates/funcs.go b/pkg/code-generator/codegen/templates/funcs.go index c9fd6642f..7ba2c556d 100644 --- a/pkg/code-generator/codegen/templates/funcs.go +++ b/pkg/code-generator/codegen/templates/funcs.go @@ -88,6 +88,9 @@ var Funcs = template.FuncMap{ "backtick": func() string { return "`" }, + "minus": func(a, b int) int { + return a - b + }, } func printPointer(format string, p *string) string { diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 85f12ff55..eecd8d360 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -11,6 +11,7 @@ import ( // resources. // ClusterScoped - without namespacing, get all the resources within the entire cluster. There is one watch per resource. +// this means that Expression Selectors will have no impact on namespaces. // Not using ClusterScoped - allows for using namespacing, so that each namespace has it's own watch per resource. var ResourceGroupEmitterTemplate = template.Must(template.New("resource_group_emitter").Funcs(Funcs).Parse( `package {{ .Project.ProjectConfig.Version }} @@ -248,7 +249,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} /* Watch for changes and update snapshot */ go func(namespace string) { - defer func () { + defer func() { c.namespacesWatching.Delete(namespace) }() c.namespacesWatching.Store(namespace, true) @@ -276,7 +277,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression + // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -292,11 +293,11 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) + namespacesResources, err := c.resourceNamespaceLister.GetResourceNamespaceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - // non Watched Namespaces + // non watched namespaces for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name {{- range .Resources }} @@ -348,12 +349,22 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // watch for new namespaces - // TODO-JAKE not sure if I need to watch the <- chan error here... or not - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) + namespaceWatch, errsReceiver, err := c.resourceNamespaceLister.GetResourceNamespaceWatch(namespaceWatchOptions, filterNamespaces) if err != nil { return nil, nil, err } + if errsReceiver != nil { + go func() { + for { + select{ + case <-ctx.Done(): + return + case err = <- errsReceiver: + errs <- errors.Wrapf(err, "received error from watch on resource namespaces") + } + } + }() + } go func() { for { @@ -376,6 +387,30 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } } + // delete the missing/deleted namespaces + mapOfNamespaces := make(map[string]bool) + for _,ns := range resourceNamespaces { + mapOfNamespaces[ns.Name] = true + } + + missingNamespaces := []string{} + c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { + name := key.(string) + if _, hit := mapOfNamespaces[name]; !hit { + missingNamespaces = append(missingNamespaces, name) + } + return true + }) + + for _, ns := range missingNamespaces { + c.namespacesWatching.Delete(ns) +{{- range .Resources}} +{{- if not .ClusterScoped }} + {{ lower_camel .PluralName }}ByNamespace.Delete(ns) +{{- end }} +{{- end }} + } + for _, namespace := range newNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} @@ -390,10 +425,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } {{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace {{ upper_camel .Name }} watch") continue } @@ -406,9 +437,8 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} /* Watch for changes and update snapshot */ - // REFACTOR go func(namespace string) { - defer func () { + defer func() { c.namespacesWatching.Delete(namespace) }() c.namespacesWatching.Store(namespace, true) @@ -439,10 +469,9 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } {{- range .Resources}} {{- if .ClusterScoped }} - // TODO-JAKE verify that this is what we should be doing with Cluster Scoped Resources /* Setup cluster-wide watch for {{ .Name }} */ var err error - currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, Selector: opts.Selector}) + currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") } @@ -559,4 +588,5 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }() return snapshots, errs, nil } + `)) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index aba4fc44c..8a8c47100 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -194,8 +194,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMessageSent() + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -212,8 +212,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } - assertSnapshot{{ .PluralName }}(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}2a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -230,8 +230,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}3a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -249,14 +249,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - - // TODO need to fix clusterScoped {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) - assertNoMessageSent() + watched = append(watched, {{ lower_camel .Name }}4a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -272,8 +271,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}5a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -291,8 +290,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}6a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -313,8 +312,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - assertNoMessageSent() + watched = append(watched, {{ lower_camel .Name }}7a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -332,8 +331,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) - assertSnapshot{{ .PluralName }}(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}8a ) + assertSnapshot{{ .PluralName }}(watched, nil) {{- else }} @@ -344,20 +343,25 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) assertNoMessageSent() -{{- end }} - for _, r := range notWatched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() +{{- end }} + + {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched ={{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} @@ -374,10 +378,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a}...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} @@ -394,10 +400,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} @@ -414,10 +420,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a} assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} @@ -434,9 +440,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) assertSnapshot{{ .PluralName }}(nil, notWatched) {{- else }} @@ -832,11 +838,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO need to add in ClusterScoped {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMocksSent() + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshotMocks(watched, nil) {{- else }} @@ -856,8 +861,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }...) - assertNoMocksSent() + watched = append(watched, {{ lower_camel .Name }}2a ) + assertSnapshotMocks(watched, nil) {{- else }} @@ -875,7 +880,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(watched, nil) {{- else }} @@ -892,8 +897,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a }...) - assertSnapshotMocks(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}4a ) + assertSnapshotMocks(watched, nil) {{- else }} @@ -912,8 +917,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - assertNoMocksSent() + watched = append(watched, {{ lower_camel .Name }}5a) + assertSnapshotMocks(watched, nil) {{- else }} @@ -930,8 +935,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a }...) - assertSnapshotMocks(watched, notWatched) + watched = append(watched, {{ lower_camel .Name }}6a) + assertSnapshotMocks(watched, nil) {{- else }} @@ -949,8 +954,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - assertNoMessageSent() + watched = append(watched, {{ lower_camel .Name }}7a) + assertSnapshotMocks(watched, nil) {{- else }} @@ -961,19 +966,25 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) assertNoMessageSent() -{{- end }} - for _, r := range notWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() +{{- end }} + {{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}6a } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = append(notWatched, {{ lower_camel .Name }}2a) assertSnapshot{{ .PluralName }}(watched, notWatched) {{- else }} @@ -990,11 +1001,35 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = append(notWatched, {{ lower_camel .Name }}3a) + assertSnapshot{{ .PluralName }}(watched, notWatched) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a } assertSnapshot{{ .PluralName }}(watched, notWatched) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } + assertSnapshot{{ .PluralName }}(watched, notWatched) + + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(nil, notWatched) + {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1023,21 +1058,23 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *TestingSnapshot +{{- range .Resources }} + /* - MockResource + {{ .Name }} */ - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: for { select { case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain } } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { continue drain } } @@ -1045,24 +1082,29 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) +{{- if .ClusterScoped }} + combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) +{{- else }} + nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) + nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) combined := append(nsList1, nsList2...) +{{- end }} Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } - } + } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} + watched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} assertSnapshotMocks(watched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockResource1a, mockResource1b} + notWatched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} assertSnapshotMocks(nil, notWatched) +{{- end }} }) It("Should not contain resources from a deleted namespace, that is filtered", func () { @@ -1097,6 +1139,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } {{- $length := len .Resources }} +{{- $last_entry := minus $length 1 }} {{- range $i, $r := .Resources }} {{ with $r }} /* @@ -1136,11 +1179,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO need to add in ClusterScoped {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMocksSent() + watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshotMocks(watched, nil) {{- else }} @@ -1153,7 +1195,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -1163,11 +1204,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO need to add in ClusterScoped - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMocksSent() + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} + assertSnapshotMocks(watched, nil) {{- else }} @@ -1180,7 +1220,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, {{ lower_camel .Name }}2a) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b} @@ -1190,11 +1229,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - // TODO need to add in ClusterScoped - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertNoMocksSent() + watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace4) + assertSnapshotMocks(watched, nil) {{- else }} @@ -1203,8 +1244,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func watched = append(watched, {{ lower_camel .Name }}3a) assertSnapshotMocks(watched, notWatched) -{{- end }} - deleteNamespaces(ctx, kube, namespace4) notWatched = append(notWatched, {{ lower_camel .Name }}2b) watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} @@ -1216,11 +1255,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } assertNoMocksSent() +{{- end }} + + deleteNamespaces(ctx, kube, namespace5) + {{/* after each resource we have to create the namespaces that were just deleted */}} -{{if ne $length $i }} +{{if ne $last_entry $i }} createNamespaces(ctx, kube, namespace1, namespace2) {{- end }} - {{- end }}{{/* end of with */}} {{- end }}{{/* end of range */}} }) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 8e0f30083..7b6e92f4b 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -97,9 +97,14 @@ func NewKubeconfigsEmitterWithEmit(kubeConfigClient KubeConfigClient, resourceNa } type kubeconfigsEmitter struct { - forceEmit <-chan struct{} - kubeConfig KubeConfigClient + forceEmit <-chan struct{} + kubeConfig KubeConfigClient + // resourceNamespaceLister is used to watch for new namespaces when they are created. + // It is used when Expression Selector is in the Watch Opts set in Snapshot(). resourceNamespaceLister resources.ResourceNamespaceLister + // namespacesWatching is the set of namespaces that we are watching. This is helpful + // when Expression Selector is set on the Watch Opts in Snapshot(). + namespacesWatching sync.Map } func (c *kubeconfigsEmitter) Register() error { @@ -117,6 +122,15 @@ func (c *kubeconfigsEmitter) KubeConfig() KubeConfigClient { // event_loop and resource clients -> resource client implementations work in a README.md // this would be helpful for documentation purposes +// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. +// we will need a way to deal with DELETES and CREATES and updates seperately +// I believe this is delt with in the last tests, but I want to check the snapshots once more. +// with the interface, we have lost the ability to know the event type. +// so the interface must be able to identify the type of event that occured as well +// not just return the list of namespaces + +// TODO-JAKE test that we can create a huge field selector of massive size + func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *KubeconfigsSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -176,6 +190,10 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -196,9 +214,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression - // TODO-JAKE might want to get rid of the FieldSelectors - //setting up the options for both Listing and Watching namespaces + // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -208,18 +224,17 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa ExpressionSelector: opts.ExpressionSelector, } - // TODO-JAKE test that we can create a huge field selector of massive size filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) + namespacesResources, err := c.resourceNamespaceLister.GetResourceNamespaceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - // non Watched Namespaces + // non watched namespaces for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for KubeConfig */ @@ -263,16 +278,22 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. - // we will need a way to deal with DELETES and CREATES and updates seperately - // I believe this is delt with in the last tests, but I want to check the snapshots once more. - - // watch for new namespaces - // TODO-JAKE not sure if I need to watch the <- chan error here... or not - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) + namespaceWatch, errsReceiver, err := c.resourceNamespaceLister.GetResourceNamespaceWatch(namespaceWatchOptions, filterNamespaces) if err != nil { return nil, nil, err } + if errsReceiver != nil { + go func() { + for { + select { + case <-ctx.Done(): + return + case err = <-errsReceiver: + errs <- errors.Wrapf(err, "received error from watch on resource namespaces") + } + } + }() + } go func() { for { @@ -283,26 +304,43 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa if !ok { return } - // TODO-JAKE with the interface, we have lost the ability to know the event type. - // so the interface must be able to identify the type of event that occured as well - // not just return the list of namespaces - newNamespaces := []string{} + // get the list of new namespaces, if there is a new namespace + // get the list of resources from that namespace, and add + // a watch for new resources created/deleted on that namespace - // TODO-JAKE get a map of the namespaces that are currently being watched + newNamespaces := []string{} for _, ns := range resourceNamespaces { - if _, hit := kubeconfigsByNamespace.Load(ns.Name); !hit { + if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } } - // add a watch for all the new namespaces + + // delete the missing/deleted namespaces + mapOfNamespaces := make(map[string]bool) + for _, ns := range resourceNamespaces { + mapOfNamespaces[ns.Name] = true + } + + missingNamespaces := []string{} + c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { + name := key.(string) + if _, hit := mapOfNamespaces[name]; !hit { + missingNamespaces = append(missingNamespaces, name) + } + return true + }) + + for _, ns := range missingNamespaces { + c.namespacesWatching.Delete(ns) + kubeconfigsByNamespace.Delete(ns) + } + for _, namespace := range newNamespaces { /* Setup namespaced watch for KubeConfig for new namespace */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") continue } @@ -310,10 +348,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } kubeConfigNamespacesChan, kubeConfigErrs, err := c.kubeConfig.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace KubeConfig watch") continue } @@ -324,8 +358,11 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa errutils.AggregateErrs(ctx, errs, kubeConfigErrs, namespace+"-new-namespace-kubeconfigs") }(namespace) /* Watch for changes and update snapshot */ - // REFACTOR go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 422feee7f..55bf5e7c6 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -181,6 +181,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfig(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -620,20 +621,20 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot /* - MockResource + KubeConfig */ - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: for { select { case snap = <-snapshots: - for _, expected := range expectMocks { - if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + for _, expected := range expectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain } } - for _, unexpected := range unexpectMocks { - if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + for _, unexpected := range unexpectkubeconfigs { + if _, err := snap.Kubeconfigs.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { continue drain } } @@ -641,23 +642,23 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) + nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) combined := append(nsList1, nsList2...) Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} + watched := MockResourceList{kubeConfig1a, kubeConfig1b} assertSnapshotMocks(watched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockResource1a, mockResource1b} + notWatched := MockResourceList{kubeConfig1a, kubeConfig1b} assertSnapshotMocks(nil, notWatched) }) @@ -731,7 +732,6 @@ var _ = Describe("V1Emitter", func() { notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -746,7 +746,6 @@ var _ = Describe("V1Emitter", func() { watched := KubeConfigList{kubeConfig2a, kubeConfig2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, kubeConfig2a) watched = KubeConfigList{kubeConfig2b} @@ -770,7 +769,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace5) + }) }) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 20e857065..c19e75ef9 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -111,15 +111,20 @@ func NewTestingEmitterWithEmit(simpleMockResourceClient SimpleMockResourceClient } type testingEmitter struct { - forceEmit <-chan struct{} - simpleMockResource SimpleMockResourceClient - mockResource MockResourceClient - fakeResource FakeResourceClient - anotherMockResource AnotherMockResourceClient - clusterResource ClusterResourceClient - mockCustomType MockCustomTypeClient - pod github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + forceEmit <-chan struct{} + simpleMockResource SimpleMockResourceClient + mockResource MockResourceClient + fakeResource FakeResourceClient + anotherMockResource AnotherMockResourceClient + clusterResource ClusterResourceClient + mockCustomType MockCustomTypeClient + pod github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + // resourceNamespaceLister is used to watch for new namespaces when they are created. + // It is used when Expression Selector is in the Watch Opts set in Snapshot(). resourceNamespaceLister resources.ResourceNamespaceLister + // namespacesWatching is the set of namespaces that we are watching. This is helpful + // when Expression Selector is set on the Watch Opts in Snapshot(). + namespacesWatching sync.Map } func (c *testingEmitter) Register() error { @@ -179,6 +184,15 @@ func (c *testingEmitter) Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_ // event_loop and resource clients -> resource client implementations work in a README.md // this would be helpful for documentation purposes +// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. +// we will need a way to deal with DELETES and CREATES and updates seperately +// I believe this is delt with in the last tests, but I want to check the snapshots once more. +// with the interface, we have lost the ability to know the event type. +// so the interface must be able to identify the type of event that occured as well +// not just return the list of namespaces + +// TODO-JAKE test that we can create a huge field selector of massive size + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -374,6 +388,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -439,9 +457,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression - // TODO-JAKE might want to get rid of the FieldSelectors - //setting up the options for both Listing and Watching namespaces + // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -451,18 +467,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ExpressionSelector: opts.ExpressionSelector, } - // TODO-JAKE test that we can create a huge field selector of massive size filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) + namespacesResources, err := c.resourceNamespaceLister.GetResourceNamespaceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - // non Watched Namespaces + // non watched namespaces for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for SimpleMockResource */ @@ -646,16 +661,22 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. - // we will need a way to deal with DELETES and CREATES and updates seperately - // I believe this is delt with in the last tests, but I want to check the snapshots once more. - - // watch for new namespaces - // TODO-JAKE not sure if I need to watch the <- chan error here... or not - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) + namespaceWatch, errsReceiver, err := c.resourceNamespaceLister.GetResourceNamespaceWatch(namespaceWatchOptions, filterNamespaces) if err != nil { return nil, nil, err } + if errsReceiver != nil { + go func() { + for { + select { + case <-ctx.Done(): + return + case err = <-errsReceiver: + errs <- errors.Wrapf(err, "received error from watch on resource namespaces") + } + } + }() + } go func() { for { @@ -666,46 +687,48 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } - // TODO-JAKE with the interface, we have lost the ability to know the event type. - // so the interface must be able to identify the type of event that occured as well - // not just return the list of namespaces - newNamespaces := []string{} + // get the list of new namespaces, if there is a new namespace + // get the list of resources from that namespace, and add + // a watch for new resources created/deleted on that namespace - // TODO-JAKE get a map of the namespaces that are currently being watched + newNamespaces := []string{} for _, ns := range resourceNamespaces { - if _, hit := simplemocksByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue - } - if _, hit := mocksByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue - } - if _, hit := fakesByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue - } - if _, hit := anothermockresourcesByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue - } - if _, hit := mctsByNamespace.Load(ns.Name); !hit { + if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - if _, hit := podsByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue + } + + // delete the missing/deleted namespaces + mapOfNamespaces := make(map[string]bool) + for _, ns := range resourceNamespaces { + mapOfNamespaces[ns.Name] = true + } + + missingNamespaces := []string{} + c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { + name := key.(string) + if _, hit := mapOfNamespaces[name]; !hit { + missingNamespaces = append(missingNamespaces, name) } + return true + }) + + for _, ns := range missingNamespaces { + c.namespacesWatching.Delete(ns) + simplemocksByNamespace.Delete(ns) + mocksByNamespace.Delete(ns) + fakesByNamespace.Delete(ns) + anothermockresourcesByNamespace.Delete(ns) + mctsByNamespace.Delete(ns) + podsByNamespace.Delete(ns) } - // add a watch for all the new namespaces + for _, namespace := range newNamespaces { /* Setup namespaced watch for SimpleMockResource for new namespace */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") continue } @@ -713,10 +736,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace SimpleMockResource watch") continue } @@ -730,8 +749,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } @@ -739,10 +756,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace MockResource watch") continue } @@ -756,8 +769,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } @@ -765,10 +776,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") continue } @@ -782,8 +789,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") continue } @@ -791,10 +796,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } anotherMockResourceNamespacesChan, anotherMockResourceErrs, err := c.anotherMockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace AnotherMockResource watch") continue } @@ -808,8 +809,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") continue } @@ -817,10 +816,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mockCustomTypeNamespacesChan, mockCustomTypeErrs, err := c.mockCustomType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace MockCustomType watch") continue } @@ -834,8 +829,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace Pod list") continue } @@ -843,10 +836,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } podNamespacesChan, podErrs, err := c.pod.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace Pod watch") continue } @@ -857,8 +846,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-new-namespace-pods") }(namespace) /* Watch for changes and update snapshot */ - // REFACTOR go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -933,10 +925,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot.Fakes = initialFakeResourceList.Sort() /* Initialize snapshot for Anothermockresources */ currentSnapshot.Anothermockresources = initialAnotherMockResourceList.Sort() - // TODO-JAKE verify that this is what we should be doing with Cluster Scoped Resources /* Setup cluster-wide watch for ClusterResource */ var err error - currentSnapshot.Clusterresources, err = c.clusterResource.List(clients.ListOpts{Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, Selector: opts.Selector}) + currentSnapshot.Clusterresources, err = c.clusterResource.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { return nil, nil, errors.Wrapf(err, "initial ClusterResource list") } diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index ebd4608b8..e5cf93b5c 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -222,6 +222,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -362,6 +363,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -502,6 +504,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + fakeResource4a, err := fakeResourceClient.Write(NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -642,6 +645,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -759,84 +763,83 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMessageSent() + watched := ClusterResourceList{clusterResource1a} + assertSnapshotClusterresources(watched, nil) clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource2a} - assertSnapshotClusterresources(watched, notWatched) + watched = append(watched, clusterResource2a) + assertSnapshotClusterresources(watched, nil) clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource3a}...) - assertSnapshotClusterresources(watched, notWatched) + watched = append(watched, clusterResource3a) + assertSnapshotClusterresources(watched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) - // TODO need to fix clusterScoped clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource4a}...) - assertNoMessageSent() + watched = append(watched, clusterResource4a) + assertSnapshotClusterresources(watched, nil) clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource5a}...) - assertSnapshotClusterresources(watched, notWatched) + watched = append(watched, clusterResource5a) + assertSnapshotClusterresources(watched, nil) clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource6a}...) - assertSnapshotClusterresources(watched, notWatched) + watched = append(watched, clusterResource6a) + assertSnapshotClusterresources(watched, nil) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) - assertNoMessageSent() + watched = append(watched, clusterResource7a) + assertSnapshotClusterresources(watched, nil) clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource8a}...) - assertSnapshotClusterresources(watched, notWatched) - - for _, r := range notWatched { - err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() + watched = append(watched, clusterResource8a) + assertSnapshotClusterresources(watched, nil) + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = ClusterResourceList{clusterResource2a} - watched = ClusterResourceList{clusterResource3a, clusterResource5a, clusterResource6a, clusterResource7a} - assertSnapshotClusterresources(watched, notWatched) - err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource3a}...) - watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} + notWatched = ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} + watched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a, clusterResource8a} assertSnapshotClusterresources(watched, notWatched) + err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) - watched = ClusterResourceList{clusterResource6a, clusterResource7a} + notWatched = append(notWatched, ClusterResourceList{clusterResource4a, clusterResource5a}...) + watched = ClusterResourceList{clusterResource6a, clusterResource7a, clusterResource8a} assertSnapshotClusterresources(watched, notWatched) err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) - watched = ClusterResourceList{clusterResource7a} + watched = ClusterResourceList{clusterResource7a, clusterResource8a} assertSnapshotClusterresources(watched, notWatched) err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) + watched = ClusterResourceList{clusterResource8a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = append(notWatched, ClusterResourceList{clusterResource8a}...) assertSnapshotClusterresources(nil, notWatched) /* @@ -892,6 +895,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -1032,6 +1036,7 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -2526,63 +2531,86 @@ var _ = Describe("V1Emitter", func() { } } - // TODO need to add in ClusterScoped clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMocksSent() + watched := ClusterResourceList{clusterResource1a} + assertSnapshotMocks(watched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource2a}...) - assertNoMocksSent() + watched = append(watched, clusterResource2a) + assertSnapshotMocks(watched, nil) clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) watched := ClusterResourceList{clusterResource3a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(watched, nil) clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource4a}...) - assertSnapshotMocks(watched, notWatched) + watched = append(watched, clusterResource4a) + assertSnapshotMocks(watched, nil) createNamespaces(ctx, kube, namespace5, namespace6) clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource5a}...) - assertNoMocksSent() + watched = append(watched, clusterResource5a) + assertSnapshotMocks(watched, nil) clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, ClusterResourceList{clusterResource5a}...) - assertSnapshotMocks(watched, notWatched) + watched = append(watched, clusterResource6a) + assertSnapshotMocks(watched, nil) clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) - assertNoMessageSent() + watched = append(watched, clusterResource7a) + assertSnapshotMocks(watched, nil) - for _, r := range notWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMessageSent() + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource2a, clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + notWatched = ClusterResourceList{clusterResource1a} + assertSnapshotClusterresources(watched, notWatched) + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + notWatched = append(notWatched, clusterResource2a) + assertSnapshotClusterresources(watched, notWatched) err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource4a, clusterResource6a} + watched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + notWatched = append(notWatched, clusterResource3a) assertSnapshotClusterresources(watched, notWatched) err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource6a} + watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} + notWatched = ClusterResourceList{clusterResource4a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource6a, clusterResource7a} + notWatched = ClusterResourceList{clusterResource5a} assertSnapshotClusterresources(watched, notWatched) + err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched = ClusterResourceList{clusterResource7a} + notWatched = ClusterResourceList{clusterResource6a} + assertSnapshotClusterresources(watched, notWatched) + + err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + notWatched = ClusterResourceList{clusterResource7a} + assertSnapshotClusterresources(nil, notWatched) + /* MockCustomType */ @@ -2809,6 +2837,47 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot + /* + SimpleMockResource + */ + assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectSimplemocks { + if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectSimplemocks { + if _, err := snap.Simplemocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotMocks(nil, notWatched) + /* MockResource */ @@ -2849,6 +2918,209 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace1, namespace2) notWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(nil, notWatched) + + /* + AnotherMockResource + */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotMocks(nil, notWatched) + + /* + ClusterResource + */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectClusterresources { + if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectClusterresources { + if _, err := snap.Clusterresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + combined, _ := clusterResourceClient.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + clusterResource1b, err := clusterResourceClient.Write(NewClusterResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{clusterResource1a, clusterResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{clusterResource1a, clusterResource1b} + assertSnapshotMocks(nil, notWatched) + + /* + MockCustomType + */ + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcts { + if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcts { + if _, err := snap.Mcts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{mockCustomType1a, mockCustomType1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{mockCustomType1a, mockCustomType1b} + assertSnapshotMocks(nil, notWatched) + + /* + Pod + */ + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectpods { + if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectpods { + if _, err := snap.Pods.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{pod1a, pod1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{pod1a, pod1b} + assertSnapshotMocks(nil, notWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -2921,7 +3193,6 @@ var _ = Describe("V1Emitter", func() { notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -2936,7 +3207,6 @@ var _ = Describe("V1Emitter", func() { watched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, simpleMockResource2a) watched = SimpleMockResourceList{simpleMockResource2b} @@ -2960,6 +3230,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3000,7 +3272,6 @@ var _ = Describe("V1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3015,7 +3286,6 @@ var _ = Describe("V1Emitter", func() { watched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, mockResource2a) watched = MockResourceList{mockResource2b} @@ -3039,6 +3309,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3079,7 +3351,6 @@ var _ = Describe("V1Emitter", func() { notWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3094,7 +3365,6 @@ var _ = Describe("V1Emitter", func() { watched := FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, fakeResource2a) watched = FakeResourceList{fakeResource2b} @@ -3118,6 +3388,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3158,7 +3430,6 @@ var _ = Describe("V1Emitter", func() { notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3173,7 +3444,6 @@ var _ = Describe("V1Emitter", func() { watched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, anotherMockResource2a) watched = AnotherMockResourceList{anotherMockResource2b} @@ -3197,6 +3467,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3228,13 +3500,11 @@ var _ = Describe("V1Emitter", func() { } } - // TODO need to add in ClusterScoped clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMocksSent() + watched := ClusterResourceList{clusterResource1a} + assertSnapshotMocks(watched, nil) - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3242,13 +3512,11 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) - // TODO need to add in ClusterScoped - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMocksSent() + watched = ClusterResourceList{clusterResource2a} + assertSnapshotMocks(watched, nil) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, clusterResource2a) watched = ClusterResourceList{clusterResource2b} @@ -3256,22 +3524,15 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace5, labels1) - // TODO need to add in ClusterScoped - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := ClusterResourceList{clusterResource1a} - assertNoMocksSent() + watched = ClusterResourceList{clusterResource3a} + assertSnapshotMocks(watched, nil) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, clusterResource2b) - watched = ClusterResourceList{clusterResource3a} - assertSnapshotMocks(watched, notWatched) + assertSnapshotMocks(watched, nil) - for _, r := range watched { - err = clusterResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - } - assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) createNamespaces(ctx, kube, namespace1, namespace2) @@ -3313,7 +3574,6 @@ var _ = Describe("V1Emitter", func() { notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3328,7 +3588,6 @@ var _ = Describe("V1Emitter", func() { watched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, mockCustomType2a) watched = MockCustomTypeList{mockCustomType2b} @@ -3352,6 +3611,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3392,7 +3653,6 @@ var _ = Describe("V1Emitter", func() { notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3407,7 +3667,6 @@ var _ = Describe("V1Emitter", func() { watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, pod2a) watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} @@ -3431,7 +3690,8 @@ var _ = Describe("V1Emitter", func() { } assertNoMocksSent() - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace5) + }) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index c581a53ed..662d1fb9e 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -97,8 +97,10 @@ func NewTestingEmitterWithEmit(mockResourceClient MockResourceClient, resourceNa } type testingEmitter struct { - forceEmit <-chan struct{} - mockResource MockResourceClient + forceEmit <-chan struct{} + mockResource MockResourceClient + // resourceNamespaceLister is used to watch for new namespaces when they are created. + // It is used when Expression Selector is in the Watch Opts set in Snapshot(). resourceNamespaceLister resources.ResourceNamespaceLister // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). @@ -188,7 +190,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { - defer func () { + defer func() { c.namespacesWatching.Delete(namespace) }() c.namespacesWatching.Store(namespace, true) @@ -212,7 +214,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression Selector + // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -228,11 +230,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) + namespacesResources, err := c.resourceNamespaceLister.GetResourceNamespaceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - // non Watched Namespaces + // non watched namespaces for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ @@ -276,12 +278,22 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // watch for new namespaces - // TODO-JAKE not sure if I need to watch the <- chan error here... or not - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) + namespaceWatch, errsReceiver, err := c.resourceNamespaceLister.GetResourceNamespaceWatch(namespaceWatchOptions, filterNamespaces) if err != nil { return nil, nil, err } + if errsReceiver != nil { + go func() { + for { + select { + case <-ctx.Done(): + return + case err = <-errsReceiver: + errs <- errors.Wrapf(err, "received error from watch on resource namespaces") + } + } + }() + } go func() { for { @@ -304,6 +316,26 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } } + // delete the missing/deleted namespaces + mapOfNamespaces := make(map[string]bool) + for _, ns := range resourceNamespaces { + mapOfNamespaces[ns.Name] = true + } + + missingNamespaces := []string{} + c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { + name := key.(string) + if _, hit := mapOfNamespaces[name]; !hit { + missingNamespaces = append(missingNamespaces, name) + } + return true + }) + + for _, ns := range missingNamespaces { + c.namespacesWatching.Delete(ns) + mocksByNamespace.Delete(ns) + } + for _, namespace := range newNamespaces { /* Setup namespaced watch for MockResource for new namespace */ { @@ -316,11 +348,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? - // TODO-JAKE ensure that the MockResource is set to the correct format errs <- errors.Wrapf(err, "starting new namespace MockResource watch") continue } @@ -332,7 +359,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { - defer func () { + defer func() { c.namespacesWatching.Delete(namespace) }() c.namespacesWatching.Store(namespace, true) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 9316a5163..7e84c64ec 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -183,6 +183,7 @@ var _ = Describe("V1Alpha1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -744,7 +745,6 @@ var _ = Describe("V1Alpha1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -759,7 +759,6 @@ var _ = Describe("V1Alpha1Emitter", func() { watched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, mockResource2a) watched = MockResourceList{mockResource2b} @@ -783,7 +782,8 @@ var _ = Describe("V1Alpha1Emitter", func() { } assertNoMocksSent() - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace5) + }) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 3baa5f209..3c4361994 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -107,7 +107,12 @@ type testingEmitter struct { mockResource MockResourceClient frequentlyChangingAnnotationsResource FrequentlyChangingAnnotationsResourceClient fakeResource testing_solo_io.FakeResourceClient - resourceNamespaceLister resources.ResourceNamespaceLister + // resourceNamespaceLister is used to watch for new namespaces when they are created. + // It is used when Expression Selector is in the Watch Opts set in Snapshot(). + resourceNamespaceLister resources.ResourceNamespaceLister + // namespacesWatching is the set of namespaces that we are watching. This is helpful + // when Expression Selector is set on the Watch Opts in Snapshot(). + namespacesWatching sync.Map } func (c *testingEmitter) Register() error { @@ -139,6 +144,15 @@ func (c *testingEmitter) FakeResource() testing_solo_io.FakeResourceClient { // event_loop and resource clients -> resource client implementations work in a README.md // this would be helpful for documentation purposes +// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. +// we will need a way to deal with DELETES and CREATES and updates seperately +// I believe this is delt with in the last tests, but I want to check the snapshots once more. +// with the interface, we have lost the ability to know the event type. +// so the interface must be able to identify the type of event that occured as well +// not just return the list of namespaces + +// TODO-JAKE test that we can create a huge field selector of massive size + func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -252,6 +266,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) /* Watch for changes and update snapshot */ go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): @@ -290,9 +308,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // watch all other namespaces that fit the Expression Selectors if opts.ExpressionSelector != "" { - // watch resources of non-watched namespaces that fit the Expression - // TODO-JAKE might want to get rid of the FieldSelectors - //setting up the options for both Listing and Watching namespaces + // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ Ctx: opts.Ctx, ExpressionSelector: opts.ExpressionSelector, @@ -302,18 +318,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ExpressionSelector: opts.ExpressionSelector, } - // TODO-JAKE test that we can create a huge field selector of massive size filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } } - namespacesResources, err := c.resourceNamespaceLister.GetNamespaceResourceList(namespaceListOptions, filterNamespaces) + namespacesResources, err := c.resourceNamespaceLister.GetResourceNamespaceList(namespaceListOptions, filterNamespaces) if err != nil { return nil, nil, err } - // non Watched Namespaces + // non watched namespaces for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ @@ -413,16 +428,22 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter - // TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. - // we will need a way to deal with DELETES and CREATES and updates seperately - // I believe this is delt with in the last tests, but I want to check the snapshots once more. - - // watch for new namespaces - // TODO-JAKE not sure if I need to watch the <- chan error here... or not - namespaceWatch, _, err := c.resourceNamespaceLister.GetNamespaceResourceWatch(namespaceWatchOptions, filterNamespaces, errs) + namespaceWatch, errsReceiver, err := c.resourceNamespaceLister.GetResourceNamespaceWatch(namespaceWatchOptions, filterNamespaces) if err != nil { return nil, nil, err } + if errsReceiver != nil { + go func() { + for { + select { + case <-ctx.Done(): + return + case err = <-errsReceiver: + errs <- errors.Wrapf(err, "received error from watch on resource namespaces") + } + } + }() + } go func() { for { @@ -433,34 +454,45 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if !ok { return } - // TODO-JAKE with the interface, we have lost the ability to know the event type. - // so the interface must be able to identify the type of event that occured as well - // not just return the list of namespaces - newNamespaces := []string{} + // get the list of new namespaces, if there is a new namespace + // get the list of resources from that namespace, and add + // a watch for new resources created/deleted on that namespace - // TODO-JAKE get a map of the namespaces that are currently being watched + newNamespaces := []string{} for _, ns := range resourceNamespaces { - if _, hit := mocksByNamespace.Load(ns.Name); !hit { + if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) continue } - if _, hit := fcarsByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue - } - if _, hit := fakesByNamespace.Load(ns.Name); !hit { - newNamespaces = append(newNamespaces, ns.Name) - continue + } + + // delete the missing/deleted namespaces + mapOfNamespaces := make(map[string]bool) + for _, ns := range resourceNamespaces { + mapOfNamespaces[ns.Name] = true + } + + missingNamespaces := []string{} + c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { + name := key.(string) + if _, hit := mapOfNamespaces[name]; !hit { + missingNamespaces = append(missingNamespaces, name) } + return true + }) + + for _, ns := range missingNamespaces { + c.namespacesWatching.Delete(ns) + mocksByNamespace.Delete(ns) + fcarsByNamespace.Delete(ns) + fakesByNamespace.Delete(ns) } - // add a watch for all the new namespaces + for _, namespace := range newNamespaces { /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace MockResource list") continue } @@ -468,10 +500,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } mockResourceNamespacesChan, mockResourceErrs, err := c.mockResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace MockResource watch") continue } @@ -485,8 +513,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") continue } @@ -494,10 +520,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } frequentlyChangingAnnotationsResourceNamespacesChan, frequentlyChangingAnnotationsResourceErrs, err := c.frequentlyChangingAnnotationsResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace FrequentlyChangingAnnotationsResource watch") continue } @@ -511,8 +533,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // INFO-JAKE not sure if we want to do something else - // but since this is occuring in async I think it should be fine errs <- errors.Wrapf(err, "initial new namespace FakeResource list") continue } @@ -520,10 +540,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } fakeResourceNamespacesChan, fakeResourceErrs, err := c.fakeResource.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - // TODO-JAKE if we do decide to have the namespaceErrs from the watch namespaces functionality - // , then we could add it here namespaceErrs <- error(*) . the namespaceErrs is coming from the - // ResourceNamespaceLister currently - // INFO-JAKE is this what we really want to do when there is an error? errs <- errors.Wrapf(err, "starting new namespace FakeResource watch") continue } @@ -534,8 +550,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") }(namespace) /* Watch for changes and update snapshot */ - // REFACTOR go func(namespace string) { + defer func() { + c.namespacesWatching.Delete(namespace) + }() + c.namespacesWatching.Store(namespace, true) for { select { case <-ctx.Done(): diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 2b62d87a3..7037df32c 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -198,6 +198,7 @@ var _ = Describe("V2Alpha1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + mockResource4a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -338,6 +339,7 @@ var _ = Describe("V2Alpha1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -478,6 +480,7 @@ var _ = Describe("V2Alpha1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) + fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) @@ -1419,6 +1422,88 @@ var _ = Describe("V2Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace1, namespace2) notWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(nil, notWatched) + + /* + FrequentlyChangingAnnotationsResource + */ + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFcars { + if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFcars { + if _, err := snap.Fcars.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotMocks(nil, notWatched) + + /* + FakeResource + */ + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectFakes { + if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectFakes { + if _, err := snap.Fakes.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + watched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(watched, nil) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + notWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(nil, notWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -1491,7 +1576,6 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -1506,7 +1590,6 @@ var _ = Describe("V2Alpha1Emitter", func() { watched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, mockResource2a) watched = MockResourceList{mockResource2b} @@ -1530,6 +1613,8 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -1570,7 +1655,6 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -1585,7 +1669,6 @@ var _ = Describe("V2Alpha1Emitter", func() { watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, frequentlyChangingAnnotationsResource2a) watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} @@ -1609,6 +1692,8 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMocksSent() + deleteNamespaces(ctx, kube, namespace5) + createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -1649,7 +1734,6 @@ var _ = Describe("V2Alpha1Emitter", func() { notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() - // TODO-JAKE we need to create namespaces at the end so that the other resources work too. deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -1664,7 +1748,6 @@ var _ = Describe("V2Alpha1Emitter", func() { watched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotMocks(watched, notWatched) - // TODO-JAKE need to ensure that this will work for each resource deleteNamespaces(ctx, kube, namespace3) notWatched = append(notWatched, fakeResource2a) watched = testing_solo_io.FakeResourceList{fakeResource2b} @@ -1688,7 +1771,8 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertNoMocksSent() - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace5) + }) }) From 644a6f9b55a149f5ebae8b88fc8f5772037dc3b8 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 11:30:49 -0500 Subject: [PATCH 21/98] clean up and reviews --- .../namespace/resource_namespace.go | 37 ++++++++++--------- .../templates/snapshot_emitter_template.go | 4 +- .../snapshot_emitter_test_template.go | 2 +- .../v1/kubeconfigs_snapshot_emitter.sk.go | 2 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 2 +- test/mocks/v1/testing_snapshot_emitter.sk.go | 2 +- .../v1alpha1/testing_snapshot_emitter.sk.go | 2 +- .../v1alpha1/testing_snapshot_emitter_test.go | 2 +- .../v2alpha1/testing_snapshot_emitter.sk.go | 2 +- .../v2alpha1/testing_snapshot_emitter_test.go | 2 +- 10 files changed, 29 insertions(+), 28 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 2e2942022..1f829a3ce 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -15,29 +15,31 @@ import ( ) var _ resources.ResourceNamespaceLister = &kubeResourceNamespaceLister{} -var _ resources.ResourceNamespaceLister = &kubeResourceNamespaceClient{} +var _ resources.ResourceNamespaceLister = &kubeClientResourceNamespaceLister{} -// if we use the list thingy I guess we could try it out, if it does not work, then lets move forward with the base client -// solution, that uses the kubernetes interface... -func NewKubeResourceNamespaceLister(kube kubernetes.Interface, cache cache.KubeCoreCache) resources.ResourceNamespaceLister { +// NewKubeClientCacheResourceNamespaceLister will create a new resource namespace lister that requires the kubernestes +// client and cache. +func NewKubeClientCacheResourceNamespaceLister(kube kubernetes.Interface, cache cache.KubeCoreCache) resources.ResourceNamespaceLister { return &kubeResourceNamespaceLister{ - namespace: NewNamespaceClient(kube, cache), + client: NewNamespaceClient(kube, cache), } } +// NewKubeClientResourceNamespaceLister will create a new resource namespace lister that requires the kubernetes client +// interface. func NewKubeClientResourceNamespaceLister(kube kubernetes.Interface) resources.ResourceNamespaceLister { - return &kubeResourceNamespaceClient{ + return &kubeClientResourceNamespaceLister{ kube: kube, } } type kubeResourceNamespaceLister struct { - namespace skkube.KubeNamespaceClient + client skkube.KubeNamespaceClient } // GetResourceNamespaceList is the kubernetes implementation that returns the list of namespaces func (kns *kubeResourceNamespaceLister) GetResourceNamespaceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { - namespaces, err := kns.namespace.List(clients.TranslateResourceNamespaceListToListOptions(opts)) + namespaces, err := kns.client.List(clients.TranslateResourceNamespaceListToListOptions(opts)) if err != nil { return nil, err } @@ -49,24 +51,23 @@ func (kns *kubeResourceNamespaceLister) GetResourceNamespaceList(opts resources. func (kns *kubeResourceNamespaceLister) GetResourceNamespaceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList) (chan resources.ResourceNamespaceList, <-chan error, error) { ctx := opts.Ctx wopts := clients.TranslateResourceNamespaceListToWatchOptions(opts) - namespaceChan, errorChan, err := kns.namespace.Watch(wopts) + namespaceChan, errorChan, err := kns.client.Watch(wopts) if err != nil { return nil, nil, err } resourceNamespaceChan := make(chan resources.ResourceNamespaceList) go func() { + defer close(resourceNamespaceChan) for { select { case namespaceList := <-namespaceChan: select { case resourceNamespaceChan <- kns.filter(convertNamespaceListToResourceNamespace(namespaceList), filtered): case <-ctx.Done(): - close(resourceNamespaceChan) return } case <-ctx.Done(): - close(resourceNamespaceChan) return } } @@ -92,19 +93,19 @@ func (kns *kubeResourceNamespaceLister) filter(namespaces resources.ResourceName } func convertNamespaceListToResourceNamespace(namespaces skkube.KubeNamespaceList) resources.ResourceNamespaceList { - l := resources.ResourceNamespaceList{} - for _, ns := range namespaces { - l = append(l, resources.ResourceNamespace{Name: ns.ObjectMeta.Name}) + l := make(resources.ResourceNamespaceList, len(namespaces)) + for i, ns := range namespaces { + l[i] = resources.ResourceNamespace{Name: ns.ObjectMeta.Name} } return l } -type kubeResourceNamespaceClient struct { +type kubeClientResourceNamespaceLister struct { kube kubernetes.Interface } // GetResourceNamespaceList is the kubernetes implementation that returns the list of namespaces -func (client *kubeResourceNamespaceClient) GetResourceNamespaceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { +func (client *kubeClientResourceNamespaceLister) GetResourceNamespaceList(opts resources.ResourceNamespaceListOptions, filtered resources.ResourceNamespaceList) (resources.ResourceNamespaceList, error) { excludeNamespaces := client.getExcludeFieldSelector(filtered) namespaceList, err := client.kube.CoreV1().Namespaces().List(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) if err != nil { @@ -114,7 +115,7 @@ func (client *kubeResourceNamespaceClient) GetResourceNamespaceList(opts resourc } // GetResourceNamespaceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces -func (client *kubeResourceNamespaceClient) GetResourceNamespaceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList) (chan resources.ResourceNamespaceList, <-chan error, error) { +func (client *kubeClientResourceNamespaceLister) GetResourceNamespaceWatch(opts resources.ResourceNamespaceWatchOptions, filtered resources.ResourceNamespaceList) (chan resources.ResourceNamespaceList, <-chan error, error) { excludeNamespaces := client.getExcludeFieldSelector(filtered) namespaceWatcher, err := client.kube.CoreV1().Namespaces().Watch(opts.Ctx, metav1.ListOptions{FieldSelector: excludeNamespaces, LabelSelector: opts.ExpressionSelector}) if err != nil { @@ -153,7 +154,7 @@ func (client *kubeResourceNamespaceClient) GetResourceNamespaceWatch(opts resour return resourceNamespaceChan, errorChannel, nil } -func (client *kubeResourceNamespaceClient) getExcludeFieldSelector(filtered resources.ResourceNamespaceList) string { +func (client *kubeClientResourceNamespaceLister) getExcludeFieldSelector(filtered resources.ResourceNamespaceList) string { var buffer bytes.Buffer for i, rns := range filtered { ns := rns.Name diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index eecd8d360..bdf2576ad 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -388,7 +388,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool) + mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) for _,ns := range resourceNamespaces { mapOfNamespaces[ns.Name] = true } @@ -405,7 +405,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o for _, ns := range missingNamespaces { c.namespacesWatching.Delete(ns) {{- range .Resources}} -{{- if not .ClusterScoped }} +{{- if (not .ClusterScoped) }} {{ lower_camel .PluralName }}ByNamespace.Delete(ns) {{- end }} {{- end }} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 8a8c47100..70826577a 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -473,7 +473,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func kube = helpers.MustKubeClient() kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + resourceNamespaceLister = namespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 7b6e92f4b..cbd9e3f50 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -317,7 +317,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool) + mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) for _, ns := range resourceNamespaces { mapOfNamespaces[ns.Name] = true } diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 55bf5e7c6..b936e7105 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -285,7 +285,7 @@ var _ = Describe("V1Emitter", func() { kube = helpers.MustKubeClient() kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + resourceNamespaceLister = namespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) createNamespaces(ctx, kube, namespace1, namespace2) // KubeConfig Constructor diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index c19e75ef9..97eefe154 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -700,7 +700,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool) + mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) for _, ns := range resourceNamespaces { mapOfNamespaces[ns.Name] = true } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 662d1fb9e..9a6c3a8a2 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -317,7 +317,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool) + mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) for _, ns := range resourceNamespaces { mapOfNamespaces[ns.Name] = true } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 7e84c64ec..9d3d41596 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -287,7 +287,7 @@ var _ = Describe("V1Alpha1Emitter", func() { kube = helpers.MustKubeClient() kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + resourceNamespaceLister = namespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 3c4361994..590efd9af 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -467,7 +467,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool) + mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) for _, ns := range resourceNamespaces { mapOfNamespaces[ns.Name] = true } diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 7037df32c..355885628 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -584,7 +584,7 @@ var _ = Describe("V2Alpha1Emitter", func() { kube = helpers.MustKubeClient() kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + resourceNamespaceLister = namespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) createNamespaces(ctx, kube, namespace1, namespace2) From 4c4192fdbd93cf091c43210985d4f65fe8554cb9 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 11:33:11 -0500 Subject: [PATCH 22/98] add missing file generated --- test/mocks/v1/testing_snapshot_emitter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index e5cf93b5c..92f504e9a 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1140,7 +1140,7 @@ var _ = Describe("V1Emitter", func() { kube = helpers.MustKubeClient() kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) - resourceNamespaceLister = namespace.NewKubeResourceNamespaceLister(kube, kubeCache) + resourceNamespaceLister = namespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) createNamespaces(ctx, kube, namespace1, namespace2) From 5c53707b8ecace690b742427e23e0139db582ea6 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 11:39:15 -0500 Subject: [PATCH 23/98] change variable name --- .../codegen/templates/snapshot_emitter_template.go | 3 +-- pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go | 3 +-- test/mocks/v1/testing_snapshot_emitter.sk.go | 3 +-- test/mocks/v1alpha1/testing_snapshot_emitter.sk.go | 3 +-- test/mocks/v2alpha1/testing_snapshot_emitter.sk.go | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index bdf2576ad..9586feb68 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -190,7 +190,6 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") - watchNamespacesIsEmpty := ! hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx @@ -218,7 +217,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{- end }} {{- end }} - if ! watchNamespacesIsEmpty || opts.ExpressionSelector == "" { + if hasWatchedNamespaces || opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index cbd9e3f50..cf4aa141a 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -146,7 +146,6 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") - watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx @@ -164,7 +163,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa currentSnapshot := KubeconfigsSnapshot{} kubeconfigsByNamespace := sync.Map{} - if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { + if hasWatchedNamespaces|| opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 97eefe154..ca4684fb3 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -208,7 +208,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") - watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx @@ -267,7 +266,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mctsByNamespace := sync.Map{} podsByNamespace := sync.Map{} - if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { + if hasWatchedNamespaces|| opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 9a6c3a8a2..cd47d7d83 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -146,7 +146,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") - watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx @@ -164,7 +163,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot := TestingSnapshot{} mocksByNamespace := sync.Map{} - if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { + if hasWatchedNamespaces|| opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 590efd9af..ab9422e36 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -168,7 +168,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") - watchNamespacesIsEmpty := !hasWatchedNamespaces var done sync.WaitGroup ctx := opts.Ctx @@ -202,7 +201,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fcarsByNamespace := sync.Map{} fakesByNamespace := sync.Map{} - if !watchNamespacesIsEmpty || opts.ExpressionSelector == "" { + if hasWatchedNamespaces|| opts.ExpressionSelector == "" { // then watch all resources on watch Namespaces // watched namespaces From ac6124f17a592b3ac525084d8204fa62df00bac4 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 13:27:56 -0500 Subject: [PATCH 24/98] update with comments, and nits --- .../templates/snapshot_emitter_template.go | 41 ++++++++----------- test/mocks/v1/testing_snapshot_emitter.sk.go | 41 ++++++++----------- .../v1alpha1/testing_snapshot_emitter.sk.go | 41 ++++++++----------- .../v2alpha1/testing_snapshot_emitter.sk.go | 41 ++++++++----------- 4 files changed, 64 insertions(+), 100 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 9586feb68..472170805 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -162,19 +162,13 @@ func (c *{{ lower_camel $.GoName }}Emitter) {{ .Name }}() {{ .ImportPrefix }}{{ } {{- end}} -// TODO-JAKE may want to add some comments around how the snapshot_emitter -// event_loop and resource clients -> resource client implementations work in a README.md -// this would be helpful for documentation purposes - -// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. -// we will need a way to deal with DELETES and CREATES and updates seperately -// I believe this is delt with in the last tests, but I want to check the snapshots once more. -// with the interface, we have lost the ability to know the event type. -// so the interface must be able to identify the type of event that occured as well -// not just return the list of namespaces - -// TODO-JAKE test that we can create a huge field selector of massive size - +// Snapshots will return a channel that can be used to receive snapshots of the +// state of the resources it is watching +// when watching resources, you can set the watchNamespaces, and you can set the +// ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources +// that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is +// set, then all namespaces that meet the label criteria of the ExpressionSelector will +// also be watched. func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -190,6 +184,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchingLabeledNamespaces := ! (opts.ExpressionSelector == "") var done sync.WaitGroup ctx := opts.Ctx @@ -216,8 +211,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o {{ lower_camel .PluralName }}ByNamespace := sync.Map{} {{- end }} {{- end }} - - if hasWatchedNamespaces || opts.ExpressionSelector == "" { + if hasWatchedNamespaces || ! watchingLabeledNamespaces { // then watch all resources on watch Namespaces // watched namespaces @@ -274,7 +268,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }(namespace) } } - // watch all other namespaces that fit the Expression Selectors + // watch all other namespaces that are labeled and fit the Expression Selector if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ @@ -296,7 +290,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o if err != nil { return nil, nil, err } - // non watched namespaces + // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name {{- range .Resources }} @@ -378,24 +372,21 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + // get the new namespaces, and get a map of the namespaces + mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) - continue } + mapOfResourceNamespaces[ns.Name] = true } - // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) - for _,ns := range resourceNamespaces { - mapOfNamespaces[ns.Name] = true - } - missingNamespaces := []string{} + // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { name := key.(string) - if _, hit := mapOfNamespaces[name]; !hit { + if _, hit := mapOfResourceNamespaces[name]; !hit { missingNamespaces = append(missingNamespaces, name) } return true diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index ca4684fb3..e0e85ba8a 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -180,19 +180,13 @@ func (c *testingEmitter) Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_ return c.pod } -// TODO-JAKE may want to add some comments around how the snapshot_emitter -// event_loop and resource clients -> resource client implementations work in a README.md -// this would be helpful for documentation purposes - -// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. -// we will need a way to deal with DELETES and CREATES and updates seperately -// I believe this is delt with in the last tests, but I want to check the snapshots once more. -// with the interface, we have lost the ability to know the event type. -// so the interface must be able to identify the type of event that occured as well -// not just return the list of namespaces - -// TODO-JAKE test that we can create a huge field selector of massive size - +// Snapshots will return a channel that can be used to receive snapshots of the +// state of the resources it is watching +// when watching resources, you can set the watchNamespaces, and you can set the +// ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources +// that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is +// set, then all namespaces that meet the label criteria of the ExpressionSelector will +// also be watched. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -208,6 +202,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchingLabeledNamespaces := !(opts.ExpressionSelector == "") var done sync.WaitGroup ctx := opts.Ctx @@ -265,8 +260,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO anothermockresourcesByNamespace := sync.Map{} mctsByNamespace := sync.Map{} podsByNamespace := sync.Map{} - - if hasWatchedNamespaces|| opts.ExpressionSelector == "" { + if hasWatchedNamespaces || !watchingLabeledNamespaces { // then watch all resources on watch Namespaces // watched namespaces @@ -454,7 +448,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } } - // watch all other namespaces that fit the Expression Selectors + // watch all other namespaces that are labeled and fit the Expression Selector if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ @@ -476,7 +470,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } - // non watched namespaces + // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for SimpleMockResource */ @@ -690,24 +684,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + // get the new namespaces, and get a map of the namespaces + mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) - continue } - } - - // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) - for _, ns := range resourceNamespaces { - mapOfNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = true } missingNamespaces := []string{} + // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { name := key.(string) - if _, hit := mapOfNamespaces[name]; !hit { + if _, hit := mapOfResourceNamespaces[name]; !hit { missingNamespaces = append(missingNamespaces, name) } return true diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index cd47d7d83..9ed684840 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -118,19 +118,13 @@ func (c *testingEmitter) MockResource() MockResourceClient { return c.mockResource } -// TODO-JAKE may want to add some comments around how the snapshot_emitter -// event_loop and resource clients -> resource client implementations work in a README.md -// this would be helpful for documentation purposes - -// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. -// we will need a way to deal with DELETES and CREATES and updates seperately -// I believe this is delt with in the last tests, but I want to check the snapshots once more. -// with the interface, we have lost the ability to know the event type. -// so the interface must be able to identify the type of event that occured as well -// not just return the list of namespaces - -// TODO-JAKE test that we can create a huge field selector of massive size - +// Snapshots will return a channel that can be used to receive snapshots of the +// state of the resources it is watching +// when watching resources, you can set the watchNamespaces, and you can set the +// ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources +// that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is +// set, then all namespaces that meet the label criteria of the ExpressionSelector will +// also be watched. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -146,6 +140,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchingLabeledNamespaces := !(opts.ExpressionSelector == "") var done sync.WaitGroup ctx := opts.Ctx @@ -162,8 +157,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO currentSnapshot := TestingSnapshot{} mocksByNamespace := sync.Map{} - - if hasWatchedNamespaces|| opts.ExpressionSelector == "" { + if hasWatchedNamespaces || !watchingLabeledNamespaces { // then watch all resources on watch Namespaces // watched namespaces @@ -211,7 +205,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } } - // watch all other namespaces that fit the Expression Selectors + // watch all other namespaces that are labeled and fit the Expression Selector if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ @@ -233,7 +227,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } - // non watched namespaces + // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ @@ -307,24 +301,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + // get the new namespaces, and get a map of the namespaces + mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) - continue } - } - - // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) - for _, ns := range resourceNamespaces { - mapOfNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = true } missingNamespaces := []string{} + // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { name := key.(string) - if _, hit := mapOfNamespaces[name]; !hit { + if _, hit := mapOfResourceNamespaces[name]; !hit { missingNamespaces = append(missingNamespaces, name) } return true diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index ab9422e36..0c60d4bf1 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -140,19 +140,13 @@ func (c *testingEmitter) FakeResource() testing_solo_io.FakeResourceClient { return c.fakeResource } -// TODO-JAKE may want to add some comments around how the snapshot_emitter -// event_loop and resource clients -> resource client implementations work in a README.md -// this would be helpful for documentation purposes - -// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. -// we will need a way to deal with DELETES and CREATES and updates seperately -// I believe this is delt with in the last tests, but I want to check the snapshots once more. -// with the interface, we have lost the ability to know the event type. -// so the interface must be able to identify the type of event that occured as well -// not just return the list of namespaces - -// TODO-JAKE test that we can create a huge field selector of massive size - +// Snapshots will return a channel that can be used to receive snapshots of the +// state of the resources it is watching +// when watching resources, you can set the watchNamespaces, and you can set the +// ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources +// that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is +// set, then all namespaces that meet the label criteria of the ExpressionSelector will +// also be watched. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -168,6 +162,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchingLabeledNamespaces := !(opts.ExpressionSelector == "") var done sync.WaitGroup ctx := opts.Ctx @@ -200,8 +195,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mocksByNamespace := sync.Map{} fcarsByNamespace := sync.Map{} fakesByNamespace := sync.Map{} - - if hasWatchedNamespaces|| opts.ExpressionSelector == "" { + if hasWatchedNamespaces || !watchingLabeledNamespaces { // then watch all resources on watch Namespaces // watched namespaces @@ -305,7 +299,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }(namespace) } } - // watch all other namespaces that fit the Expression Selectors + // watch all other namespaces that are labeled and fit the Expression Selector if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ @@ -327,7 +321,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } - // non watched namespaces + // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for MockResource */ @@ -457,24 +451,21 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + // get the new namespaces, and get a map of the namespaces + mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) - continue } - } - - // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) - for _, ns := range resourceNamespaces { - mapOfNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = true } missingNamespaces := []string{} + // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { name := key.(string) - if _, hit := mapOfNamespaces[name]; !hit { + if _, hit := mapOfResourceNamespaces[name]; !hit { missingNamespaces = append(missingNamespaces, name) } return true From 3f600568140eda0f82d3071f292549e4a763cd7a Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 13:34:27 -0500 Subject: [PATCH 25/98] moving some functions --- pkg/api/v1/clients/client_interface.go | 17 ----------------- pkg/api/v1/clients/mocks/client_interface.go | 3 ++- pkg/api/v1/clients/options.go | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 169787baf..36db15e00 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -178,20 +178,3 @@ func (o WatchOpts) WithDefaults() WatchOpts { } return o } - -func TranslateWatchOptsIntoListOpts(wopts WatchOpts) ListOpts { - clopts := ListOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector, Selector: wopts.Selector} - return clopts -} - -// TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options -func TranslateResourceNamespaceListToListOptions(lopts resources.ResourceNamespaceListOptions) ListOpts { - clopts := ListOpts{Ctx: lopts.Ctx, ExpressionSelector: lopts.ExpressionSelector} - return clopts -} - -// TranslateResourceNamespaceListToWatchOptions translates the resource namespace watch options to Watch Options -func TranslateResourceNamespaceListToWatchOptions(wopts resources.ResourceNamespaceWatchOptions) WatchOpts { - clopts := WatchOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector} - return clopts -} diff --git a/pkg/api/v1/clients/mocks/client_interface.go b/pkg/api/v1/clients/mocks/client_interface.go index bc3832ab1..132839c31 100644 --- a/pkg/api/v1/clients/mocks/client_interface.go +++ b/pkg/api/v1/clients/mocks/client_interface.go @@ -5,10 +5,11 @@ package mocks import ( + reflect "reflect" + gomock "github.com/golang/mock/gomock" clients "github.com/solo-io/solo-kit/pkg/api/v1/clients" resources "github.com/solo-io/solo-kit/pkg/api/v1/resources" - reflect "reflect" ) // MockResourceWatcher is a mock of ResourceWatcher interface diff --git a/pkg/api/v1/clients/options.go b/pkg/api/v1/clients/options.go index c48312f41..c05f6982c 100644 --- a/pkg/api/v1/clients/options.go +++ b/pkg/api/v1/clients/options.go @@ -1,9 +1,11 @@ package clients import ( + "github.com/solo-io/solo-kit/pkg/api/v1/resources" "k8s.io/apimachinery/pkg/labels" ) +// GetLabelSelector will parse ExpresionSelector if present, else it selects Selector. func GetLabelSelector(listOpts ListOpts) (labels.Selector, error) { // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement if listOpts.ExpressionSelector != "" { @@ -13,3 +15,21 @@ func GetLabelSelector(listOpts ListOpts) (labels.Selector, error) { // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#equality-based-requirement return labels.SelectorFromSet(listOpts.Selector), nil } + +// TranslateWatchOptsIntoListOpts translates the watch options into list options +func TranslateWatchOptsIntoListOpts(wopts WatchOpts) ListOpts { + clopts := ListOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector, Selector: wopts.Selector} + return clopts +} + +// TranslateResourceNamespaceListToListOptions translates the resource namespace list options to List Options +func TranslateResourceNamespaceListToListOptions(lopts resources.ResourceNamespaceListOptions) ListOpts { + clopts := ListOpts{Ctx: lopts.Ctx, ExpressionSelector: lopts.ExpressionSelector} + return clopts +} + +// TranslateResourceNamespaceListToWatchOptions translates the resource namespace watch options to Watch Options +func TranslateResourceNamespaceListToWatchOptions(wopts resources.ResourceNamespaceWatchOptions) WatchOpts { + clopts := WatchOpts{Ctx: wopts.Ctx, ExpressionSelector: wopts.ExpressionSelector} + return clopts +} From 24ded9eb25fa6a209f19f15b99b667cbb4031d9a Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 13:36:11 -0500 Subject: [PATCH 26/98] small change --- pkg/api/external/kubernetes/namespace/resource_namespace.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 1f829a3ce..e8556e31b 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -170,10 +170,10 @@ func (client *kubeClientResourceNamespaceLister) getExcludeFieldSelector(filtere } func convertNamespaceListToResourceNamespaceList(namespaceList *kubev1.NamespaceList) resources.ResourceNamespaceList { - resourceNamespaces := resources.ResourceNamespaceList{} - for _, item := range namespaceList.Items { + resourceNamespaces := make(resources.ResourceNamespaceList, len(namespaceList.Items)) + for i, item := range namespaceList.Items { ns := item.Name - resourceNamespaces = append(resourceNamespaces, resources.ResourceNamespace{Name: ns}) + resourceNamespaces[i] = resources.ResourceNamespace{Name: ns} } return resourceNamespaces } From 8bd5ec5777e00f645710bebe2df95edeba7bb583 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 13:41:03 -0500 Subject: [PATCH 27/98] closing the channels --- pkg/api/external/kubernetes/namespace/resource_namespace.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index e8556e31b..3d40a94a9 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -125,6 +125,10 @@ func (client *kubeClientResourceNamespaceLister) GetResourceNamespaceWatch(opts resourceNamespaceChan := make(chan resources.ResourceNamespaceList) errorChannel := make(chan error) go func() { + defer func() { + close(resourceNamespaceChan) + close(errorChannel) + }() for { select { case <-opts.Ctx.Done(): From 2a9cf1c356c0d7d8fe5e58f96c0708c2c5812a7b Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 14:01:33 -0500 Subject: [PATCH 28/98] updated the tests --- changelog/v0.30.4/namespace-selectors.yaml | 9 + .../snapshot_emitter_test_template.go | 272 ++--- .../v1/kubeconfigs_snapshot_emitter.sk.go | 41 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 130 +-- .../mocks/v1/testing_snapshot_emitter_test.go | 936 +++++++++--------- .../v1alpha1/testing_snapshot_emitter_test.go | 130 +-- .../v2alpha1/testing_snapshot_emitter_test.go | 390 ++++---- 7 files changed, 954 insertions(+), 954 deletions(-) create mode 100644 changelog/v0.30.4/namespace-selectors.yaml diff --git a/changelog/v0.30.4/namespace-selectors.yaml b/changelog/v0.30.4/namespace-selectors.yaml new file mode 100644 index 000000000..19da8c5af --- /dev/null +++ b/changelog/v0.30.4/namespace-selectors.yaml @@ -0,0 +1,9 @@ +changelog: + - type: NEW_FEATURE + issueLink: https://github.com/solo-io/solo-projects/issues/3338 + description: | + Added the ability to watch namespaces given be Expression Selectors in the Watch Opts + Watched Namespaces work as normally. When Expression Selectors the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set. \ No newline at end of file diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 70826577a..01dd116ed 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -194,8 +194,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -203,8 +203,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- end }} @@ -212,8 +212,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}2a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -221,8 +221,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- end }} @@ -230,8 +230,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}3a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -239,8 +239,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- end }} @@ -251,8 +251,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}4a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -260,9 +260,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}4a) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -271,8 +271,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}5a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -280,9 +280,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}5a) - notWatched = append(notWatched, {{ lower_camel .Name }}5b) - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}5b) + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -290,8 +290,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}6a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -299,9 +299,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}6a) - notWatched = append(notWatched, {{ lower_camel .Name }}6b) - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}6b) + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -312,8 +312,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}7a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -321,9 +321,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}7a) - notWatched = append(notWatched, {{ lower_camel .Name }}7b) - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}7b) + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -331,8 +331,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}8a ) - assertSnapshot{{ .PluralName }}(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}8a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -340,10 +340,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range {{ lower_camel .Name }}NotWatched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -360,9 +360,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -370,9 +370,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -382,9 +382,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -392,9 +392,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -402,9 +402,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -412,9 +412,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -422,9 +422,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -432,9 +432,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -442,8 +442,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) - assertSnapshot{{ .PluralName }}(nil, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -451,8 +451,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) - assertSnapshot{{ .PluralName }}(nil, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} {{- end }} @@ -840,8 +840,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -849,7 +849,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } assertNoMocksSent() {{- end }} @@ -861,8 +861,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}2a ) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -870,8 +870,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b} - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b} + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -879,8 +879,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -888,7 +888,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) assertNoMocksSent() {{- end }} @@ -897,8 +897,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}4a ) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -906,8 +906,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -917,8 +917,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}5a) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -926,7 +926,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) assertNoMessageSent() {{- end }} @@ -935,8 +935,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}6a) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -944,7 +944,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) assertNoMessageSent() {{- end }} @@ -954,8 +954,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}7a) - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -963,10 +963,10 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range {{ lower_camel .Name }}NotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -978,14 +978,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - notWatched = append(notWatched, {{ lower_camel .Name }}2a) - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -993,9 +993,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b} - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -1003,32 +1003,32 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - notWatched = append(notWatched, {{ lower_camel .Name }}3a) - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}3a) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a } - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a } - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } - assertSnapshot{{ .PluralName }}(watched, notWatched) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } - assertSnapshot{{ .PluralName }}(nil, notWatched) + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -1036,8 +1036,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4b.GetMetadata().Namespace, {{ lower_camel .Name }}4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) - assertSnapshot{{ .PluralName }}(nil, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} {{- end }} @@ -1098,12 +1098,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} - assertSnapshotMocks(nil, notWatched) + {{ lower_camel .Name }}NotWatched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} + assertSnapshotMocks(nil, {{ lower_camel .Name }}NotWatched) {{- end }} }) @@ -1181,8 +1181,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -1190,7 +1190,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } assertNoMocksSent() {{- end }} @@ -1206,8 +1206,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -1215,15 +1215,15 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, {{ lower_camel .Name }}2a) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b} - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b} + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1231,25 +1231,25 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshotMocks(watched, nil) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotMocks(watched, nil) + assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) {{- else }} {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, {{ lower_camel .Name }}3a) - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a) + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, {{ lower_camel .Name }}2b) - watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshotMocks(watched, notWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2b) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} + assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - for _, r := range watched { + for _, r := range {{ lower_camel .Name }}Watched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index cf4aa141a..f89fb3552 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -118,19 +118,13 @@ func (c *kubeconfigsEmitter) KubeConfig() KubeConfigClient { return c.kubeConfig } -// TODO-JAKE may want to add some comments around how the snapshot_emitter -// event_loop and resource clients -> resource client implementations work in a README.md -// this would be helpful for documentation purposes - -// TODO-JAKE this interface has to deal with the event types of kubernetes independently without the interface knowing about it. -// we will need a way to deal with DELETES and CREATES and updates seperately -// I believe this is delt with in the last tests, but I want to check the snapshots once more. -// with the interface, we have lost the ability to know the event type. -// so the interface must be able to identify the type of event that occured as well -// not just return the list of namespaces - -// TODO-JAKE test that we can create a huge field selector of massive size - +// Snapshots will return a channel that can be used to receive snapshots of the +// state of the resources it is watching +// when watching resources, you can set the watchNamespaces, and you can set the +// ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources +// that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is +// set, then all namespaces that meet the label criteria of the ExpressionSelector will +// also be watched. func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *KubeconfigsSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -146,6 +140,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa errs := make(chan error) hasWatchedNamespaces := len(watchNamespaces) > 1 || (len(watchNamespaces) == 1 && watchNamespaces[0] != "") + watchingLabeledNamespaces := !(opts.ExpressionSelector == "") var done sync.WaitGroup ctx := opts.Ctx @@ -162,8 +157,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa currentSnapshot := KubeconfigsSnapshot{} kubeconfigsByNamespace := sync.Map{} - - if hasWatchedNamespaces|| opts.ExpressionSelector == "" { + if hasWatchedNamespaces || !watchingLabeledNamespaces { // then watch all resources on watch Namespaces // watched namespaces @@ -211,7 +205,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }(namespace) } } - // watch all other namespaces that fit the Expression Selectors + // watch all other namespaces that are labeled and fit the Expression Selector if opts.ExpressionSelector != "" { // watch resources of non-watched namespaces that fit the expression selectors namespaceListOptions := resources.ResourceNamespaceListOptions{ @@ -233,7 +227,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa if err != nil { return nil, nil, err } - // non watched namespaces + // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name /* Setup namespaced watch for KubeConfig */ @@ -307,24 +301,21 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + // get the new namespaces, and get a map of the namespaces + mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) - continue } - } - - // delete the missing/deleted namespaces - mapOfNamespaces := make(map[string]bool, len(resourceNamespaces)) - for _, ns := range resourceNamespaces { - mapOfNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = true } missingNamespaces := []string{} + // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { name := key.(string) - if _, hit := mapOfNamespaces[name]; !hit { + if _, hit := mapOfResourceNamespaces[name]; !hit { missingNamespaces = append(missingNamespaces, name) } return true diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index b936e7105..f287c6498 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -162,22 +162,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertSnapshotkubeconfigs(watched, nil) + kubeConfigWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(kubeConfigWatched, nil) kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - assertSnapshotMocks(watched, nil) + kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + assertSnapshotMocks(kubeConfigWatched, nil) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertSnapshotMocks(watched, nil) + kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + assertSnapshotMocks(kubeConfigWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -186,25 +186,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig4a) - notWatched := KubeConfigList{kubeConfig4b} - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig4a) + kubeConfigNotWatched := KubeConfigList{kubeConfig4b} + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig5a) - notWatched = append(notWatched, kubeConfig5b) - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig5a) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig5b) + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig6a) - notWatched = append(notWatched, kubeConfig6b) - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig6a) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig6b) + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -213,18 +213,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig7a) - notWatched = append(notWatched, kubeConfig7b) - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig7a) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig7b) + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range kubeConfigNotWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -234,40 +234,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) - watched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) + kubeConfigWatched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - watched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + kubeConfigWatched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - watched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + kubeConfigWatched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) - watched = KubeConfigList{kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + kubeConfigWatched = KubeConfigList{kubeConfig6a, kubeConfig7a} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) - assertSnapshotkubeconfigs(nil, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) } BeforeEach(func() { @@ -533,7 +533,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -543,22 +543,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := KubeConfigList{kubeConfig2a, kubeConfig2b} - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) assertNoMocksSent() kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -566,24 +566,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) assertNoMessageSent() kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) assertNoMessageSent() kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range kubeConfigNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -593,16 +593,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - watched = KubeConfigList{kubeConfig4a, kubeConfig4b} - assertSnapshotkubeconfigs(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + kubeConfigWatched = KubeConfigList{kubeConfig4a, kubeConfig4b} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig4b.GetMetadata().Namespace, kubeConfig4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) - assertSnapshotkubeconfigs(nil, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) }) }) @@ -654,12 +654,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{kubeConfig1a, kubeConfig1b} - assertSnapshotMocks(watched, nil) + kubeConfigWatched := MockResourceList{kubeConfig1a, kubeConfig1b} + assertSnapshotMocks(kubeConfigWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{kubeConfig1a, kubeConfig1b} - assertSnapshotMocks(nil, notWatched) + kubeConfigNotWatched := MockResourceList{kubeConfig1a, kubeConfig1b} + assertSnapshotMocks(nil, kubeConfigNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -729,7 +729,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -743,27 +743,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := KubeConfigList{kubeConfig2a, kubeConfig2b} - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, kubeConfig2a) - watched = KubeConfigList{kubeConfig2b} - assertSnapshotMocks(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2a) + kubeConfigWatched = KubeConfigList{kubeConfig2b} + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, kubeConfig3a) - assertSnapshotMocks(watched, notWatched) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig3a) + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, kubeConfig2b) - watched = KubeConfigList{kubeConfig3a} - assertSnapshotMocks(watched, notWatched) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2b) + kubeConfigWatched = KubeConfigList{kubeConfig3a} + assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) - for _, r := range watched { + for _, r := range kubeConfigWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 92f504e9a..f86d9241d 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -203,22 +203,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotSimplemocks(watched, nil) + simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(simpleMockResourceWatched, nil) simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - assertSnapshotMocks(watched, nil) + simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + assertSnapshotMocks(simpleMockResourceWatched, nil) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertSnapshotMocks(watched, nil) + simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + assertSnapshotMocks(simpleMockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -227,25 +227,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource4a) - notWatched := SimpleMockResourceList{simpleMockResource4b} - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource4a) + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource4b} + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource5a) - notWatched = append(notWatched, simpleMockResource5b) - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource5a) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource5b) + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource6a) - notWatched = append(notWatched, simpleMockResource6b) - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource6a) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource6b) + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -254,18 +254,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource7a) - notWatched = append(notWatched, simpleMockResource7b) - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource7a) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource7b) + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range simpleMockResourceNotWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -275,40 +275,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) - watched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - watched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) - watched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) - assertSnapshotSimplemocks(nil, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) /* MockResource @@ -344,22 +344,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(mockResourceWatched, nil) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(mockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -368,25 +368,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource4a) + mockResourceNotWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource5a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource6a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -395,18 +395,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource7a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -416,40 +416,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) + mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, mockResourceNotWatched) /* FakeResource @@ -485,22 +485,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(watched, nil) + fakeResourceWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(fakeResourceWatched, nil) fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(watched, nil) + fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(fakeResourceWatched, nil) fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(watched, nil) + fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(fakeResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -509,25 +509,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource4a) - notWatched := FakeResourceList{fakeResource4b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource4a) + fakeResourceNotWatched := FakeResourceList{fakeResource4b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource5a) - notWatched = append(notWatched, fakeResource5b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource5a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource6a) - notWatched = append(notWatched, fakeResource6b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -536,18 +536,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource7a) - notWatched = append(notWatched, fakeResource7b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -557,40 +557,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) + fakeResourceWatched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + fakeResourceWatched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + fakeResourceWatched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceWatched = FakeResourceList{fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) - assertSnapshotFakes(nil, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) + assertSnapshotFakes(nil, fakeResourceNotWatched) /* AnotherMockResource @@ -626,22 +626,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotAnothermockresources(watched, nil) + anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - assertSnapshotMocks(watched, nil) + anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + assertSnapshotMocks(anotherMockResourceWatched, nil) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertSnapshotMocks(watched, nil) + anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + assertSnapshotMocks(anotherMockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -650,25 +650,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource4a) - notWatched := AnotherMockResourceList{anotherMockResource4b} - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource4a) + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource4b} + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource5a) - notWatched = append(notWatched, anotherMockResource5b) - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource5a) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource5b) + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource6a) - notWatched = append(notWatched, anotherMockResource6b) - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource6a) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource6b) + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -677,18 +677,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource7a) - notWatched = append(notWatched, anotherMockResource7b) - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource7a) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource7b) + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range anotherMockResourceNotWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -698,40 +698,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) - watched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - watched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) - watched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) - assertSnapshotAnothermockresources(nil, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) /* ClusterResource @@ -763,49 +763,49 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource1a} - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched := ClusterResourceList{clusterResource1a} + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource2a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource3a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource3a) + assertSnapshotClusterresources(clusterResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource4a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource5a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource6a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) + assertSnapshotClusterresources(clusterResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource7a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource8a) - assertSnapshotClusterresources(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource8a) + assertSnapshotClusterresources(clusterResourceWatched, nil) err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -813,34 +813,34 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} - watched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a, clusterResource8a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceNotWatched = ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} + clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a, clusterResource8a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource4a, clusterResource5a}...) - watched = ClusterResourceList{clusterResource6a, clusterResource7a, clusterResource8a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource4a, clusterResource5a}...) + clusterResourceWatched = ClusterResourceList{clusterResource6a, clusterResource7a, clusterResource8a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource6a}...) - watched = ClusterResourceList{clusterResource7a, clusterResource8a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource6a}...) + clusterResourceWatched = ClusterResourceList{clusterResource7a, clusterResource8a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource7a}...) - watched = ClusterResourceList{clusterResource8a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource7a}...) + clusterResourceWatched = ClusterResourceList{clusterResource8a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, ClusterResourceList{clusterResource8a}...) - assertSnapshotClusterresources(nil, notWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource8a}...) + assertSnapshotClusterresources(nil, clusterResourceNotWatched) /* MockCustomType @@ -876,22 +876,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertSnapshotmcts(watched, nil) + mockCustomTypeWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(mockCustomTypeWatched, nil) mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - assertSnapshotMocks(watched, nil) + mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + assertSnapshotMocks(mockCustomTypeWatched, nil) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertSnapshotMocks(watched, nil) + mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + assertSnapshotMocks(mockCustomTypeWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -900,25 +900,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType4a) - notWatched := MockCustomTypeList{mockCustomType4b} - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType4a) + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType4b} + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType5a) - notWatched = append(notWatched, mockCustomType5b) - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType5a) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType5b) + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType6a) - notWatched = append(notWatched, mockCustomType6b) - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType6a) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType6b) + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -927,18 +927,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType7a) - notWatched = append(notWatched, mockCustomType7b) - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType7a) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType7b) + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockCustomTypeNotWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -948,40 +948,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) - watched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - watched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - watched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) - watched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) - assertSnapshotmcts(nil, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) + assertSnapshotmcts(nil, mockCustomTypeNotWatched) /* Pod @@ -1017,22 +1017,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertSnapshotpods(watched, nil) + podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(podWatched, nil) pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - assertSnapshotMocks(watched, nil) + podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + assertSnapshotMocks(podWatched, nil) pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertSnapshotMocks(watched, nil) + podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + assertSnapshotMocks(podWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -1041,25 +1041,25 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod4a) - notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, pod4a) + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} + assertSnapshotMocks(podWatched, podNotWatched) pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod5a) - notWatched = append(notWatched, pod5b) - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, pod5a) + podNotWatched = append(podNotWatched, pod5b) + assertSnapshotMocks(podWatched, podNotWatched) pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod6a) - notWatched = append(notWatched, pod6b) - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, pod6a) + podNotWatched = append(podNotWatched, pod6b) + assertSnapshotMocks(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -1068,18 +1068,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod7a) - notWatched = append(notWatched, pod7b) - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, pod7a) + podNotWatched = append(podNotWatched, pod7b) + assertSnapshotMocks(podWatched, podNotWatched) pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range podNotWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1089,40 +1089,40 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(watched, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} + assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} - assertSnapshotpods(watched, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} + assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) - assertSnapshotpods(nil, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) + assertSnapshotpods(nil, podNotWatched) } BeforeEach(func() { @@ -2117,7 +2117,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2127,22 +2127,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) assertNoMocksSent() simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2150,24 +2150,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) assertNoMessageSent() simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) assertNoMessageSent() simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range simpleMockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2177,16 +2177,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - watched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} - assertSnapshotSimplemocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource4b.GetMetadata().Namespace, simpleMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) - assertSnapshotSimplemocks(nil, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) /* MockResource @@ -2222,7 +2222,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2232,22 +2232,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2255,24 +2255,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2282,16 +2282,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(nil, mockResourceNotWatched) /* FakeResource @@ -2327,7 +2327,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FakeResourceList{fakeResource1a, fakeResource1b} + fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2337,22 +2337,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) assertNoMocksSent() fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2360,24 +2360,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) assertNoMessageSent() fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) assertNoMessageSent() fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range fakeResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2387,16 +2387,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = FakeResourceList{fakeResource4a, fakeResource4b} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + fakeResourceWatched = FakeResourceList{fakeResource4a, fakeResource4b} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotFakes(nil, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotFakes(nil, fakeResourceNotWatched) /* AnotherMockResource @@ -2432,7 +2432,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2442,22 +2442,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) assertNoMocksSent() anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2465,24 +2465,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) assertNoMessageSent() anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) assertNoMessageSent() anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range anotherMockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2492,16 +2492,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - watched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} - assertSnapshotAnothermockresources(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource4b.GetMetadata().Namespace, anotherMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) - assertSnapshotAnothermockresources(nil, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) /* ClusterResource @@ -2533,83 +2533,83 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource1a} - assertSnapshotMocks(watched, nil) + clusterResourceWatched := ClusterResourceList{clusterResource1a} + assertSnapshotMocks(clusterResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource2a) - assertSnapshotMocks(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) + assertSnapshotMocks(clusterResourceWatched, nil) clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource3a} - assertSnapshotMocks(watched, nil) + clusterResourceWatched := ClusterResourceList{clusterResource3a} + assertSnapshotMocks(clusterResourceWatched, nil) clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource4a) - assertSnapshotMocks(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) + assertSnapshotMocks(clusterResourceWatched, nil) createNamespaces(ctx, kube, namespace5, namespace6) clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource5a) - assertSnapshotMocks(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) + assertSnapshotMocks(clusterResourceWatched, nil) clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource6a) - assertSnapshotMocks(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) + assertSnapshotMocks(clusterResourceWatched, nil) clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, clusterResource7a) - assertSnapshotMocks(watched, nil) + clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) + assertSnapshotMocks(clusterResourceWatched, nil) err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource2a, clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} - notWatched = ClusterResourceList{clusterResource1a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource2a, clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceNotWatched = ClusterResourceList{clusterResource1a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} - notWatched = append(notWatched, clusterResource2a) - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} - notWatched = append(notWatched, clusterResource3a) - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource3a) + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} - notWatched = ClusterResourceList{clusterResource4a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceNotWatched = ClusterResourceList{clusterResource4a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource6a, clusterResource7a} - notWatched = ClusterResourceList{clusterResource5a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource6a, clusterResource7a} + clusterResourceNotWatched = ClusterResourceList{clusterResource5a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource7a} - notWatched = ClusterResourceList{clusterResource6a} - assertSnapshotClusterresources(watched, notWatched) + clusterResourceWatched = ClusterResourceList{clusterResource7a} + clusterResourceNotWatched = ClusterResourceList{clusterResource6a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = ClusterResourceList{clusterResource7a} - assertSnapshotClusterresources(nil, notWatched) + clusterResourceNotWatched = ClusterResourceList{clusterResource7a} + assertSnapshotClusterresources(nil, clusterResourceNotWatched) /* MockCustomType @@ -2645,7 +2645,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2655,22 +2655,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) assertNoMocksSent() mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2678,24 +2678,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) assertNoMessageSent() mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) assertNoMessageSent() mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockCustomTypeNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2705,16 +2705,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - watched = MockCustomTypeList{mockCustomType4a, mockCustomType4b} - assertSnapshotmcts(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType4a, mockCustomType4b} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType4b.GetMetadata().Namespace, mockCustomType4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) - assertSnapshotmcts(nil, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) + assertSnapshotmcts(nil, mockCustomTypeNotWatched) /* Pod @@ -2750,7 +2750,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -2760,22 +2760,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} - assertSnapshotMocks(watched, notWatched) + podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} + assertSnapshotMocks(podWatched, podNotWatched) pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) assertNoMocksSent() pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) + assertSnapshotMocks(podWatched, podNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2783,24 +2783,24 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) assertNoMessageSent() pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) assertNoMessageSent() pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range podNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -2810,16 +2810,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} - assertSnapshotpods(watched, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} + assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod4b.GetMetadata().Namespace, pod4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) - assertSnapshotpods(nil, notWatched) + podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) + assertSnapshotpods(nil, podNotWatched) }) }) @@ -2871,12 +2871,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotMocks(watched, nil) + simpleMockResourceWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotMocks(simpleMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotMocks(nil, notWatched) + simpleMockResourceNotWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotMocks(nil, simpleMockResourceNotWatched) /* MockResource @@ -2912,12 +2912,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) /* FakeResource @@ -2953,12 +2953,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(watched, nil) + fakeResourceWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(nil, notWatched) + fakeResourceNotWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(nil, fakeResourceNotWatched) /* AnotherMockResource @@ -2994,12 +2994,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotMocks(watched, nil) + anotherMockResourceWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotMocks(anotherMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotMocks(nil, notWatched) + anotherMockResourceNotWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotMocks(nil, anotherMockResourceNotWatched) /* ClusterResource @@ -3033,12 +3033,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) clusterResource1b, err := clusterResourceClient.Write(NewClusterResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{clusterResource1a, clusterResource1b} - assertSnapshotMocks(watched, nil) + clusterResourceWatched := MockResourceList{clusterResource1a, clusterResource1b} + assertSnapshotMocks(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{clusterResource1a, clusterResource1b} - assertSnapshotMocks(nil, notWatched) + clusterResourceNotWatched := MockResourceList{clusterResource1a, clusterResource1b} + assertSnapshotMocks(nil, clusterResourceNotWatched) /* MockCustomType @@ -3074,12 +3074,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockCustomType1a, mockCustomType1b} - assertSnapshotMocks(watched, nil) + mockCustomTypeWatched := MockResourceList{mockCustomType1a, mockCustomType1b} + assertSnapshotMocks(mockCustomTypeWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockCustomType1a, mockCustomType1b} - assertSnapshotMocks(nil, notWatched) + mockCustomTypeNotWatched := MockResourceList{mockCustomType1a, mockCustomType1b} + assertSnapshotMocks(nil, mockCustomTypeNotWatched) /* Pod @@ -3115,12 +3115,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{pod1a, pod1b} - assertSnapshotMocks(watched, nil) + podWatched := MockResourceList{pod1a, pod1b} + assertSnapshotMocks(podWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{pod1a, pod1b} - assertSnapshotMocks(nil, notWatched) + podNotWatched := MockResourceList{pod1a, pod1b} + assertSnapshotMocks(nil, podNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -3190,7 +3190,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3204,27 +3204,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, simpleMockResource2a) - watched = SimpleMockResourceList{simpleMockResource2b} - assertSnapshotMocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2a) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2b} + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, simpleMockResource3a) - assertSnapshotMocks(watched, notWatched) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource3a) + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, simpleMockResource2b) - watched = SimpleMockResourceList{simpleMockResource3a} - assertSnapshotMocks(watched, notWatched) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2b) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a} + assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - for _, r := range watched { + for _, r := range simpleMockResourceWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -3269,7 +3269,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3283,27 +3283,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, mockResource2a) - watched = MockResourceList{mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + mockResourceWatched = MockResourceList{mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource3a) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource3a) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, mockResource2b) - watched = MockResourceList{mockResource3a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) + mockResourceWatched = MockResourceList{mockResource3a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range watched { + for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -3348,7 +3348,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FakeResourceList{fakeResource1a, fakeResource1b} + fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3362,27 +3362,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, fakeResource2a) - watched = FakeResourceList{fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) + fakeResourceWatched = FakeResourceList{fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) fakeResource3a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource3a) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, fakeResource2b) - watched = FakeResourceList{fakeResource3a} - assertSnapshotMocks(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) + fakeResourceWatched = FakeResourceList{fakeResource3a} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) - for _, r := range watched { + for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -3427,7 +3427,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3441,27 +3441,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, anotherMockResource2a) - watched = AnotherMockResourceList{anotherMockResource2b} - assertSnapshotMocks(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2a) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2b} + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, anotherMockResource3a) - assertSnapshotMocks(watched, notWatched) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource3a) + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, anotherMockResource2b) - watched = AnotherMockResourceList{anotherMockResource3a} - assertSnapshotMocks(watched, notWatched) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2b) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a} + assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) - for _, r := range watched { + for _, r := range anotherMockResourceWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -3502,8 +3502,8 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := ClusterResourceList{clusterResource1a} - assertSnapshotMocks(watched, nil) + clusterResourceWatched := ClusterResourceList{clusterResource1a} + assertSnapshotMocks(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMocksSent() @@ -3514,23 +3514,23 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource2a} - assertSnapshotMocks(watched, nil) + clusterResourceWatched = ClusterResourceList{clusterResource2a} + assertSnapshotMocks(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, clusterResource2a) - watched = ClusterResourceList{clusterResource2b} - assertSnapshotMocks(watched, notWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) + clusterResourceWatched = ClusterResourceList{clusterResource2b} + assertSnapshotMocks(clusterResourceWatched, clusterResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = ClusterResourceList{clusterResource3a} - assertSnapshotMocks(watched, nil) + clusterResourceWatched = ClusterResourceList{clusterResource3a} + assertSnapshotMocks(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotMocks(watched, nil) + assertSnapshotMocks(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace5) @@ -3571,7 +3571,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3585,27 +3585,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, mockCustomType2a) - watched = MockCustomTypeList{mockCustomType2b} - assertSnapshotMocks(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2a) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType2b} + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockCustomType3a) - assertSnapshotMocks(watched, notWatched) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType3a) + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, mockCustomType2b) - watched = MockCustomTypeList{mockCustomType3a} - assertSnapshotMocks(watched, notWatched) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2b) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a} + assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) - for _, r := range watched { + for _, r := range mockCustomTypeWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -3650,7 +3650,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -3664,27 +3664,27 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} - assertSnapshotMocks(watched, notWatched) + podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} + assertSnapshotMocks(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, pod2a) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} - assertSnapshotMocks(watched, notWatched) + podNotWatched = append(podNotWatched, pod2a) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} + assertSnapshotMocks(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, pod3a) - assertSnapshotMocks(watched, notWatched) + podWatched = append(podWatched, pod3a) + assertSnapshotMocks(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, pod2b) - watched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} - assertSnapshotMocks(watched, notWatched) + podNotWatched = append(podNotWatched, pod2b) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} + assertSnapshotMocks(podWatched, podNotWatched) - for _, r := range watched { + for _, r := range podWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 9d3d41596..128f10a10 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -164,22 +164,22 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(mockResourceWatched, nil) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(mockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -188,25 +188,25 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource4a) + mockResourceNotWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource5a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource6a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -215,18 +215,18 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource7a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -236,40 +236,40 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) + mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, mockResourceNotWatched) } BeforeEach(func() { @@ -546,7 +546,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -556,22 +556,22 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -579,24 +579,24 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -606,16 +606,16 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(nil, mockResourceNotWatched) }) }) @@ -667,12 +667,12 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -742,7 +742,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -756,27 +756,27 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, mockResource2a) - watched = MockResourceList{mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + mockResourceWatched = MockResourceList{mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource3a) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource3a) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, mockResource2b) - watched = MockResourceList{mockResource3a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) + mockResourceWatched = MockResourceList{mockResource3a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range watched { + for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 355885628..a581ee7bc 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -179,22 +179,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) + assertSnapshotMocks(mockResourceWatched, nil) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource3a, mockResource3b}...) - assertSnapshotMocks(watched, nil) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource3a, mockResource3b}...) + assertSnapshotMocks(mockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -203,25 +203,25 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource4a) - notWatched := MockResourceList{mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource4a) + mockResourceNotWatched := MockResourceList{mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource5a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource5a) - notWatched = append(notWatched, mockResource5b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource5a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource6a) - notWatched = append(notWatched, mockResource6b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource6a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -230,18 +230,18 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource7a) - notWatched = append(notWatched, mockResource7b) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource7a) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource8a, mockResource8b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -251,40 +251,40 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource1a, mockResource1b}...) - watched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) + mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) - watched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) - watched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource7a}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) + assertSnapshotMocks(nil, mockResourceNotWatched) /* FrequentlyChangingAnnotationsResource @@ -320,22 +320,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotFcars(watched, nil) + frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - assertSnapshotMocks(watched, nil) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertSnapshotMocks(watched, nil) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -344,25 +344,25 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource4a) - notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource4a) + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource5a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource5b) - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource5a) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource5b) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource6a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource6b) - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource6a) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource6b) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -371,18 +371,18 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource7a) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource7b) - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource7a) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource7b) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range frequentlyChangingAnnotationsResourceNotWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -392,40 +392,40 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) - assertSnapshotFcars(nil, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) /* FakeResource @@ -461,22 +461,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(watched, nil) + fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(fakeResourceWatched, nil) fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(watched, nil) + fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + assertSnapshotMocks(fakeResourceWatched, nil) fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(watched, nil) + fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + assertSnapshotMocks(fakeResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -485,25 +485,25 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource4a) - notWatched := testing_solo_io.FakeResourceList{fakeResource4b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource4a) + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource4b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource5a) - notWatched = append(notWatched, fakeResource5b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource5a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource6a) - notWatched = append(notWatched, fakeResource6b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -512,18 +512,18 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource7a) - notWatched = append(notWatched, fakeResource7b) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -533,40 +533,40 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) - watched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) - watched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) - assertSnapshotFakes(nil, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) + assertSnapshotFakes(nil, fakeResourceNotWatched) } BeforeEach(func() { @@ -1085,7 +1085,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1095,22 +1095,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource3a, mockResource3b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) assertNoMocksSent() mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -1118,24 +1118,24 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource6a, mockResource6b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) assertNoMessageSent() mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1145,16 +1145,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource2a, mockResource2b}...) - watched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) + assertSnapshotMocks(nil, mockResourceNotWatched) /* FrequentlyChangingAnnotationsResource @@ -1190,7 +1190,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1200,22 +1200,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) assertNoMocksSent() frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -1223,24 +1223,24 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) assertNoMessageSent() frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) assertNoMessageSent() frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range frequentlyChangingAnnotationsResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1250,16 +1250,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} - assertSnapshotFcars(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) - assertSnapshotFcars(nil, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) /* FakeResource @@ -1295,7 +1295,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1305,22 +1305,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) assertNoMocksSent() fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -1328,24 +1328,24 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) assertNoMessageSent() fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) assertNoMessageSent() fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) assertNoMessageSent() - for _, r := range notWatched { + for _, r := range fakeResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1355,16 +1355,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - watched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} - assertSnapshotFakes(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched = append(notWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotFakes(nil, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) + assertSnapshotFakes(nil, fakeResourceNotWatched) }) }) @@ -1416,12 +1416,12 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(watched, nil) + mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, notWatched) + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) /* FrequentlyChangingAnnotationsResource @@ -1457,12 +1457,12 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotMocks(watched, nil) + frequentlyChangingAnnotationsResourceWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotMocks(nil, notWatched) + frequentlyChangingAnnotationsResourceNotWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotMocks(nil, frequentlyChangingAnnotationsResourceNotWatched) /* FakeResource @@ -1498,12 +1498,12 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(watched, nil) + fakeResourceWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - notWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(nil, notWatched) + fakeResourceNotWatched := MockResourceList{fakeResource1a, fakeResource1b} + assertSnapshotMocks(nil, fakeResourceNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -1573,7 +1573,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := MockResourceList{mockResource1a, mockResource1b} + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -1587,27 +1587,27 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := MockResourceList{mockResource2a, mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, mockResource2a) - watched = MockResourceList{mockResource2b} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + mockResourceWatched = MockResourceList{mockResource2b} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, mockResource3a) - assertSnapshotMocks(watched, notWatched) + mockResourceWatched = append(mockResourceWatched, mockResource3a) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, mockResource2b) - watched = MockResourceList{mockResource3a} - assertSnapshotMocks(watched, notWatched) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) + mockResourceWatched = MockResourceList{mockResource3a} + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range watched { + for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1652,7 +1652,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -1666,27 +1666,27 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource2a) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2a) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, frequentlyChangingAnnotationsResource3a) - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource3a) + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, frequentlyChangingAnnotationsResource2b) - watched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} - assertSnapshotMocks(watched, notWatched) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2b) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} + assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - for _, r := range watched { + for _, r := range frequentlyChangingAnnotationsResourceWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } @@ -1731,7 +1731,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - notWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertNoMocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) @@ -1745,27 +1745,27 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - notWatched = append(notWatched, fakeResource2a) - watched = testing_solo_io.FakeResourceList{fakeResource2b} - assertSnapshotMocks(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2b} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - watched = append(watched, fakeResource3a) - assertSnapshotMocks(watched, notWatched) + fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - notWatched = append(notWatched, fakeResource2b) - watched = testing_solo_io.FakeResourceList{fakeResource3a} - assertSnapshotMocks(watched, notWatched) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a} + assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) - for _, r := range watched { + for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } From 20770eddb14aa4827bfd5d7a5fc1cdce493c71e6 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 15:48:29 -0500 Subject: [PATCH 29/98] fix kube event loop error --- pkg/multicluster/v1/kubeconfigs_event_loop_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/multicluster/v1/kubeconfigs_event_loop_test.go b/pkg/multicluster/v1/kubeconfigs_event_loop_test.go index 0b39e01f2..e276cefcf 100644 --- a/pkg/multicluster/v1/kubeconfigs_event_loop_test.go +++ b/pkg/multicluster/v1/kubeconfigs_event_loop_test.go @@ -15,6 +15,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" + "github.com/solo-io/solo-kit/test/helpers" ) var _ = Describe("KubeconfigsEventLoop", func() { @@ -28,13 +31,17 @@ var _ = Describe("KubeconfigsEventLoop", func() { BeforeEach(func() { ctx = context.Background() + kube := helpers.MustKubeClient() + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister := skNamespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) + kubeConfigClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), } kubeConfigClient, err := NewKubeConfigClient(ctx, kubeConfigClientFactory) Expect(err).NotTo(HaveOccurred()) - - emitter = NewKubeconfigsEmitter(kubeConfigClient) + emitter = NewKubeconfigsEmitter(kubeConfigClient, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { _, err = emitter.KubeConfig().Write(NewKubeConfig(namespace, "jerry"), clients.WriteOpts{}) From 835b2f9c44645901b858c5a65da79a942909497b Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 15:53:06 -0500 Subject: [PATCH 30/98] update changelog --- changelog/{v0.30.4 => v0.30.3}/namespace-selectors.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename changelog/{v0.30.4 => v0.30.3}/namespace-selectors.yaml (86%) diff --git a/changelog/v0.30.4/namespace-selectors.yaml b/changelog/v0.30.3/namespace-selectors.yaml similarity index 86% rename from changelog/v0.30.4/namespace-selectors.yaml rename to changelog/v0.30.3/namespace-selectors.yaml index 19da8c5af..9e8920465 100644 --- a/changelog/v0.30.4/namespace-selectors.yaml +++ b/changelog/v0.30.3/namespace-selectors.yaml @@ -1,6 +1,6 @@ changelog: - type: NEW_FEATURE - issueLink: https://github.com/solo-io/solo-projects/issues/3338 + issueLink: https://github.com/solo-io/gloo/issues/5868 description: | Added the ability to watch namespaces given be Expression Selectors in the Watch Opts Watched Namespaces work as normally. When Expression Selectors the snapshot From 4c86983c9c6e322a1654c2c974cfd8c373b8cfb6 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 30 Aug 2022 16:43:33 -0500 Subject: [PATCH 31/98] to pass CI retro 1 --- .../templates/event_loop_test_template.go | 11 +- .../snapshot_emitter_test_template.go | 178 +++--- .../v1/kubeconfigs_event_loop_test.go | 6 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 81 ++- test/mocks/v1/testing_event_loop_test.go | 11 +- .../mocks/v1/testing_snapshot_emitter_test.go | 554 ++++++++++++------ .../mocks/v1alpha1/testing_event_loop_test.go | 11 +- .../v1alpha1/testing_snapshot_emitter_test.go | 23 +- .../mocks/v2alpha1/testing_event_loop_test.go | 11 +- .../v2alpha1/testing_snapshot_emitter_test.go | 227 ++++--- 10 files changed, 703 insertions(+), 410 deletions(-) diff --git a/pkg/code-generator/codegen/templates/event_loop_test_template.go b/pkg/code-generator/codegen/templates/event_loop_test_template.go index 64076ad3d..321a43cc9 100644 --- a/pkg/code-generator/codegen/templates/event_loop_test_template.go +++ b/pkg/code-generator/codegen/templates/event_loop_test_template.go @@ -25,6 +25,9 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" + "github.com/solo-io/solo-kit/test/helpers" ) var _ = Describe("{{ .GoName }}EventLoop", func() { @@ -37,6 +40,12 @@ var _ = Describe("{{ .GoName }}EventLoop", func() { BeforeEach(func() { ctx = context.Background() + + kube := helpers.MustKubeClient() + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister := skNamespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) + {{- range .Resources}} {{ lower_camel .Name }}ClientFactory := &factory.MemoryResourceClientFactory{ @@ -46,7 +55,7 @@ var _ = Describe("{{ .GoName }}EventLoop", func() { Expect(err).NotTo(HaveOccurred()) {{- end}} - emitter = New{{ .GoName }}Emitter({{ $clients }}) + emitter = New{{ .GoName }}Emitter({{ $clients }}, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { {{- range .Resources }} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 01dd116ed..bb0a95efe 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -42,7 +42,7 @@ import ( kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" "k8s.io/client-go/rest" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // Needed to run tests in GKE @@ -85,7 +85,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- range .Resources }} New{{ .Name }}WithLabels := func(namespace, name string, labels map[string]string) (*{{ .ImportPrefix }}{{ .Name }}) { resource := {{ .ImportPrefix }}New{{ .Name }}(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } {{- end }} @@ -96,7 +96,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { - _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + _, err := kube.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, Labels: labels, @@ -210,7 +210,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -222,25 +222,25 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b }...) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- end }} @@ -262,46 +262,46 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}5b) - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}6b) - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -310,35 +310,35 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}7b) - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}8a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) assertNoMessageSent() @@ -360,7 +360,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) @@ -782,12 +782,20 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } - assertNoMocksSent := func() { + +{{- range .Resources }} + + /* + {{ .Name }} + */ + +{{- if (not .ClusterScoped) }} + assertNo{{ .PluralName }}Sent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.{{ upper_camel .PluralName }}) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -799,11 +807,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } } -{{- range .Resources }} +{{- end }} - /* - {{ .Name }} - */ assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: for { @@ -823,13 +828,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - {{- if .ClusterScoped }} +{{- if .ClusterScoped }} combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) - {{- else }} +{{- else }} nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) combined := append(nsList1, nsList2...) - {{- end }} +{{- end }} Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } @@ -841,7 +846,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -850,7 +855,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertNoMocksSent() + assertNo{{ .PluralName }}Sent() {{- end }} @@ -862,7 +867,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -871,43 +876,43 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b} - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - assertNoMocksSent() + assertNo{{ .PluralName }}Sent() {{- end }} {{- if .ClusterScoped }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -918,7 +923,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -933,16 +938,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) assertNoMessageSent() @@ -955,7 +960,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -967,7 +972,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertNoMessageSent() for _, r := range {{ lower_camel .Name }}NotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -979,7 +984,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1056,7 +1061,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func }) Expect(err).NotTo(HaveOccurred()) - var snap *TestingSnapshot + var snap *{{ .GoName }}Snapshot {{- range .Resources }} @@ -1098,12 +1103,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - {{ lower_camel .Name }}NotWatched := MockResourceList{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} - assertSnapshotMocks(nil, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} }) @@ -1121,12 +1126,21 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot - assertNoMocksSent := func() { + +{{- $length := len .Resources }} +{{- $last_entry := minus $length 1 }} +{{- range $i, $r := .Resources }} +{{ with $r }} + /* + {{ .Name }} + */ + + assertNo{{ .PluralName }}Sent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.{{ upper_camel .PluralName }}) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -1138,13 +1152,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } } -{{- $length := len .Resources }} -{{- $last_entry := minus $length 1 }} -{{- range $i, $r := .Resources }} -{{ with $r }} - /* - {{ .Name }} - */ assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: @@ -1182,7 +1189,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -1191,12 +1198,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertNoMocksSent() + assertNo{{ .PluralName }}Sent() {{- end }} deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNo{{ .PluralName }}Sent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1207,7 +1214,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -1216,14 +1223,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} deleteNamespaces(ctx, kube, namespace3) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b} - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1232,28 +1238,28 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotMocks({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a) - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) deleteNamespaces(ctx, kube, namespace4) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2b) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshotMocks({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) for _, r := range {{ lower_camel .Name }}Watched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNo{{ .PluralName }}Sent() {{- end }} @@ -1271,7 +1277,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Context("use different resource namespace listers", func() { BeforeEach(func () { resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) - emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + emitter = New{{ .GoName }}Emitter({{ $clients }}, resourceNamespaceLister) }) It("Should work with the Kube Client Namespace Lister", func () { diff --git a/pkg/multicluster/v1/kubeconfigs_event_loop_test.go b/pkg/multicluster/v1/kubeconfigs_event_loop_test.go index e276cefcf..94d36ef06 100644 --- a/pkg/multicluster/v1/kubeconfigs_event_loop_test.go +++ b/pkg/multicluster/v1/kubeconfigs_event_loop_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 @@ -12,11 +11,11 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" - "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" - skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/test/helpers" ) @@ -41,6 +40,7 @@ var _ = Describe("KubeconfigsEventLoop", func() { } kubeConfigClient, err := NewKubeConfigClient(ctx, kubeConfigClientFactory) Expect(err).NotTo(HaveOccurred()) + emitter = NewKubeconfigsEmitter(kubeConfigClient, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index f287c6498..e0a224bac 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -9,8 +9,6 @@ import ( "os" "time" - "github.com/solo-io/solo-kit/api/multicluster/v1" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" @@ -23,7 +21,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -60,7 +58,7 @@ var _ = Describe("V1Emitter", func() { ) NewKubeConfigWithLabels := func(namespace, name string, labels map[string]string) *KubeConfig { resource := NewKubeConfig(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } @@ -70,7 +68,7 @@ var _ = Describe("V1Emitter", func() { } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { - _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + _, err := kube.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, Labels: labels, @@ -170,14 +168,14 @@ var _ = Describe("V1Emitter", func() { kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - assertSnapshotMocks(kubeConfigWatched, nil) + assertSnapshotkubeconfigs(kubeConfigWatched, nil) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertSnapshotMocks(kubeConfigWatched, nil) + assertSnapshotkubeconfigs(kubeConfigWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -188,7 +186,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, kubeConfig4a) kubeConfigNotWatched := KubeConfigList{kubeConfig4b} - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -196,7 +194,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, kubeConfig5a) kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig5b) - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -204,7 +202,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, kubeConfig6a) kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig6b) - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -215,7 +213,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, kubeConfig7a) kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig7b) - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -481,12 +479,15 @@ var _ = Describe("V1Emitter", func() { } } - assertNoMocksSent := func() { + /* + KubeConfig + */ + assertNokubeconfigsSent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.Kubeconfigs) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -499,9 +500,6 @@ var _ = Describe("V1Emitter", func() { } } - /* - KubeConfig - */ assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: for { @@ -534,7 +532,7 @@ var _ = Describe("V1Emitter", func() { kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertNoMocksSent() + assertNokubeconfigsSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -544,21 +542,21 @@ var _ = Describe("V1Emitter", func() { kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertNoMocksSent() + assertNokubeconfigsSent() kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -584,7 +582,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range kubeConfigNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -618,7 +616,7 @@ var _ = Describe("V1Emitter", func() { }) Expect(err).NotTo(HaveOccurred()) - var snap *TestingSnapshot + var snap *KubeconfigsSnapshot /* KubeConfig @@ -654,12 +652,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched := MockResourceList{kubeConfig1a, kubeConfig1b} - assertSnapshotMocks(kubeConfigWatched, nil) + kubeConfigWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(kubeConfigWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - kubeConfigNotWatched := MockResourceList{kubeConfig1a, kubeConfig1b} - assertSnapshotMocks(nil, kubeConfigNotWatched) + kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -676,12 +674,16 @@ var _ = Describe("V1Emitter", func() { var snap *KubeconfigsSnapshot - assertNoMocksSent := func() { + /* + KubeConfig + */ + + assertNokubeconfigsSent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.Kubeconfigs) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -694,10 +696,6 @@ var _ = Describe("V1Emitter", func() { } } - /* - KubeConfig - */ - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: for { @@ -730,10 +728,10 @@ var _ = Describe("V1Emitter", func() { kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertNoMocksSent() + assertNokubeconfigsSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNokubeconfigsSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -744,30 +742,29 @@ var _ = Describe("V1Emitter", func() { kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace3) - kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2a) - kubeConfigWatched = KubeConfigList{kubeConfig2b} - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + kubeConfigNotWatched := KubeConfigList{kubeConfig2a} + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched = append(kubeConfigWatched, kubeConfig3a) - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace4) kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2b) kubeConfigWatched = KubeConfigList{kubeConfig3a} - assertSnapshotMocks(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) for _, r := range kubeConfigWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNokubeconfigsSent() deleteNamespaces(ctx, kube, namespace5) @@ -777,7 +774,7 @@ var _ = Describe("V1Emitter", func() { Context("use different resource namespace listers", func() { BeforeEach(func() { resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) - emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + emitter = NewKubeconfigsEmitter(kubeConfigClient, resourceNamespaceLister) }) It("Should work with the Kube Client Namespace Lister", func() { diff --git a/test/mocks/v1/testing_event_loop_test.go b/test/mocks/v1/testing_event_loop_test.go index a3bf64b9d..d076fb257 100644 --- a/test/mocks/v1/testing_event_loop_test.go +++ b/test/mocks/v1/testing_event_loop_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 @@ -14,9 +13,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/test/helpers" ) var _ = Describe("TestingEventLoop", func() { @@ -30,6 +32,11 @@ var _ = Describe("TestingEventLoop", func() { BeforeEach(func() { ctx = context.Background() + kube := helpers.MustKubeClient() + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister := skNamespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) + simpleMockResourceClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), } @@ -72,7 +79,7 @@ var _ = Describe("TestingEventLoop", func() { podClient, err := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { _, err = emitter.SimpleMockResource().Write(NewSimpleMockResource(namespace, "jerry"), clients.WriteOpts{}) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index f86d9241d..ae0070406 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -24,7 +24,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/test/helpers" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -71,37 +71,37 @@ var _ = Describe("V1Emitter", func() { ) NewSimpleMockResourceWithLabels := func(namespace, name string, labels map[string]string) *SimpleMockResource { resource := NewSimpleMockResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { resource := NewMockResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewFakeResourceWithLabels := func(namespace, name string, labels map[string]string) *FakeResource { resource := NewFakeResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewAnotherMockResourceWithLabels := func(namespace, name string, labels map[string]string) *AnotherMockResource { resource := NewAnotherMockResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewClusterResourceWithLabels := func(namespace, name string, labels map[string]string) *ClusterResource { resource := NewClusterResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewMockCustomTypeWithLabels := func(namespace, name string, labels map[string]string) *MockCustomType { resource := NewMockCustomType(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewPodWithLabels := func(namespace, name string, labels map[string]string) *github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.Pod { resource := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } @@ -111,7 +111,7 @@ var _ = Describe("V1Emitter", func() { } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { - _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + _, err := kube.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, Labels: labels, @@ -211,14 +211,14 @@ var _ = Describe("V1Emitter", func() { simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - assertSnapshotMocks(simpleMockResourceWatched, nil) + assertSnapshotSimplemocks(simpleMockResourceWatched, nil) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertSnapshotMocks(simpleMockResourceWatched, nil) + assertSnapshotSimplemocks(simpleMockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -229,7 +229,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource4a) simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource4b} - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -237,7 +237,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource5a) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource5b) - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -245,7 +245,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource6a) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource6b) - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -256,7 +256,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource7a) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource7b) - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -493,14 +493,14 @@ var _ = Describe("V1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(fakeResourceWatched, nil) + assertSnapshotFakes(fakeResourceWatched, nil) fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(fakeResourceWatched, nil) + assertSnapshotFakes(fakeResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -511,7 +511,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource4a) fakeResourceNotWatched := FakeResourceList{fakeResource4b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -519,7 +519,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource5a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -527,7 +527,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -538,7 +538,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -634,14 +634,14 @@ var _ = Describe("V1Emitter", func() { anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - assertSnapshotMocks(anotherMockResourceWatched, nil) + assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertSnapshotMocks(anotherMockResourceWatched, nil) + assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -652,7 +652,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource4a) anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource4b} - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -660,7 +660,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource5a) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource5b) - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -668,7 +668,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource6a) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource6b) - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -679,7 +679,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource7a) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource7b) - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -813,7 +813,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched = ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} + clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a, clusterResource8a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) @@ -884,14 +884,14 @@ var _ = Describe("V1Emitter", func() { mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - assertSnapshotMocks(mockCustomTypeWatched, nil) + assertSnapshotmcts(mockCustomTypeWatched, nil) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertSnapshotMocks(mockCustomTypeWatched, nil) + assertSnapshotmcts(mockCustomTypeWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -902,7 +902,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType4a) mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType4b} - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -910,7 +910,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType5a) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType5b) - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -918,7 +918,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType6a) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType6b) - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -929,7 +929,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType7a) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType7b) - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1025,14 +1025,14 @@ var _ = Describe("V1Emitter", func() { pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - assertSnapshotMocks(podWatched, nil) + assertSnapshotpods(podWatched, nil) - pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + pod3a, err := podClient.Write(NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + pod3b, err := podClient.Write(NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertSnapshotMocks(podWatched, nil) + assertSnapshotpods(podWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -1043,38 +1043,38 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, pod4a) podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4b} - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) - pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod5a, err := podClient.Write(NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod5b, err := podClient.Write(NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, pod5a) podNotWatched = append(podNotWatched, pod5b) - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) - pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + pod6a, err := podClient.Write(NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + pod6b, err := podClient.Write(NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, pod6a) podNotWatched = append(podNotWatched, pod6b) - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) - pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + pod7a, err := podClient.Write(NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + pod7b, err := podClient.Write(NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, pod7a) podNotWatched = append(podNotWatched, pod7b) - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) - pod8a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + pod8a, err := podClient.Write(NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod8b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + pod8b, err := podClient.Write(NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) assertNoMessageSent() @@ -2065,12 +2065,15 @@ var _ = Describe("V1Emitter", func() { } } - assertNoMocksSent := func() { + /* + SimpleMockResource + */ + assertNoSimplemocksSent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.Simplemocks) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -2083,9 +2086,6 @@ var _ = Describe("V1Emitter", func() { } } - /* - SimpleMockResource - */ assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: for { @@ -2118,7 +2118,7 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertNoMocksSent() + assertNoSimplemocksSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2128,21 +2128,21 @@ var _ = Describe("V1Emitter", func() { simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertNoMocksSent() + assertNoSimplemocksSent() simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2168,7 +2168,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range simpleMockResourceNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -2191,6 +2191,24 @@ var _ = Describe("V1Emitter", func() { /* MockResource */ + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -2296,6 +2314,24 @@ var _ = Describe("V1Emitter", func() { /* FakeResource */ + assertNoFakesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fakes) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: for { @@ -2328,7 +2364,7 @@ var _ = Describe("V1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMocksSent() + assertNoFakesSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2338,21 +2374,21 @@ var _ = Describe("V1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoMocksSent() + assertNoFakesSent() fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2378,7 +2414,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range fakeResourceNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -2401,6 +2437,24 @@ var _ = Describe("V1Emitter", func() { /* AnotherMockResource */ + assertNoAnothermockresourcesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Anothermockresources) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: for { @@ -2433,7 +2487,7 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertNoMocksSent() + assertNoAnothermockresourcesSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2443,21 +2497,21 @@ var _ = Describe("V1Emitter", func() { anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertNoMocksSent() + assertNoAnothermockresourcesSent() anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2483,7 +2537,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range anotherMockResourceNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -2506,6 +2560,7 @@ var _ = Describe("V1Emitter", func() { /* ClusterResource */ + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { drain: for { @@ -2534,7 +2589,7 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched := ClusterResourceList{clusterResource1a} - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2542,39 +2597,39 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched := ClusterResourceList{clusterResource3a} - assertSnapshotMocks(clusterResourceWatched, nil) + clusterResourceWatched = ClusterResourceList{clusterResource3a} + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) createNamespaces(ctx, kube, namespace5, namespace6) clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource2a, clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} - clusterResourceNotWatched = ClusterResourceList{clusterResource1a} + clusterResourceNotWatched := ClusterResourceList{clusterResource1a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2614,6 +2669,24 @@ var _ = Describe("V1Emitter", func() { /* MockCustomType */ + assertNomctsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mcts) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: for { @@ -2646,7 +2719,7 @@ var _ = Describe("V1Emitter", func() { mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertNoMocksSent() + assertNomctsSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2656,21 +2729,21 @@ var _ = Describe("V1Emitter", func() { mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertNoMocksSent() + assertNomctsSent() mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2696,7 +2769,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range mockCustomTypeNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -2719,6 +2792,24 @@ var _ = Describe("V1Emitter", func() { /* Pod */ + assertNopodsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Pods) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: for { @@ -2751,7 +2842,7 @@ var _ = Describe("V1Emitter", func() { pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertNoMocksSent() + assertNopodsSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2761,21 +2852,21 @@ var _ = Describe("V1Emitter", func() { pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) - pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod3a, err := podClient.Write(NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod3b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod3b, err := podClient.Write(NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertNoMocksSent() + assertNopodsSent() - pod4a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod4a, err := podClient.Write(NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod4b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + pod4b, err := podClient.Write(NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -2786,9 +2877,9 @@ var _ = Describe("V1Emitter", func() { podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) assertNoMessageSent() - pod6a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + pod6a, err := podClient.Write(NewPodWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod6b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + pod6b, err := podClient.Write(NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) assertNoMessageSent() @@ -2801,7 +2892,7 @@ var _ = Describe("V1Emitter", func() { assertNoMessageSent() for _, r := range podNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -2871,12 +2962,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotMocks(simpleMockResourceWatched, nil) + simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(simpleMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - simpleMockResourceNotWatched := MockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotMocks(nil, simpleMockResourceNotWatched) + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) /* MockResource @@ -2953,12 +3044,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(fakeResourceWatched, nil) + fakeResourceWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - fakeResourceNotWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(nil, fakeResourceNotWatched) + fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(nil, fakeResourceNotWatched) /* AnotherMockResource @@ -2994,12 +3085,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotMocks(anotherMockResourceWatched, nil) + anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - anotherMockResourceNotWatched := MockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotMocks(nil, anotherMockResourceNotWatched) + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) /* ClusterResource @@ -3033,12 +3124,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) clusterResource1b, err := clusterResourceClient.Write(NewClusterResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched := MockResourceList{clusterResource1a, clusterResource1b} - assertSnapshotMocks(clusterResourceWatched, nil) + clusterResourceWatched := ClusterResourceList{clusterResource1a, clusterResource1b} + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - clusterResourceNotWatched := MockResourceList{clusterResource1a, clusterResource1b} - assertSnapshotMocks(nil, clusterResourceNotWatched) + clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource1b} + assertSnapshotClusterresources(nil, clusterResourceNotWatched) /* MockCustomType @@ -3074,12 +3165,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched := MockResourceList{mockCustomType1a, mockCustomType1b} - assertSnapshotMocks(mockCustomTypeWatched, nil) + mockCustomTypeWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(mockCustomTypeWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - mockCustomTypeNotWatched := MockResourceList{mockCustomType1a, mockCustomType1b} - assertSnapshotMocks(nil, mockCustomTypeNotWatched) + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(nil, mockCustomTypeNotWatched) /* Pod @@ -3115,12 +3206,12 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - podWatched := MockResourceList{pod1a, pod1b} - assertSnapshotMocks(podWatched, nil) + podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(podWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - podNotWatched := MockResourceList{pod1a, pod1b} - assertSnapshotMocks(nil, podNotWatched) + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(nil, podNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -3137,12 +3228,16 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot - assertNoMocksSent := func() { + /* + SimpleMockResource + */ + + assertNoSimplemocksSent := func() { drain: for { select { case snap = <-snapshots: - if len(snap.Mocks) == 0 { + if len(snap.Simplemocks) == 0 { continue drain } Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) @@ -3155,10 +3250,6 @@ var _ = Describe("V1Emitter", func() { } } - /* - SimpleMockResource - */ - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: for { @@ -3191,10 +3282,10 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertNoMocksSent() + assertNoSimplemocksSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoSimplemocksSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3205,30 +3296,29 @@ var _ = Describe("V1Emitter", func() { simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2a) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2b} - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource2a} + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource3a) - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2b) simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a} - assertSnapshotMocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) for _, r := range simpleMockResourceWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNoSimplemocksSent() deleteNamespaces(ctx, kube, namespace5) @@ -3238,6 +3328,24 @@ var _ = Describe("V1Emitter", func() { MockResource */ + assertNoMocksSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mocks) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -3287,8 +3395,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched := MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3317,6 +3424,24 @@ var _ = Describe("V1Emitter", func() { FakeResource */ + assertNoFakesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fakes) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: for { @@ -3349,10 +3474,10 @@ var _ = Describe("V1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMocksSent() + assertNoFakesSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoFakesSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3363,30 +3488,29 @@ var _ = Describe("V1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) - fakeResourceWatched = FakeResourceList{fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + fakeResourceNotWatched := FakeResourceList{fakeResource2a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) fakeResource3a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) fakeResourceWatched = FakeResourceList{fakeResource3a} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNoFakesSent() deleteNamespaces(ctx, kube, namespace5) @@ -3396,6 +3520,24 @@ var _ = Describe("V1Emitter", func() { AnotherMockResource */ + assertNoAnothermockresourcesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Anothermockresources) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: for { @@ -3428,10 +3570,10 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertNoMocksSent() + assertNoAnothermockresourcesSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoAnothermockresourcesSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3442,30 +3584,29 @@ var _ = Describe("V1Emitter", func() { anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2a) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2b} - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource2a} + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource3a) - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2b) anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a} - assertSnapshotMocks(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) for _, r := range anotherMockResourceWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNoAnothermockresourcesSent() deleteNamespaces(ctx, kube, namespace5) @@ -3475,6 +3616,24 @@ var _ = Describe("V1Emitter", func() { ClusterResource */ + assertNoClusterresourcesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Clusterresources) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { drain: for { @@ -3503,10 +3662,10 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched := ClusterResourceList{clusterResource1a} - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoClusterresourcesSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3515,22 +3674,21 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource2a} - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace3) - clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) - clusterResourceWatched = ClusterResourceList{clusterResource2b} - assertSnapshotMocks(clusterResourceWatched, clusterResourceNotWatched) + clusterResourceNotWatched := ClusterResourceList{clusterResource2a} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource3a} - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotMocks(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace5) @@ -3540,6 +3698,24 @@ var _ = Describe("V1Emitter", func() { MockCustomType */ + assertNomctsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Mcts) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: for { @@ -3572,10 +3748,10 @@ var _ = Describe("V1Emitter", func() { mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertNoMocksSent() + assertNomctsSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNomctsSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3586,30 +3762,29 @@ var _ = Describe("V1Emitter", func() { mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2a) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType2b} - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType2a} + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType3a) - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace4) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2b) mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a} - assertSnapshotMocks(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) for _, r := range mockCustomTypeWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNomctsSent() deleteNamespaces(ctx, kube, namespace5) @@ -3619,6 +3794,24 @@ var _ = Describe("V1Emitter", func() { Pod */ + assertNopodsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Pods) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: for { @@ -3651,10 +3844,10 @@ var _ = Describe("V1Emitter", func() { pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertNoMocksSent() + assertNopodsSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNopodsSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -3665,30 +3858,29 @@ var _ = Describe("V1Emitter", func() { pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace3) - podNotWatched = append(podNotWatched, pod2a) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} - assertSnapshotMocks(podWatched, podNotWatched) + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a} + assertSnapshotpods(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched = append(podWatched, pod3a) - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace4) podNotWatched = append(podNotWatched, pod2b) podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} - assertSnapshotMocks(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) for _, r := range podWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNopodsSent() deleteNamespaces(ctx, kube, namespace5) @@ -3698,7 +3890,7 @@ var _ = Describe("V1Emitter", func() { Context("use different resource namespace listers", func() { BeforeEach(func() { resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) - emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) }) It("Should work with the Kube Client Namespace Lister", func() { diff --git a/test/mocks/v1alpha1/testing_event_loop_test.go b/test/mocks/v1alpha1/testing_event_loop_test.go index fb3255d4f..4bdbb5a80 100644 --- a/test/mocks/v1alpha1/testing_event_loop_test.go +++ b/test/mocks/v1alpha1/testing_event_loop_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1alpha1 @@ -12,9 +11,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/test/helpers" ) var _ = Describe("TestingEventLoop", func() { @@ -28,13 +30,18 @@ var _ = Describe("TestingEventLoop", func() { BeforeEach(func() { ctx = context.Background() + kube := helpers.MustKubeClient() + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister := skNamespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) + mockResourceClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), } mockResourceClient, err := NewMockResourceClient(ctx, mockResourceClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(mockResourceClient) + emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { _, err = emitter.MockResource().Write(NewMockResource(namespace, "jerry"), clients.WriteOpts{}) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 128f10a10..c6bb19a7d 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -21,7 +21,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -62,7 +62,7 @@ var _ = Describe("V1Alpha1Emitter", func() { ) NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { resource := NewMockResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } @@ -72,7 +72,7 @@ var _ = Describe("V1Alpha1Emitter", func() { } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { - _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + _, err := kube.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, Labels: labels, @@ -494,6 +494,9 @@ var _ = Describe("V1Alpha1Emitter", func() { } } + /* + MockResource + */ assertNoMocksSent := func() { drain: for { @@ -512,9 +515,6 @@ var _ = Describe("V1Alpha1Emitter", func() { } } - /* - MockResource - */ assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -689,6 +689,10 @@ var _ = Describe("V1Alpha1Emitter", func() { var snap *TestingSnapshot + /* + MockResource + */ + assertNoMocksSent := func() { drain: for { @@ -707,10 +711,6 @@ var _ = Describe("V1Alpha1Emitter", func() { } } - /* - MockResource - */ - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -760,8 +760,7 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched := MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v2alpha1/testing_event_loop_test.go b/test/mocks/v2alpha1/testing_event_loop_test.go index 3985d62d8..867460177 100644 --- a/test/mocks/v2alpha1/testing_event_loop_test.go +++ b/test/mocks/v2alpha1/testing_event_loop_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v2alpha1 @@ -14,9 +13,12 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + skNamespace "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" + "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/test/helpers" ) var _ = Describe("TestingEventLoop", func() { @@ -30,6 +32,11 @@ var _ = Describe("TestingEventLoop", func() { BeforeEach(func() { ctx = context.Background() + kube := helpers.MustKubeClient() + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kube) + Expect(err).NotTo(HaveOccurred()) + resourceNamespaceLister := skNamespace.NewKubeClientCacheResourceNamespaceLister(kube, kubeCache) + mockResourceClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), } @@ -48,7 +55,7 @@ var _ = Describe("TestingEventLoop", func() { fakeResourceClient, err := testing_solo_io.NewFakeResourceClient(ctx, fakeResourceClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient) + emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister) }) It("runs sync function on a new snapshot", func() { _, err = emitter.MockResource().Write(NewMockResource(namespace, "jerry"), clients.WriteOpts{}) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index a581ee7bc..048458ceb 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -24,7 +24,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/resources" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -67,17 +67,17 @@ var _ = Describe("V2Alpha1Emitter", func() { ) NewMockResourceWithLabels := func(namespace, name string, labels map[string]string) *MockResource { resource := NewMockResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewFrequentlyChangingAnnotationsResourceWithLabels := func(namespace, name string, labels map[string]string) *FrequentlyChangingAnnotationsResource { resource := NewFrequentlyChangingAnnotationsResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } NewFakeResourceWithLabels := func(namespace, name string, labels map[string]string) *testing_solo_io.FakeResource { resource := testing_solo_io.NewFakeResource(namespace, name) - resource.Metadata.Labels = labels + resource.GetMetadata().Labels = labels return resource } @@ -87,7 +87,7 @@ var _ = Describe("V2Alpha1Emitter", func() { } createNamespaceWithLabel := func(ctx context.Context, kube kubernetes.Interface, namespace string, labels map[string]string) { - _, err := kube.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ + _, err := kube.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespace, Labels: labels, @@ -328,14 +328,14 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -346,7 +346,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource4a) frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4b} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -354,7 +354,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource5a) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource5b) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -362,7 +362,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource6a) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource6b) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) @@ -373,7 +373,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource7a) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource7b) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -469,14 +469,14 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotMocks(fakeResourceWatched, nil) + assertSnapshotFakes(fakeResourceWatched, nil) - fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertSnapshotMocks(fakeResourceWatched, nil) + assertSnapshotFakes(fakeResourceWatched, nil) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) @@ -487,38 +487,38 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource4a) fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource4b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource5a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource5b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource5a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) - fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource8a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) + fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) assertNoMessageSent() @@ -1033,6 +1033,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } + /* + MockResource + */ assertNoMocksSent := func() { drain: for { @@ -1051,9 +1054,6 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - /* - MockResource - */ assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -1159,6 +1159,24 @@ var _ = Describe("V2Alpha1Emitter", func() { /* FrequentlyChangingAnnotationsResource */ + assertNoFcarsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fcars) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: for { @@ -1191,7 +1209,7 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertNoMocksSent() + assertNoFcarsSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1201,21 +1219,21 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertNoMocksSent() + assertNoFcarsSent() frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -1241,7 +1259,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertNoMessageSent() for _, r := range frequentlyChangingAnnotationsResourceNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -1264,6 +1282,24 @@ var _ = Describe("V2Alpha1Emitter", func() { /* FakeResource */ + assertNoFakesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fakes) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: for { @@ -1296,7 +1332,7 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMocksSent() + assertNoFakesSent() createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1306,21 +1342,21 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoMocksSent() + assertNoFakesSent() - fakeResource4a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource4b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaces(ctx, kube, namespace5, namespace6) @@ -1331,9 +1367,9 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) assertNoMessageSent() - fakeResource6a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) + fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) assertNoMessageSent() @@ -1346,7 +1382,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertNoMessageSent() for _, r := range fakeResourceNotWatched { - err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } assertNoMessageSent() @@ -1457,12 +1493,12 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, nil) + frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - frequentlyChangingAnnotationsResourceNotWatched := MockResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotMocks(nil, frequentlyChangingAnnotationsResourceNotWatched) + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) /* FakeResource @@ -1498,12 +1534,12 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(fakeResourceWatched, nil) + fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - fakeResourceNotWatched := MockResourceList{fakeResource1a, fakeResource1b} - assertSnapshotMocks(nil, fakeResourceNotWatched) + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(nil, fakeResourceNotWatched) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -1520,6 +1556,10 @@ var _ = Describe("V2Alpha1Emitter", func() { var snap *TestingSnapshot + /* + MockResource + */ + assertNoMocksSent := func() { drain: for { @@ -1538,10 +1578,6 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - /* - MockResource - */ - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -1591,8 +1627,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched := MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1621,6 +1656,24 @@ var _ = Describe("V2Alpha1Emitter", func() { FrequentlyChangingAnnotationsResource */ + assertNoFcarsSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fcars) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: for { @@ -1653,10 +1706,10 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertNoMocksSent() + assertNoFcarsSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoFcarsSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1667,30 +1720,29 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2a) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a} + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource3a) - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2b) frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} - assertSnapshotMocks(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) for _, r := range frequentlyChangingAnnotationsResourceWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNoFcarsSent() deleteNamespaces(ctx, kube, namespace5) @@ -1700,6 +1752,24 @@ var _ = Describe("V2Alpha1Emitter", func() { FakeResource */ + assertNoFakesSent := func() { + drain: + for { + select { + case snap = <-snapshots: + if len(snap.Fakes) == 0 { + continue drain + } + Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: for { @@ -1732,10 +1802,10 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertNoMocksSent() + assertNoFakesSent() deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() + assertNoFakesSent() // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) @@ -1746,30 +1816,29 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2b} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource2a} + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a} - assertSnapshotMocks(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMocksSent() + assertNoFakesSent() deleteNamespaces(ctx, kube, namespace5) @@ -1779,7 +1848,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Context("use different resource namespace listers", func() { BeforeEach(func() { resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) - emitter = NewTestingEmitter(mockResourceClient, resourceNamespaceLister) + emitter = NewTestingEmitter(mockResourceClient, frequentlyChangingAnnotationsResourceClient, fakeResourceClient, resourceNamespaceLister) }) It("Should work with the Kube Client Namespace Lister", func() { From 62de6b88b1b1ca4779c451cb4a1955d9f14ab9e5 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 31 Aug 2022 08:16:48 -0500 Subject: [PATCH 32/98] fix --- .../templates/snapshot_emitter_test_template.go | 5 +++-- .../v1/kubeconfigs_snapshot_emitter_test.go | 2 +- test/mocks/v1/testing_snapshot_emitter_test.go | 17 +++++++++-------- .../v1alpha1/testing_snapshot_emitter_test.go | 2 +- .../v2alpha1/testing_snapshot_emitter_test.go | 6 +++--- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index bb0a95efe..c042e49c9 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -1214,7 +1214,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -1228,7 +1229,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} deleteNamespaces(ctx, kube, namespace3) - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } + {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index e0a224bac..8772cb90e 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -745,7 +745,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace3) - kubeConfigNotWatched := KubeConfigList{kubeConfig2a} + kubeConfigNotWatched = KubeConfigList{kubeConfig2a} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index ae0070406..a4c42a327 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -3299,7 +3299,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource2a} + simpleMockResourceNotWatched = SimpleMockResourceList{simpleMockResource2a} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3395,7 +3395,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched := MockResourceList{mockResource2a} + mockResourceNotWatched = MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3491,7 +3491,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched := FakeResourceList{fakeResource2a} + fakeResourceNotWatched = FakeResourceList{fakeResource2a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3587,7 +3587,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource2a} + anotherMockResourceNotWatched = AnotherMockResourceList{anotherMockResource2a} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3674,10 +3674,11 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource2a} - assertSnapshotClusterresources(clusterResourceWatched, nil) + clusterResourceNotWatched := ClusterResourceList{} + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - clusterResourceNotWatched := ClusterResourceList{clusterResource2a} + clusterResourceNotWatched = ClusterResourceList{clusterResource2a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3765,7 +3766,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType2a} + mockCustomTypeNotWatched = MockCustomTypeList{mockCustomType2a} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3861,7 +3862,7 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace3) - podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a} + podNotWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a} assertSnapshotpods(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index c6bb19a7d..ea08ce9d4 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -760,7 +760,7 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched := MockResourceList{mockResource2a} + mockResourceNotWatched = MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 048458ceb..63f6ff862 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1627,7 +1627,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched := MockResourceList{mockResource2a} + mockResourceNotWatched = MockResourceList{mockResource2a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1723,7 +1723,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a} + frequentlyChangingAnnotationsResourceNotWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1819,7 +1819,7 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource2a} + fakeResourceNotWatched = testing_solo_io.FakeResourceList{fakeResource2a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) From 0d847d1b60c4ac7581a48ec95f3e870be78273ab Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 31 Aug 2022 08:26:17 -0500 Subject: [PATCH 33/98] fix --- test/mocks/v1/non_template_tests_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/mocks/v1/non_template_tests_test.go b/test/mocks/v1/non_template_tests_test.go index b4e9d2a5b..a9622f80b 100644 --- a/test/mocks/v1/non_template_tests_test.go +++ b/test/mocks/v1/non_template_tests_test.go @@ -11,6 +11,7 @@ import ( . "github.com/onsi/gomega" "github.com/solo-io/go-utils/log" "github.com/solo-io/k8s-utils/kubeutils" + "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" @@ -135,7 +136,9 @@ var _ = Describe("V1Emitter", func() { podClient, err = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient) + + resourceNamespaceLister := namespace.NewKubeClientResourceNamespaceLister(kube) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) // create `FakeResource`s in "namespace1" and "slowWatchNamespace" _, err = fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) From feb1c793c155e485e4b2561df34234aa824b3fcc Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 31 Aug 2022 16:56:35 -0500 Subject: [PATCH 34/98] race conditions, still working on fixing issues with deleted namespaces --- .../templates/snapshot_emitter_template.go | 16 +- .../snapshot_emitter_test_template.go | 95 ++++++--- .../v1/kubeconfigs_snapshot_emitter.sk.go | 10 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 36 +++- test/mocks/v1/testing_snapshot_emitter.sk.go | 31 ++- .../mocks/v1/testing_snapshot_emitter_test.go | 196 ++++++++++++------ .../v1alpha1/testing_snapshot_emitter.sk.go | 10 +- .../v1alpha1/testing_snapshot_emitter_test.go | 36 +++- .../v2alpha1/testing_snapshot_emitter.sk.go | 16 +- .../v2alpha1/testing_snapshot_emitter_test.go | 80 +++++-- 10 files changed, 379 insertions(+), 147 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 472170805..52a402614 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -144,6 +144,8 @@ type {{ lower_camel .GoName }}Emitter struct { // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). namespacesWatching sync.Map + // updateNamespaces is used to perform locks and unlocks when watches on namespaces are being updated/created + updateNamespaces sync.Mutex } func (c *{{ lower_camel .GoName }}Emitter) Register() error { @@ -282,6 +284,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { + // we do not want to filter out "" which equals all namespaces if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -371,6 +374,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o // get the list of new namespaces, if there is a new namespace // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) @@ -393,10 +397,11 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o }) for _, ns := range missingNamespaces { - c.namespacesWatching.Delete(ns) + // c.namespacesWatching.Delete(ns) {{- range .Resources}} {{- if (not .ClusterScoped) }} - {{ lower_camel .PluralName }}ByNamespace.Delete(ns) + {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ .ImportPrefix }}{{ .Name }}List{}, namespace: ns} + // {{ lower_camel .PluralName }}ByNamespace.Delete(ns) {{- end }} {{- end }} } @@ -453,6 +458,7 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o } }(namespace) } + c.updateNamespaces.Unlock() } } }() @@ -465,7 +471,11 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o if err != nil { return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list") } - {{ lower_camel .Name }}Chan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(opts) + // for Cluster scoped resources, we do not use Expression Selectors + {{ lower_camel .Name }}Chan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(clients.WatchOpts{ + Ctx: opts.Ctx, + Selector: opts.Selector, + }) if err != nil { return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch") } diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index c042e49c9..d6c4a3167 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -20,8 +20,10 @@ package {{ .Project.ProjectConfig.Version }} {{- $clients := (join_str_slice $clients ", ") }} import ( + "bytes" "context" "os" + "fmt" "time" {{ .Imports }} @@ -64,6 +66,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace5, namespace6 string name1, name2 = "angela"+helpers.RandString(3), "bob"+helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) + name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" @@ -125,6 +129,18 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) } + // getNewNamespaces is used to generate new namespace names, so that we do not have to wait + // when deleting namespaces in runNamespacedSelectorsWithWatchNamespaces. Since + // runNamespacedSelectorsWithWatchNamespaces uses watchNamespaces set to namespace1 and + // namespace2, this will work. Because the emitter willl only be watching namespaces that are + // labeled. + getNewNamespaces := func() { + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -249,7 +265,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -269,7 +285,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name5, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -288,7 +304,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name6, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -310,7 +326,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name7, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -329,7 +345,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name8, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}8a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -455,6 +471,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + {{- end }} } @@ -609,6 +630,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) + {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -693,63 +715,67 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } {{- if .ClusterScoped }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) + {{- else }} + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) + {{- end }} {{- if .ClusterScoped }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }, nil) + {{- else }} + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }, nil) + {{- end }} {{- if .ClusterScoped }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a }) + {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} {{- if .ClusterScoped }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a }) + {{- else }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshot{{ .PluralName }}(nil, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }) + {{- end }} {{- end}} }) @@ -864,7 +890,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -882,7 +908,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -900,7 +926,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name4, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -920,7 +946,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -938,7 +964,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name6, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -957,7 +983,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name7), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) @@ -1045,6 +1071,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + {{- end }} }) }) @@ -1211,12 +1242,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ } + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + deleteNamespaces(ctx, kube, namespace3) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) + {{- else }} {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) @@ -1226,23 +1261,24 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- end }} + deleteNamespaces(ctx, kube, namespace3) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b } + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) + assertSnapshotClusterresources({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - deleteNamespaces(ctx, kube, namespace3) - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) +{{- end }} createNamespaceWithLabel(ctx, kube, namespace5, labels1) {{- if .ClusterScoped }} - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) deleteNamespaces(ctx, kube, namespace4) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -1259,8 +1295,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func for _, r := range {{ lower_camel .Name }}Watched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched , r) } - assertNo{{ .PluralName }}Sent() + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index f89fb3552..319329115 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -105,6 +105,8 @@ type kubeconfigsEmitter struct { // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). namespacesWatching sync.Map + // updateNamespaces is used to perform locks and unlocks when watches on namespaces are being updated/created + updateNamespaces sync.Mutex } func (c *kubeconfigsEmitter) Register() error { @@ -219,6 +221,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { + // we do not want to filter out "" which equals all namespaces if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -300,6 +303,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // get the list of new namespaces, if there is a new namespace // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) @@ -322,8 +326,9 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }) for _, ns := range missingNamespaces { - c.namespacesWatching.Delete(ns) - kubeconfigsByNamespace.Delete(ns) + // c.namespacesWatching.Delete(ns) + kubeConfigChan <- kubeConfigListWithNamespace{list: KubeConfigList{}, namespace: ns} + // kubeconfigsByNamespace.Delete(ns) } for _, namespace := range newNamespaces { @@ -370,6 +375,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } }(namespace) } + c.updateNamespaces.Unlock() } } }() diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 8772cb90e..2580d8144 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -44,6 +44,8 @@ var _ = Describe("V1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) + name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" @@ -97,6 +99,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + // getNewNamespaces is used to generate new namespace names, so that we do not have to wait + // when deleting namespaces in runNamespacedSelectorsWithWatchNamespaces. Since + // runNamespacedSelectorsWithWatchNamespaces uses watchNamespaces set to namespace1 and + // namespace2, this will work. Because the emitter willl only be watching namespaces that are + // labeled. + getNewNamespaces := func() { + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -266,6 +280,10 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() } BeforeEach(func() { @@ -423,31 +441,29 @@ var _ = Describe("V1Emitter", func() { } } } + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) + kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}, nil) err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, KubeConfigList{kubeConfig2a, kubeConfig2b}) err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotkubeconfigs(nil, KubeConfigList{kubeConfig1a, kubeConfig1b, kubeConfig2a, kubeConfig2b}) }) @@ -601,6 +617,10 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() }) }) @@ -745,8 +765,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace3) - kubeConfigNotWatched = KubeConfigList{kubeConfig2a} - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) + kubeConfigWatched = KubeConfigList{kubeConfig2b} + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2a) + assertSnapshotClusterresources(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -763,8 +784,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range kubeConfigWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + kubeConfigNotWatched = append(kubeConfigNotWatched, r) } - assertNokubeconfigsSent() + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace5) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index e0e85ba8a..57fe7cc13 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -125,6 +125,8 @@ type testingEmitter struct { // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). namespacesWatching sync.Map + // updateNamespaces is used to perform locks and unlocks when watches on namespaces are being updated/created + updateNamespaces sync.Mutex } func (c *testingEmitter) Register() error { @@ -462,6 +464,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { + // we do not want to filter out "" which equals all namespaces if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -683,6 +686,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of new namespaces, if there is a new namespace // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) @@ -705,13 +709,19 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - c.namespacesWatching.Delete(ns) - simplemocksByNamespace.Delete(ns) - mocksByNamespace.Delete(ns) - fakesByNamespace.Delete(ns) - anothermockresourcesByNamespace.Delete(ns) - mctsByNamespace.Delete(ns) - podsByNamespace.Delete(ns) + // c.namespacesWatching.Delete(ns) + simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: SimpleMockResourceList{}, namespace: ns} + // simplemocksByNamespace.Delete(ns) + mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} + // mocksByNamespace.Delete(ns) + fakeResourceChan <- fakeResourceListWithNamespace{list: FakeResourceList{}, namespace: ns} + // fakesByNamespace.Delete(ns) + anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: AnotherMockResourceList{}, namespace: ns} + // anothermockresourcesByNamespace.Delete(ns) + mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: MockCustomTypeList{}, namespace: ns} + // mctsByNamespace.Delete(ns) + podChan <- podListWithNamespace{list: github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{}, namespace: ns} + // podsByNamespace.Delete(ns) } for _, namespace := range newNamespaces { @@ -903,6 +913,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + c.updateNamespaces.Unlock() } } }() @@ -921,7 +932,11 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, errors.Wrapf(err, "initial ClusterResource list") } - clusterResourceChan, clusterResourceErrs, err := c.clusterResource.Watch(opts) + // for Cluster scoped resources, we do not use Expression Selectors + clusterResourceChan, clusterResourceErrs, err := c.clusterResource.Watch(clients.WatchOpts{ + Ctx: opts.Ctx, + Selector: opts.Selector, + }) if err != nil { return nil, nil, errors.Wrapf(err, "starting ClusterResource watch") } diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index a4c42a327..581f4d8b0 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -49,6 +49,8 @@ var _ = Describe("V1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) + name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" @@ -140,6 +142,18 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + // getNewNamespaces is used to generate new namespace names, so that we do not have to wait + // when deleting namespaces in runNamespacedSelectorsWithWatchNamespaces. Since + // runNamespacedSelectorsWithWatchNamespaces uses watchNamespaces set to namespace1 and + // namespace2, this will work. Because the emitter willl only be watching namespaces that are + // labeled. + getNewNamespaces := func() { + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -310,6 +324,10 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* MockResource */ @@ -451,6 +469,10 @@ var _ = Describe("V1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FakeResource */ @@ -592,6 +614,10 @@ var _ = Describe("V1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, fakeResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* AnotherMockResource */ @@ -733,6 +759,10 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* ClusterResource */ @@ -779,17 +809,17 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaces(ctx, kube, namespace4) - clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource4a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource5a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name5, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name3, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name6, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) assertSnapshotClusterresources(clusterResourceWatched, nil) @@ -797,12 +827,12 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace5, labels1) createNamespaces(ctx, kube, namespace6) - clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels2), clients.WriteOpts{Ctx: ctx}) + clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name7, labels2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name8, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource8a) assertSnapshotClusterresources(clusterResourceWatched, nil) @@ -842,6 +872,10 @@ var _ = Describe("V1Emitter", func() { clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource8a}...) assertSnapshotClusterresources(nil, clusterResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* MockCustomType */ @@ -983,6 +1017,10 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) assertSnapshotmcts(nil, mockCustomTypeNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* Pod */ @@ -1123,6 +1161,10 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) assertSnapshotpods(nil, podNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() } BeforeEach(func() { @@ -1677,31 +1719,29 @@ var _ = Describe("V1Emitter", func() { } } } + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) + simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}, nil) err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}) err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotSimplemocks(nil, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b, simpleMockResource2a, simpleMockResource2b}) /* @@ -1734,31 +1774,29 @@ var _ = Describe("V1Emitter", func() { } } } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) /* @@ -1791,31 +1829,29 @@ var _ = Describe("V1Emitter", func() { } } } + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, FakeResourceList{fakeResource2a, fakeResource2b}) err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(nil, FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) /* @@ -1848,31 +1884,29 @@ var _ = Describe("V1Emitter", func() { } } } + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}, nil) err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}) err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotAnothermockresources(nil, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b, anotherMockResource2a, anotherMockResource2b}) /* @@ -1903,23 +1937,21 @@ var _ = Describe("V1Emitter", func() { } } } + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a, clusterResource2a}, nil) err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, ClusterResourceList{clusterResource2a}) err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotClusterresources(nil, ClusterResourceList{clusterResource1a, clusterResource2a}) /* @@ -1952,31 +1984,29 @@ var _ = Describe("V1Emitter", func() { } } } + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) + mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}, nil) err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, MockCustomTypeList{mockCustomType2a, mockCustomType2b}) err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) /* @@ -2009,31 +2039,29 @@ var _ = Describe("V1Emitter", func() { } } } + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) + pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}, nil) err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}) err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotpods(nil, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b, pod2a, pod2b}) }) @@ -2188,6 +2216,10 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* MockResource */ @@ -2311,6 +2343,10 @@ var _ = Describe("V1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FakeResource */ @@ -2434,6 +2470,10 @@ var _ = Describe("V1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) assertSnapshotFakes(nil, fakeResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* AnotherMockResource */ @@ -2557,6 +2597,10 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* ClusterResource */ @@ -2594,34 +2638,34 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource3a} assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name4, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) assertSnapshotClusterresources(clusterResourceWatched, nil) createNamespaces(ctx, kube, namespace5, namespace6) - clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name2, labels1), clients.WriteOpts{Ctx: ctx}) + clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name6, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) + clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name7), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) assertSnapshotClusterresources(clusterResourceWatched, nil) @@ -2666,6 +2710,10 @@ var _ = Describe("V1Emitter", func() { clusterResourceNotWatched = ClusterResourceList{clusterResource7a} assertSnapshotClusterresources(nil, clusterResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* MockCustomType */ @@ -2789,6 +2837,10 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) assertSnapshotmcts(nil, mockCustomTypeNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* Pod */ @@ -2911,6 +2963,10 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) assertSnapshotpods(nil, podNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() }) }) @@ -3299,8 +3355,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - simpleMockResourceNotWatched = SimpleMockResourceList{simpleMockResource2a} - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2b} + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2a) + assertSnapshotClusterresources(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3317,8 +3374,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range simpleMockResourceWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, r) } - assertNoSimplemocksSent() + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3395,8 +3453,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = MockResourceList{mockResource2a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) + mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3413,8 +3472,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) } - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3491,8 +3551,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched = FakeResourceList{fakeResource2a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) + fakeResourceWatched = FakeResourceList{fakeResource2b} + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) + assertSnapshotClusterresources(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3509,8 +3570,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched = append(fakeResourceNotWatched, r) } - assertNoFakesSent() + assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3587,8 +3649,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - anotherMockResourceNotWatched = AnotherMockResourceList{anotherMockResource2a} - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2b} + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2a) + assertSnapshotClusterresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3605,8 +3668,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range anotherMockResourceWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, r) } - assertNoAnothermockresourcesSent() + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3671,25 +3735,25 @@ var _ = Describe("V1Emitter", func() { createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource2a} - clusterResourceNotWatched := ClusterResourceList{} + clusterResourceNotWatched := ClusterResourceList{clusterResource1a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - clusterResourceNotWatched = ClusterResourceList{clusterResource2a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) + assertSnapshotClusterresources(nil, clusterResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) - clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = ClusterResourceList{clusterResource3a} - assertSnapshotClusterresources(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotClusterresources(clusterResourceWatched, nil) + assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3766,8 +3830,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockCustomTypeNotWatched = MockCustomTypeList{mockCustomType2a} - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType2b} + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2a) + assertSnapshotClusterresources(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3784,8 +3849,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range mockCustomTypeWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, r) } - assertNomctsSent() + assertSnapshotmcts(nil, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -3862,8 +3928,9 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace3) - podNotWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a} - assertSnapshotpods(podWatched, podNotWatched) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} + podNotWatched = append(podNotWatched, pod2a) + assertSnapshotClusterresources(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3880,8 +3947,9 @@ var _ = Describe("V1Emitter", func() { for _, r := range podWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + podNotWatched = append(podNotWatched, r) } - assertNopodsSent() + assertSnapshotpods(nil, podNotWatched) deleteNamespaces(ctx, kube, namespace5) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 9ed684840..0171122f6 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -105,6 +105,8 @@ type testingEmitter struct { // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). namespacesWatching sync.Map + // updateNamespaces is used to perform locks and unlocks when watches on namespaces are being updated/created + updateNamespaces sync.Mutex } func (c *testingEmitter) Register() error { @@ -219,6 +221,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { + // we do not want to filter out "" which equals all namespaces if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -300,6 +303,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of new namespaces, if there is a new namespace // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) @@ -322,8 +326,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - c.namespacesWatching.Delete(ns) - mocksByNamespace.Delete(ns) + // c.namespacesWatching.Delete(ns) + mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} + // mocksByNamespace.Delete(ns) } for _, namespace := range newNamespaces { @@ -370,6 +375,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + c.updateNamespaces.Unlock() } } }() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index ea08ce9d4..2fbde2202 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -46,6 +46,8 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) + name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" @@ -101,6 +103,18 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + // getNewNamespaces is used to generate new namespace names, so that we do not have to wait + // when deleting namespaces in runNamespacedSelectorsWithWatchNamespaces. Since + // runNamespacedSelectorsWithWatchNamespaces uses watchNamespaces set to namespace1 and + // namespace2, this will work. Because the emitter willl only be watching namespaces that are + // labeled. + getNewNamespaces := func() { + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -270,6 +284,10 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() } BeforeEach(func() { @@ -438,31 +456,29 @@ var _ = Describe("V1Alpha1Emitter", func() { } } } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) }) @@ -616,6 +632,10 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() }) }) @@ -760,8 +780,9 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = MockResourceList{mockResource2a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) + mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -778,8 +799,9 @@ var _ = Describe("V1Alpha1Emitter", func() { for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) } - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 0c60d4bf1..45b7bfcb9 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -113,6 +113,8 @@ type testingEmitter struct { // namespacesWatching is the set of namespaces that we are watching. This is helpful // when Expression Selector is set on the Watch Opts in Snapshot(). namespacesWatching sync.Map + // updateNamespaces is used to perform locks and unlocks when watches on namespaces are being updated/created + updateNamespaces sync.Mutex } func (c *testingEmitter) Register() error { @@ -313,6 +315,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { + // we do not want to filter out "" which equals all namespaces if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -450,6 +453,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // get the list of new namespaces, if there is a new namespace // get the list of resources from that namespace, and add // a watch for new resources created/deleted on that namespace + c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) @@ -472,10 +476,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - c.namespacesWatching.Delete(ns) - mocksByNamespace.Delete(ns) - fcarsByNamespace.Delete(ns) - fakesByNamespace.Delete(ns) + // c.namespacesWatching.Delete(ns) + mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} + // mocksByNamespace.Delete(ns) + frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: FrequentlyChangingAnnotationsResourceList{}, namespace: ns} + // fcarsByNamespace.Delete(ns) + fakeResourceChan <- fakeResourceListWithNamespace{list: testing_solo_io.FakeResourceList{}, namespace: ns} + // fakesByNamespace.Delete(ns) } for _, namespace := range newNamespaces { @@ -580,6 +587,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + c.updateNamespaces.Unlock() } } }() diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 63f6ff862..4b4bb0845 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -49,6 +49,8 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) + name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" @@ -116,6 +118,18 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } + // getNewNamespaces is used to generate new namespace names, so that we do not have to wait + // when deleting namespaces in runNamespacedSelectorsWithWatchNamespaces. Since + // runNamespacedSelectorsWithWatchNamespaces uses watchNamespaces set to namespace1 and + // namespace2, this will work. Because the emitter willl only be watching namespaces that are + // labeled. + getNewNamespaces := func() { + namespace3 = helpers.RandString(8) + namespace4 = helpers.RandString(8) + namespace5 = helpers.RandString(8) + namespace6 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -286,6 +300,10 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FrequentlyChangingAnnotationsResource */ @@ -427,6 +445,10 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FakeResource */ @@ -567,6 +589,10 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, fakeResourceNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() } BeforeEach(func() { @@ -863,31 +889,29 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}, nil) err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, MockResourceList{mockResource2a, mockResource2b}) err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotMocks(nil, MockResourceList{mockResource1a, mockResource1b, mockResource2a, mockResource2b}) /* @@ -920,31 +944,29 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) + frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}, nil) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFcars(nil, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b, frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}) /* @@ -977,31 +999,29 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) + fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}, nil) err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}) err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - assertSnapshotFakes(nil, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b, fakeResource2a, fakeResource2b}) }) @@ -1156,6 +1176,10 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FrequentlyChangingAnnotationsResource */ @@ -1279,6 +1303,10 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* FakeResource */ @@ -1401,6 +1429,10 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) assertSnapshotFakes(nil, fakeResourceNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() }) }) @@ -1627,8 +1659,9 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - mockResourceNotWatched = MockResourceList{mockResource2a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) + mockResourceWatched = MockResourceList{mockResource2b} + mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) + assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1645,8 +1678,9 @@ var _ = Describe("V2Alpha1Emitter", func() { for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) } - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -1723,8 +1757,9 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - frequentlyChangingAnnotationsResourceNotWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a} - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2a) + assertSnapshotClusterresources(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1741,8 +1776,9 @@ var _ = Describe("V2Alpha1Emitter", func() { for _, r := range frequentlyChangingAnnotationsResourceWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, r) } - assertNoFcarsSent() + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) @@ -1819,8 +1855,9 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) - fakeResourceNotWatched = testing_solo_io.FakeResourceList{fakeResource2a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2b} + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) + assertSnapshotClusterresources(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1837,8 +1874,9 @@ var _ = Describe("V2Alpha1Emitter", func() { for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched = append(fakeResourceNotWatched, r) } - assertNoFakesSent() + assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace5) From c2377cf6bc878178c4bfd59971e4c9ba60e59f20 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 10:38:53 -0500 Subject: [PATCH 35/98] fix issue with non Expression Selector emitters updating when namespace is deleted fix issue with Expression Selector emitters deleting resources from watched namespace --- .../templates/snapshot_emitter_template.go | 4 ++ .../snapshot_emitter_test_template.go | 36 +++++++--- .../v1/kubeconfigs_snapshot_emitter.sk.go | 4 ++ .../v1/kubeconfigs_snapshot_emitter_test.go | 20 +++++- test/mocks/v1/testing_snapshot_emitter.sk.go | 4 ++ .../mocks/v1/testing_snapshot_emitter_test.go | 69 +++++++++++-------- .../v1alpha1/testing_snapshot_emitter.sk.go | 4 ++ .../v1alpha1/testing_snapshot_emitter_test.go | 20 +++++- .../v2alpha1/testing_snapshot_emitter.sk.go | 4 ++ .../v2alpha1/testing_snapshot_emitter_test.go | 32 ++++++--- 10 files changed, 143 insertions(+), 54 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 52a402614..f27ab9d74 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -386,6 +386,10 @@ func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, o mapOfResourceNamespaces[ns.Name] = true } + for _, ns := range watchNamespaces { + mapOfResourceNamespaces[ns] = true + } + missingNamespaces := []string{} // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index d6c4a3167..b7a63f84c 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -1094,6 +1094,20 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + {{- range .Resources }} /* @@ -1138,8 +1152,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} - assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) + {{- end }} }) @@ -1217,6 +1232,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} + // cluster scoped resources never get deleted from a namespace delete {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } @@ -1244,13 +1260,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a} - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace3) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) - assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) + assertNo{{ .PluralName }}Sent() {{- else }} @@ -1264,7 +1278,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func deleteNamespaces(ctx, kube, namespace3) {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b } {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) - assertSnapshotClusterresources({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -1274,11 +1288,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertNo{{ .PluralName }}Sent() {{- else }} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 319329115..0d9abaf96 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -315,6 +315,10 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa mapOfResourceNamespaces[ns.Name] = true } + for _, ns := range watchNamespaces { + mapOfResourceNamespaces[ns] = true + } + missingNamespaces := []string{} // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 2580d8144..40e0c780d 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -638,6 +638,20 @@ var _ = Describe("V1Emitter", func() { var snap *KubeconfigsSnapshot + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + /* KubeConfig */ @@ -676,8 +690,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(kubeConfigWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -767,7 +781,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) kubeConfigWatched = KubeConfigList{kubeConfig2b} kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2a) - assertSnapshotClusterresources(kubeConfigWatched, kubeConfigNotWatched) + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 57fe7cc13..56d5e23f0 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -698,6 +698,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mapOfResourceNamespaces[ns.Name] = true } + for _, ns := range watchNamespaces { + mapOfResourceNamespaces[ns] = true + } + missingNamespaces := []string{} // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 581f4d8b0..76a09e5ac 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -2984,6 +2984,20 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + /* SimpleMockResource */ @@ -3022,8 +3036,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotSimplemocks(simpleMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* MockResource @@ -3063,8 +3077,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* FakeResource @@ -3104,8 +3118,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(nil, fakeResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* AnotherMockResource @@ -3145,8 +3159,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* ClusterResource @@ -3184,8 +3198,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource1b} - assertSnapshotClusterresources(nil, clusterResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* MockCustomType @@ -3225,8 +3239,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotmcts(mockCustomTypeWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertSnapshotmcts(nil, mockCustomTypeNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* Pod @@ -3266,8 +3280,8 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(podWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertSnapshotpods(nil, podNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -3357,7 +3371,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2b} simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2a) - assertSnapshotClusterresources(simpleMockResourceWatched, simpleMockResourceNotWatched) + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3455,7 +3469,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3553,7 +3567,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) fakeResourceWatched = FakeResourceList{fakeResource2b} fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) - assertSnapshotClusterresources(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3651,7 +3665,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2b} anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2a) - assertSnapshotClusterresources(anotherMockResourceWatched, anotherMockResourceNotWatched) + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3723,6 +3737,7 @@ var _ = Describe("V1Emitter", func() { } } + // cluster scoped resources never get deleted from a namespace delete clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched := ClusterResourceList{clusterResource1a} @@ -3737,23 +3752,21 @@ var _ = Describe("V1Emitter", func() { clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource2a} - clusterResourceNotWatched := ClusterResourceList{clusterResource1a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) + clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace3) - clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) - assertSnapshotClusterresources(nil, clusterResourceNotWatched) + assertNoClusterresourcesSent() createNamespaceWithLabel(ctx, kube, namespace5, labels1) clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource3a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) + clusterResourceWatched = append(clusterResourceWatched, clusterResource3a) + assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace4) - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) + assertNoClusterresourcesSent() deleteNamespaces(ctx, kube, namespace5) @@ -3832,7 +3845,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) mockCustomTypeWatched = MockCustomTypeList{mockCustomType2b} mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2a) - assertSnapshotClusterresources(mockCustomTypeWatched, mockCustomTypeNotWatched) + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -3930,7 +3943,7 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} podNotWatched = append(podNotWatched, pod2a) - assertSnapshotClusterresources(podWatched, podNotWatched) + assertSnapshotpods(podWatched, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 0171122f6..fdbd38cc5 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -315,6 +315,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mapOfResourceNamespaces[ns.Name] = true } + for _, ns := range watchNamespaces { + mapOfResourceNamespaces[ns] = true + } + missingNamespaces := []string{} // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 2fbde2202..b58096903 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -653,6 +653,20 @@ var _ = Describe("V1Alpha1Emitter", func() { var snap *TestingSnapshot + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + /* MockResource */ @@ -691,8 +705,8 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -782,7 +796,7 @@ var _ = Describe("V1Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 45b7bfcb9..b56ec15e7 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -465,6 +465,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO mapOfResourceNamespaces[ns.Name] = true } + for _, ns := range watchNamespaces { + mapOfResourceNamespaces[ns] = true + } + missingNamespaces := []string{} // use the map of namespace resources to find missing/deleted namespaces c.namespacesWatching.Range(func(key interface{}, value interface{}) bool { diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 4b4bb0845..df24d7f1f 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1450,6 +1450,20 @@ var _ = Describe("V2Alpha1Emitter", func() { var snap *TestingSnapshot + assertNoMessageSent := func() { + for { + select { + case snap = <-snapshots: + Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 5): + // this means that we have not recieved any mocks that we are not expecting + return + } + } + } + /* MockResource */ @@ -1488,8 +1502,8 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotMocks(mockResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* FrequentlyChangingAnnotationsResource @@ -1529,8 +1543,8 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) /* FakeResource @@ -1570,8 +1584,8 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(fakeResourceWatched, nil) deleteNamespaces(ctx, kube, namespace1, namespace2) - fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(nil, fakeResourceNotWatched) + assertNoMessageSent() + createNamespaces(ctx, kube, namespace1, namespace2) }) It("Should not contain resources from a deleted namespace, that is filtered", func() { @@ -1661,7 +1675,7 @@ var _ = Describe("V2Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) - assertSnapshotClusterresources(mockResourceWatched, mockResourceNotWatched) + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1759,7 +1773,7 @@ var _ = Describe("V2Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2a) - assertSnapshotClusterresources(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) @@ -1857,7 +1871,7 @@ var _ = Describe("V2Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace3) fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2b} fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) - assertSnapshotClusterresources(fakeResourceWatched, fakeResourceNotWatched) + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace5, labels1) From b806cfc176139b198355529406580a2486e805cc Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 12:25:57 -0500 Subject: [PATCH 36/98] update fix problem with namespaces getting terminated, but not deleted --- .../snapshot_emitter_test_template.go | 27 +++++- .../v1/kubeconfigs_snapshot_emitter_test.go | 18 +++- .../mocks/v1/testing_snapshot_emitter_test.go | 84 +++++++++++++++++-- .../v1alpha1/testing_snapshot_emitter_test.go | 18 +++- .../v2alpha1/testing_snapshot_emitter_test.go | 40 ++++++++- 5 files changed, 173 insertions(+), 14 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index b7a63f84c..17f9cda48 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -141,6 +141,13 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace6 = helpers.RandString(8) } + // getNewNamespaces1and2 is used to generate new namespaces for namespace 1 and 2. + // used for the same reason as getNewNamespaces() above + getNewNamespaces1and2 := func() { + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -1146,15 +1153,31 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) +{{- if .ClusterScoped }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) +{{- else }} + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) +{{- end }} + + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b} + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) + deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() - createNamespaces(ctx, kube, namespace1, namespace2) + getNewNamespaces1and2() + createNamespaces(ctx, kube, namespace1, namespace2) {{- end }} }) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 40e0c780d..2b9c882f6 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -111,6 +111,13 @@ var _ = Describe("V1Emitter", func() { namespace6 = helpers.RandString(8) } + // getNewNamespaces1and2 is used to generate new namespaces for namespace 1 and 2. + // used for the same reason as getNewNamespaces() above + getNewNamespaces1and2 := func() { + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -684,13 +691,22 @@ var _ = Describe("V1Emitter", func() { kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertSnapshotkubeconfigs(kubeConfigWatched, nil) + err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) }) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 76a09e5ac..605adff43 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -154,6 +154,13 @@ var _ = Describe("V1Emitter", func() { namespace6 = helpers.RandString(8) } + // getNewNamespaces1and2 is used to generate new namespaces for namespace 1 and 2. + // used for the same reason as getNewNamespaces() above + getNewNamespaces1and2 := func() { + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -3030,13 +3037,22 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertSnapshotSimplemocks(simpleMockResourceWatched, nil) + err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3071,13 +3087,22 @@ var _ = Describe("V1Emitter", func() { mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3112,13 +3137,22 @@ var _ = Describe("V1Emitter", func() { fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertSnapshotFakes(fakeResourceWatched, nil) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3153,13 +3187,22 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) + err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3192,13 +3235,22 @@ var _ = Describe("V1Emitter", func() { clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResource1b, err := clusterResourceClient.Write(NewClusterResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource1b, err := clusterResourceClient.Write(NewClusterResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched := ClusterResourceList{clusterResource1a, clusterResource1b} assertSnapshotClusterresources(clusterResourceWatched, nil) + err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = clusterResourceClient.Delete(clusterResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource1b} + assertSnapshotClusterresources(nil, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3233,13 +3285,22 @@ var _ = Describe("V1Emitter", func() { mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertSnapshotmcts(mockCustomTypeWatched, nil) + err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} + assertSnapshotmcts(nil, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -3274,13 +3335,22 @@ var _ = Describe("V1Emitter", func() { pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertSnapshotpods(podWatched, nil) + err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} + assertSnapshotpods(nil, podNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index b58096903..488815347 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -115,6 +115,13 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace6 = helpers.RandString(8) } + // getNewNamespaces1and2 is used to generate new namespaces for namespace 1 and 2. + // used for the same reason as getNewNamespaces() above + getNewNamespaces1and2 := func() { + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -699,13 +706,22 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index df24d7f1f..6ecd43727 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -130,6 +130,13 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace6 = helpers.RandString(8) } + // getNewNamespaces1and2 is used to generate new namespaces for namespace 1 and 2. + // used for the same reason as getNewNamespaces() above + getNewNamespaces1and2 := func() { + namespace1 = helpers.RandString(8) + namespace2 = helpers.RandString(8) + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -1496,13 +1503,22 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) + err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} + assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -1537,13 +1553,22 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) /* @@ -1578,13 +1603,22 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertSnapshotFakes(fakeResourceWatched, nil) + err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} + assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) assertNoMessageSent() + + getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) }) From 8587388db2cbc5f3afe1959967e66df597c6f3e0 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 14:40:08 -0500 Subject: [PATCH 37/98] refactored the test code, to make it more easier to manage --- .../snapshot_emitter_test_template.go | 406 +------- .../v1/kubeconfigs_snapshot_emitter_test.go | 134 +-- .../mocks/v1/testing_snapshot_emitter_test.go | 944 ++---------------- .../v1alpha1/testing_snapshot_emitter_test.go | 134 +-- .../v2alpha1/testing_snapshot_emitter_test.go | 396 +------- 5 files changed, 143 insertions(+), 1871 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 17f9cda48..07dbeb3ab 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -66,10 +66,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace5, namespace6 string name1, name2 = "angela"+helpers.RandString(3), "bob"+helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) - name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} - labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" {{- if $need_kube_config }} cfg *rest.Config @@ -212,7 +210,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } - {{- if .ClusterScoped }} {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) @@ -220,43 +217,19 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) -{{- else }} - - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a ) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} - {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b }...) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -272,9 +245,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- if .ClusterScoped }} - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name4), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) + + {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name5, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a ) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) {{- else }} @@ -285,19 +263,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4b } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name5, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{ lower_camel .Name }}5a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -305,66 +271,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5a) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}5b) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name6, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}6b) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name7, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}7b) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name8, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}8a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}8a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}8b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a, {{ lower_camel .Name }}8b }...) - assertNoMessageSent() + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) for _, r := range {{ lower_camel .Name }}NotWatched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -374,113 +281,52 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - {{- if .ClusterScoped }} err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a } - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}3a } + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- end }} - -{{- if .ClusterScoped }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Namespace, {{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1b.GetMetadata().Namespace, {{ lower_camel .Name }}1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a }...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}8a} + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b}...) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Namespace, {{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3b.GetMetadata().Namespace, {{ lower_camel .Name }}3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a }...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Namespace, {{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}8a }...) - assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) - -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Namespace, {{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Namespace, {{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a}...) assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() {{- end }} @@ -911,42 +757,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b} assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}3b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}3b}...) - assertNo{{ .PluralName }}Sent() - -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name4, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}4a ) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}4a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}4b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b }...) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - {{- end }} createNamespaces(ctx, kube, namespace5, namespace6) @@ -967,36 +777,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) assertNoMessageSent() -{{- end }} - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name6, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}6a) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}6a, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}6b, err := {{ lower_camel .Name }}Client.Write(New{{ .Name }}WithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}6b}...) - assertNoMessageSent() - -{{- end }} - - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name7), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7a) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -1016,65 +796,28 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}5a } {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a, {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a } {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- else }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - -{{- end }} - -{{- if .ClusterScoped }} - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}3a) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a, {{ lower_camel .Name }}7a } - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}6a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a } + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}5a) assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4a.GetMetadata().Namespace, {{ lower_camel .Name }}4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}4b.GetMetadata().Namespace, {{ lower_camel .Name }}4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}4a, {{ lower_camel .Name }}4b}...) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -1195,7 +938,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot - {{- $length := len .Resources }} {{- $last_entry := minus $length 1 }} {{- range $i, $r := .Resources }} @@ -1204,24 +946,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ .Name }} */ - assertNo{{ .PluralName }}Sent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.{{ upper_camel .PluralName }}) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: for { @@ -1253,28 +977,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } } -{{- if .ClusterScoped }} - - // cluster scoped resources never get deleted from a namespace delete - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a } - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - -{{- else }} - - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertNo{{ .PluralName }}Sent() - -{{- end }} - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNo{{ .PluralName }}Sent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1283,11 +985,18 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}2a) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{} + {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) deleteNamespaces(ctx, kube, namespace3) - assertNo{{ .PluralName }}Sent() + + for _, r := range {{ lower_camel .Name }}Watched { + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched , r) + } + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- else }} @@ -1295,40 +1004,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}2b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{} {{ lower_camel .Name }}Watched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b } assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) deleteNamespaces(ctx, kube, namespace3) + {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2b } {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2a) assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) -{{- end }} - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - -{{- if .ClusterScoped }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name3), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, nil) - - deleteNamespaces(ctx, kube, namespace4) - assertNo{{ .PluralName }}Sent() - -{{- else }} - - {{ lower_camel .Name }}3a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}3a) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - - deleteNamespaces(ctx, kube, namespace4) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}2b) - {{ lower_camel .Name }}Watched = {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}3a} - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) - for _, r := range {{ lower_camel .Name }}Watched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1338,12 +1023,9 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - deleteNamespaces(ctx, kube, namespace5) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() -{{/* after each resource we have to create the namespaces that were just deleted */}} -{{if ne $last_entry $i }} - createNamespaces(ctx, kube, namespace1, namespace2) -{{- end }} {{- end }}{{/* end of with */}} {{- end }}{{/* end of range */}} }) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 2b9c882f6..7a816f81b 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -44,10 +44,8 @@ var _ = Describe("V1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) - name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} - labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" kube kubernetes.Interface emitter KubeconfigsEmitter @@ -184,13 +182,6 @@ var _ = Describe("V1Emitter", func() { kubeConfigWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} assertSnapshotkubeconfigs(kubeConfigWatched, nil) - kubeConfig2a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - assertSnapshotkubeconfigs(kubeConfigWatched, nil) - kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -217,32 +208,6 @@ var _ = Describe("V1Emitter", func() { kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig5b) assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched = append(kubeConfigWatched, kubeConfig6a) - kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig6b) - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched = append(kubeConfigWatched, kubeConfig7a) - kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig7b) - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - kubeConfig8a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig8b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig8a, kubeConfig8b}...) - assertNoMessageSent() - for _, r := range kubeConfigNotWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -254,15 +219,7 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig1b.GetMetadata().Namespace, kubeConfig1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig1a, kubeConfig1b}...) - kubeConfigWatched = KubeConfigList{kubeConfig2a, kubeConfig2b, kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - kubeConfigWatched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + kubeConfigWatched = KubeConfigList{kubeConfig3a, kubeConfig3b, kubeConfig4a, kubeConfig5a} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig3a.GetMetadata().Namespace, kubeConfig3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -270,7 +227,7 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig3b.GetMetadata().Namespace, kubeConfig3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - kubeConfigWatched = KubeConfigList{kubeConfig4a, kubeConfig5a, kubeConfig6a, kubeConfig7a} + kubeConfigWatched = KubeConfigList{kubeConfig4a, kubeConfig5a} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -278,18 +235,10 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig5a.GetMetadata().Namespace, kubeConfig5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) - kubeConfigWatched = KubeConfigList{kubeConfig6a, kubeConfig7a} - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - err = kubeConfigClient.Delete(kubeConfig6a.GetMetadata().Namespace, kubeConfig6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig7a.GetMetadata().Namespace, kubeConfig7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig6a, kubeConfig7a}...) assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() } @@ -567,20 +516,6 @@ var _ = Describe("V1Emitter", func() { kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig3b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig3a, kubeConfig3b}...) - assertNokubeconfigsSent() - - kubeConfig4a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig4b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched = append(kubeConfigWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -590,13 +525,6 @@ var _ = Describe("V1Emitter", func() { kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) assertNoMessageSent() - kubeConfig6a, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig6b, err := kubeConfigClient.Write(NewKubeConfigWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig6a, kubeConfig6b}...) - assertNoMessageSent() - kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -615,14 +543,6 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) - kubeConfigWatched = KubeConfigList{kubeConfig4a, kubeConfig4b} - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - err = kubeConfigClient.Delete(kubeConfig4a.GetMetadata().Namespace, kubeConfig4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig4b.GetMetadata().Namespace, kubeConfig4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig4a, kubeConfig4b}...) assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) // clean up environment @@ -728,24 +648,6 @@ var _ = Describe("V1Emitter", func() { KubeConfig */ - assertNokubeconfigsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Kubeconfigs) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: for { @@ -773,16 +675,6 @@ var _ = Describe("V1Emitter", func() { } } - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertNokubeconfigsSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNokubeconfigsSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -791,26 +683,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) kubeConfig2b, err := kubeConfigClient.Write(NewKubeConfig(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + kubeConfigNotWatched := KubeConfigList{} kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace3) + kubeConfigWatched = KubeConfigList{kubeConfig2b} kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2a) assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - kubeConfig3a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigWatched = append(kubeConfigWatched, kubeConfig3a) - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig2b) - kubeConfigWatched = KubeConfigList{kubeConfig3a} - assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - for _, r := range kubeConfigWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -818,8 +700,8 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) - deleteNamespaces(ctx, kube, namespace5) - + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() }) }) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 605adff43..f37419fea 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,7 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -// +build solokit - package v1 import ( @@ -49,10 +47,8 @@ var _ = Describe("V1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) - name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} - labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" cfg *rest.Config clientset *apiext.Clientset @@ -227,13 +223,6 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} assertSnapshotSimplemocks(simpleMockResourceWatched, nil) - simpleMockResource2a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - assertSnapshotSimplemocks(simpleMockResourceWatched, nil) - simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -260,32 +249,6 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource5b) assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource6a) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource6b) - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource7a) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource7b) - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - simpleMockResource8a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource8b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource8a, simpleMockResource8b}...) - assertNoMessageSent() - for _, r := range simpleMockResourceNotWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -297,15 +260,7 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource1b.GetMetadata().Namespace, simpleMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}...) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b, simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b, simpleMockResource4a, simpleMockResource5a} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource3a.GetMetadata().Namespace, simpleMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -313,7 +268,7 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource3b.GetMetadata().Namespace, simpleMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a, simpleMockResource6a, simpleMockResource7a} + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource5a} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -321,18 +276,10 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource5a.GetMetadata().Namespace, simpleMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a} - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - err = simpleMockResourceClient.Delete(simpleMockResource6a.GetMetadata().Namespace, simpleMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource7a.GetMetadata().Namespace, simpleMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource7a}...) assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -372,13 +319,6 @@ var _ = Describe("V1Emitter", func() { mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(mockResourceWatched, nil) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -405,32 +345,6 @@ var _ = Describe("V1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource6a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource7a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -442,15 +356,7 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) - mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -458,7 +364,7 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -466,18 +372,10 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -517,13 +415,6 @@ var _ = Describe("V1Emitter", func() { fakeResourceWatched := FakeResourceList{fakeResource1a, fakeResource1b} assertSnapshotFakes(fakeResourceWatched, nil) - fakeResource2a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotFakes(fakeResourceWatched, nil) - fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -550,32 +441,6 @@ var _ = Describe("V1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource8a, fakeResource8b}...) - assertNoMessageSent() - for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -587,15 +452,7 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource1a, fakeResource1b}...) - fakeResourceWatched = FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - fakeResourceWatched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + fakeResourceWatched = FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -603,7 +460,7 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - fakeResourceWatched = FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + fakeResourceWatched = FakeResourceList{fakeResource4a, fakeResource5a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -611,18 +468,10 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) - fakeResourceWatched = FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -662,13 +511,6 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) - anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - assertSnapshotAnothermockresources(anotherMockResourceWatched, nil) - anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -695,32 +537,6 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource5b) assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource6a) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource6b) - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource7a) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource7b) - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - anotherMockResource8a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource8b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource8a, anotherMockResource8b}...) - assertNoMessageSent() - for _, r := range anotherMockResourceNotWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -732,15 +548,7 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource1b.GetMetadata().Namespace, anotherMockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}...) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b, anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b, anotherMockResource4a, anotherMockResource5a} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource3a.GetMetadata().Namespace, anotherMockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -748,7 +556,7 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource3b.GetMetadata().Namespace, anotherMockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a, anotherMockResource6a, anotherMockResource7a} + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource5a} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -756,18 +564,10 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource5a.GetMetadata().Namespace, anotherMockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a} - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - err = anotherMockResourceClient.Delete(anotherMockResource6a.GetMetadata().Namespace, anotherMockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource7a.GetMetadata().Namespace, anotherMockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource7a}...) assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -803,11 +603,6 @@ var _ = Describe("V1Emitter", func() { clusterResourceWatched := ClusterResourceList{clusterResource1a} assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource2a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceWatched = append(clusterResourceWatched, clusterResource3a) @@ -826,32 +621,12 @@ var _ = Describe("V1Emitter", func() { clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name6, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - clusterResource7a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name7, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - - clusterResource8a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name8, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource8a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource2a, clusterResource3a} - clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a, clusterResource8a} + clusterResourceNotWatched := ClusterResourceList{clusterResource1a, clusterResource3a} + clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -859,28 +634,10 @@ var _ = Describe("V1Emitter", func() { err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource4a, clusterResource5a}...) - clusterResourceWatched = ClusterResourceList{clusterResource6a, clusterResource7a, clusterResource8a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource6a}...) - clusterResourceWatched = ClusterResourceList{clusterResource7a, clusterResource8a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource7a}...) - clusterResourceWatched = ClusterResourceList{clusterResource8a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource8a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched = append(clusterResourceNotWatched, ClusterResourceList{clusterResource8a}...) assertSnapshotClusterresources(nil, clusterResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -920,13 +677,6 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} assertSnapshotmcts(mockCustomTypeWatched, nil) - mockCustomType2a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - assertSnapshotmcts(mockCustomTypeWatched, nil) - mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -953,32 +703,6 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType5b) assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType6a) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType6b) - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType7a) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType7b) - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - mockCustomType8a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType8b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType8a, mockCustomType8b}...) - assertNoMessageSent() - for _, r := range mockCustomTypeNotWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -990,15 +714,7 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType1b.GetMetadata().Namespace, mockCustomType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType1a, mockCustomType1b}...) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType2a, mockCustomType2b, mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a, mockCustomType3b, mockCustomType4a, mockCustomType5a} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType3a.GetMetadata().Namespace, mockCustomType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1006,7 +722,7 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType3b.GetMetadata().Namespace, mockCustomType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType4a, mockCustomType5a, mockCustomType6a, mockCustomType7a} + mockCustomTypeWatched = MockCustomTypeList{mockCustomType4a, mockCustomType5a} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1014,18 +730,10 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType5a.GetMetadata().Namespace, mockCustomType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType6a, mockCustomType7a} - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - err = mockCustomTypeClient.Delete(mockCustomType6a.GetMetadata().Namespace, mockCustomType6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType7a.GetMetadata().Namespace, mockCustomType7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType6a, mockCustomType7a}...) assertSnapshotmcts(nil, mockCustomTypeNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -1065,13 +773,6 @@ var _ = Describe("V1Emitter", func() { podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} assertSnapshotpods(podWatched, nil) - pod2a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - assertSnapshotpods(podWatched, nil) - pod3a, err := podClient.Write(NewPodWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod3b, err := podClient.Write(NewPodWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -1098,32 +799,6 @@ var _ = Describe("V1Emitter", func() { podNotWatched = append(podNotWatched, pod5b) assertSnapshotpods(podWatched, podNotWatched) - pod6a, err := podClient.Write(NewPodWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod6b, err := podClient.Write(NewPodWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podWatched = append(podWatched, pod6a) - podNotWatched = append(podNotWatched, pod6b) - assertSnapshotpods(podWatched, podNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - pod7a, err := podClient.Write(NewPodWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod7b, err := podClient.Write(NewPodWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podWatched = append(podWatched, pod7a) - podNotWatched = append(podNotWatched, pod7b) - assertSnapshotpods(podWatched, podNotWatched) - - pod8a, err := podClient.Write(NewPodWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod8b, err := podClient.Write(NewPodWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod8a, pod8b}...) - assertNoMessageSent() - for _, r := range podNotWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1135,15 +810,7 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod1b.GetMetadata().Namespace, pod1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}...) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b, pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} - assertSnapshotpods(podWatched, podNotWatched) - - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a, pod6a, pod7a} + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b, pod4a, pod5a} assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod3a.GetMetadata().Namespace, pod3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1151,7 +818,7 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod3b.GetMetadata().Namespace, pod3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a, pod6a, pod7a} + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod5a} assertSnapshotpods(podWatched, podNotWatched) err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -1159,18 +826,10 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod5a.GetMetadata().Namespace, pod5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a} - assertSnapshotpods(podWatched, podNotWatched) - - err = podClient.Delete(pod6a.GetMetadata().Namespace, pod6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod7a.GetMetadata().Namespace, pod7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod7a}...) assertSnapshotpods(nil, podNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() } @@ -2165,20 +1824,6 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource3b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource3a, simpleMockResource3b}...) - assertNoSimplemocksSent() - - simpleMockResource4a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource4b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched = append(simpleMockResourceWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2188,13 +1833,6 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) assertNoMessageSent() - simpleMockResource6a, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource6b, err := simpleMockResourceClient.Write(NewSimpleMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource6a, simpleMockResource6b}...) - assertNoMessageSent() - simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2213,14 +1851,6 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b} - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - err = simpleMockResourceClient.Delete(simpleMockResource4a.GetMetadata().Namespace, simpleMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource4b.GetMetadata().Namespace, simpleMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource4a, simpleMockResource4b}...) assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) // clean up environment @@ -2292,20 +1922,6 @@ var _ = Describe("V1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMocksSent() - - mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2315,13 +1931,6 @@ var _ = Describe("V1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() - mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2340,14 +1949,6 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment @@ -2419,20 +2020,6 @@ var _ = Describe("V1Emitter", func() { fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoFakesSent() - - fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) fakeResource5a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2442,13 +2029,6 @@ var _ = Describe("V1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) assertNoMessageSent() - fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource6a, fakeResource6b}...) - assertNoMessageSent() - fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2467,14 +2047,6 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) - fakeResourceWatched = FakeResourceList{fakeResource4a, fakeResource4b} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource4a, fakeResource4b}...) assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment @@ -2546,20 +2118,6 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource3b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource3a, anotherMockResource3b}...) - assertNoAnothermockresourcesSent() - - anotherMockResource4a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource4b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched = append(anotherMockResourceWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2569,13 +2127,6 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) assertNoMessageSent() - anotherMockResource6a, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource6b, err := anotherMockResourceClient.Write(NewAnotherMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource6a, anotherMockResource6b}...) - assertNoMessageSent() - anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2594,14 +2145,6 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b} - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - err = anotherMockResourceClient.Delete(anotherMockResource4a.GetMetadata().Namespace, anotherMockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource4b.GetMetadata().Namespace, anotherMockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource4a, anotherMockResource4b}...) assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) // clean up environment @@ -2650,16 +2193,6 @@ var _ = Describe("V1Emitter", func() { clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource3a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource3a} - assertSnapshotClusterresources(clusterResourceWatched, nil) - - clusterResource4a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace3, name4, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource4a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - createNamespaces(ctx, kube, namespace5, namespace6) clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name5), clients.WriteOpts{Ctx: ctx}) @@ -2667,55 +2200,22 @@ var _ = Describe("V1Emitter", func() { clusterResourceWatched = append(clusterResourceWatched, clusterResource5a) assertSnapshotClusterresources(clusterResourceWatched, nil) - clusterResource6a, err := clusterResourceClient.Write(NewClusterResourceWithLabels(namespace5, name6, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource6a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - - clusterResource7a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name7), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource7a) - assertSnapshotClusterresources(clusterResourceWatched, nil) - err = clusterResourceClient.Delete(clusterResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource2a, clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceWatched = ClusterResourceList{clusterResource2a, clusterResource5a} clusterResourceNotWatched := ClusterResourceList{clusterResource1a} assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) + err = clusterResourceClient.Delete(clusterResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource3a, clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} + clusterResourceWatched = ClusterResourceList{clusterResource5a} clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource2a) assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - err = clusterResourceClient.Delete(clusterResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource4a, clusterResource5a, clusterResource6a, clusterResource7a} - clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource3a) - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource5a, clusterResource6a, clusterResource7a} - clusterResourceNotWatched = ClusterResourceList{clusterResource4a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource6a, clusterResource7a} - clusterResourceNotWatched = ClusterResourceList{clusterResource5a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = ClusterResourceList{clusterResource7a} - clusterResourceNotWatched = ClusterResourceList{clusterResource6a} - assertSnapshotClusterresources(clusterResourceWatched, clusterResourceNotWatched) - - err = clusterResourceClient.Delete(clusterResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceNotWatched = ClusterResourceList{clusterResource7a} - assertSnapshotClusterresources(nil, clusterResourceNotWatched) + clusterResourceNotWatched = append(clusterResourceNotWatched, clusterResource5a) + assertSnapshotClusterresources(nil, clusterResourceNotWatched) // clean up environment deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) @@ -2786,20 +2286,6 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType3b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType3a, mockCustomType3b}...) - assertNomctsSent() - - mockCustomType4a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType4b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched = append(mockCustomTypeWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2809,13 +2295,6 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) assertNoMessageSent() - mockCustomType6a, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType6b, err := mockCustomTypeClient.Write(NewMockCustomTypeWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType6a, mockCustomType6b}...) - assertNoMessageSent() - mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2834,14 +2313,6 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType4a, mockCustomType4b} - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - err = mockCustomTypeClient.Delete(mockCustomType4a.GetMetadata().Namespace, mockCustomType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType4b.GetMetadata().Namespace, mockCustomType4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType4a, mockCustomType4b}...) assertSnapshotmcts(nil, mockCustomTypeNotWatched) // clean up environment @@ -2913,20 +2384,6 @@ var _ = Describe("V1Emitter", func() { podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} assertSnapshotpods(podWatched, podNotWatched) - pod3a, err := podClient.Write(NewPodWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod3b, err := podClient.Write(NewPodWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a, pod3b}...) - assertNopodsSent() - - pod4a, err := podClient.Write(NewPodWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod4b, err := podClient.Write(NewPodWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podWatched = append(podWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) - assertSnapshotpods(podWatched, podNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -2936,13 +2393,6 @@ var _ = Describe("V1Emitter", func() { podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) assertNoMessageSent() - pod6a, err := podClient.Write(NewPodWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod6b, err := podClient.Write(NewPodWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod6a, pod6b}...) - assertNoMessageSent() - pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -2961,14 +2411,6 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b} - assertSnapshotpods(podWatched, podNotWatched) - - err = podClient.Delete(pod4a.GetMetadata().Namespace, pod4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod4b.GetMetadata().Namespace, pod4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod4a, pod4b}...) assertSnapshotpods(nil, podNotWatched) // clean up environment @@ -3372,24 +2814,6 @@ var _ = Describe("V1Emitter", func() { SimpleMockResource */ - assertNoSimplemocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Simplemocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: for { @@ -3417,16 +2841,6 @@ var _ = Describe("V1Emitter", func() { } } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertNoSimplemocksSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoSimplemocksSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -3435,26 +2849,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) simpleMockResource2b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + simpleMockResourceNotWatched := SimpleMockResourceList{} simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource2b} simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2a) assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - simpleMockResource3a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource3a) - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource2b) - simpleMockResourceWatched = SimpleMockResourceList{simpleMockResource3a} - assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - for _, r := range simpleMockResourceWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -3462,32 +2866,13 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -3515,16 +2900,6 @@ var _ = Describe("V1Emitter", func() { } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -3533,26 +2908,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched := MockResourceList{} mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource3a) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) - mockResourceWatched = MockResourceList{mockResource3a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -3560,32 +2925,13 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotMocks(nil, mockResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* FakeResource */ - assertNoFakesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fakes) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: for { @@ -3613,16 +2959,6 @@ var _ = Describe("V1Emitter", func() { } } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertNoFakesSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoFakesSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -3631,26 +2967,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched := FakeResourceList{} fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + fakeResourceWatched = FakeResourceList{fakeResource2b} fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - fakeResource3a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) - fakeResourceWatched = FakeResourceList{fakeResource3a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -3658,32 +2984,13 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotFakes(nil, fakeResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* AnotherMockResource */ - assertNoAnothermockresourcesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Anothermockresources) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: for { @@ -3711,16 +3018,6 @@ var _ = Describe("V1Emitter", func() { } } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertNoAnothermockresourcesSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoAnothermockresourcesSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -3729,26 +3026,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) anotherMockResource2b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + anotherMockResourceNotWatched := AnotherMockResourceList{} anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource2b} anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2a) assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - anotherMockResource3a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource3a) - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource2b) - anotherMockResourceWatched = AnotherMockResourceList{anotherMockResource3a} - assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - for _, r := range anotherMockResourceWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -3756,32 +3043,13 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* ClusterResource */ - assertNoClusterresourcesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Clusterresources) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotClusterresources := func(expectClusterresources ClusterResourceList, unexpectClusterresources ClusterResourceList) { drain: for { @@ -3807,63 +3075,32 @@ var _ = Describe("V1Emitter", func() { } } - // cluster scoped resources never get deleted from a namespace delete - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched := ClusterResourceList{clusterResource1a} - assertSnapshotClusterresources(clusterResourceWatched, nil) - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoClusterresourcesSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) clusterResource2a, err := clusterResourceClient.Write(NewClusterResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) + clusterResourceNotWatched := ClusterResourceList{} + clusterResourceWatched := ClusterResourceList{clusterResource2a} assertSnapshotClusterresources(clusterResourceWatched, nil) deleteNamespaces(ctx, kube, namespace3) - assertNoClusterresourcesSent() - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - clusterResource3a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name3), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - clusterResourceWatched = append(clusterResourceWatched, clusterResource3a) - assertSnapshotClusterresources(clusterResourceWatched, nil) + for _, r := range clusterResourceWatched { + err = clusterResourceClient.Delete(r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + clusterResourceNotWatched = append(clusterResourceNotWatched, r) + } + assertSnapshotClusterresources(nil, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace4) - assertNoClusterresourcesSent() - - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + getNewNamespaces() /* MockCustomType */ - assertNomctsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mcts) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: for { @@ -3891,16 +3128,6 @@ var _ = Describe("V1Emitter", func() { } } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertNomctsSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNomctsSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -3909,26 +3136,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockCustomType2b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockCustomTypeNotWatched := MockCustomTypeList{} mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace3) + mockCustomTypeWatched = MockCustomTypeList{mockCustomType2b} mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2a) assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - mockCustomType3a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType3a) - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType2b) - mockCustomTypeWatched = MockCustomTypeList{mockCustomType3a} - assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - for _, r := range mockCustomTypeWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -3936,32 +3153,13 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotmcts(nil, mockCustomTypeNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* Pod */ - assertNopodsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Pods) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: for { @@ -3989,16 +3187,6 @@ var _ = Describe("V1Emitter", func() { } } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertNopodsSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNopodsSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -4007,26 +3195,16 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) pod2b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{} podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} assertSnapshotpods(podWatched, podNotWatched) deleteNamespaces(ctx, kube, namespace3) + podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2b} podNotWatched = append(podNotWatched, pod2a) assertSnapshotpods(podWatched, podNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - pod3a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podWatched = append(podWatched, pod3a) - assertSnapshotpods(podWatched, podNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - podNotWatched = append(podNotWatched, pod2b) - podWatched = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod3a} - assertSnapshotpods(podWatched, podNotWatched) - for _, r := range podWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -4034,8 +3212,8 @@ var _ = Describe("V1Emitter", func() { } assertSnapshotpods(nil, podNotWatched) - deleteNamespaces(ctx, kube, namespace5) - + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() }) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 488815347..0183e5bfd 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -46,10 +46,8 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) - name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} - labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" cfg *rest.Config clientset *apiext.Clientset @@ -188,13 +186,6 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(mockResourceWatched, nil) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -221,32 +212,6 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource6a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource7a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -258,15 +223,7 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) - mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -274,7 +231,7 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -282,18 +239,10 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() } @@ -582,20 +531,6 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMocksSent() - - mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -605,13 +540,6 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() - mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -630,14 +558,6 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment @@ -743,24 +663,6 @@ var _ = Describe("V1Alpha1Emitter", func() { MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -788,16 +690,6 @@ var _ = Describe("V1Alpha1Emitter", func() { } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -806,26 +698,16 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched := MockResourceList{} mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource3a) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) - mockResourceWatched = MockResourceList{mockResource3a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -833,8 +715,8 @@ var _ = Describe("V1Alpha1Emitter", func() { } assertSnapshotMocks(nil, mockResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() }) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 6ecd43727..02c69413d 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -49,10 +49,8 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace5, namespace6 string name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5, name6 = "melisa" + helpers.RandString(3), "blake" + helpers.RandString(3) - name7, name8 = "britany" + helpers.RandString(3), "john" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) labels1 = map[string]string{"env": "test"} - labels2 = map[string]string{"env": "testenv", "owner": "foo"} labelExpression1 = "env in (test)" cfg *rest.Config clientset *apiext.Clientset @@ -203,13 +201,6 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource1a, mockResource1b} assertSnapshotMocks(mockResourceWatched, nil) - mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource2a, mockResource2b}...) - assertSnapshotMocks(mockResourceWatched, nil) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -236,32 +227,6 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource6a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource6b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - mockResource7a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource7b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource7a) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource7b) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - mockResource8a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource8b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource8a, mockResource8b}...) - assertNoMessageSent() - for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -273,15 +238,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource1b.GetMetadata().Namespace, mockResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource1a, mockResource1b}...) - mockResourceWatched = MockResourceList{mockResource2a, mockResource2b, mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource3a, mockResource3b, mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource3a.GetMetadata().Namespace, mockResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -289,7 +246,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource3b.GetMetadata().Namespace, mockResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource5a, mockResource6a, mockResource7a} + mockResourceWatched = MockResourceList{mockResource4a, mockResource5a} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -297,18 +254,10 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource5a.GetMetadata().Namespace, mockResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - mockResourceWatched = MockResourceList{mockResource6a, mockResource7a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource6a.GetMetadata().Namespace, mockResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource7a.GetMetadata().Namespace, mockResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource7a}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -348,13 +297,6 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) - frequentlyChangingAnnotationsResource2a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, nil) - frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -381,32 +323,6 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource5b) assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource6a) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource6b) - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource7a) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource7b) - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - frequentlyChangingAnnotationsResource8a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource8b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource8a, frequentlyChangingAnnotationsResource8b}...) - assertNoMessageSent() - for _, r := range frequentlyChangingAnnotationsResourceNotWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -418,15 +334,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}...) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b, frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b, frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -434,7 +342,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource3b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource5a} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -442,18 +350,10 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource5a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a} - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource6a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource7a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource7a}...) assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() /* @@ -493,13 +393,6 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} assertSnapshotFakes(fakeResourceWatched, nil) - fakeResource2a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - assertSnapshotFakes(fakeResourceWatched, nil) - fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) @@ -526,32 +419,6 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource6a) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource6b) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - createNamespaces(ctx, kube, namespace6) - - fakeResource7a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource7b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name1, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource7a) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7b) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - fakeResource8a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name2, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource8b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels2), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource8a, fakeResource8b}...) - assertNoMessageSent() - for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -563,15 +430,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource1b.GetMetadata().Namespace, fakeResource1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}...) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b, fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b, fakeResource4a, fakeResource5a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource3a.GetMetadata().Namespace, fakeResource3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -579,7 +438,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource3b.GetMetadata().Namespace, fakeResource3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a, fakeResource6a, fakeResource7a} + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource5a} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -587,18 +446,10 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource5a.GetMetadata().Namespace, fakeResource5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource6a.GetMetadata().Namespace, fakeResource6a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource7a.GetMetadata().Namespace, fakeResource7a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource7a}...) assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment - deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() } @@ -1125,20 +976,6 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - mockResource3a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource3b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource3a, mockResource3b}...) - assertNoMocksSent() - - mockResource4a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource4b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, MockResourceList{mockResource4a, mockResource4b}...) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -1148,13 +985,6 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) assertNoMessageSent() - mockResource6a, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource6b, err := mockResourceClient.Write(NewMockResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource6a, mockResource6b}...) - assertNoMessageSent() - mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -1173,14 +1003,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) - mockResourceWatched = MockResourceList{mockResource4a, mockResource4b} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - err = mockResourceClient.Delete(mockResource4a.GetMetadata().Namespace, mockResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource4b.GetMetadata().Namespace, mockResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource4a, mockResource4b}...) assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment @@ -1252,20 +1074,6 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource3b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a, frequentlyChangingAnnotationsResource3b}...) - assertNoFcarsSent() - - frequentlyChangingAnnotationsResource4a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource4b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -1275,13 +1083,6 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) assertNoMessageSent() - frequentlyChangingAnnotationsResource6a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource6b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource6a, frequentlyChangingAnnotationsResource6b}...) - assertNoMessageSent() - frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -1300,14 +1101,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b} - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource4b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource4a, frequentlyChangingAnnotationsResource4b}...) assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) // clean up environment @@ -1379,20 +1172,6 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - fakeResource3a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace1, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource3b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace2, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource3a, fakeResource3b}...) - assertNoFakesSent() - - fakeResource4a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource4b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) @@ -1402,13 +1181,6 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) assertNoMessageSent() - fakeResource6a, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace5, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource6b, err := fakeResourceClient.Write(NewFakeResourceWithLabels(namespace6, name3, labels1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource6a, fakeResource6b}...) - assertNoMessageSent() - fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) @@ -1427,14 +1199,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - err = fakeResourceClient.Delete(fakeResource4a.GetMetadata().Namespace, fakeResource4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource4b.GetMetadata().Namespace, fakeResource4b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource4a, fakeResource4b}...) assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment @@ -1640,24 +1404,6 @@ var _ = Describe("V2Alpha1Emitter", func() { MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: for { @@ -1685,16 +1431,6 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMocksSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1703,26 +1439,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) mockResource2b, err := mockResourceClient.Write(NewMockResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched := MockResourceList{} mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + mockResourceWatched = MockResourceList{mockResource2b} mockResourceNotWatched = append(mockResourceNotWatched, mockResource2a) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - mockResource3a, err := mockResourceClient.Write(NewMockResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceWatched = append(mockResourceWatched, mockResource3a) - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - mockResourceNotWatched = append(mockResourceNotWatched, mockResource2b) - mockResourceWatched = MockResourceList{mockResource3a} - assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - for _, r := range mockResourceWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1730,32 +1456,13 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertSnapshotMocks(nil, mockResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* FrequentlyChangingAnnotationsResource */ - assertNoFcarsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fcars) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: for { @@ -1783,16 +1490,6 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertNoFcarsSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoFcarsSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1801,26 +1498,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource2b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{} frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2b} frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2a) assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - frequentlyChangingAnnotationsResource3a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource3a) - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource2b) - frequentlyChangingAnnotationsResourceWatched = FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource3a} - assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - for _, r := range frequentlyChangingAnnotationsResourceWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1828,32 +1515,13 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - - createNamespaces(ctx, kube, namespace1, namespace2) + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() /* FakeResource */ - assertNoFakesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fakes) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: for { @@ -1881,16 +1549,6 @@ var _ = Describe("V2Alpha1Emitter", func() { } } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertNoFakesSent() - - deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoFakesSent() - // create namespaces createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1899,26 +1557,16 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) fakeResource2b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace4, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched := testing_solo_io.FakeResourceList{} fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace3) + fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource2b} fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2a) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaceWithLabel(ctx, kube, namespace5, labels1) - - fakeResource3a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name1), clients.WriteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceWatched = append(fakeResourceWatched, fakeResource3a) - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - - deleteNamespaces(ctx, kube, namespace4) - fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource2b) - fakeResourceWatched = testing_solo_io.FakeResourceList{fakeResource3a} - assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - for _, r := range fakeResourceWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1926,8 +1574,8 @@ var _ = Describe("V2Alpha1Emitter", func() { } assertSnapshotFakes(nil, fakeResourceNotWatched) - deleteNamespaces(ctx, kube, namespace5) - + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() }) }) From 148dc610bb606943fcda1144373183760dad2eb3 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 14:41:56 -0500 Subject: [PATCH 38/98] missing build solokit --- test/mocks/v1/testing_snapshot_emitter_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index f37419fea..d84d42fde 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,5 +1,7 @@ // Code generated by solo-kit. DO NOT EDIT. +// +build solokit + package v1 import ( From 576f2aac795d0a74e73838e027377732bd031bbb Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 14:46:11 -0500 Subject: [PATCH 39/98] fix issue with name5 --- .../snapshot_emitter_test_template.go | 6 ++--- .../v1/kubeconfigs_snapshot_emitter_test.go | 4 +-- .../mocks/v1/testing_snapshot_emitter_test.go | 26 +++++++++---------- .../v1alpha1/testing_snapshot_emitter_test.go | 4 +-- .../v2alpha1/testing_snapshot_emitter_test.go | 12 ++++----- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 07dbeb3ab..4be77f39f 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -450,14 +450,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } {{- if .ClusterScoped }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 7a816f81b..8b0398e21 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -320,9 +320,9 @@ var _ = Describe("V1Emitter", func() { } } } - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index d84d42fde..43de6ba53 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -978,9 +978,9 @@ var _ = Describe("V1Emitter", func() { } } } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) @@ -1035,9 +1035,9 @@ var _ = Describe("V1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) @@ -1092,9 +1092,9 @@ var _ = Describe("V1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) @@ -1149,9 +1149,9 @@ var _ = Describe("V1Emitter", func() { } } } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) @@ -1204,7 +1204,7 @@ var _ = Describe("V1Emitter", func() { } } } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) @@ -1253,9 +1253,9 @@ var _ = Describe("V1Emitter", func() { } } } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) @@ -1310,9 +1310,9 @@ var _ = Describe("V1Emitter", func() { } } } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 0183e5bfd..69f382cbe 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -335,9 +335,9 @@ var _ = Describe("V1Alpha1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 02c69413d..39c65b036 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -556,9 +556,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) @@ -613,9 +613,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) @@ -670,9 +670,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) From 1a0989a5de36e0039fd80c25190135b1daec939e Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 14:49:06 -0500 Subject: [PATCH 40/98] fix name5 --- .../snapshot_emitter_test_template.go | 6 ++--- .../v1/kubeconfigs_snapshot_emitter_test.go | 4 +-- .../mocks/v1/testing_snapshot_emitter_test.go | 26 +++++++++---------- .../v2alpha1/testing_snapshot_emitter_test.go | 12 ++++----- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 4be77f39f..6e6860bbb 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -450,14 +450,14 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func } {{- if .ClusterScoped }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a }, nil) {{- else }} - {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b }, nil) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 8b0398e21..fbeed928a 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -320,9 +320,9 @@ var _ = Describe("V1Emitter", func() { } } } - kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + kubeConfig1a, err := kubeConfigClient.Write(NewKubeConfig(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotkubeconfigs(KubeConfigList{kubeConfig1a, kubeConfig1b}, nil) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 43de6ba53..44726cee1 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -978,9 +978,9 @@ var _ = Describe("V1Emitter", func() { } } } - simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + simpleMockResource1a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotSimplemocks(SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b}, nil) @@ -1035,9 +1035,9 @@ var _ = Describe("V1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) @@ -1092,9 +1092,9 @@ var _ = Describe("V1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + fakeResource1a, err := fakeResourceClient.Write(NewFakeResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFakes(FakeResourceList{fakeResource1a, fakeResource1b}, nil) @@ -1149,9 +1149,9 @@ var _ = Describe("V1Emitter", func() { } } } - anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b}, nil) @@ -1204,7 +1204,7 @@ var _ = Describe("V1Emitter", func() { } } } - clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + clusterResource1a, err := clusterResourceClient.Write(NewClusterResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotClusterresources(ClusterResourceList{clusterResource1a}, nil) @@ -1253,9 +1253,9 @@ var _ = Describe("V1Emitter", func() { } } } - mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + mockCustomType1a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotmcts(MockCustomTypeList{mockCustomType1a, mockCustomType1b}, nil) @@ -1310,9 +1310,9 @@ var _ = Describe("V1Emitter", func() { } } } - pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + pod1a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotpods(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b}, nil) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 39c65b036..71a34e279 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -556,9 +556,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) @@ -613,9 +613,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource1a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFcars(FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b}, nil) @@ -670,9 +670,9 @@ var _ = Describe("V2Alpha1Emitter", func() { } } } - fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + fakeResource1a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotFakes(testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b}, nil) From 3aa046c2dbc06cd1951ff43feced26827002a62f Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 14:57:36 -0500 Subject: [PATCH 41/98] v1 alpha --- test/mocks/v1alpha1/testing_snapshot_emitter_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 69f382cbe..f9b9931c8 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -335,9 +335,9 @@ var _ = Describe("V1Alpha1Emitter", func() { } } } - mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace1, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, nam5), clients.WriteOpts{Ctx: ctx}) + mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) assertSnapshotMocks(MockResourceList{mockResource1a, mockResource1b}, nil) From f09e7e6565e3eb0c3c5976693949c75ba61371ba Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 16:18:47 -0500 Subject: [PATCH 42/98] fix problems with parallel tests --- .../snapshot_emitter_test_template.go | 87 +------ .../v1/kubeconfigs_snapshot_emitter_test.go | 85 +------ .../mocks/v1/testing_snapshot_emitter_test.go | 216 ++---------------- .../v1alpha1/testing_snapshot_emitter_test.go | 85 +------ .../v2alpha1/testing_snapshot_emitter_test.go | 137 +---------- 5 files changed, 41 insertions(+), 569 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 6e6860bbb..3c68326ca 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -107,20 +107,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).ToNot(HaveOccurred()) } - deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { - // clean up your local environment - namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - Expect(err).ToNot(HaveOccurred()) - defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} - var namespacesToDelete []string - for _,ns := range namespaces.Items { - if _, hit := defaultNamespaces[ns.Name]; ! hit{ - namespacesToDelete = append(namespacesToDelete, ns.Name) - } - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).ToNot(HaveOccurred()) - } deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) @@ -161,19 +147,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } {{- range .Resources }} /* @@ -277,7 +250,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() {{- end }} @@ -389,7 +361,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - deleteNonDefaultKubeNamespaces(ctx, kube) + kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) {{- range .Resources }} {{- if .ClusterScoped }} @@ -647,47 +619,12 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - - {{- range .Resources }} /* {{ .Name }} */ -{{- if (not .ClusterScoped) }} - assertNo{{ .PluralName }}Sent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.{{ upper_camel .PluralName }}) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } -{{- end }} - assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { drain: for { @@ -734,7 +671,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertNo{{ .PluralName }}Sent() + assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -775,20 +712,19 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) - assertNoMessageSent() + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) - assertNoMessageSent() + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) for _, r := range {{ lower_camel .Name }}NotWatched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() {{- end }} @@ -844,20 +780,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func var snap *{{ .GoName }}Snapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - {{- range .Resources }} /* @@ -917,7 +839,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index fbeed928a..7a2ee30c7 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -77,21 +77,6 @@ var _ = Describe("V1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } - deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { - // clean up your local environment - namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - Expect(err).ToNot(HaveOccurred()) - defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} - var namespacesToDelete []string - for _, ns := range namespaces.Items { - if _, hit := defaultNamespaces[ns.Name]; !hit { - namespacesToDelete = append(namespacesToDelete, ns.Name) - } - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).ToNot(HaveOccurred()) - } - deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) @@ -131,20 +116,6 @@ var _ = Describe("V1Emitter", func() { var snap *KubeconfigsSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* KubeConfig */ @@ -212,7 +183,6 @@ var _ = Describe("V1Emitter", func() { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = kubeConfigClient.Delete(kubeConfig1a.GetMetadata().Namespace, kubeConfig1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -273,7 +243,7 @@ var _ = Describe("V1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - deleteNonDefaultKubeNamespaces(ctx, kube) + kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) }) Context("Tracking watched namespaces", func() { @@ -437,40 +407,9 @@ var _ = Describe("V1Emitter", func() { var snap *KubeconfigsSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* KubeConfig */ - assertNokubeconfigsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Kubeconfigs) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotkubeconfigs := func(expectkubeconfigs KubeConfigList, unexpectkubeconfigs KubeConfigList) { drain: @@ -504,7 +443,7 @@ var _ = Describe("V1Emitter", func() { kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertNokubeconfigsSent() + assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -523,20 +462,19 @@ var _ = Describe("V1Emitter", func() { kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) - assertNoMessageSent() + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) - assertNoMessageSent() + assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) for _, r := range kubeConfigNotWatched { err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -565,20 +503,6 @@ var _ = Describe("V1Emitter", func() { var snap *KubeconfigsSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* KubeConfig */ @@ -624,7 +548,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 44726cee1..dc2d8b158 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -120,21 +120,6 @@ var _ = Describe("V1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } - deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { - // clean up your local environment - namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - Expect(err).ToNot(HaveOccurred()) - defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} - var namespacesToDelete []string - for _, ns := range namespaces.Items { - if _, hit := defaultNamespaces[ns.Name]; !hit { - namespacesToDelete = append(namespacesToDelete, ns.Name) - } - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).ToNot(HaveOccurred()) - } - deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) @@ -174,20 +159,6 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* SimpleMockResource */ @@ -255,7 +226,6 @@ var _ = Describe("V1Emitter", func() { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = simpleMockResourceClient.Delete(simpleMockResource1a.GetMetadata().Namespace, simpleMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -351,7 +321,6 @@ var _ = Describe("V1Emitter", func() { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -447,7 +416,6 @@ var _ = Describe("V1Emitter", func() { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -543,7 +511,6 @@ var _ = Describe("V1Emitter", func() { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = anotherMockResourceClient.Delete(anotherMockResource1a.GetMetadata().Namespace, anotherMockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -709,7 +676,6 @@ var _ = Describe("V1Emitter", func() { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockCustomTypeClient.Delete(mockCustomType1a.GetMetadata().Namespace, mockCustomType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -805,7 +771,6 @@ var _ = Describe("V1Emitter", func() { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = podClient.Delete(pod1a.GetMetadata().Namespace, pod1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -929,7 +894,7 @@ var _ = Describe("V1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - deleteNonDefaultKubeNamespaces(ctx, kube) + kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) clusterResourceClient.Delete(name1, clients.DeleteOpts{}) clusterResourceClient.Delete(name2, clients.DeleteOpts{}) }) @@ -1747,40 +1712,9 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* SimpleMockResource */ - assertNoSimplemocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Simplemocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotSimplemocks := func(expectSimplemocks SimpleMockResourceList, unexpectSimplemocks SimpleMockResourceList) { drain: @@ -1814,7 +1748,7 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertNoSimplemocksSent() + assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1833,20 +1767,19 @@ var _ = Describe("V1Emitter", func() { simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) - assertNoMessageSent() + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) - assertNoMessageSent() + assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) for _, r := range simpleMockResourceNotWatched { err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1862,23 +1795,6 @@ var _ = Describe("V1Emitter", func() { /* MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: @@ -1912,7 +1828,7 @@ var _ = Describe("V1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1931,20 +1847,19 @@ var _ = Describe("V1Emitter", func() { mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1960,23 +1875,6 @@ var _ = Describe("V1Emitter", func() { /* FakeResource */ - assertNoFakesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fakes) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotFakes := func(expectFakes FakeResourceList, unexpectFakes FakeResourceList) { drain: @@ -2010,7 +1908,7 @@ var _ = Describe("V1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertNoFakesSent() + assertSnapshotFakes(nil, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2029,20 +1927,19 @@ var _ = Describe("V1Emitter", func() { fakeResource5b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) - assertNoMessageSent() + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) - assertNoMessageSent() + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2058,23 +1955,6 @@ var _ = Describe("V1Emitter", func() { /* AnotherMockResource */ - assertNoAnothermockresourcesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Anothermockresources) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { drain: @@ -2108,7 +1988,7 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertNoAnothermockresourcesSent() + assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2127,20 +2007,19 @@ var _ = Describe("V1Emitter", func() { anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) - assertNoMessageSent() + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) - assertNoMessageSent() + assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) for _, r := range anotherMockResourceNotWatched { err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2226,23 +2105,6 @@ var _ = Describe("V1Emitter", func() { /* MockCustomType */ - assertNomctsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mcts) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotmcts := func(expectmcts MockCustomTypeList, unexpectmcts MockCustomTypeList) { drain: @@ -2276,7 +2138,7 @@ var _ = Describe("V1Emitter", func() { mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertNomctsSent() + assertSnapshotmcts(nil, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2295,20 +2157,19 @@ var _ = Describe("V1Emitter", func() { mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) - assertNoMessageSent() + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) - assertNoMessageSent() + assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) for _, r := range mockCustomTypeNotWatched { err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2324,23 +2185,6 @@ var _ = Describe("V1Emitter", func() { /* Pod */ - assertNopodsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Pods) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotpods := func(expectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList, unexpectpods github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) { drain: @@ -2374,7 +2218,7 @@ var _ = Describe("V1Emitter", func() { pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertNopodsSent() + assertSnapshotpods(nil, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2393,20 +2237,19 @@ var _ = Describe("V1Emitter", func() { pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) - assertNoMessageSent() + assertSnapshotpods(podWatched, podNotWatched) pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) - assertNoMessageSent() + assertSnapshotpods(podWatched, podNotWatched) for _, r := range podNotWatched { err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2435,20 +2278,6 @@ var _ = Describe("V1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* SimpleMockResource */ @@ -2494,7 +2323,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2544,7 +2372,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2594,7 +2421,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2644,7 +2470,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2692,7 +2517,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotClusterresources(nil, clusterResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2742,7 +2566,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotmcts(nil, mockCustomTypeNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -2792,7 +2615,6 @@ var _ = Describe("V1Emitter", func() { assertSnapshotpods(nil, podNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index f9b9931c8..8e31e5356 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -81,21 +81,6 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } - deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { - // clean up your local environment - namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - Expect(err).ToNot(HaveOccurred()) - defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} - var namespacesToDelete []string - for _, ns := range namespaces.Items { - if _, hit := defaultNamespaces[ns.Name]; !hit { - namespacesToDelete = append(namespacesToDelete, ns.Name) - } - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).ToNot(HaveOccurred()) - } - deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) @@ -135,20 +120,6 @@ var _ = Describe("V1Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ @@ -216,7 +187,6 @@ var _ = Describe("V1Alpha1Emitter", func() { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -288,7 +258,7 @@ var _ = Describe("V1Alpha1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - deleteNonDefaultKubeNamespaces(ctx, kube) + kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) }) Context("Tracking watched namespaces", func() { @@ -452,40 +422,9 @@ var _ = Describe("V1Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: @@ -519,7 +458,7 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -538,20 +477,19 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -580,20 +518,6 @@ var _ = Describe("V1Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ @@ -639,7 +563,6 @@ var _ = Describe("V1Alpha1Emitter", func() { assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 71a34e279..f3e54ea9d 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -96,21 +96,6 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).ToNot(HaveOccurred()) } - deleteNonDefaultKubeNamespaces := func(ctx context.Context, kube kubernetes.Interface) { - // clean up your local environment - namespaces, err := kube.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - Expect(err).ToNot(HaveOccurred()) - defaultNamespaces := map[string]bool{"kube-node-lease": true, "kube-public": true, "kube-system": true, "local-path-storage": true, "default": true} - var namespacesToDelete []string - for _, ns := range namespaces.Items { - if _, hit := defaultNamespaces[ns.Name]; !hit { - namespacesToDelete = append(namespacesToDelete, ns.Name) - } - } - err = kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespacesToDelete...) - Expect(err).ToNot(HaveOccurred()) - } - deleteNamespaces := func(ctx context.Context, kube kubernetes.Interface, namespaces ...string) { err := kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespaces...) Expect(err).NotTo(HaveOccurred()) @@ -150,20 +135,6 @@ var _ = Describe("V2Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ @@ -231,7 +202,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource1a.GetMetadata().Namespace, mockResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -327,7 +297,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource1a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -423,7 +392,6 @@ var _ = Describe("V2Alpha1Emitter", func() { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = fakeResourceClient.Delete(fakeResource1a.GetMetadata().Namespace, fakeResource1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -509,7 +477,7 @@ var _ = Describe("V2Alpha1Emitter", func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) Expect(err).NotTo(HaveOccurred()) - deleteNonDefaultKubeNamespaces(ctx, kube) + kubeutils.DeleteNamespacesInParallelBlocking(ctx, kube, namespace1, namespace2) }) Context("Tracking watched namespaces", func() { @@ -897,40 +865,9 @@ var _ = Describe("V2Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots wouldbe recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ - assertNoMocksSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Mocks) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { drain: @@ -964,7 +901,7 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertNoMocksSent() + assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -983,20 +920,19 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) - assertNoMessageSent() + assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1012,23 +948,6 @@ var _ = Describe("V2Alpha1Emitter", func() { /* FrequentlyChangingAnnotationsResource */ - assertNoFcarsSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fcars) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotFcars := func(expectFcars FrequentlyChangingAnnotationsResourceList, unexpectFcars FrequentlyChangingAnnotationsResourceList) { drain: @@ -1062,7 +981,7 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertNoFcarsSent() + assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1081,20 +1000,19 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) - assertNoMessageSent() + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) - assertNoMessageSent() + assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) for _, r := range frequentlyChangingAnnotationsResourceNotWatched { err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1110,23 +1028,6 @@ var _ = Describe("V2Alpha1Emitter", func() { /* FakeResource */ - assertNoFakesSent := func() { - drain: - for { - select { - case snap = <-snapshots: - if len(snap.Fakes) == 0 { - continue drain - } - Fail("expected that no snapshots containing resources would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } assertSnapshotFakes := func(expectFakes testing_solo_io.FakeResourceList, unexpectFakes testing_solo_io.FakeResourceList) { drain: @@ -1160,7 +1061,7 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertNoFakesSent() + assertSnapshotFakes(nil, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1179,20 +1080,19 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) - assertNoMessageSent() + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) - assertNoMessageSent() + assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceNotWatched { err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) } - assertNoMessageSent() err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -1221,20 +1121,6 @@ var _ = Describe("V2Alpha1Emitter", func() { var snap *TestingSnapshot - assertNoMessageSent := func() { - for { - select { - case snap = <-snapshots: - Fail("expected that no snapshots would be recieved " + log.Sprintf("%v", snap)) - case err := <-errs: - Expect(err).NotTo(HaveOccurred()) - case <-time.After(time.Second * 5): - // this means that we have not recieved any mocks that we are not expecting - return - } - } - } - /* MockResource */ @@ -1280,7 +1166,6 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotMocks(nil, mockResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -1330,7 +1215,6 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) @@ -1380,7 +1264,6 @@ var _ = Describe("V2Alpha1Emitter", func() { assertSnapshotFakes(nil, fakeResourceNotWatched) deleteNamespaces(ctx, kube, namespace1, namespace2) - assertNoMessageSent() getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) From 217108b31d2e7f02017030e217d6d624101fd005 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 18:03:27 -0500 Subject: [PATCH 43/98] update to fix notifications about snapshots --- .../snapshot_emitter_test_template.go | 24 ++-- .../v1/kubeconfigs_snapshot_emitter_test.go | 21 +-- .../mocks/v1/testing_snapshot_emitter_test.go | 124 ++++++++++-------- .../v1alpha1/testing_snapshot_emitter_test.go | 21 +-- .../v2alpha1/testing_snapshot_emitter_test.go | 61 +++++---- 5 files changed, 136 insertions(+), 115 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 3c68326ca..1d116c314 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -671,7 +671,6 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{ lower_camel .Name }}1b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}NotWatched := {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a, {{ lower_camel .Name }}1b } - assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} @@ -696,7 +695,8 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) {{- if .ClusterScoped }} @@ -711,15 +711,17 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}5b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}5a, {{ lower_camel .Name }}5b }...) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}5a) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}5b) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) {{ lower_camel .Name }}7a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) {{ lower_camel .Name }}7b, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}7a, {{ lower_camel .Name }}7b }...) - assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ lower_camel .Name }}7a) + {{ lower_camel .Name }}Watched = append({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}7b) + assertSnapshot{{ .PluralName }}({{ lower_camel .Name }}Watched, {{ lower_camel .Name }}NotWatched) for _, r := range {{ lower_camel .Name }}NotWatched { err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) @@ -749,11 +751,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- else }} - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2a.GetMetadata().Namespace, {{ lower_camel .Name }}2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = {{ lower_camel .Name }}Client.Delete({{ lower_camel .Name }}2b.GetMetadata().Namespace, {{ lower_camel .Name }}2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a, {{ lower_camel .Name }}2b}...) + for _, r := range {{ lower_camel .Name }}Watched { + err = {{ lower_camel .Name }}Client.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + {{ lower_camel .Name }}NotWatched = append({{ lower_camel .Name }}NotWatched, r) + } assertSnapshot{{ .PluralName }}(nil, {{ lower_camel .Name }}NotWatched) {{- end }} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 7a2ee30c7..d8402b64d 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -// +build solokit package v1 @@ -443,7 +442,6 @@ var _ = Describe("V1Emitter", func() { kubeConfig1b, err := kubeConfigClient.Write(NewKubeConfig(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfigNotWatched := KubeConfigList{kubeConfig1a, kubeConfig1b} - assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -455,20 +453,23 @@ var _ = Describe("V1Emitter", func() { kubeConfigWatched := KubeConfigList{kubeConfig2a, kubeConfig2b} assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) kubeConfig5a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig5b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig5a, kubeConfig5b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig5a) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig5b) assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) kubeConfig7a, err := kubeConfigClient.Write(NewKubeConfig(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) kubeConfig7b, err := kubeConfigClient.Write(NewKubeConfig(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig7a, kubeConfig7b}...) + kubeConfigNotWatched = append(kubeConfigNotWatched, kubeConfig7a) + kubeConfigWatched = append(kubeConfigWatched, kubeConfig7b) assertSnapshotkubeconfigs(kubeConfigWatched, kubeConfigNotWatched) for _, r := range kubeConfigNotWatched { @@ -476,11 +477,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = kubeConfigClient.Delete(kubeConfig2a.GetMetadata().Namespace, kubeConfig2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = kubeConfigClient.Delete(kubeConfig2b.GetMetadata().Namespace, kubeConfig2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - kubeConfigNotWatched = append(kubeConfigNotWatched, KubeConfigList{kubeConfig2a, kubeConfig2b}...) + for _, r := range kubeConfigWatched { + err = kubeConfigClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + kubeConfigNotWatched = append(kubeConfigNotWatched, r) + } assertSnapshotkubeconfigs(nil, kubeConfigNotWatched) // clean up environment diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index dc2d8b158..c8704766e 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -// +build solokit package v1 @@ -1748,7 +1747,6 @@ var _ = Describe("V1Emitter", func() { simpleMockResource1b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResourceNotWatched := SimpleMockResourceList{simpleMockResource1a, simpleMockResource1b} - assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1760,20 +1758,23 @@ var _ = Describe("V1Emitter", func() { simpleMockResourceWatched := SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b} assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) simpleMockResource5a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource5b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource5a, simpleMockResource5b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource5a) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource5b) assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) simpleMockResource7a, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) simpleMockResource7b, err := simpleMockResourceClient.Write(NewSimpleMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource7a, simpleMockResource7b}...) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, simpleMockResource7a) + simpleMockResourceWatched = append(simpleMockResourceWatched, simpleMockResource7b) assertSnapshotSimplemocks(simpleMockResourceWatched, simpleMockResourceNotWatched) for _, r := range simpleMockResourceNotWatched { @@ -1781,11 +1782,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = simpleMockResourceClient.Delete(simpleMockResource2a.GetMetadata().Namespace, simpleMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = simpleMockResourceClient.Delete(simpleMockResource2b.GetMetadata().Namespace, simpleMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, SimpleMockResourceList{simpleMockResource2a, simpleMockResource2b}...) + for _, r := range simpleMockResourceWatched { + err = simpleMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + simpleMockResourceNotWatched = append(simpleMockResourceNotWatched, r) + } assertSnapshotSimplemocks(nil, simpleMockResourceNotWatched) // clean up environment @@ -1828,7 +1829,6 @@ var _ = Describe("V1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1840,20 +1840,23 @@ var _ = Describe("V1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5a) + mockResourceWatched = append(mockResourceWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7a) + mockResourceWatched = append(mockResourceWatched, mockResource7b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { @@ -1861,11 +1864,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + for _, r := range mockResourceWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) + } assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment @@ -1908,7 +1911,6 @@ var _ = Describe("V1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(nil, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1920,20 +1922,23 @@ var _ = Describe("V1Emitter", func() { fakeResourceWatched := FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) fakeResource5a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5a) + fakeResourceWatched = append(fakeResourceWatched, fakeResource5b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource7a, err := fakeResourceClient.Write(NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource7a, fakeResource7b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7a) + fakeResourceWatched = append(fakeResourceWatched, fakeResource7b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceNotWatched { @@ -1941,11 +1946,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, FakeResourceList{fakeResource2a, fakeResource2b}...) + for _, r := range fakeResourceWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched = append(fakeResourceNotWatched, r) + } assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment @@ -1988,7 +1993,6 @@ var _ = Describe("V1Emitter", func() { anotherMockResource1b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResourceNotWatched := AnotherMockResourceList{anotherMockResource1a, anotherMockResource1b} - assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2000,20 +2004,23 @@ var _ = Describe("V1Emitter", func() { anotherMockResourceWatched := AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b} assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) anotherMockResource5a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource5b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource5a, anotherMockResource5b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource5a) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource5b) assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) anotherMockResource7a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) anotherMockResource7b, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource7a, anotherMockResource7b}...) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, anotherMockResource7a) + anotherMockResourceWatched = append(anotherMockResourceWatched, anotherMockResource7b) assertSnapshotAnothermockresources(anotherMockResourceWatched, anotherMockResourceNotWatched) for _, r := range anotherMockResourceNotWatched { @@ -2021,11 +2028,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = anotherMockResourceClient.Delete(anotherMockResource2a.GetMetadata().Namespace, anotherMockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = anotherMockResourceClient.Delete(anotherMockResource2b.GetMetadata().Namespace, anotherMockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, AnotherMockResourceList{anotherMockResource2a, anotherMockResource2b}...) + for _, r := range anotherMockResourceWatched { + err = anotherMockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + anotherMockResourceNotWatched = append(anotherMockResourceNotWatched, r) + } assertSnapshotAnothermockresources(nil, anotherMockResourceNotWatched) // clean up environment @@ -2074,7 +2081,8 @@ var _ = Describe("V1Emitter", func() { clusterResourceWatched = append(clusterResourceWatched, clusterResource2a) assertSnapshotClusterresources(clusterResourceWatched, nil) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) clusterResource5a, err := clusterResourceClient.Write(NewClusterResource(namespace5, name5), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) @@ -2138,7 +2146,6 @@ var _ = Describe("V1Emitter", func() { mockCustomType1b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomTypeNotWatched := MockCustomTypeList{mockCustomType1a, mockCustomType1b} - assertSnapshotmcts(nil, mockCustomTypeNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2150,20 +2157,23 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeWatched := MockCustomTypeList{mockCustomType2a, mockCustomType2b} assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) mockCustomType5a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType5b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType5a, mockCustomType5b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType5a) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType5b) assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) mockCustomType7a, err := mockCustomTypeClient.Write(NewMockCustomType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockCustomType7b, err := mockCustomTypeClient.Write(NewMockCustomType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType7a, mockCustomType7b}...) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, mockCustomType7a) + mockCustomTypeWatched = append(mockCustomTypeWatched, mockCustomType7b) assertSnapshotmcts(mockCustomTypeWatched, mockCustomTypeNotWatched) for _, r := range mockCustomTypeNotWatched { @@ -2171,11 +2181,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = mockCustomTypeClient.Delete(mockCustomType2a.GetMetadata().Namespace, mockCustomType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockCustomTypeClient.Delete(mockCustomType2b.GetMetadata().Namespace, mockCustomType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, MockCustomTypeList{mockCustomType2a, mockCustomType2b}...) + for _, r := range mockCustomTypeWatched { + err = mockCustomTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomTypeNotWatched = append(mockCustomTypeNotWatched, r) + } assertSnapshotmcts(nil, mockCustomTypeNotWatched) // clean up environment @@ -2218,7 +2228,6 @@ var _ = Describe("V1Emitter", func() { pod1b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) podNotWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod1a, pod1b} - assertSnapshotpods(nil, podNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -2230,20 +2239,23 @@ var _ = Describe("V1Emitter", func() { podWatched := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b} assertSnapshotpods(podWatched, podNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) pod5a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod5b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod5a, pod5b}...) + podNotWatched = append(podNotWatched, pod5a) + podWatched = append(podWatched, pod5b) assertSnapshotpods(podWatched, podNotWatched) pod7a, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) pod7b, err := podClient.Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod7a, pod7b}...) + podNotWatched = append(podNotWatched, pod7a) + podWatched = append(podWatched, pod7b) assertSnapshotpods(podWatched, podNotWatched) for _, r := range podNotWatched { @@ -2251,11 +2263,11 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = podClient.Delete(pod2a.GetMetadata().Namespace, pod2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = podClient.Delete(pod2b.GetMetadata().Namespace, pod2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - podNotWatched = append(podNotWatched, github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{pod2a, pod2b}...) + for _, r := range podWatched { + err = podClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + podNotWatched = append(podNotWatched, r) + } assertSnapshotpods(nil, podNotWatched) // clean up environment diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 8e31e5356..5553392e1 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -// +build solokit package v1alpha1 @@ -458,7 +457,6 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -470,20 +468,23 @@ var _ = Describe("V1Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5a) + mockResourceWatched = append(mockResourceWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7a) + mockResourceWatched = append(mockResourceWatched, mockResource7b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { @@ -491,11 +492,11 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + for _, r := range mockResourceWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) + } assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index f3e54ea9d..c69a1a10e 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -// +build solokit package v2alpha1 @@ -901,7 +900,6 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResource1b, err := mockResourceClient.Write(NewMockResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResourceNotWatched := MockResourceList{mockResource1a, mockResource1b} - assertSnapshotMocks(nil, mockResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -913,20 +911,23 @@ var _ = Describe("V2Alpha1Emitter", func() { mockResourceWatched := MockResourceList{mockResource2a, mockResource2b} assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) mockResource5a, err := mockResourceClient.Write(NewMockResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource5b, err := mockResourceClient.Write(NewMockResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource5a, mockResource5b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource5a) + mockResourceWatched = append(mockResourceWatched, mockResource5b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) mockResource7a, err := mockResourceClient.Write(NewMockResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) mockResource7b, err := mockResourceClient.Write(NewMockResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource7a, mockResource7b}...) + mockResourceNotWatched = append(mockResourceNotWatched, mockResource7a) + mockResourceWatched = append(mockResourceWatched, mockResource7b) assertSnapshotMocks(mockResourceWatched, mockResourceNotWatched) for _, r := range mockResourceNotWatched { @@ -934,11 +935,11 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = mockResourceClient.Delete(mockResource2a.GetMetadata().Namespace, mockResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = mockResourceClient.Delete(mockResource2b.GetMetadata().Namespace, mockResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - mockResourceNotWatched = append(mockResourceNotWatched, MockResourceList{mockResource2a, mockResource2b}...) + for _, r := range mockResourceWatched { + err = mockResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockResourceNotWatched = append(mockResourceNotWatched, r) + } assertSnapshotMocks(nil, mockResourceNotWatched) // clean up environment @@ -981,7 +982,6 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResource1b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResourceNotWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource1a, frequentlyChangingAnnotationsResource1b} - assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -993,20 +993,23 @@ var _ = Describe("V2Alpha1Emitter", func() { frequentlyChangingAnnotationsResourceWatched := FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b} assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) frequentlyChangingAnnotationsResource5a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource5b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource5a, frequentlyChangingAnnotationsResource5b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource5a) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource5b) assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) frequentlyChangingAnnotationsResource7a, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) frequentlyChangingAnnotationsResource7b, err := frequentlyChangingAnnotationsResourceClient.Write(NewFrequentlyChangingAnnotationsResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource7a, frequentlyChangingAnnotationsResource7b}...) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, frequentlyChangingAnnotationsResource7a) + frequentlyChangingAnnotationsResourceWatched = append(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResource7b) assertSnapshotFcars(frequentlyChangingAnnotationsResourceWatched, frequentlyChangingAnnotationsResourceNotWatched) for _, r := range frequentlyChangingAnnotationsResourceNotWatched { @@ -1014,11 +1017,11 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2a.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = frequentlyChangingAnnotationsResourceClient.Delete(frequentlyChangingAnnotationsResource2b.GetMetadata().Namespace, frequentlyChangingAnnotationsResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, FrequentlyChangingAnnotationsResourceList{frequentlyChangingAnnotationsResource2a, frequentlyChangingAnnotationsResource2b}...) + for _, r := range frequentlyChangingAnnotationsResourceWatched { + err = frequentlyChangingAnnotationsResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + frequentlyChangingAnnotationsResourceNotWatched = append(frequentlyChangingAnnotationsResourceNotWatched, r) + } assertSnapshotFcars(nil, frequentlyChangingAnnotationsResourceNotWatched) // clean up environment @@ -1061,7 +1064,6 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResource1b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace2, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResourceNotWatched := testing_solo_io.FakeResourceList{fakeResource1a, fakeResource1b} - assertSnapshotFakes(nil, fakeResourceNotWatched) createNamespaceWithLabel(ctx, kube, namespace3, labels1) createNamespaceWithLabel(ctx, kube, namespace4, labels1) @@ -1073,20 +1075,23 @@ var _ = Describe("V2Alpha1Emitter", func() { fakeResourceWatched := testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b} assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) - createNamespaces(ctx, kube, namespace5, namespace6) + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) fakeResource5a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource5b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name2), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource5a, fakeResource5b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource5a) + fakeResourceWatched = append(fakeResourceWatched, fakeResource5b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) fakeResource7a, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace5, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) fakeResource7b, err := fakeResourceClient.Write(testing_solo_io.NewFakeResource(namespace6, name4), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource7a, fakeResource7b}...) + fakeResourceNotWatched = append(fakeResourceNotWatched, fakeResource7a) + fakeResourceWatched = append(fakeResourceWatched, fakeResource7b) assertSnapshotFakes(fakeResourceWatched, fakeResourceNotWatched) for _, r := range fakeResourceNotWatched { @@ -1094,11 +1099,11 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) } - err = fakeResourceClient.Delete(fakeResource2a.GetMetadata().Namespace, fakeResource2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - err = fakeResourceClient.Delete(fakeResource2b.GetMetadata().Namespace, fakeResource2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) - Expect(err).NotTo(HaveOccurred()) - fakeResourceNotWatched = append(fakeResourceNotWatched, testing_solo_io.FakeResourceList{fakeResource2a, fakeResource2b}...) + for _, r := range fakeResourceWatched { + err = fakeResourceClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + fakeResourceNotWatched = append(fakeResourceNotWatched, r) + } assertSnapshotFakes(nil, fakeResourceNotWatched) // clean up environment From a1fb027a31878a8204417ddd7de8f35fb93aa82e Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 18:26:10 -0500 Subject: [PATCH 44/98] missing file updates --- pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go | 1 + test/mocks/v1/testing_snapshot_emitter_test.go | 1 + test/mocks/v1alpha1/testing_snapshot_emitter_test.go | 1 + test/mocks/v2alpha1/testing_snapshot_emitter_test.go | 1 + 4 files changed, 4 insertions(+) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index d8402b64d..4a01591ce 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +// +build solokit package v1 diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index c8704766e..ec4b21592 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +// +build solokit package v1 diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 5553392e1..bcaede35b 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +// +build solokit package v1alpha1 diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index c69a1a10e..51269ebfc 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +// +build solokit package v2alpha1 From f1db36e3c7296e532f58da725301d934450ca146 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 1 Sep 2022 18:28:59 -0500 Subject: [PATCH 45/98] add updated go build comments --- pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go | 1 + test/mocks/v1/testing_snapshot_emitter_test.go | 1 + test/mocks/v1alpha1/testing_snapshot_emitter_test.go | 1 + test/mocks/v2alpha1/testing_snapshot_emitter_test.go | 1 + 4 files changed, 4 insertions(+) diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 4a01591ce..550951adc 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v1 diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index ec4b21592..4351deb45 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v1 diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index bcaede35b..688cc6d05 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v1alpha1 diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 51269ebfc..997c8e79f 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,5 +1,6 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v2alpha1 From 9aafe60cf8db10cbd7816b043c983af17f3ac36e Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Sat, 10 Sep 2022 22:23:14 -0500 Subject: [PATCH 46/98] updated the controller to allow adding informers --- .../v1/clients/kube/controller/controller.go | 64 ++++++++++++---- .../kube/controller/controller_test.go | 73 ++++++++++++++----- 2 files changed, 105 insertions(+), 32 deletions(-) diff --git a/pkg/api/v1/clients/kube/controller/controller.go b/pkg/api/v1/clients/kube/controller/controller.go index 43551f3ef..b28519e9e 100644 --- a/pkg/api/v1/clients/kube/controller/controller.go +++ b/pkg/api/v1/clients/kube/controller/controller.go @@ -16,6 +16,7 @@ import ( type Controller struct { name string + // informers are the caching indexes used to retrieve events of add,update, and deletes from. informers []cache.SharedIndexInformer // WorkQueue is a rate limited work queue. This is used to queue work to be @@ -27,6 +28,13 @@ type Controller struct { // handler to call handler cache.ResourceEventHandler + // suncFunctions of the informers used to ensure that the informers + // are set up and ready to transmit information to the controller. + syncFunctions []cache.InformerSynced + // stopCh is used to stop all the go routines of the controller. + stopCh <-chan struct{} + // isRunning this flag is used to identify when the controller is running, or not. + isRunning bool } // Returns a new kubernetes controller without starting it. @@ -55,31 +63,25 @@ func NewController( func (c *Controller) Run(parallelism int, stopCh <-chan struct{}) error { defer runtime.HandleCrash() + c.stopCh = stopCh + log.Debugf("Starting %v controller", c.name) // For each informer - var syncFunctions []cache.InformerSynced for _, informer := range c.informers { - - // 1. Get the function to tell if it has synced - syncFunctions = append(syncFunctions, informer.HasSynced) - - // 2. Register the event handler with the informer - informer.AddEventHandler(c.eventHandlerFunctions()) - - // 3. Run the informer - go informer.Run(stopCh) + c.setupInformer(informer) } - // Wait for all the informer caches to be synced before starting workers - log.Debugf("Waiting for informer caches to sync") - if ok := cache.WaitForCacheSync(stopCh, []cache.InformerSynced(syncFunctions)...); !ok { - return fmt.Errorf("error while waiting for caches to sync") + if err := c.waitForCacheToSync(); err != nil { + return err } // Start workers in goroutine so we can defer the queue shutdown go func() { - defer c.workQueue.ShutDown() + defer func() { + c.workQueue.ShutDown() + c.isRunning = false + }() log.Debugf("Starting workers") // Launch parallel workers to process resources @@ -93,7 +95,39 @@ func (c *Controller) Run(parallelism int, stopCh <-chan struct{}) error { <-stopCh log.Debugf("Stopping workers") }() + c.isRunning = true + return nil +} + +// Wait for all the informer caches to be synced before starting workers +func (c *Controller) waitForCacheToSync() error { + log.Debugf("Waiting for informer caches to sync") + if ok := cache.WaitForCacheSync(c.stopCh, []cache.InformerSynced(c.syncFunctions)...); !ok { + return fmt.Errorf("error while waiting for caches to sync") + } + return nil +} +// 1. Get the function to tell if it has synced +// 2. Register the event handler with the informer +// 3. Run the informer +func (c *Controller) setupInformer(informer cache.SharedIndexInformer) { + c.syncFunctions = append(c.syncFunctions, informer.HasSynced) + informer.AddEventHandler(c.eventHandlerFunctions()) + go informer.Run(c.stopCh) +} + +// AddNewInformer will add a new informer to the already running controller +// if the controller is not running, it will just append the informer to the controllers +// list of informers +func (c *Controller) AddNewInformer(newInformer cache.SharedIndexInformer) error { + c.informers = append(c.informers, newInformer) + if c.isRunning { + c.setupInformer(newInformer) + if err := c.waitForCacheToSync(); err != nil { + return err + } + } return nil } diff --git a/pkg/api/v1/clients/kube/controller/controller_test.go b/pkg/api/v1/clients/kube/controller/controller_test.go index 24a381245..58ddbdeeb 100644 --- a/pkg/api/v1/clients/kube/controller/controller_test.go +++ b/pkg/api/v1/clients/kube/controller/controller_test.go @@ -2,6 +2,7 @@ package controller_test import ( "context" + "fmt" "time" . "github.com/onsi/ginkgo" @@ -43,6 +44,29 @@ var _ = Describe("Test KubeController", func() { err error ) + const ( + name1 = "res-1" + value1 = "test" + name2 = "res-2" + value2 = "secondNamespaceValue" + ) + + getResultFromkMockResource := func(namespace, name, value string) { + select { + case res := <-resultChan: + Expect(res.Namespace).To(BeEquivalentTo(namespace)) + Expect(res.Name).To(BeEquivalentTo(name)) + Expect(res.Kind).To(BeEquivalentTo("MockResource")) + Expect(res.Spec).To(Not(BeNil())) + + fieldValue, ok := (*res.Spec)["someDumbField"] + Expect(ok).To(BeTrue()) + Expect(fieldValue).To(BeEquivalentTo(value)) + case <-time.After(50 * time.Millisecond): + Fail("timed out waiting for watch event") + } + } + BeforeEach(func() { clientset = fake.NewSimpleClientset(mocksv1.MockResourceCrd) resyncPeriod = time.Duration(0) @@ -76,23 +100,7 @@ var _ = Describe("Test KubeController", func() { err = util.CreateMockResource(ctx, clientset, namespace1, "res-1", "test") Expect(err).NotTo(HaveOccurred()) - for { - select { - case res := <-resultChan: - Expect(res.Namespace).To(BeEquivalentTo(namespace1)) - Expect(res.Name).To(BeEquivalentTo("res-1")) - Expect(res.Kind).To(BeEquivalentTo("MockResource")) - Expect(res.Spec).To(Not(BeNil())) - - fieldValue, ok := (*res.Spec)["someDumbField"] - Expect(ok).To(BeTrue()) - Expect(fieldValue).To(BeEquivalentTo("test")) - return - case <-time.After(50 * time.Millisecond): - Fail("timed out waiting for watch event") - return - } - } + getResultFromkMockResource(namespace1, name1, value1) }) It("does not react to events in a non relevant namespace", func() { @@ -107,6 +115,37 @@ var _ = Describe("Test KubeController", func() { Succeed() } }) + + It("can add new informers so that events can be received on the new informer", func() { + err = util.CreateMockResource(ctx, clientset, namespace1, name1, value1) + Expect(err).NotTo(HaveOccurred()) + + newInformer := cache.NewSharedIndexInformer( + listWatchForClientAndNamespace(ctx, clientset, namespace2), + &solov1.Resource{}, + resyncPeriod, + cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, + ) + + getResultFromkMockResource(namespace1, name1, value1) + + // create the second value that we want to look at, but do not set up the informer quit + // yet, we still want to ensure that the controller does not learn about the new resource + // until the new informer has been added to the kube controller + err = util.CreateMockResource(ctx, clientset, namespace2, name2, value2) + Expect(err).NotTo(HaveOccurred()) + + select { + case res := <-resultChan: + Fail(fmt.Sprintf("Should not have received the resource %s from Namespace %s as the informer has not yet been added to the KubeController yet", res.Name, res.Namespace)) + case <-time.After(100 * time.Millisecond): + } + + err = kubeController.AddNewInformer(newInformer) + Expect(err).NotTo(HaveOccurred()) + + getResultFromkMockResource(namespace2, name2, value2) + }) }) Context("controller is configured with a resync period", func() { From e7371cab5072145feddcf26c82ee3b0dd6eb0f39 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 12 Sep 2022 11:51:25 -0500 Subject: [PATCH 47/98] register new namespace to cache --- pkg/api/v1/clients/kube/cache/cache.go | 219 ++++++++++-------- pkg/api/v1/clients/kube/cache/cache_test.go | 81 ++++--- .../v1/clients/kube/controller/controller.go | 16 ++ 3 files changed, 199 insertions(+), 117 deletions(-) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index 8c90eb3e1..a54cfb066 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -52,6 +52,9 @@ type KubeCoreCache interface { Cache clustercache.ClusterCache + // RegisterNewNamespaceCache will register the namespace so that the resources + // are available in the cache listers. + RegisterNewNamespaceCache(ns string) error // Deprecated: Use NamespacedPodLister instead PodLister() kubelisters.PodLister // Deprecated: Use NamespacedServiceLister instead @@ -75,6 +78,18 @@ type kubeCoreCaches struct { configMapListers map[string]kubelisters.ConfigMapLister secretListers map[string]kubelisters.SecretLister namespaceLister kubelisters.NamespaceLister + // ctx is the context of the cache + ctx context.Context + // client kubernetes client + client kubernetes.Interface + // kubeController is the controller used to start the informers, and is used to + // watch events that occur on the informers. This is used to send information back to the + // [resource]listers. + kubeController *controller.Controller + // resyncDuration is the time + resyncDuration time.Duration + // informers are the kube resources that provide events + informers []cache.SharedIndexInformer cacheUpdatedWatchers []chan struct{} cacheUpdatedWatchersMutex sync.Mutex @@ -136,107 +151,36 @@ func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interfac configMaps := map[string]kubelisters.ConfigMapLister{} secrets := map[string]kubelisters.SecretLister{} - for _, nsToWatch := range namesapcesToWatch { - nsToWatch := nsToWatch - nsCtx := ctx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyNamespaceKind, skkube.NotEmptyValue(nsToWatch))); err == nil { - nsCtx = ctxWithTags - } - - { - var typeCtx = nsCtx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyKind, "Pods")); err == nil { - typeCtx = ctxWithTags - } - // Pods - watch := client.CoreV1().Pods(nsToWatch).Watch - list := func(options metav1.ListOptions) (runtime.Object, error) { - return client.CoreV1().Pods(nsToWatch).List(ctx, options) - } - informer := skkube.NewSharedInformer(typeCtx, resyncDuration, &v1.Pod{}, list, watch) - informers = append(informers, informer) - lister := kubelisters.NewPodLister(informer.GetIndexer()) - pods[nsToWatch] = lister - } - { - var typeCtx = nsCtx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyKind, "Services")); err == nil { - typeCtx = ctxWithTags - } - // Services - watch := client.CoreV1().Services(nsToWatch).Watch - list := func(options metav1.ListOptions) (runtime.Object, error) { - return client.CoreV1().Services(nsToWatch).List(ctx, options) - } - informer := skkube.NewSharedInformer(typeCtx, resyncDuration, &v1.Service{}, list, watch) - informers = append(informers, informer) - lister := kubelisters.NewServiceLister(informer.GetIndexer()) - services[nsToWatch] = lister - } - { - var typeCtx = nsCtx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyKind, "ConfigMap")); err == nil { - typeCtx = ctxWithTags - } - // ConfigMap - watch := client.CoreV1().ConfigMaps(nsToWatch).Watch - list := func(options metav1.ListOptions) (runtime.Object, error) { - return client.CoreV1().ConfigMaps(nsToWatch).List(ctx, options) - } - informer := skkube.NewSharedInformer(typeCtx, resyncDuration, &v1.ConfigMap{}, list, watch) - informers = append(informers, informer) - lister := kubelisters.NewConfigMapLister(informer.GetIndexer()) - configMaps[nsToWatch] = lister - } - { - var typeCtx = nsCtx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyKind, "Secrets")); err == nil { - typeCtx = ctxWithTags - } - // Secrets - watch := client.CoreV1().Secrets(nsToWatch).Watch - list := func(options metav1.ListOptions) (runtime.Object, error) { - return client.CoreV1().Secrets(nsToWatch).List(ctx, options) - } - informer := skkube.NewSharedInformer(typeCtx, resyncDuration, &v1.Secret{}, list, watch) - informers = append(informers, informer) - lister := kubelisters.NewSecretLister(informer.GetIndexer()) - secrets[nsToWatch] = lister - } - - } - - var namespaceLister kubelisters.NamespaceLister - if len(namesapcesToWatch) == 1 && namesapcesToWatch[0] == metav1.NamespaceAll { - - // Pods - watch := client.CoreV1().Namespaces().Watch - list := func(options metav1.ListOptions) (runtime.Object, error) { - return client.CoreV1().Namespaces().List(ctx, options) - } - nsCtx := ctx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyNamespaceKind, skkube.NotEmptyValue(metav1.NamespaceAll)), tag.Insert(skkube.KeyKind, "Namespaces")); err == nil { - nsCtx = ctxWithTags - } - informer := skkube.NewSharedInformer(nsCtx, resyncDuration, &v1.Namespace{}, list, watch) - informers = append(informers, informer) - namespaceLister = kubelisters.NewNamespaceLister(informer.GetIndexer()) - } - k := &kubeCoreCaches{ podListers: pods, serviceListers: services, configMapListers: configMaps, secretListers: secrets, - namespaceLister: namespaceLister, + client: client, + ctx: ctx, + resyncDuration: resyncDuration, + informers: informers, + } + + for _, nsToWatch := range namesapcesToWatch { + k.addNewNamespace(nsToWatch) + } + + // TODO allows add in the namespaceLister, all the time, or make it an option. + // TODO-JAKE the namespace lister is used for something in Gloo, we need to make sure that it is going to + // dealt with. It might have side affects that I am unaware of, so please go and investigate that. + + // TODO-JAKE please look into the Reconciler as well, those might need to updated too. I am not sure though, as they work differently. + if len(namesapcesToWatch) == 1 && namesapcesToWatch[0] == metav1.NamespaceAll { + k.addNamespaceLister() } - kubeController := controller.NewController("kube-plugin-controller", - controller.NewLockingSyncHandler(k.updatedOccured), informers..., + k.kubeController = controller.NewController("kube-plugin-controller", + controller.NewLockingSyncHandler(k.updatedOccured), k.informers..., ) stop := ctx.Done() - err := kubeController.Run(2, stop) + err := k.kubeController.Run(2, stop) if err != nil { return nil, err } @@ -244,6 +188,99 @@ func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interfac return k, nil } +func (c *kubeCoreCaches) addPod(namespace string, typeCtx context.Context) cache.SharedIndexInformer { + if ctxWithTags, err := tag.New(typeCtx, tag.Insert(skkube.KeyKind, "Pods")); err == nil { + typeCtx = ctxWithTags + } + watch := c.client.CoreV1().Pods(namespace).Watch + list := func(options metav1.ListOptions) (runtime.Object, error) { + return c.client.CoreV1().Pods(namespace).List(c.ctx, options) + } + informer := skkube.NewSharedInformer(typeCtx, c.resyncDuration, &v1.Pod{}, list, watch) + c.informers = append(c.informers, informer) + lister := kubelisters.NewPodLister(informer.GetIndexer()) + c.podListers[namespace] = lister + return informer +} + +func (c *kubeCoreCaches) addService(namespace string, typeCtx context.Context) cache.SharedIndexInformer { + if ctxWithTags, err := tag.New(typeCtx, tag.Insert(skkube.KeyKind, "Services")); err == nil { + typeCtx = ctxWithTags + } + watch := c.client.CoreV1().Services(namespace).Watch + list := func(options metav1.ListOptions) (runtime.Object, error) { + return c.client.CoreV1().Services(namespace).List(c.ctx, options) + } + informer := skkube.NewSharedInformer(typeCtx, c.resyncDuration, &v1.Service{}, list, watch) + c.informers = append(c.informers, informer) + lister := kubelisters.NewServiceLister(informer.GetIndexer()) + c.serviceListers[namespace] = lister + return informer +} + +func (c *kubeCoreCaches) addConfigMap(namespace string, typeCtx context.Context) cache.SharedIndexInformer { + if ctxWithTags, err := tag.New(typeCtx, tag.Insert(skkube.KeyKind, "ConfigMap")); err == nil { + typeCtx = ctxWithTags + } + watch := c.client.CoreV1().ConfigMaps(namespace).Watch + list := func(options metav1.ListOptions) (runtime.Object, error) { + return c.client.CoreV1().ConfigMaps(namespace).List(c.ctx, options) + } + informer := skkube.NewSharedInformer(typeCtx, c.resyncDuration, &v1.ConfigMap{}, list, watch) + c.informers = append(c.informers, informer) + lister := kubelisters.NewConfigMapLister(informer.GetIndexer()) + c.configMapListers[namespace] = lister + return informer +} + +func (c *kubeCoreCaches) addSecret(namespace string, typeCtx context.Context) cache.SharedIndexInformer { + if ctxWithTags, err := tag.New(typeCtx, tag.Insert(skkube.KeyKind, "Secrets")); err == nil { + typeCtx = ctxWithTags + } + watch := c.client.CoreV1().Secrets(namespace).Watch + list := func(options metav1.ListOptions) (runtime.Object, error) { + return c.client.CoreV1().Secrets(namespace).List(c.ctx, options) + } + informer := skkube.NewSharedInformer(typeCtx, c.resyncDuration, &v1.Secret{}, list, watch) + c.informers = append(c.informers, informer) + lister := kubelisters.NewSecretLister(informer.GetIndexer()) + c.secretListers[namespace] = lister + return informer +} + +func (c *kubeCoreCaches) addNamespaceLister() { + watch := c.client.CoreV1().Namespaces().Watch + list := func(options metav1.ListOptions) (runtime.Object, error) { + return c.client.CoreV1().Namespaces().List(c.ctx, options) + } + nsCtx := c.ctx + if ctxWithTags, err := tag.New(nsCtx, tag.Insert(skkube.KeyNamespaceKind, skkube.NotEmptyValue(metav1.NamespaceAll)), tag.Insert(skkube.KeyKind, "Namespaces")); err == nil { + nsCtx = ctxWithTags + } + informer := skkube.NewSharedInformer(nsCtx, c.resyncDuration, &v1.Namespace{}, list, watch) + c.informers = append(c.informers, informer) + c.namespaceLister = kubelisters.NewNamespaceLister(informer.GetIndexer()) +} + +func (k *kubeCoreCaches) addNewNamespace(namespace string) []cache.SharedIndexInformer { + nsCtx := k.ctx + if ctxWithTags, err := tag.New(k.ctx, tag.Insert(skkube.KeyNamespaceKind, skkube.NotEmptyValue(namespace))); err == nil { + nsCtx = ctxWithTags + } + podInformer := k.addPod(namespace, nsCtx) + serviceInformer := k.addService(namespace, nsCtx) + configMapInformer := k.addConfigMap(namespace, nsCtx) + secretInformer := k.addSecret(namespace, nsCtx) + return []cache.SharedIndexInformer{podInformer, serviceInformer, configMapInformer, secretInformer} +} + +// RegisterNewNamespaceCache will create the cache informers for each resource type +// this will add the informer to the kube controller so that events can be watched. +func (k *kubeCoreCaches) RegisterNewNamespaceCache(namespace string) error { + informers := k.addNewNamespace(namespace) + return k.kubeController.AddNewListOfInformers(informers) +} + // Deprecated: Use NamespacedPodLister instead func (k *kubeCoreCaches) PodLister() kubelisters.PodLister { return k.podListers[metav1.NamespaceAll] diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index a0cf5eeb0..f01f573b5 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -35,6 +35,25 @@ var _ = Describe("kube core cache tests", func() { selectors = labels.SelectorFromSet(make(map[string]string)) ) + createNamespaceAndResource := func(namespace string) { + _, err := client.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, metav1.CreateOptions{}) + Expect(err).NotTo(HaveOccurred()) + _, err = client.CoreV1().ConfigMaps(namespace).Create(ctx, &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "cfg"}}, metav1.CreateOptions{}) + Expect(err).NotTo(HaveOccurred()) + } + + validateNamespaceResource := func(namespace string) { + _, err := cache.NamespacedPodLister(namespace).List(selectors) + Expect(err).NotTo(HaveOccurred()) + cfgMap, err := cache.NamespacedConfigMapLister(namespace).List(selectors) + Expect(err).NotTo(HaveOccurred()) + cfgMap = cleanConfigMaps(cfgMap) + _, err = cache.NamespacedSecretLister(namespace).List(selectors) + Expect(err).NotTo(HaveOccurred()) + Expect(cfgMap).To(HaveLen(1)) + Expect(cfgMap[0].Namespace).To(Equal(namespace)) + } + BeforeEach(func() { var err error cfg, err = kubeutils.GetConfig("", "") @@ -111,11 +130,7 @@ var _ = Describe("kube core cache tests", func() { testns = fmt.Sprintf("test-%d", randomvalue) testns2 = fmt.Sprintf("test2-%d", randomvalue) for _, ns := range []string{testns, testns2} { - _, err := client.CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{}) - Expect(err).NotTo(HaveOccurred()) - _, err = client.CoreV1().ConfigMaps(ns).Create(ctx, &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "cfg"}}, metav1.CreateOptions{}) - Expect(err).NotTo(HaveOccurred()) - + createNamespaceAndResource(ns) } var err error cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{testns, testns2}) @@ -130,27 +145,8 @@ var _ = Describe("kube core cache tests", func() { It("can list resources for all listers", func() { Expect(cache.NamespaceLister()).To(BeNil()) - _, err := cache.NamespacedPodLister(testns).List(selectors) - Expect(err).NotTo(HaveOccurred()) - cfgMaps, err := cache.NamespacedConfigMapLister(testns).List(selectors) - Expect(err).NotTo(HaveOccurred()) - cfgMaps = cleanConfigMaps(cfgMaps) - _, err = cache.NamespacedSecretLister(testns).List(selectors) - Expect(err).NotTo(HaveOccurred()) - - Expect(cache.NamespaceLister()).To(BeNil()) - _, err = cache.NamespacedPodLister(testns2).List(selectors) - Expect(err).NotTo(HaveOccurred()) - cfgMaps2, err := cache.NamespacedConfigMapLister(testns2).List(selectors) - Expect(err).NotTo(HaveOccurred()) - cfgMaps2 = cleanConfigMaps(cfgMaps2) - _, err = cache.NamespacedSecretLister(testns2).List(selectors) - Expect(err).NotTo(HaveOccurred()) - - Expect(cfgMaps).To(HaveLen(1)) - Expect(cfgMaps2).To(HaveLen(1)) - Expect(cfgMaps[0].Namespace).To(Equal(testns)) - Expect(cfgMaps2[0].Namespace).To(Equal(testns2)) + validateNamespaceResource(testns) + validateNamespaceResource(testns2) }) }) @@ -161,6 +157,39 @@ var _ = Describe("kube core cache tests", func() { Expect(err).To(HaveOccurred()) }) }) + Context("Register a new namespace", func() { + var ( + initialNs string + registeredNs string + ) + + BeforeEach(func() { + randomvalue := rand.Int31() + initialNs = fmt.Sprintf("initial-%d", randomvalue) + registeredNs = fmt.Sprintf("registered-%d", randomvalue) + + createNamespaceAndResource(initialNs) + + var err error + cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{initialNs}) + Expect(err).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + client.CoreV1().Namespaces().Delete(ctx, initialNs, metav1.DeleteOptions{}) + client.CoreV1().Namespaces().Delete(ctx, registeredNs, metav1.DeleteOptions{}) + }) + + It("should be able to register a new namespace", func() { + createNamespaceAndResource(registeredNs) + + err := cache.RegisterNewNamespaceCache(registeredNs) + Expect(err).NotTo(HaveOccurred()) + + validateNamespaceResource(initialNs) + validateNamespaceResource(registeredNs) + }) + }) }) }) }) diff --git a/pkg/api/v1/clients/kube/controller/controller.go b/pkg/api/v1/clients/kube/controller/controller.go index b28519e9e..c042d2654 100644 --- a/pkg/api/v1/clients/kube/controller/controller.go +++ b/pkg/api/v1/clients/kube/controller/controller.go @@ -131,6 +131,22 @@ func (c *Controller) AddNewInformer(newInformer cache.SharedIndexInformer) error return nil } +// AddNewListOfInformers will add a list of new informers to the already running controller. +// if the controller is not running, it will just append the informers to the controllers +// list of informers +func (c *Controller) AddNewListOfInformers(newInformers []cache.SharedIndexInformer) error { + c.informers = append(c.informers, newInformers...) + if c.isRunning { + for _, in := range newInformers { + c.setupInformer(in) + } + if err := c.waitForCacheToSync(); err != nil { + return err + } + } + return nil +} + // runWorker is a long-running function that will continually call the processNextWorkItem function // in order to read and process a message on the work queue. func (c *Controller) runWorker() { From d3c5196d070d0dc9d1e6a1544fd111ef3e8e60e7 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 12 Sep 2022 13:53:45 -0500 Subject: [PATCH 48/98] register new namespaces for the Resoruce Client Shared Informer Factory --- pkg/api/v1/clients/kube/resource_client.go | 2 + .../clients/kube/resource_client_factory.go | 87 +++++++++++++------ .../kube/resource_client_factory_test.go | 36 +++++++- 3 files changed, 97 insertions(+), 28 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client.go b/pkg/api/v1/clients/kube/resource_client.go index 3b1d85c51..cfdf31af2 100644 --- a/pkg/api/v1/clients/kube/resource_client.go +++ b/pkg/api/v1/clients/kube/resource_client.go @@ -338,6 +338,8 @@ func (rc *ResourceClient) List(namespace string, opts clients.ListOpts) (resourc func (rc *ResourceClient) Watch(namespace string, opts clients.WatchOpts) (<-chan resources.ResourceList, <-chan error, error) { + // JAKE-TODO we will need to update the validation list here as well. + // JAKE-TODO ensure that this is done as well for the other resource client too. if err := rc.validateNamespace(namespace); err != nil { return nil, nil, err } diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 9ba767173..9b0e0d959 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -120,6 +120,9 @@ type ResourceClientSharedInformerFactory struct { // Determines how long the controller will wait for a watch channel to accept an event before aborting the delivery watchTimeout time.Duration + // kubeController is the controller used to watch for events on the informers. It can be used to add new informers too. + kubeController *controller.Controller + // Mutexes lock sync.Mutex cacheUpdatedWatchersMutex sync.Mutex @@ -132,6 +135,7 @@ func NotEmptyValue(ns string) string { return ns } +// NewSharedInformer creates a new Shared Index Informer with the list and watch template functions. func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType runtime.Object, listFunc func(options metav1.ListOptions) (runtime.Object, error), watchFunc func(context.Context, metav1.ListOptions) (kubewatch.Interface, error)) cache.SharedIndexInformer { @@ -159,11 +163,13 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType }, objType, resyncPeriod, + // TODO-JAKE make a note here, that is where we would want to look at creating namespace indexes if needed. cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, ) } // Creates a new SharedIndexInformer and adds it to the factory's informer registry. +// This method is meant to be called once per rc namespace set, please call Register New Namespace when adding new namespaces. // NOTE: Currently we cannot share informers between resource clients, because the listWatch functions are configured // with the client's specific token. Hence, we must enforce a one-to-one relationship between informers and clients. func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error { @@ -179,40 +185,46 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error ctx = ctxWithTags } + // Create a shared informer for each of the given namespaces. + // NOTE: We do not distinguish between the value "" (all namespaces) and a regular namespace here. + // TODO-JAKE there is a special rule, when the namespaces is [""], we need an option + // to be able to change the Informer so that it does not watch on "", but only those namespaces + // that we want to watch. This only happens if the client(user) is setting the namespaceLabelSelectors field in the API. + for _, ns := range rc.namespaceWhitelist { + if _, err := f.addNewNamespaceToRegistry(ctx, ns, rc); err != nil { + return err + } + } + + return nil +} + +func (f *ResourceClientSharedInformerFactory) addNewNamespaceToRegistry(ctx context.Context, ns string, rc *ResourceClient) (cache.SharedIndexInformer, error) { + nsCtx := ctx resourceType := reflect.TypeOf(rc.crd.Version.Type) - namespaces := rc.namespaceWhitelist // will always contain at least one element + // get the resync value resyncPeriod := f.defaultResync if rc.resyncPeriod != 0 { resyncPeriod = rc.resyncPeriod } - // Create a shared informer for each of the given namespaces. - // NOTE: We do not distinguish between the value "" (all namespaces) and a regular namespace here. - for _, ns := range namespaces { - // copy to variable, so we can send it to closures - ns := ns - // To nip configuration errors in the bud, error if the registry already contains an informer for the given resource/namespace. - if f.registry.get(resourceType, ns) != nil { - return errors.Errorf("Shared cache already contains informer for resource [%v] and namespace [%v]", resourceType, ns) - - } - - nsCtx := ctx - if ctxWithTags, err := tag.New(nsCtx, tag.Insert(KeyNamespaceKind, NotEmptyValue(ns))); err == nil { - nsCtx = ctxWithTags - } - - resourceList := rc.crdClientset.ResourcesV1().Resources(ns).List - list := func(options metav1.ListOptions) (runtime.Object, error) { - return resourceList(ctx, options) - } - watch := rc.crdClientset.ResourcesV1().Resources(ns).Watch - sharedInformer := NewSharedInformer(nsCtx, resyncPeriod, &v1.Resource{}, list, watch) - f.registry.add(resourceType, ns, sharedInformer) + // To nip configuration errors in the bud, error if the registry already contains an informer for the given resource/namespace. + if f.registry.get(resourceType, ns) != nil { + return nil, errors.Errorf("Shared cache already contains informer for resource [%v] and namespace [%v]", resourceType, ns) + } + if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyNamespaceKind, NotEmptyValue(ns))); err == nil { + nsCtx = ctxWithTags } - return nil + listResourceFunc := rc.crdClientset.ResourcesV1().Resources(ns).List + list := func(options metav1.ListOptions) (runtime.Object, error) { + return listResourceFunc(ctx, options) + } + watch := rc.crdClientset.ResourcesV1().Resources(ns).Watch + sharedInformer := NewSharedInformer(nsCtx, resyncPeriod, &v1.Resource{}, list, watch) + f.registry.add(resourceType, ns, sharedInformer) + return sharedInformer, nil } func (f *ResourceClientSharedInformerFactory) IsClusterCache() {} @@ -238,10 +250,11 @@ func (f *ResourceClientSharedInformerFactory) Start() { ctx := f.ctx // Collect all registered informers + sharedInformers := f.registry.list() // Initialize a new kubernetes controller - kubeController := controller.NewController("solo-resource-controller", + f.kubeController = controller.NewController("solo-resource-controller", controller.NewLockingCallbackHandler(f.updatedOccurred), sharedInformers...) // Start the controller @@ -249,7 +262,7 @@ func (f *ResourceClientSharedInformerFactory) Start() { go func() { // If there is a problem with the ListWatch, the Run method might wait indefinitely for the informer caches // to sync, so we start it in a goroutine to be able to timeout. - runResult <- kubeController.Run(2, ctx.Done()) + runResult <- f.kubeController.Run(2, ctx.Done()) }() // Fail if the caches have not synchronized after 10 seconds. This prevents the controller from hanging forever. @@ -272,6 +285,26 @@ func (f *ResourceClientSharedInformerFactory) Start() { }) } +// RegisterNewNamespace is used when the resource client is running. This will add a new namespace to the +// kube controller so that events can be received. +func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace string, rc *ResourceClient) error { + if !f.IsRunning() { + contextutils.LoggerFrom(f.ctx).Panicf("failed to register the new namespace [%v] to the resource client [%v]", namespace, reflect.TypeOf(rc)) + } + ctx := f.ctx + if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { + ctx = ctxWithTags + } + informer, err := f.addNewNamespaceToRegistry(ctx, namespace, rc) + if err != nil { + return err + } + if err := f.kubeController.AddNewInformer(informer); err != nil { + return err + } + return nil +} + func (f *ResourceClientSharedInformerFactory) GetLister(namespace string, obj runtime.Object) (ResourceLister, error) { f.lock.Lock() defer f.lock.Unlock() diff --git a/pkg/api/v1/clients/kube/resource_client_factory_test.go b/pkg/api/v1/clients/kube/resource_client_factory_test.go index 448dae3c7..de6e25807 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory_test.go +++ b/pkg/api/v1/clients/kube/resource_client_factory_test.go @@ -82,6 +82,13 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { Expect(func() { _ = kubeCache.Register(client1) }).To(Panic()) }) + It("can register a new namespace even when the factory is running", func() { + kubeCache.Start() + Expect(kubeCache.IsRunning()).To(BeTrue()) + + err := kubeCache.RegisterNewNamespace("newNamespace", client2) + Expect(err).NotTo(HaveOccurred()) + }) }) Describe("starting the factory", func() { @@ -112,12 +119,13 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { var ( clientset *fake.Clientset preStartGoroutines int + client *kube.ResourceClient ) BeforeEach(func() { clientset = fake.NewSimpleClientset(mocksv1.MockResourceCrd) // We need the resourceClient so that we can register its resourceType/namespaces with the cache - client := util.ClientForClientsetAndResource(clientset, kubeCache, mocksv1.MockResourceCrd, &mocksv1.MockResource{}, []string{namespace1}) + client = util.ClientForClientsetAndResource(clientset, kubeCache, mocksv1.MockResourceCrd, &mocksv1.MockResource{}, []string{namespace1}) err := kubeCache.Register(client) Expect(err).NotTo(HaveOccurred()) @@ -191,6 +199,32 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { Expect(len(watchResults)).To(BeEquivalentTo(3)) Expect(watchResults).To(ConsistOf("mock-res-1", "mock-res-3", "mock-res-1")) }) + It("should be able to register a new namespace", func() { + err := kubeCache.RegisterNewNamespace(namespace2, client) + Expect(err).NotTo(HaveOccurred()) + + var watchResults []string + + ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*100)) + + go func() { + for { + select { + case <-ctx.Done(): + return + case res := <-watch: + watchResults = append(watchResults, res.ObjectMeta.Name) + } + } + }() + + go Expect(util.CreateMockResource(ctx, clientset, namespace2, "mock-res-2", "test")).To(BeNil()) + + <-ctx.Done() + + Expect(len(watchResults)).To(BeEquivalentTo(1)) + Expect(watchResults).To(ConsistOf("mock-res-2")) + }) }) Context("multiple watches", func() { From 1ae4371cce39339d06266107b324fce4f6b5ae78 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 12 Sep 2022 16:41:31 -0500 Subject: [PATCH 49/98] added in Register Namespace to the Resouce client interface. --- .../kubernetes/namespace/resource_namespace.go | 2 ++ pkg/api/v1/clients/client_interface.go | 1 + pkg/api/v1/clients/configmap/resource_client.go | 4 ++++ pkg/api/v1/clients/consul/resource_client.go | 4 ++++ pkg/api/v1/clients/file/resource_client.go | 4 ++++ pkg/api/v1/clients/kube/controller/controller.go | 2 +- pkg/api/v1/clients/kube/resource_client.go | 4 ++++ pkg/api/v1/clients/kube/resource_client_factory.go | 3 +++ pkg/api/v1/clients/kubesecret/resource_client.go | 4 ++++ pkg/api/v1/clients/memory/resource_client.go | 4 ++++ pkg/api/v1/clients/vault/resource_client.go | 4 ++++ .../codegen/templates/snapshot_emitter_template.go | 9 +++++++++ test/mocks/v1alpha1/mock_resource_client.sk.go | 5 +++++ test/mocks/v1alpha1/testing_snapshot_emitter.sk.go | 10 ++++++++++ 14 files changed, 59 insertions(+), 1 deletion(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_namespace.go b/pkg/api/external/kubernetes/namespace/resource_namespace.go index 3d40a94a9..f5ffd78c4 100644 --- a/pkg/api/external/kubernetes/namespace/resource_namespace.go +++ b/pkg/api/external/kubernetes/namespace/resource_namespace.go @@ -159,6 +159,8 @@ func (client *kubeClientResourceNamespaceLister) GetResourceNamespaceWatch(opts } func (client *kubeClientResourceNamespaceLister) getExcludeFieldSelector(filtered resources.ResourceNamespaceList) string { + // you can filter the namespaces by using metadata.name for more information on field selectors + // https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ var buffer bytes.Buffer for i, rns := range filtered { ns := rns.Name diff --git a/pkg/api/v1/clients/client_interface.go b/pkg/api/v1/clients/client_interface.go index 36db15e00..8dba02949 100644 --- a/pkg/api/v1/clients/client_interface.go +++ b/pkg/api/v1/clients/client_interface.go @@ -32,6 +32,7 @@ type ResourceClient interface { NewResource() resources.Resource // Deprecated: implemented only by the kubernetes resource client. Will be removed from the interface. Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts ReadOpts) (resources.Resource, error) Write(resource resources.Resource, opts WriteOpts) (resources.Resource, error) Delete(namespace, name string, opts DeleteOpts) error diff --git a/pkg/api/v1/clients/configmap/resource_client.go b/pkg/api/v1/clients/configmap/resource_client.go index 32ae6527d..c992d9004 100644 --- a/pkg/api/v1/clients/configmap/resource_client.go +++ b/pkg/api/v1/clients/configmap/resource_client.go @@ -52,6 +52,10 @@ func NewResourceClientWithConverter(kube kubernetes.Interface, resourceType reso var _ clients.ResourceClient = &ResourceClient{} +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return rc.cache.RegisterNewNamespaceCache(namespace) +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/consul/resource_client.go b/pkg/api/v1/clients/consul/resource_client.go index 4b3293d39..8f94bc408 100644 --- a/pkg/api/v1/clients/consul/resource_client.go +++ b/pkg/api/v1/clients/consul/resource_client.go @@ -50,6 +50,10 @@ func (rc *ResourceClient) Register() error { return nil } +func (rc *ResourceClient) RegisterNamespace(ns string) error { + return nil +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/file/resource_client.go b/pkg/api/v1/clients/file/resource_client.go index 37ca0fabc..ad22b9370 100644 --- a/pkg/api/v1/clients/file/resource_client.go +++ b/pkg/api/v1/clients/file/resource_client.go @@ -52,6 +52,10 @@ func (rc *ResourceClient) Register() error { return nil } +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/kube/controller/controller.go b/pkg/api/v1/clients/kube/controller/controller.go index c042d2654..36f39c97f 100644 --- a/pkg/api/v1/clients/kube/controller/controller.go +++ b/pkg/api/v1/clients/kube/controller/controller.go @@ -132,7 +132,7 @@ func (c *Controller) AddNewInformer(newInformer cache.SharedIndexInformer) error } // AddNewListOfInformers will add a list of new informers to the already running controller. -// if the controller is not running, it will just append the informers to the controllers +// If the controller is not running, it will just append the informers to the controllers // list of informers func (c *Controller) AddNewListOfInformers(newInformers []cache.SharedIndexInformer) error { c.informers = append(c.informers, newInformers...) diff --git a/pkg/api/v1/clients/kube/resource_client.go b/pkg/api/v1/clients/kube/resource_client.go index 41211edb0..8d8d145e9 100644 --- a/pkg/api/v1/clients/kube/resource_client.go +++ b/pkg/api/v1/clients/kube/resource_client.go @@ -151,6 +151,10 @@ func (rc *ResourceClient) Register() error { return rc.sharedCache.Register(rc) } +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return rc.sharedCache.RegisterNewNamespace(namespace, rc) +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 9b0e0d959..794001b75 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -64,6 +64,8 @@ type SharedCache interface { // Registers the client with the shared cache Register(rc *ResourceClient) error + // RegisterNewNamespace will register the client with a new namespace + RegisterNewNamespace(namespace string, rc *ResourceClient) error // Starts all informers in the factory's registry. Must be idempotent. Start() // Returns a lister for resources of the given type in the given namespace. @@ -199,6 +201,7 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error return nil } +// addNewNamespaceToRegistry will create a watch for the resource client type and namespace func (f *ResourceClientSharedInformerFactory) addNewNamespaceToRegistry(ctx context.Context, ns string, rc *ResourceClient) (cache.SharedIndexInformer, error) { nsCtx := ctx resourceType := reflect.TypeOf(rc.crd.Version.Type) diff --git a/pkg/api/v1/clients/kubesecret/resource_client.go b/pkg/api/v1/clients/kubesecret/resource_client.go index 07ea70ccf..0ed3783fb 100644 --- a/pkg/api/v1/clients/kubesecret/resource_client.go +++ b/pkg/api/v1/clients/kubesecret/resource_client.go @@ -118,6 +118,10 @@ func NewResourceClientWithSecretConverter(kube kubernetes.Interface, resourceTyp var _ clients.ResourceClient = &ResourceClient{} +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return rc.cache.RegisterNewNamespaceCache(namespace) +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/memory/resource_client.go b/pkg/api/v1/clients/memory/resource_client.go index a35a4d8d8..0a880e193 100644 --- a/pkg/api/v1/clients/memory/resource_client.go +++ b/pkg/api/v1/clients/memory/resource_client.go @@ -126,6 +126,10 @@ func (rc *ResourceClient) Register() error { return nil } +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/vault/resource_client.go b/pkg/api/v1/clients/vault/resource_client.go index d1f48cc50..5a7dc0462 100644 --- a/pkg/api/v1/clients/vault/resource_client.go +++ b/pkg/api/v1/clients/vault/resource_client.go @@ -104,6 +104,10 @@ func (rc *ResourceClient) Register() error { return nil } +func (rc *ResourceClient) RegisterNamespace(ns string) error { + return nil +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 4ad87a221..d4f915048 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -285,6 +285,9 @@ type {{ lower_camel .Name }}ListWithNamespace struct { filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { // we do not want to filter out "" which equals all namespaces + // the reason is because we will never create a watch on ""(all namespaces) because + // doing so means we watch all resources regardless of namespace. Our intent is to + // watch only certain namespaces. if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -298,6 +301,7 @@ type {{ lower_camel .Name }}ListWithNamespace struct { namespace := resourceNamespace.Name {{- range .Resources }} {{- if (not .ClusterScoped) }} + c.{{ lower_camel .Name }}.RegisterNamespace(namespace) /* Setup namespaced watch for {{ upper_camel .Name }} */ { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -401,6 +405,10 @@ type {{ lower_camel .Name }}ListWithNamespace struct { }) for _, ns := range missingNamespaces { + // TODO-JAKE clean this up, so that + // 1. we send a notification that there is an empty list + // 2. have a way to delete the namespace from the list as well. + // 3. any clean up of the resources to that are needed as well. // c.namespacesWatching.Delete(ns) {{- range .Resources}} {{- if (not .ClusterScoped) }} @@ -413,6 +421,7 @@ type {{ lower_camel .Name }}ListWithNamespace struct { for _, namespace := range newNamespaces { {{- range .Resources }} {{- if (not .ClusterScoped) }} + c.{{ lower_camel .Name }}.RegisterNamespace(namespace) /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) diff --git a/test/mocks/v1alpha1/mock_resource_client.sk.go b/test/mocks/v1alpha1/mock_resource_client.sk.go index d6dfd501a..05ccc0210 100644 --- a/test/mocks/v1alpha1/mock_resource_client.sk.go +++ b/test/mocks/v1alpha1/mock_resource_client.sk.go @@ -19,6 +19,7 @@ type MockResourceWatcher interface { type MockResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) Write(resource *MockResource, opts clients.WriteOpts) (*MockResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *mockResourceClient) Register() error { return client.rc.Register() } +func (client *mockResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *mockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index fdbd38cc5..727b3095b 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -222,6 +222,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { // we do not want to filter out "" which equals all namespaces + // the reason is because we will never create a watch on ""(all namespaces) because + // doing so means we watch all resources regardless of namespace. Our intent is to + // watch only certain namespaces. if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -233,6 +236,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -330,12 +334,18 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { + // TODO-JAKE clean this up, so that + // 1. we send a notification that there is an empty list + // 2. have a way to delete the namespace from the list as well. + // 3. any clean up of the resources to that are needed as well. + // c.namespacesWatching.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} // mocksByNamespace.Delete(ns) } for _, namespace := range newNamespaces { + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) From 5e9f466466712c9cf7716bdf7807c42343322f28 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 08:17:55 -0500 Subject: [PATCH 50/98] update resource clients --- .../resource_client.go | 4 ++++ .../kubernetes/deployment/resource_client.go | 4 ++++ .../kubernetes/job/resource_client.go | 4 ++++ .../kubernetes/namespace/resource_client.go | 4 ++++ .../kubernetes/pod/resource_client.go | 4 ++++ .../kubernetes/service/resource_client.go | 4 ++++ .../v1/clients/apiclient/resource_client.go | 4 ++++ .../common/kubernetes/config_map_client.sk.go | 5 +++++ .../custom_resource_definition_client.sk.go | 5 +++++ .../common/kubernetes/deployment_client.sk.go | 5 +++++ .../common/kubernetes/job_client.sk.go | 5 +++++ .../kubernetes/kube_namespace_client.sk.go | 5 +++++ .../common/kubernetes/pod_client.sk.go | 5 +++++ .../common/kubernetes/service_client.sk.go | 5 +++++ pkg/multicluster/v1/kube_config_client.sk.go | 5 +++++ .../v1/kubeconfigs_snapshot_emitter.sk.go | 9 +++++++++ .../v1/another_mock_resource_client.sk.go | 5 +++++ test/mocks/v1/cluster_resource_client.sk.go | 5 +++++ test/mocks/v1/fake_resource_client.sk.go | 5 +++++ test/mocks/v1/mock_custom_type_client.sk.go | 5 +++++ test/mocks/v1/mock_resource_client.sk.go | 5 +++++ .../v1/simple_mock_resource_client.sk.go | 5 +++++ test/mocks/v1/testing_snapshot_emitter.sk.go | 19 +++++++++++++++++++ .../mocks/v1alpha1/fake_resource_client.sk.go | 5 +++++ .../v1alpha1/testing_snapshot_emitter.sk.go | 1 - ...changing_annotations_resource_client.sk.go | 5 +++++ .../mocks/v2alpha1/mock_resource_client.sk.go | 5 +++++ .../v2alpha1/mock_resource_client_test.go | 1 - .../v2alpha1/testing_snapshot_emitter.sk.go | 13 +++++++++++++ 29 files changed, 154 insertions(+), 2 deletions(-) diff --git a/pkg/api/external/kubernetes/customresourcedefinition/resource_client.go b/pkg/api/external/kubernetes/customresourcedefinition/resource_client.go index 9020e09ea..a6f742e71 100644 --- a/pkg/api/external/kubernetes/customresourcedefinition/resource_client.go +++ b/pkg/api/external/kubernetes/customresourcedefinition/resource_client.go @@ -65,6 +65,10 @@ func ToKubeCustomResourceDefinition(resource resources.Resource) (*v1.CustomReso var _ clients.ResourceClient = &customResourceDefinitionResourceClient{} +func (rc *customResourceDefinitionResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *customResourceDefinitionResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/external/kubernetes/deployment/resource_client.go b/pkg/api/external/kubernetes/deployment/resource_client.go index 83b747086..155f97a95 100644 --- a/pkg/api/external/kubernetes/deployment/resource_client.go +++ b/pkg/api/external/kubernetes/deployment/resource_client.go @@ -64,6 +64,10 @@ func ToKubeDeployment(resource resources.Resource) (*appsv1.Deployment, error) { var _ clients.ResourceClient = &deploymentResourceClient{} +func (rc *deploymentResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *deploymentResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/external/kubernetes/job/resource_client.go b/pkg/api/external/kubernetes/job/resource_client.go index daec97e21..ce54e2ef8 100644 --- a/pkg/api/external/kubernetes/job/resource_client.go +++ b/pkg/api/external/kubernetes/job/resource_client.go @@ -64,6 +64,10 @@ func ToKubeJob(resource resources.Resource) (*batchv1.Job, error) { var _ clients.ResourceClient = &jobResourceClient{} +func (rc *jobResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *jobResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/external/kubernetes/namespace/resource_client.go b/pkg/api/external/kubernetes/namespace/resource_client.go index c6204066d..4130d602c 100644 --- a/pkg/api/external/kubernetes/namespace/resource_client.go +++ b/pkg/api/external/kubernetes/namespace/resource_client.go @@ -62,6 +62,10 @@ func ToKubeNamespace(resource resources.Resource) (*kubev1.Namespace, error) { return &namespace, nil } +func (rc *namespaceResourceClient) RegisterNamespace(namespace string) error { + return rc.cache.RegisterNewNamespaceCache(namespace) +} + func (rc *namespaceResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, eris.Wrapf(err, "validation error") diff --git a/pkg/api/external/kubernetes/pod/resource_client.go b/pkg/api/external/kubernetes/pod/resource_client.go index 333c44c70..91ccb01b7 100644 --- a/pkg/api/external/kubernetes/pod/resource_client.go +++ b/pkg/api/external/kubernetes/pod/resource_client.go @@ -64,6 +64,10 @@ func ToKubePod(resource resources.Resource) (*kubev1.Pod, error) { var _ clients.ResourceClient = &podResourceClient{} +func (rc *podResourceClient) RegisterNamespace(namespace string) error { + return rc.cache.RegisterNewNamespaceCache(namespace) +} + func (rc *podResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/external/kubernetes/service/resource_client.go b/pkg/api/external/kubernetes/service/resource_client.go index 406ba504e..4240a28cd 100644 --- a/pkg/api/external/kubernetes/service/resource_client.go +++ b/pkg/api/external/kubernetes/service/resource_client.go @@ -64,6 +64,10 @@ func ToKubeService(resource resources.Resource) (*kubev1.Service, error) { var _ clients.ResourceClient = &serviceResourceClient{} +func (rc *serviceResourceClient) RegisterNamespace(namespace string) error { + return rc.cache.RegisterNewNamespaceCache(namespace) +} + func (rc *serviceResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/clients/apiclient/resource_client.go b/pkg/api/v1/clients/apiclient/resource_client.go index 1246e73b5..a7ecfe1be 100644 --- a/pkg/api/v1/clients/apiclient/resource_client.go +++ b/pkg/api/v1/clients/apiclient/resource_client.go @@ -46,6 +46,10 @@ func (rc *ResourceClient) Register() error { return nil } +func (rc *ResourceClient) RegisterNamespace(namespace string) error { + return nil +} + func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if err := resources.ValidateName(name); err != nil { return nil, errors.Wrapf(err, "validation error") diff --git a/pkg/api/v1/resources/common/kubernetes/config_map_client.sk.go b/pkg/api/v1/resources/common/kubernetes/config_map_client.sk.go index b70d81a02..b79724b49 100644 --- a/pkg/api/v1/resources/common/kubernetes/config_map_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/config_map_client.sk.go @@ -19,6 +19,7 @@ type ConfigMapWatcher interface { type ConfigMapClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*ConfigMap, error) Write(resource *ConfigMap, opts clients.WriteOpts) (*ConfigMap, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *configMapClient) Register() error { return client.rc.Register() } +func (client *configMapClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *configMapClient) Read(namespace, name string, opts clients.ReadOpts) (*ConfigMap, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/custom_resource_definition_client.sk.go b/pkg/api/v1/resources/common/kubernetes/custom_resource_definition_client.sk.go index 0051b2d02..b8af5d1c1 100644 --- a/pkg/api/v1/resources/common/kubernetes/custom_resource_definition_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/custom_resource_definition_client.sk.go @@ -19,6 +19,7 @@ type CustomResourceDefinitionWatcher interface { type CustomResourceDefinitionClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(name string, opts clients.ReadOpts) (*CustomResourceDefinition, error) Write(resource *CustomResourceDefinition, opts clients.WriteOpts) (*CustomResourceDefinition, error) Delete(name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *customResourceDefinitionClient) Register() error { return client.rc.Register() } +func (client *customResourceDefinitionClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *customResourceDefinitionClient) Read(name string, opts clients.ReadOpts) (*CustomResourceDefinition, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/deployment_client.sk.go b/pkg/api/v1/resources/common/kubernetes/deployment_client.sk.go index 300c8da3c..ab218b93c 100644 --- a/pkg/api/v1/resources/common/kubernetes/deployment_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/deployment_client.sk.go @@ -19,6 +19,7 @@ type DeploymentWatcher interface { type DeploymentClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*Deployment, error) Write(resource *Deployment, opts clients.WriteOpts) (*Deployment, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *deploymentClient) Register() error { return client.rc.Register() } +func (client *deploymentClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *deploymentClient) Read(namespace, name string, opts clients.ReadOpts) (*Deployment, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/job_client.sk.go b/pkg/api/v1/resources/common/kubernetes/job_client.sk.go index e059ed9fb..3186b16ec 100644 --- a/pkg/api/v1/resources/common/kubernetes/job_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/job_client.sk.go @@ -19,6 +19,7 @@ type JobWatcher interface { type JobClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*Job, error) Write(resource *Job, opts clients.WriteOpts) (*Job, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *jobClient) Register() error { return client.rc.Register() } +func (client *jobClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *jobClient) Read(namespace, name string, opts clients.ReadOpts) (*Job, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/kube_namespace_client.sk.go b/pkg/api/v1/resources/common/kubernetes/kube_namespace_client.sk.go index e89d918fb..8abcd58c6 100644 --- a/pkg/api/v1/resources/common/kubernetes/kube_namespace_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/kube_namespace_client.sk.go @@ -19,6 +19,7 @@ type KubeNamespaceWatcher interface { type KubeNamespaceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(name string, opts clients.ReadOpts) (*KubeNamespace, error) Write(resource *KubeNamespace, opts clients.WriteOpts) (*KubeNamespace, error) Delete(name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *kubeNamespaceClient) Register() error { return client.rc.Register() } +func (client *kubeNamespaceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *kubeNamespaceClient) Read(name string, opts clients.ReadOpts) (*KubeNamespace, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/pod_client.sk.go b/pkg/api/v1/resources/common/kubernetes/pod_client.sk.go index c84531b3f..fc936b5fb 100644 --- a/pkg/api/v1/resources/common/kubernetes/pod_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/pod_client.sk.go @@ -19,6 +19,7 @@ type PodWatcher interface { type PodClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*Pod, error) Write(resource *Pod, opts clients.WriteOpts) (*Pod, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *podClient) Register() error { return client.rc.Register() } +func (client *podClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *podClient) Read(namespace, name string, opts clients.ReadOpts) (*Pod, error) { opts = opts.WithDefaults() diff --git a/pkg/api/v1/resources/common/kubernetes/service_client.sk.go b/pkg/api/v1/resources/common/kubernetes/service_client.sk.go index c4f6efef6..b10d853aa 100644 --- a/pkg/api/v1/resources/common/kubernetes/service_client.sk.go +++ b/pkg/api/v1/resources/common/kubernetes/service_client.sk.go @@ -19,6 +19,7 @@ type ServiceWatcher interface { type ServiceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*Service, error) Write(resource *Service, opts clients.WriteOpts) (*Service, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *serviceClient) Register() error { return client.rc.Register() } +func (client *serviceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *serviceClient) Read(namespace, name string, opts clients.ReadOpts) (*Service, error) { opts = opts.WithDefaults() diff --git a/pkg/multicluster/v1/kube_config_client.sk.go b/pkg/multicluster/v1/kube_config_client.sk.go index 0deb1dcab..6689ff54b 100644 --- a/pkg/multicluster/v1/kube_config_client.sk.go +++ b/pkg/multicluster/v1/kube_config_client.sk.go @@ -19,6 +19,7 @@ type KubeConfigWatcher interface { type KubeConfigClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*KubeConfig, error) Write(resource *KubeConfig, opts clients.WriteOpts) (*KubeConfig, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *kubeConfigClient) Register() error { return client.rc.Register() } +func (client *kubeConfigClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *kubeConfigClient) Read(namespace, name string, opts clients.ReadOpts) (*KubeConfig, error) { opts = opts.WithDefaults() diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 0d9abaf96..a5a26295e 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -222,6 +222,9 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { // we do not want to filter out "" which equals all namespaces + // the reason is because we will never create a watch on ""(all namespaces) because + // doing so means we watch all resources regardless of namespace. Our intent is to + // watch only certain namespaces. if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -233,6 +236,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name + c.kubeConfig.RegisterNamespace(namespace) /* Setup namespaced watch for KubeConfig */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -330,12 +334,17 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }) for _, ns := range missingNamespaces { + // TODO-JAKE clean this up, so that + // 1. we send a notification that there is an empty list + // 2. have a way to delete the namespace from the list as well. + // 3. any clean up of the resources to that are needed as well. // c.namespacesWatching.Delete(ns) kubeConfigChan <- kubeConfigListWithNamespace{list: KubeConfigList{}, namespace: ns} // kubeconfigsByNamespace.Delete(ns) } for _, namespace := range newNamespaces { + c.kubeConfig.RegisterNamespace(namespace) /* Setup namespaced watch for KubeConfig for new namespace */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) diff --git a/test/mocks/v1/another_mock_resource_client.sk.go b/test/mocks/v1/another_mock_resource_client.sk.go index f5cbd2bda..fb55ec0cb 100644 --- a/test/mocks/v1/another_mock_resource_client.sk.go +++ b/test/mocks/v1/another_mock_resource_client.sk.go @@ -19,6 +19,7 @@ type AnotherMockResourceWatcher interface { type AnotherMockResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*AnotherMockResource, error) Write(resource *AnotherMockResource, opts clients.WriteOpts) (*AnotherMockResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *anotherMockResourceClient) Register() error { return client.rc.Register() } +func (client *anotherMockResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *anotherMockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*AnotherMockResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/cluster_resource_client.sk.go b/test/mocks/v1/cluster_resource_client.sk.go index ec6c8f1ca..d6b9040ae 100644 --- a/test/mocks/v1/cluster_resource_client.sk.go +++ b/test/mocks/v1/cluster_resource_client.sk.go @@ -19,6 +19,7 @@ type ClusterResourceWatcher interface { type ClusterResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(name string, opts clients.ReadOpts) (*ClusterResource, error) Write(resource *ClusterResource, opts clients.WriteOpts) (*ClusterResource, error) Delete(name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *clusterResourceClient) Register() error { return client.rc.Register() } +func (client *clusterResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *clusterResourceClient) Read(name string, opts clients.ReadOpts) (*ClusterResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/fake_resource_client.sk.go b/test/mocks/v1/fake_resource_client.sk.go index 33132ca6d..73a06329d 100644 --- a/test/mocks/v1/fake_resource_client.sk.go +++ b/test/mocks/v1/fake_resource_client.sk.go @@ -19,6 +19,7 @@ type FakeResourceWatcher interface { type FakeResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*FakeResource, error) Write(resource *FakeResource, opts clients.WriteOpts) (*FakeResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *fakeResourceClient) Register() error { return client.rc.Register() } +func (client *fakeResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *fakeResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*FakeResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/mock_custom_type_client.sk.go b/test/mocks/v1/mock_custom_type_client.sk.go index 3b21d9ae0..372b1de03 100644 --- a/test/mocks/v1/mock_custom_type_client.sk.go +++ b/test/mocks/v1/mock_custom_type_client.sk.go @@ -19,6 +19,7 @@ type MockCustomTypeWatcher interface { type MockCustomTypeClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*MockCustomType, error) Write(resource *MockCustomType, opts clients.WriteOpts) (*MockCustomType, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *mockCustomTypeClient) Register() error { return client.rc.Register() } +func (client *mockCustomTypeClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *mockCustomTypeClient) Read(namespace, name string, opts clients.ReadOpts) (*MockCustomType, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/mock_resource_client.sk.go b/test/mocks/v1/mock_resource_client.sk.go index 2ef1fb113..abca019b7 100644 --- a/test/mocks/v1/mock_resource_client.sk.go +++ b/test/mocks/v1/mock_resource_client.sk.go @@ -19,6 +19,7 @@ type MockResourceWatcher interface { type MockResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) Write(resource *MockResource, opts clients.WriteOpts) (*MockResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *mockResourceClient) Register() error { return client.rc.Register() } +func (client *mockResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *mockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/simple_mock_resource_client.sk.go b/test/mocks/v1/simple_mock_resource_client.sk.go index 89bf97341..40babc6e8 100644 --- a/test/mocks/v1/simple_mock_resource_client.sk.go +++ b/test/mocks/v1/simple_mock_resource_client.sk.go @@ -19,6 +19,7 @@ type SimpleMockResourceWatcher interface { type SimpleMockResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*SimpleMockResource, error) Write(resource *SimpleMockResource, opts clients.WriteOpts) (*SimpleMockResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *simpleMockResourceClient) Register() error { return client.rc.Register() } +func (client *simpleMockResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *simpleMockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*SimpleMockResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 56d5e23f0..266bcd471 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -465,6 +465,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { // we do not want to filter out "" which equals all namespaces + // the reason is because we will never create a watch on ""(all namespaces) because + // doing so means we watch all resources regardless of namespace. Our intent is to + // watch only certain namespaces. if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -476,6 +479,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name + c.simpleMockResource.RegisterNamespace(namespace) /* Setup namespaced watch for SimpleMockResource */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -495,6 +499,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-simplemocks") }(namespace) + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -514,6 +519,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") }(namespace) + c.fakeResource.RegisterNamespace(namespace) /* Setup namespaced watch for FakeResource */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -533,6 +539,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") }(namespace) + c.anotherMockResource.RegisterNamespace(namespace) /* Setup namespaced watch for AnotherMockResource */ { anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -552,6 +559,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-anothermockresources") }(namespace) + c.mockCustomType.RegisterNamespace(namespace) /* Setup namespaced watch for MockCustomType */ { mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -571,6 +579,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") }(namespace) + c.pod.RegisterNamespace(namespace) /* Setup namespaced watch for Pod */ { pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -713,6 +722,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { + // TODO-JAKE clean this up, so that + // 1. we send a notification that there is an empty list + // 2. have a way to delete the namespace from the list as well. + // 3. any clean up of the resources to that are needed as well. // c.namespacesWatching.Delete(ns) simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: SimpleMockResourceList{}, namespace: ns} // simplemocksByNamespace.Delete(ns) @@ -729,6 +742,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } for _, namespace := range newNamespaces { + c.simpleMockResource.RegisterNamespace(namespace) /* Setup namespaced watch for SimpleMockResource for new namespace */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -749,6 +763,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") }(namespace) + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -769,6 +784,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") }(namespace) + c.fakeResource.RegisterNamespace(namespace) /* Setup namespaced watch for FakeResource for new namespace */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -789,6 +805,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") }(namespace) + c.anotherMockResource.RegisterNamespace(namespace) /* Setup namespaced watch for AnotherMockResource for new namespace */ { anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -809,6 +826,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") }(namespace) + c.mockCustomType.RegisterNamespace(namespace) /* Setup namespaced watch for MockCustomType for new namespace */ { mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -829,6 +847,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") }(namespace) + c.pod.RegisterNamespace(namespace) /* Setup namespaced watch for Pod for new namespace */ { pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) diff --git a/test/mocks/v1alpha1/fake_resource_client.sk.go b/test/mocks/v1alpha1/fake_resource_client.sk.go index 4d6e3e461..a7aadcdc4 100644 --- a/test/mocks/v1alpha1/fake_resource_client.sk.go +++ b/test/mocks/v1alpha1/fake_resource_client.sk.go @@ -19,6 +19,7 @@ type FakeResourceWatcher interface { type FakeResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*FakeResource, error) Write(resource *FakeResource, opts clients.WriteOpts) (*FakeResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *fakeResourceClient) Register() error { return client.rc.Register() } +func (client *fakeResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *fakeResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*FakeResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 727b3095b..dc4a5a48d 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -338,7 +338,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // 1. we send a notification that there is an empty list // 2. have a way to delete the namespace from the list as well. // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} // mocksByNamespace.Delete(ns) diff --git a/test/mocks/v2alpha1/frequently_changing_annotations_resource_client.sk.go b/test/mocks/v2alpha1/frequently_changing_annotations_resource_client.sk.go index 70c60a40d..0aa490399 100644 --- a/test/mocks/v2alpha1/frequently_changing_annotations_resource_client.sk.go +++ b/test/mocks/v2alpha1/frequently_changing_annotations_resource_client.sk.go @@ -19,6 +19,7 @@ type FrequentlyChangingAnnotationsResourceWatcher interface { type FrequentlyChangingAnnotationsResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*FrequentlyChangingAnnotationsResource, error) Write(resource *FrequentlyChangingAnnotationsResource, opts clients.WriteOpts) (*FrequentlyChangingAnnotationsResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *frequentlyChangingAnnotationsResourceClient) Register() error { return client.rc.Register() } +func (client *frequentlyChangingAnnotationsResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *frequentlyChangingAnnotationsResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*FrequentlyChangingAnnotationsResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v2alpha1/mock_resource_client.sk.go b/test/mocks/v2alpha1/mock_resource_client.sk.go index 05b2cac3e..993b4ba26 100644 --- a/test/mocks/v2alpha1/mock_resource_client.sk.go +++ b/test/mocks/v2alpha1/mock_resource_client.sk.go @@ -19,6 +19,7 @@ type MockResourceWatcher interface { type MockResourceClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) Write(resource *MockResource, opts clients.WriteOpts) (*MockResource, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *mockResourceClient) Register() error { return client.rc.Register() } +func (client *mockResourceClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *mockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (*MockResource, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v2alpha1/mock_resource_client_test.go b/test/mocks/v2alpha1/mock_resource_client_test.go index c0f1fd56e..eba59bcc5 100644 --- a/test/mocks/v2alpha1/mock_resource_client_test.go +++ b/test/mocks/v2alpha1/mock_resource_client_test.go @@ -1,6 +1,5 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v2alpha1 diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index b56ec15e7..d4c002be5 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -316,6 +316,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO filterNamespaces := resources.ResourceNamespaceList{} for _, ns := range watchNamespaces { // we do not want to filter out "" which equals all namespaces + // the reason is because we will never create a watch on ""(all namespaces) because + // doing so means we watch all resources regardless of namespace. Our intent is to + // watch only certain namespaces. if ns != "" { filterNamespaces = append(filterNamespaces, resources.ResourceNamespace{Name: ns}) } @@ -327,6 +330,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO // non watched namespaces that are labeled for _, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -346,6 +350,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") }(namespace) + c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ { fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -365,6 +370,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-fcars") }(namespace) + c.fakeResource.RegisterNamespace(namespace) /* Setup namespaced watch for FakeResource */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) @@ -480,6 +486,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { + // TODO-JAKE clean this up, so that + // 1. we send a notification that there is an empty list + // 2. have a way to delete the namespace from the list as well. + // 3. any clean up of the resources to that are needed as well. // c.namespacesWatching.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} // mocksByNamespace.Delete(ns) @@ -490,6 +500,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } for _, namespace := range newNamespaces { + c.mockResource.RegisterNamespace(namespace) /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -510,6 +521,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") }(namespace) + c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ { fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) @@ -530,6 +542,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") }(namespace) + c.fakeResource.RegisterNamespace(namespace) /* Setup namespaced watch for FakeResource for new namespace */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) From cb7159b0421fc798022b5576806069e702543793 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 08:47:51 -0500 Subject: [PATCH 51/98] add rc template --- .../codegen/templates/resource_client_template.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/code-generator/codegen/templates/resource_client_template.go b/pkg/code-generator/codegen/templates/resource_client_template.go index d285f928c..d92fdd718 100644 --- a/pkg/code-generator/codegen/templates/resource_client_template.go +++ b/pkg/code-generator/codegen/templates/resource_client_template.go @@ -28,6 +28,7 @@ type {{ .Name }}Watcher interface { type {{ .Name }}Client interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error {{- if .ClusterScoped }} Read(name string, opts clients.ReadOpts) (*{{ .Name }}, error) {{- else }} @@ -77,6 +78,10 @@ func (client *{{ lower_camel .Name }}Client) Register() error { return client.rc.Register() } +func (client *{{ lower_camel .Name }}Client) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + {{ if .ClusterScoped }} func (client *{{ lower_camel .Name }}Client) Read(name string, opts clients.ReadOpts) (*{{ .Name }}, error) { {{- else }} From d05edecdb35266183a672e0926a46839cdc42605 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 10:28:51 -0500 Subject: [PATCH 52/98] add in once functionality to register calls --- Makefile | 3 ++ pkg/api/v1/clients/kube/cache/cache.go | 16 ++++++++-- .../clients/kube/resource_client_factory.go | 31 ++++++++++++------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 26b142f26..89e0a4052 100644 --- a/Makefile +++ b/Makefile @@ -96,6 +96,9 @@ $(OUTPUT_DIR)/.clientset: $(GENERATED_PROTO_FILES) $(SOURCES) .PHONY: generated-code generated-code: $(OUTPUT_DIR)/.generated-code update-licenses +.PHONY: generate-all +generate-all: generated-code + SUBDIRS:=pkg test $(OUTPUT_DIR)/.generated-code: mkdir -p ${OUTPUT_DIR} diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index a54cfb066..4cbf327b8 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -5,6 +5,7 @@ import ( "sync" "time" + "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/stringutils" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/controller" "github.com/solo-io/solo-kit/pkg/errors" @@ -91,6 +92,9 @@ type kubeCoreCaches struct { // informers are the kube resources that provide events informers []cache.SharedIndexInformer + // registerNamespaceLock is a map string(namespace) -> sync.Once. Is used to register namespaces only once. + registerNamespaceLock sync.Map + cacheUpdatedWatchers []chan struct{} cacheUpdatedWatchersMutex sync.Mutex } @@ -277,8 +281,16 @@ func (k *kubeCoreCaches) addNewNamespace(namespace string) []cache.SharedIndexIn // RegisterNewNamespaceCache will create the cache informers for each resource type // this will add the informer to the kube controller so that events can be watched. func (k *kubeCoreCaches) RegisterNewNamespaceCache(namespace string) error { - informers := k.addNewNamespace(namespace) - return k.kubeController.AddNewListOfInformers(informers) + once, _ := k.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) + onceFunc := once.(*sync.Once) + // TODO-JAKE should this panic? + onceFunc.Do(func() { + informers := k.addNewNamespace(namespace) + if err := k.kubeController.AddNewListOfInformers(informers); err != nil { + contextutils.LoggerFrom(k.ctx).Panicf("failed to add new list of informers to kube controller: %v", err) + } + }) + return nil } // Deprecated: Use NamespacedPodLister instead diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 794001b75..52113bee7 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -125,6 +125,9 @@ type ResourceClientSharedInformerFactory struct { // kubeController is the controller used to watch for events on the informers. It can be used to add new informers too. kubeController *controller.Controller + // registerNamespaceLock is a map of string(namespace) -> sync.Once. It is used to register a new namespace. + registerNamespaceLock sync.Map + // Mutexes lock sync.Mutex cacheUpdatedWatchersMutex sync.Mutex @@ -294,17 +297,23 @@ func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace str if !f.IsRunning() { contextutils.LoggerFrom(f.ctx).Panicf("failed to register the new namespace [%v] to the resource client [%v]", namespace, reflect.TypeOf(rc)) } - ctx := f.ctx - if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { - ctx = ctxWithTags - } - informer, err := f.addNewNamespaceToRegistry(ctx, namespace, rc) - if err != nil { - return err - } - if err := f.kubeController.AddNewInformer(informer); err != nil { - return err - } + // we should only register a namespace once and only once + once, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) + once.(*sync.Once).Do(func() { + ctx := f.ctx + if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { + ctx = ctxWithTags + } + informer, err := f.addNewNamespaceToRegistry(ctx, namespace, rc) + if err != nil { + // TODO-JAKE not sure if we want to panic here or to return an error. But I feel like panics are ok. + contextutils.LoggerFrom(ctx).Panicf("failed to add new namespace to registry: %v", err) + } + if err := f.kubeController.AddNewInformer(informer); err != nil { + contextutils.LoggerFrom(ctx).Panicf("failed to add new informer to kube controller: %v", err) + } + + }) return nil } From 0e2db708f476d0a2a58c15cf0d0dcaff7a710391 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 10:39:35 -0500 Subject: [PATCH 53/98] rcf must start() if we are registering namespaces to it --- pkg/api/v1/clients/kube/resource_client_factory.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 52113bee7..0f7331b8f 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -294,9 +294,7 @@ func (f *ResourceClientSharedInformerFactory) Start() { // RegisterNewNamespace is used when the resource client is running. This will add a new namespace to the // kube controller so that events can be received. func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace string, rc *ResourceClient) error { - if !f.IsRunning() { - contextutils.LoggerFrom(f.ctx).Panicf("failed to register the new namespace [%v] to the resource client [%v]", namespace, reflect.TypeOf(rc)) - } + f.Start() // we should only register a namespace once and only once once, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) once.(*sync.Once).Do(func() { From 2ad539961066ca4e7e594b73bb8689506b9c25da Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 11:10:48 -0500 Subject: [PATCH 54/98] possible race condition fix --- pkg/api/v1/clients/kube/resource_client_factory.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 0f7331b8f..311021ac5 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -178,9 +178,6 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType // NOTE: Currently we cannot share informers between resource clients, because the listWatch functions are configured // with the client's specific token. Hence, we must enforce a one-to-one relationship between informers and clients. func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error { - f.lock.Lock() - defer f.lock.Unlock() - ctx := f.ctx if f.started { contextutils.LoggerFrom(ctx).Panic("can't register informer after factory has started. This may change in the future.") @@ -206,6 +203,9 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error // addNewNamespaceToRegistry will create a watch for the resource client type and namespace func (f *ResourceClientSharedInformerFactory) addNewNamespaceToRegistry(ctx context.Context, ns string, rc *ResourceClient) (cache.SharedIndexInformer, error) { + f.lock.Lock() + defer f.lock.Unlock() + nsCtx := ctx resourceType := reflect.TypeOf(rc.crd.Version.Type) @@ -294,6 +294,8 @@ func (f *ResourceClientSharedInformerFactory) Start() { // RegisterNewNamespace is used when the resource client is running. This will add a new namespace to the // kube controller so that events can be received. func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace string, rc *ResourceClient) error { + // when does the start function get called? + // the once function below is adding a namespace once and only once f.Start() // we should only register a namespace once and only once once, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) From e9c3539f199cd3770e23007fd2f4e91c2a37877a Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 11:37:32 -0500 Subject: [PATCH 55/98] fix registering issue --- .../clients/kube/resource_client_factory.go | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 311021ac5..9ef7bb927 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -180,24 +180,27 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error { ctx := f.ctx if f.started { - contextutils.LoggerFrom(ctx).Panic("can't register informer after factory has started. This may change in the future.") - } - - if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { - ctx = ctxWithTags - } + for _, ns := range rc.namespaceWhitelist { + if err := f.RegisterNewNamespace(ns, rc); err != nil { + return err + } + } + } else { + if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { + ctx = ctxWithTags + } - // Create a shared informer for each of the given namespaces. - // NOTE: We do not distinguish between the value "" (all namespaces) and a regular namespace here. - // TODO-JAKE there is a special rule, when the namespaces is [""], we need an option - // to be able to change the Informer so that it does not watch on "", but only those namespaces - // that we want to watch. This only happens if the client(user) is setting the namespaceLabelSelectors field in the API. - for _, ns := range rc.namespaceWhitelist { - if _, err := f.addNewNamespaceToRegistry(ctx, ns, rc); err != nil { - return err + // Create a shared informer for each of the given namespaces. + // NOTE: We do not distinguish between the value "" (all namespaces) and a regular namespace here. + // TODO-JAKE there is a special rule, when the namespaces is [""], we need an option + // to be able to change the Informer so that it does not watch on "", but only those namespaces + // that we want to watch. This only happens if the client(user) is setting the namespaceLabelSelectors field in the API. + for _, ns := range rc.namespaceWhitelist { + if _, err := f.addNewNamespaceToRegistry(ctx, ns, rc); err != nil { + return err + } } } - return nil } From 7af3b64f24a30a63339141aa5bceb4c1b4174f52 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 11:56:09 -0500 Subject: [PATCH 56/98] fix reconciler --- pkg/api/v1/reconcile/reconciler_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/api/v1/reconcile/reconciler_test.go b/pkg/api/v1/reconcile/reconciler_test.go index f589b5de5..a40c7c4f0 100644 --- a/pkg/api/v1/reconcile/reconciler_test.go +++ b/pkg/api/v1/reconcile/reconciler_test.go @@ -202,6 +202,8 @@ var _ = Describe("Reconciler", func() { }) }) +var _ clients.ResourceClient = &testResourceClient{} + type testResourceClient struct { errorOnRead bool errorOnWrite bool @@ -220,6 +222,10 @@ func (c *testResourceClient) Register() error { panic("implement me") } +func (c *testResourceClient) RegisterNamespace(namespace string) error { + panic("implement me") +} + func (c *testResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { if c.errorOnRead { return nil, errors.Errorf("read should not have been called") From 7b1e280a370886d01cc2e410d11014700c3390df Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 12:19:36 -0500 Subject: [PATCH 57/98] change test so that the kube cache does not panic --- pkg/api/v1/clients/kube/resource_client_factory_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client_factory_test.go b/pkg/api/v1/clients/kube/resource_client_factory_test.go index de6e25807..0624c310b 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory_test.go +++ b/pkg/api/v1/clients/kube/resource_client_factory_test.go @@ -75,12 +75,12 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { Expect(len(kubeCache.Informers())).To(BeEquivalentTo(1)) }) - It("panics when attempting of register a client with a running factory", func() { + It("should not panic when attempting of register a client with a running factory", func() { // Start without registering clients, just to set the "started" flag kubeCache.Start() Expect(kubeCache.IsRunning()).To(BeTrue()) - Expect(func() { _ = kubeCache.Register(client1) }).To(Panic()) + Expect(func() { _ = kubeCache.Register(client1) }).ToNot(Panic()) }) It("can register a new namespace even when the factory is running", func() { kubeCache.Start() From b334e28244929471bb40ce4873ff5143a40b88d7 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 12:37:40 -0500 Subject: [PATCH 58/98] update the client interface --- pkg/api/v1/clients/mocks/client_interface.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/api/v1/clients/mocks/client_interface.go b/pkg/api/v1/clients/mocks/client_interface.go index 132839c31..bb25cf330 100644 --- a/pkg/api/v1/clients/mocks/client_interface.go +++ b/pkg/api/v1/clients/mocks/client_interface.go @@ -116,6 +116,20 @@ func (mr *MockResourceClientMockRecorder) Register() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Register", reflect.TypeOf((*MockResourceClient)(nil).Register)) } +// RegisterNamespace mocks base method +func (m *MockResourceClient) RegisterNamespace(namespace string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RegisterNamespace", namespace) + ret0, _ := ret[0].(error) + return ret0 +} + +// RegisterNamespace indicates an expected call of RegisterNamespace +func (mr *MockResourceClientMockRecorder) RegisterNamespace(namespace interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterNamespace", reflect.TypeOf((*MockResourceClient)(nil).RegisterNamespace), namespace) +} + // Read mocks base method func (m *MockResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { m.ctrl.T.Helper() From f3597e0c5cae7be1b567a3e51cfa4bd026a3b9e1 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 14:13:36 -0500 Subject: [PATCH 59/98] better mutex control --- .../clients/kube/resource_client_factory.go | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 9ef7bb927..4d8f0d9ea 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -99,6 +99,9 @@ func NewKubeSharedCacheForConfig(ctx context.Context, cluster string, restConfig // and, when started, creates a kubernetes controller that distributes notifications for changes to the watches that // have been added to the factory. // All direct operations on the ResourceClientSharedInformerFactory are synchronized. +// Note it might be best to use this as a Singleton instead of a Structure +// if a Singleton suggestion is proposed, then Register will no long be needed, +// and we can just use Register New Namespace. Also this would clear up some mutexes, that are being used. type ResourceClientSharedInformerFactory struct { // Contains all the informers managed by this factory registry *informerRegistry @@ -128,8 +131,10 @@ type ResourceClientSharedInformerFactory struct { // registerNamespaceLock is a map of string(namespace) -> sync.Once. It is used to register a new namespace. registerNamespaceLock sync.Map - // Mutexes - lock sync.Mutex + // registryLock is used when adding or getting information from the registry + registryLock sync.Mutex + // startingLock is used when checking isRunning or to lock starting of the SharedInformer + startingLock sync.Mutex cacheUpdatedWatchersMutex sync.Mutex } @@ -178,14 +183,19 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType // NOTE: Currently we cannot share informers between resource clients, because the listWatch functions are configured // with the client's specific token. Hence, we must enforce a one-to-one relationship between informers and clients. func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error { + // because we do not know that we have started yet or not, we need to make a lock + // once we know, we can proceed + f.startingLock.Lock() ctx := f.ctx if f.started { + f.startingLock.Unlock() for _, ns := range rc.namespaceWhitelist { if err := f.RegisterNewNamespace(ns, rc); err != nil { return err } } } else { + defer f.startingLock.Unlock() if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { ctx = ctxWithTags } @@ -196,6 +206,8 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error // to be able to change the Informer so that it does not watch on "", but only those namespaces // that we want to watch. This only happens if the client(user) is setting the namespaceLabelSelectors field in the API. for _, ns := range rc.namespaceWhitelist { + // we want to make sure that we have registered this namespace + f.registerNamespaceLock.LoadOrStore(ns, &sync.Once{}) if _, err := f.addNewNamespaceToRegistry(ctx, ns, rc); err != nil { return err } @@ -206,8 +218,8 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error // addNewNamespaceToRegistry will create a watch for the resource client type and namespace func (f *ResourceClientSharedInformerFactory) addNewNamespaceToRegistry(ctx context.Context, ns string, rc *ResourceClient) (cache.SharedIndexInformer, error) { - f.lock.Lock() - defer f.lock.Unlock() + f.registryLock.Lock() + defer f.registryLock.Unlock() nsCtx := ctx resourceType := reflect.TypeOf(rc.crd.Version.Type) @@ -253,9 +265,14 @@ var cacheSyncTimeout = func() time.Duration { // Starts all informers in the factory's registry (if they have not yet been started) and configures the factory to call // the updateCallback function whenever any of the resources associated with the informers changes. func (f *ResourceClientSharedInformerFactory) Start() { + // if starting and managing the locks becomes to much a problem, we can start the factory + // when the factory is instantiated. That way we will not have multiple processes trying to Start() this structure + // which should be a singleton. // Guarantees that the factory will be started at most once f.factoryStarter.Do(func() { + f.startingLock.Lock() + defer f.startingLock.Unlock() ctx := f.ctx // Collect all registered informers @@ -297,8 +314,9 @@ func (f *ResourceClientSharedInformerFactory) Start() { // RegisterNewNamespace is used when the resource client is running. This will add a new namespace to the // kube controller so that events can be received. func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace string, rc *ResourceClient) error { - // when does the start function get called? - // the once function below is adding a namespace once and only once + // because this is an exposed function, Register New Namespace could be called at any time + // in that event, we want to make sure that the cache has started, the reason is because + // we have to initiallize the default namespaces as well as this new namespace f.Start() // we should only register a namespace once and only once once, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) @@ -321,8 +339,8 @@ func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace str } func (f *ResourceClientSharedInformerFactory) GetLister(namespace string, obj runtime.Object) (ResourceLister, error) { - f.lock.Lock() - defer f.lock.Unlock() + f.registryLock.Lock() + defer f.registryLock.Unlock() // If the factory (and hence the informers) have not been started, the list operations will be meaningless. // Will not happen in our current use of this, but since this method is public it's worth having this check. From 698815345713b58bd07e8f1ed746a584b70d2d26 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 14:50:21 -0500 Subject: [PATCH 60/98] fix race condition in test --- pkg/api/v1/clients/kube/resource_client_factory_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/api/v1/clients/kube/resource_client_factory_test.go b/pkg/api/v1/clients/kube/resource_client_factory_test.go index 0624c310b..9a84029c5 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory_test.go +++ b/pkg/api/v1/clients/kube/resource_client_factory_test.go @@ -138,6 +138,9 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { var watch <-chan solov1.Resource BeforeEach(func() { + // there is a race condition when a go routine uses the watch + // before the entire test finishes + time.Sleep(50 * time.Millisecond) watch = kubeCache.AddWatch(10) }) From 0641785085672308e068703363ee7fb69299adef Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 14:51:24 -0500 Subject: [PATCH 61/98] add comment --- test/mocks/v1/testing.solo.io_suite_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/test/mocks/v1/testing.solo.io_suite_test.go b/test/mocks/v1/testing.solo.io_suite_test.go index 3c5c8674c..2ce02fcdd 100644 --- a/test/mocks/v1/testing.solo.io_suite_test.go +++ b/test/mocks/v1/testing.solo.io_suite_test.go @@ -54,6 +54,7 @@ var ( testutils.ErrorNotOccuredOrNotFound(err) err = clientset.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, "simplemocks.testing.solo.io", metav1.DeleteOptions{}) testutils.ErrorNotOccuredOrNotFound(err) + // TOOD-JAKE known race condition occurs here https://storage.googleapis.com/solo-public-build-logs/log-286333d3-2840-4323-b508-7e351539a9f9.txt Expect(lock.ReleaseLock()).NotTo(HaveOccurred()) }) From 2f06f41e86712b80f556ab3caca0214264c17feb Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 15:06:50 -0500 Subject: [PATCH 62/98] allow new namespaces in the kube resouce client --- pkg/api/v1/clients/kube/resource_client.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client.go b/pkg/api/v1/clients/kube/resource_client.go index 8d8d145e9..3985e09ff 100644 --- a/pkg/api/v1/clients/kube/resource_client.go +++ b/pkg/api/v1/clients/kube/resource_client.go @@ -152,7 +152,12 @@ func (rc *ResourceClient) Register() error { } func (rc *ResourceClient) RegisterNamespace(namespace string) error { - return rc.sharedCache.RegisterNewNamespace(namespace, rc) + err := rc.sharedCache.RegisterNewNamespace(namespace, rc) + if err != nil { + return err + } + rc.namespaceWhitelist = append(rc.namespaceWhitelist, namespace) + return nil } func (rc *ResourceClient) Read(namespace, name string, opts clients.ReadOpts) (resources.Resource, error) { @@ -340,9 +345,6 @@ func (rc *ResourceClient) List(namespace string, opts clients.ListOpts) (resourc } func (rc *ResourceClient) Watch(namespace string, opts clients.WatchOpts) (<-chan resources.ResourceList, <-chan error, error) { - - // JAKE-TODO we will need to update the validation list here as well. - // JAKE-TODO ensure that this is done as well for the other resource client too. if err := rc.validateNamespace(namespace); err != nil { return nil, nil, err } From 57873d6dd4cbfb18d159ab24b22bd3e231c80067 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 15:30:37 -0500 Subject: [PATCH 63/98] clean up --- pkg/api/v1/clients/kube/resource_client.go | 6 ++++++ .../codegen/templates/snapshot_emitter_template.go | 6 ------ .../v1/kubeconfigs_snapshot_emitter.sk.go | 6 ------ test/mocks/v1/testing_snapshot_emitter.sk.go | 11 ----------- test/mocks/v1alpha1/testing_snapshot_emitter.sk.go | 6 ------ test/mocks/v2alpha1/testing_snapshot_emitter.sk.go | 8 -------- 6 files changed, 6 insertions(+), 37 deletions(-) diff --git a/pkg/api/v1/clients/kube/resource_client.go b/pkg/api/v1/clients/kube/resource_client.go index 3985e09ff..64596b3d3 100644 --- a/pkg/api/v1/clients/kube/resource_client.go +++ b/pkg/api/v1/clients/kube/resource_client.go @@ -5,6 +5,7 @@ import ( "reflect" "sort" "strings" + "sync" "time" "github.com/solo-io/solo-kit/pkg/utils/specutils" @@ -107,6 +108,7 @@ type ResourceClient struct { namespaceWhitelist []string // Will contain at least metaV1.NamespaceAll ("") resyncPeriod time.Duration resourceStatusUnmarshaler resources.StatusUnmarshaler + namespaceLock sync.Mutex } func NewResourceClient( @@ -156,7 +158,9 @@ func (rc *ResourceClient) RegisterNamespace(namespace string) error { if err != nil { return err } + rc.namespaceLock.Lock() rc.namespaceWhitelist = append(rc.namespaceWhitelist, namespace) + rc.namespaceLock.Unlock() return nil } @@ -472,6 +476,8 @@ func (rc *ResourceClient) convertCrdToResource(resourceCrd *v1.Resource) (resour // Check whether the given namespace is in the whitelist or we allow all namespaces func (rc *ResourceClient) validateNamespace(namespace string) error { + rc.namespaceLock.Lock() + defer rc.namespaceLock.Unlock() if !stringutils.ContainsAny([]string{namespace, metav1.NamespaceAll}, rc.namespaceWhitelist) { return errors.Errorf("this client was not configured to access resources in the [%v] namespace. "+ "Allowed namespaces are %v", namespace, rc.namespaceWhitelist) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index d4f915048..befa8d633 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -405,15 +405,9 @@ type {{ lower_camel .Name }}ListWithNamespace struct { }) for _, ns := range missingNamespaces { - // TODO-JAKE clean this up, so that - // 1. we send a notification that there is an empty list - // 2. have a way to delete the namespace from the list as well. - // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) {{- range .Resources}} {{- if (not .ClusterScoped) }} {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list: {{ .ImportPrefix }}{{ .Name }}List{}, namespace: ns} - // {{ lower_camel .PluralName }}ByNamespace.Delete(ns) {{- end }} {{- end }} } diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index a5a26295e..13f452a98 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -334,13 +334,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa }) for _, ns := range missingNamespaces { - // TODO-JAKE clean this up, so that - // 1. we send a notification that there is an empty list - // 2. have a way to delete the namespace from the list as well. - // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) kubeConfigChan <- kubeConfigListWithNamespace{list: KubeConfigList{}, namespace: ns} - // kubeconfigsByNamespace.Delete(ns) } for _, namespace := range newNamespaces { diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 266bcd471..f92eee71f 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -722,23 +722,12 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - // TODO-JAKE clean this up, so that - // 1. we send a notification that there is an empty list - // 2. have a way to delete the namespace from the list as well. - // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) simpleMockResourceChan <- simpleMockResourceListWithNamespace{list: SimpleMockResourceList{}, namespace: ns} - // simplemocksByNamespace.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} - // mocksByNamespace.Delete(ns) fakeResourceChan <- fakeResourceListWithNamespace{list: FakeResourceList{}, namespace: ns} - // fakesByNamespace.Delete(ns) anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: AnotherMockResourceList{}, namespace: ns} - // anothermockresourcesByNamespace.Delete(ns) mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: MockCustomTypeList{}, namespace: ns} - // mctsByNamespace.Delete(ns) podChan <- podListWithNamespace{list: github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{}, namespace: ns} - // podsByNamespace.Delete(ns) } for _, namespace := range newNamespaces { diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index dc4a5a48d..491b152e5 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -334,13 +334,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - // TODO-JAKE clean this up, so that - // 1. we send a notification that there is an empty list - // 2. have a way to delete the namespace from the list as well. - // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} - // mocksByNamespace.Delete(ns) } for _, namespace := range newNamespaces { diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index d4c002be5..53b970e1a 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -486,17 +486,9 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO }) for _, ns := range missingNamespaces { - // TODO-JAKE clean this up, so that - // 1. we send a notification that there is an empty list - // 2. have a way to delete the namespace from the list as well. - // 3. any clean up of the resources to that are needed as well. - // c.namespacesWatching.Delete(ns) mockResourceChan <- mockResourceListWithNamespace{list: MockResourceList{}, namespace: ns} - // mocksByNamespace.Delete(ns) frequentlyChangingAnnotationsResourceChan <- frequentlyChangingAnnotationsResourceListWithNamespace{list: FrequentlyChangingAnnotationsResourceList{}, namespace: ns} - // fcarsByNamespace.Delete(ns) fakeResourceChan <- fakeResourceListWithNamespace{list: testing_solo_io.FakeResourceList{}, namespace: ns} - // fakesByNamespace.Delete(ns) } for _, namespace := range newNamespaces { From b9cbb9167e5b6a0e0836326524239b982e62524c Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 16:09:13 -0500 Subject: [PATCH 64/98] added in a test for when namespaces are deleted --- pkg/api/v1/clients/kube/cache/cache.go | 10 ++------- pkg/api/v1/clients/kube/cache/cache_test.go | 21 +++++++++++++++++++ .../clients/kube/resource_client_factory.go | 1 - 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index 4cbf327b8..3d415e5c2 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -170,14 +170,8 @@ func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interfac k.addNewNamespace(nsToWatch) } - // TODO allows add in the namespaceLister, all the time, or make it an option. - // TODO-JAKE the namespace lister is used for something in Gloo, we need to make sure that it is going to - // dealt with. It might have side affects that I am unaware of, so please go and investigate that. - - // TODO-JAKE please look into the Reconciler as well, those might need to updated too. I am not sure though, as they work differently. - if len(namesapcesToWatch) == 1 && namesapcesToWatch[0] == metav1.NamespaceAll { - k.addNamespaceLister() - } + // since we now allow namespaces to be registered dynamically we will always create the namespace lister + k.addNamespaceLister() k.kubeController = controller.NewController("kube-plugin-controller", controller.NewLockingSyncHandler(k.updatedOccured), k.informers..., diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index f01f573b5..9e4c90013 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -189,6 +189,27 @@ var _ = Describe("kube core cache tests", func() { validateNamespaceResource(initialNs) validateNamespaceResource(registeredNs) }) + + It("should be able to register a new namespace after the namespace was previously registered then deleted", func() { + createNamespaceAndResource(registeredNs) + + err := cache.RegisterNewNamespaceCache(registeredNs) + Expect(err).NotTo(HaveOccurred()) + + validateNamespaceResource(initialNs) + validateNamespaceResource(registeredNs) + + client.CoreV1().Namespaces().Delete(ctx, registeredNs, metav1.DeleteOptions{}) + // let the namespace be deleted + Eventually(func() bool { + _, err := client.CoreV1().Namespaces().Get(ctx, registeredNs, metav1.GetOptions{}) + return err != nil + }, 10*time.Second, time.Second).Should(BeTrue()) + createNamespaceAndResource(registeredNs) + // have to ensure that the configmap is created in the new namespace + time.Sleep(50 * time.Millisecond) + validateNamespaceResource(registeredNs) + }) }) }) }) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 4d8f0d9ea..6a0317345 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -173,7 +173,6 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType }, objType, resyncPeriod, - // TODO-JAKE make a note here, that is where we would want to look at creating namespace indexes if needed. cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, ) } From 8bb2c038bf2657bbbac4fb1effe3944298027f31 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 16:22:41 -0500 Subject: [PATCH 65/98] fix cache test --- pkg/api/v1/clients/kube/cache/cache_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index 9e4c90013..d19e6e93f 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -144,7 +144,6 @@ var _ = Describe("kube core cache tests", func() { }) It("can list resources for all listers", func() { - Expect(cache.NamespaceLister()).To(BeNil()) validateNamespaceResource(testns) validateNamespaceResource(testns2) }) From c0740977b391edfba28a88c8e1417eaeb6dbb006 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Tue, 13 Sep 2022 16:23:47 -0500 Subject: [PATCH 66/98] fix cache test again --- pkg/api/v1/clients/kube/cache/cache_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index d19e6e93f..96504f778 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -109,7 +109,6 @@ var _ = Describe("kube core cache tests", func() { }) It("can list resources for all listers", func() { - Expect(cache.NamespaceLister()).To(BeNil()) _, err := cache.NamespacedPodLister("default").List(selectors) Expect(err).NotTo(HaveOccurred()) _, err = cache.NamespacedConfigMapLister("default").List(selectors) From f3aa04a1da2d69a898ff8cd2cc4ae5c678d9573b Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 14 Sep 2022 16:07:46 +0000 Subject: [PATCH 67/98] Adding changelog file to new location --- changelog/v0.30.4/namespace-selectors.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 changelog/v0.30.4/namespace-selectors.yaml diff --git a/changelog/v0.30.4/namespace-selectors.yaml b/changelog/v0.30.4/namespace-selectors.yaml new file mode 100644 index 000000000..9e8920465 --- /dev/null +++ b/changelog/v0.30.4/namespace-selectors.yaml @@ -0,0 +1,9 @@ +changelog: + - type: NEW_FEATURE + issueLink: https://github.com/solo-io/gloo/issues/5868 + description: | + Added the ability to watch namespaces given be Expression Selectors in the Watch Opts + Watched Namespaces work as normally. When Expression Selectors the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set. \ No newline at end of file From ea8b754e6566ae745d33d1b934c0084e13bf9ca8 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 14 Sep 2022 16:07:46 +0000 Subject: [PATCH 68/98] Deleting changelog file from old location --- changelog/v0.30.3/namespace-selectors.yaml | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 changelog/v0.30.3/namespace-selectors.yaml diff --git a/changelog/v0.30.3/namespace-selectors.yaml b/changelog/v0.30.3/namespace-selectors.yaml deleted file mode 100644 index 9e8920465..000000000 --- a/changelog/v0.30.3/namespace-selectors.yaml +++ /dev/null @@ -1,9 +0,0 @@ -changelog: - - type: NEW_FEATURE - issueLink: https://github.com/solo-io/gloo/issues/5868 - description: | - Added the ability to watch namespaces given be Expression Selectors in the Watch Opts - Watched Namespaces work as normally. When Expression Selectors the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set. \ No newline at end of file From 39072d56549bd82b7228ccb64e5a8ad0ccf1c3ab Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 15 Sep 2022 09:47:43 -0500 Subject: [PATCH 69/98] fix merge conflict issue --- pkg/api/external/kubernetes/namespace/resource_client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/api/external/kubernetes/namespace/resource_client.go b/pkg/api/external/kubernetes/namespace/resource_client.go index afc748a43..e3c33945d 100644 --- a/pkg/api/external/kubernetes/namespace/resource_client.go +++ b/pkg/api/external/kubernetes/namespace/resource_client.go @@ -17,7 +17,6 @@ import ( kubev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" ) From 50578c7478c422f920fd589277aae547431a44f8 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Fri, 16 Sep 2022 15:15:21 -0500 Subject: [PATCH 70/98] finish todos, add logs for registered namespaces, fixed issue with Resource Cache Factory not being able to have multiple namespace, clients --- pkg/api/v1/clients/kube/cache/cache.go | 18 +-- .../clients/kube/resource_client_factory.go | 30 +++-- .../kube/resource_client_factory_test.go | 20 +++- .../templates/snapshot_emitter_template.go | 29 ++++- .../v1/kubeconfigs_snapshot_emitter.sk.go | 29 ++++- test/mocks/v1/testing.solo.io_suite_test.go | 2 +- test/mocks/v1/testing_snapshot_emitter.sk.go | 104 +++++++++++++----- .../v1alpha1/testing_snapshot_emitter.sk.go | 29 ++++- .../v2alpha1/testing_snapshot_emitter.sk.go | 59 +++++++--- 9 files changed, 242 insertions(+), 78 deletions(-) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index 3d415e5c2..e36d44ed2 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -5,7 +5,6 @@ import ( "sync" "time" - "github.com/solo-io/go-utils/contextutils" "github.com/solo-io/go-utils/stringutils" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/controller" "github.com/solo-io/solo-kit/pkg/errors" @@ -24,6 +23,12 @@ import ( "k8s.io/client-go/tools/cache" ) +// onceAndSent is used to capture errs made by once functions +type onceAndSent struct { + Err error + Once *sync.Once +} + type ServiceLister interface { // List lists all Services in the indexer. List(selector labels.Selector) (ret []*v1.Service, err error) @@ -275,16 +280,15 @@ func (k *kubeCoreCaches) addNewNamespace(namespace string) []cache.SharedIndexIn // RegisterNewNamespaceCache will create the cache informers for each resource type // this will add the informer to the kube controller so that events can be watched. func (k *kubeCoreCaches) RegisterNewNamespaceCache(namespace string) error { - once, _ := k.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) - onceFunc := once.(*sync.Once) - // TODO-JAKE should this panic? - onceFunc.Do(func() { + once, _ := k.registerNamespaceLock.LoadOrStore(namespace, &onceAndSent{Once: &sync.Once{}}) + onceFunc := once.(*onceAndSent) + onceFunc.Once.Do(func() { informers := k.addNewNamespace(namespace) if err := k.kubeController.AddNewListOfInformers(informers); err != nil { - contextutils.LoggerFrom(k.ctx).Panicf("failed to add new list of informers to kube controller: %v", err) + onceFunc.Err = errors.Wrapf(err, "failed to add new list of informers to kube controller") } }) - return nil + return onceFunc.Err } // Deprecated: Use NamespacedPodLister instead diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index 6a0317345..d295e0a9b 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -55,6 +55,11 @@ var ( } ) +type onceAndSent struct { + Err error + Once *sync.Once +} + func init() { view.Register(ListCountView, WatchCountView) } @@ -201,9 +206,6 @@ func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error // Create a shared informer for each of the given namespaces. // NOTE: We do not distinguish between the value "" (all namespaces) and a regular namespace here. - // TODO-JAKE there is a special rule, when the namespaces is [""], we need an option - // to be able to change the Informer so that it does not watch on "", but only those namespaces - // that we want to watch. This only happens if the client(user) is setting the namespaceLabelSelectors field in the API. for _, ns := range rc.namespaceWhitelist { // we want to make sure that we have registered this namespace f.registerNamespaceLock.LoadOrStore(ns, &sync.Once{}) @@ -317,24 +319,30 @@ func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace str // in that event, we want to make sure that the cache has started, the reason is because // we have to initiallize the default namespaces as well as this new namespace f.Start() - // we should only register a namespace once and only once - once, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Once{}) - once.(*sync.Once).Do(func() { + + // we should only register a (namespace, type) once and only once + mapToTypes, _ := f.registerNamespaceLock.LoadOrStore(namespace, &sync.Map{}) + once, loaded := mapToTypes.(*sync.Map).LoadOrStore(reflect.TypeOf(rc.crd.Version.Type), &onceAndSent{Once: &sync.Once{}}) + onceSent := once.(*onceAndSent) + if loaded { + return onceSent.Err + } + onceSent.Once.Do(func() { ctx := f.ctx if ctxWithTags, err := tag.New(ctx, tag.Insert(KeyKind, rc.resourceName)); err == nil { ctx = ctxWithTags } informer, err := f.addNewNamespaceToRegistry(ctx, namespace, rc) if err != nil { - // TODO-JAKE not sure if we want to panic here or to return an error. But I feel like panics are ok. - contextutils.LoggerFrom(ctx).Panicf("failed to add new namespace to registry: %v", err) + onceSent.Err = errors.Wrapf(err, "failed to add new namespace to registry:") + return } if err := f.kubeController.AddNewInformer(informer); err != nil { - contextutils.LoggerFrom(ctx).Panicf("failed to add new informer to kube controller: %v", err) + onceSent.Err = errors.Wrapf(err, "failed to add new informer to kube controller") + return } - }) - return nil + return onceSent.Err } func (f *ResourceClientSharedInformerFactory) GetLister(namespace string, obj runtime.Object) (ResourceLister, error) { diff --git a/pkg/api/v1/clients/kube/resource_client_factory_test.go b/pkg/api/v1/clients/kube/resource_client_factory_test.go index 9a84029c5..daaeae20c 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory_test.go +++ b/pkg/api/v1/clients/kube/resource_client_factory_test.go @@ -8,6 +8,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/crd/client/clientset/versioned/fake" solov1 "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/crd/solo.io/v1" @@ -118,14 +119,19 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { var ( clientset *fake.Clientset + clientset2 *fake.Clientset preStartGoroutines int client *kube.ResourceClient + client2 *kube.ResourceClient ) BeforeEach(func() { clientset = fake.NewSimpleClientset(mocksv1.MockResourceCrd) - // We need the resourceClient so that we can register its resourceType/namespaces with the cache + clientset2 = fake.NewSimpleClientset(mocksv1.AnotherMockResourceCrd) + client = util.ClientForClientsetAndResource(clientset, kubeCache, mocksv1.MockResourceCrd, &mocksv1.MockResource{}, []string{namespace1}) + client2 = util.ClientForClientsetAndResource(clientset2, kubeCache, mocksv1.AnotherMockResourceCrd, &mocksv1.AnotherMockResource{}, []string{namespace1}) + err := kubeCache.Register(client) Expect(err).NotTo(HaveOccurred()) @@ -228,6 +234,18 @@ var _ = Describe("Test ResourceClientSharedInformerFactory", func() { Expect(len(watchResults)).To(BeEquivalentTo(1)) Expect(watchResults).To(ConsistOf("mock-res-2")) }) + + It("should be able to register two or more clients in a new namespace", func() { + err := client.RegisterNamespace(namespace2) + Expect(err).ToNot(HaveOccurred()) + err = client2.RegisterNamespace(namespace2) + Expect(err).ToNot(HaveOccurred()) + + // it should fail here, saying that the client has not registered the informer for the namespace and the client + resources, err := client2.List(namespace2, clients.ListOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + Expect(len(resources)).To(BeEquivalentTo(0)) + }) }) Context("multiple watches", func() { diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index befa8d633..143ed327d 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -296,17 +296,22 @@ type {{ lower_camel .Name }}ListWithNamespace struct { if err != nil { return nil, nil, err } + newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled - for _, resourceNamespace := range namespacesResources { + for i, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name + newlyRegisteredNamespaces[i] = namespace {{- range .Resources }} {{- if (not .ClusterScoped) }} - c.{{ lower_camel .Name }}.RegisterNamespace(namespace) + err = c.{{ lower_camel .Name }}.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the {{ lower_camel .Name }}") + } /* Setup namespaced watch for {{ upper_camel .Name }} */ { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list") + return nil, nil, errors.Wrapf(err, "initial {{ upper_camel .Name }} list with new namespace") } initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List,{{ lower_camel .PluralName }}...) {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) @@ -346,6 +351,10 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } }(namespace) } + if len(newlyRegisteredNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newlyRegisteredNamespaces) + } + // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter @@ -413,14 +422,19 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } for _, namespace := range newNamespaces { + var err error {{- range .Resources }} {{- if (not .ClusterScoped) }} - c.{{ lower_camel .Name }}.RegisterNamespace(namespace) + err = c.{{ lower_camel .Name }}.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the {{ lower_camel .Name }}") + continue + } /* Setup namespaced watch for {{ upper_camel .Name }} for new namespace */ { {{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list") + errs <- errors.Wrapf(err, "initial new namespace {{ upper_camel .Name }} list in namespace watch") continue } {{ lower_camel .PluralName }}ByNamespace.Store(namespace, {{ lower_camel .PluralName }}) @@ -465,7 +479,10 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } }(namespace) } - c.updateNamespaces.Unlock() + if len(newNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) + c.updateNamespaces.Unlock() + } } } }() diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 13f452a98..19cebaf66 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -233,15 +233,20 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa if err != nil { return nil, nil, err } + newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled - for _, resourceNamespace := range namespacesResources { + for i, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name - c.kubeConfig.RegisterNamespace(namespace) + newlyRegisteredNamespaces[i] = namespace + err = c.kubeConfig.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the kubeConfig") + } /* Setup namespaced watch for KubeConfig */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial KubeConfig list") + return nil, nil, errors.Wrapf(err, "initial KubeConfig list with new namespace") } initialKubeConfigList = append(initialKubeConfigList, kubeconfigs...) kubeconfigsByNamespace.Store(namespace, kubeconfigs) @@ -275,6 +280,10 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } }(namespace) } + if len(newlyRegisteredNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newlyRegisteredNamespaces) + } + // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter @@ -338,12 +347,17 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } for _, namespace := range newNamespaces { - c.kubeConfig.RegisterNamespace(namespace) + var err error + err = c.kubeConfig.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the kubeConfig") + continue + } /* Setup namespaced watch for KubeConfig for new namespace */ { kubeconfigs, err := c.kubeConfig.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace KubeConfig list") + errs <- errors.Wrapf(err, "initial new namespace KubeConfig list in namespace watch") continue } kubeconfigsByNamespace.Store(namespace, kubeconfigs) @@ -382,7 +396,10 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } }(namespace) } - c.updateNamespaces.Unlock() + if len(newNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) + c.updateNamespaces.Unlock() + } } } }() diff --git a/test/mocks/v1/testing.solo.io_suite_test.go b/test/mocks/v1/testing.solo.io_suite_test.go index 2ce02fcdd..a86964623 100644 --- a/test/mocks/v1/testing.solo.io_suite_test.go +++ b/test/mocks/v1/testing.solo.io_suite_test.go @@ -54,7 +54,7 @@ var ( testutils.ErrorNotOccuredOrNotFound(err) err = clientset.ApiextensionsV1().CustomResourceDefinitions().Delete(ctx, "simplemocks.testing.solo.io", metav1.DeleteOptions{}) testutils.ErrorNotOccuredOrNotFound(err) - // TOOD-JAKE known race condition occurs here https://storage.googleapis.com/solo-public-build-logs/log-286333d3-2840-4323-b508-7e351539a9f9.txt + // known race condition occurs here https://storage.googleapis.com/solo-public-build-logs/log-286333d3-2840-4323-b508-7e351539a9f9.txt Expect(lock.ReleaseLock()).NotTo(HaveOccurred()) }) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index f92eee71f..fa6bfb591 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -476,15 +476,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } + newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled - for _, resourceNamespace := range namespacesResources { + for i, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name - c.simpleMockResource.RegisterNamespace(namespace) + newlyRegisteredNamespaces[i] = namespace + err = c.simpleMockResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the simpleMockResource") + } /* Setup namespaced watch for SimpleMockResource */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list") + return nil, nil, errors.Wrapf(err, "initial SimpleMockResource list with new namespace") } initialSimpleMockResourceList = append(initialSimpleMockResourceList, simplemocks...) simplemocksByNamespace.Store(namespace, simplemocks) @@ -499,12 +504,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-simplemocks") }(namespace) - c.mockResource.RegisterNamespace(namespace) + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + } /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "initial MockResource list with new namespace") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) @@ -519,12 +527,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") }(namespace) - c.fakeResource.RegisterNamespace(namespace) + err = c.fakeResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the fakeResource") + } /* Setup namespaced watch for FakeResource */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FakeResource list") + return nil, nil, errors.Wrapf(err, "initial FakeResource list with new namespace") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace.Store(namespace, fakes) @@ -539,12 +550,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-fakes") }(namespace) - c.anotherMockResource.RegisterNamespace(namespace) + err = c.anotherMockResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the anotherMockResource") + } /* Setup namespaced watch for AnotherMockResource */ { anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list") + return nil, nil, errors.Wrapf(err, "initial AnotherMockResource list with new namespace") } initialAnotherMockResourceList = append(initialAnotherMockResourceList, anothermockresources...) anothermockresourcesByNamespace.Store(namespace, anothermockresources) @@ -559,12 +573,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-anothermockresources") }(namespace) - c.mockCustomType.RegisterNamespace(namespace) + err = c.mockCustomType.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the mockCustomType") + } /* Setup namespaced watch for MockCustomType */ { mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockCustomType list") + return nil, nil, errors.Wrapf(err, "initial MockCustomType list with new namespace") } initialMockCustomTypeList = append(initialMockCustomTypeList, mcts...) mctsByNamespace.Store(namespace, mcts) @@ -579,12 +596,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") }(namespace) - c.pod.RegisterNamespace(namespace) + err = c.pod.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the pod") + } /* Setup namespaced watch for Pod */ { pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial Pod list") + return nil, nil, errors.Wrapf(err, "initial Pod list with new namespace") } initialPodList = append(initialPodList, pods...) podsByNamespace.Store(namespace, pods) @@ -663,6 +683,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if len(newlyRegisteredNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newlyRegisteredNamespaces) + } + // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter @@ -731,12 +755,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } for _, namespace := range newNamespaces { - c.simpleMockResource.RegisterNamespace(namespace) + var err error + err = c.simpleMockResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the simpleMockResource") + continue + } /* Setup namespaced watch for SimpleMockResource for new namespace */ { simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list") + errs <- errors.Wrapf(err, "initial new namespace SimpleMockResource list in namespace watch") continue } simplemocksByNamespace.Store(namespace, simplemocks) @@ -752,12 +781,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, simpleMockResourceErrs, namespace+"-new-namespace-simplemocks") }(namespace) - c.mockResource.RegisterNamespace(namespace) + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + continue + } /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace MockResource list") + errs <- errors.Wrapf(err, "initial new namespace MockResource list in namespace watch") continue } mocksByNamespace.Store(namespace, mocks) @@ -773,12 +806,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") }(namespace) - c.fakeResource.RegisterNamespace(namespace) + err = c.fakeResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the fakeResource") + continue + } /* Setup namespaced watch for FakeResource for new namespace */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") + errs <- errors.Wrapf(err, "initial new namespace FakeResource list in namespace watch") continue } fakesByNamespace.Store(namespace, fakes) @@ -794,12 +831,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, fakeResourceErrs, namespace+"-new-namespace-fakes") }(namespace) - c.anotherMockResource.RegisterNamespace(namespace) + err = c.anotherMockResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the anotherMockResource") + continue + } /* Setup namespaced watch for AnotherMockResource for new namespace */ { anothermockresources, err := c.anotherMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list") + errs <- errors.Wrapf(err, "initial new namespace AnotherMockResource list in namespace watch") continue } anothermockresourcesByNamespace.Store(namespace, anothermockresources) @@ -815,12 +856,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, anotherMockResourceErrs, namespace+"-new-namespace-anothermockresources") }(namespace) - c.mockCustomType.RegisterNamespace(namespace) + err = c.mockCustomType.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the mockCustomType") + continue + } /* Setup namespaced watch for MockCustomType for new namespace */ { mcts, err := c.mockCustomType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace MockCustomType list") + errs <- errors.Wrapf(err, "initial new namespace MockCustomType list in namespace watch") continue } mctsByNamespace.Store(namespace, mcts) @@ -836,12 +881,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") }(namespace) - c.pod.RegisterNamespace(namespace) + err = c.pod.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the pod") + continue + } /* Setup namespaced watch for Pod for new namespace */ { pods, err := c.pod.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace Pod list") + errs <- errors.Wrapf(err, "initial new namespace Pod list in namespace watch") continue } podsByNamespace.Store(namespace, pods) @@ -925,7 +974,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - c.updateNamespaces.Unlock() + if len(newNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) + c.updateNamespaces.Unlock() + } } } }() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 491b152e5..a9254c517 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -233,15 +233,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } + newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled - for _, resourceNamespace := range namespacesResources { + for i, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name - c.mockResource.RegisterNamespace(namespace) + newlyRegisteredNamespaces[i] = namespace + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + } /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "initial MockResource list with new namespace") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) @@ -275,6 +280,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if len(newlyRegisteredNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newlyRegisteredNamespaces) + } + // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter @@ -338,12 +347,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } for _, namespace := range newNamespaces { - c.mockResource.RegisterNamespace(namespace) + var err error + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + continue + } /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace MockResource list") + errs <- errors.Wrapf(err, "initial new namespace MockResource list in namespace watch") continue } mocksByNamespace.Store(namespace, mocks) @@ -382,7 +396,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - c.updateNamespaces.Unlock() + if len(newNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) + c.updateNamespaces.Unlock() + } } } }() diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 53b970e1a..e3c1a07f2 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -327,15 +327,20 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO if err != nil { return nil, nil, err } + newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled - for _, resourceNamespace := range namespacesResources { + for i, resourceNamespace := range namespacesResources { namespace := resourceNamespace.Name - c.mockResource.RegisterNamespace(namespace) + newlyRegisteredNamespaces[i] = namespace + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + } /* Setup namespaced watch for MockResource */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial MockResource list") + return nil, nil, errors.Wrapf(err, "initial MockResource list with new namespace") } initialMockResourceList = append(initialMockResourceList, mocks...) mocksByNamespace.Store(namespace, mocks) @@ -350,12 +355,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-mocks") }(namespace) - c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) + err = c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the frequentlyChangingAnnotationsResource") + } /* Setup namespaced watch for FrequentlyChangingAnnotationsResource */ { fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list") + return nil, nil, errors.Wrapf(err, "initial FrequentlyChangingAnnotationsResource list with new namespace") } initialFrequentlyChangingAnnotationsResourceList = append(initialFrequentlyChangingAnnotationsResourceList, fcars...) fcarsByNamespace.Store(namespace, fcars) @@ -370,12 +378,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-fcars") }(namespace) - c.fakeResource.RegisterNamespace(namespace) + err = c.fakeResource.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the fakeResource") + } /* Setup namespaced watch for FakeResource */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) if err != nil { - return nil, nil, errors.Wrapf(err, "initial FakeResource list") + return nil, nil, errors.Wrapf(err, "initial FakeResource list with new namespace") } initialFakeResourceList = append(initialFakeResourceList, fakes...) fakesByNamespace.Store(namespace, fakes) @@ -427,6 +438,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } + if len(newlyRegisteredNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newlyRegisteredNamespaces) + } + // create watch on all namespaces, so that we can add all resources from new namespaces // we will be watching namespaces that meet the Expression Selector filter @@ -492,12 +507,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } for _, namespace := range newNamespaces { - c.mockResource.RegisterNamespace(namespace) + var err error + err = c.mockResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the mockResource") + continue + } /* Setup namespaced watch for MockResource for new namespace */ { mocks, err := c.mockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace MockResource list") + errs <- errors.Wrapf(err, "initial new namespace MockResource list in namespace watch") continue } mocksByNamespace.Store(namespace, mocks) @@ -513,12 +533,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockResourceErrs, namespace+"-new-namespace-mocks") }(namespace) - c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) + err = c.frequentlyChangingAnnotationsResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the frequentlyChangingAnnotationsResource") + continue + } /* Setup namespaced watch for FrequentlyChangingAnnotationsResource for new namespace */ { fcars, err := c.frequentlyChangingAnnotationsResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list") + errs <- errors.Wrapf(err, "initial new namespace FrequentlyChangingAnnotationsResource list in namespace watch") continue } fcarsByNamespace.Store(namespace, fcars) @@ -534,12 +558,16 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, frequentlyChangingAnnotationsResourceErrs, namespace+"-new-namespace-fcars") }(namespace) - c.fakeResource.RegisterNamespace(namespace) + err = c.fakeResource.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the fakeResource") + continue + } /* Setup namespaced watch for FakeResource for new namespace */ { fakes, err := c.fakeResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { - errs <- errors.Wrapf(err, "initial new namespace FakeResource list") + errs <- errors.Wrapf(err, "initial new namespace FakeResource list in namespace watch") continue } fakesByNamespace.Store(namespace, fakes) @@ -596,7 +624,10 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } }(namespace) } - c.updateNamespaces.Unlock() + if len(newNamespaces) > 0 { + contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) + c.updateNamespaces.Unlock() + } } } }() From 7b307884c1ef3864c85d13fcbe5cbf2eb40d5c5c Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Fri, 16 Sep 2022 15:59:24 -0500 Subject: [PATCH 71/98] update tests --- pkg/api/v1/clients/kube/cache/cache_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index 96504f778..77da276af 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -109,6 +109,7 @@ var _ = Describe("kube core cache tests", func() { }) It("can list resources for all listers", func() { + Expect(cache.NamespaceLister()).ToNot(BeNil()) _, err := cache.NamespacedPodLister("default").List(selectors) Expect(err).NotTo(HaveOccurred()) _, err = cache.NamespacedConfigMapLister("default").List(selectors) @@ -143,6 +144,7 @@ var _ = Describe("kube core cache tests", func() { }) It("can list resources for all listers", func() { + Expect(cache.NamespaceLister()).ToNot(BeNil()) validateNamespaceResource(testns) validateNamespaceResource(testns2) }) From 24c3941db5e01de55fdaed40ad5769b8a726b630 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 19 Sep 2022 15:49:08 -0500 Subject: [PATCH 72/98] fixed issue with test, and cleaned up tests a little --- .../templates/snapshot_emitter_template.go | 3 +- .../snapshot_emitter_test_template.go | 233 ++++++- .../v1/kubeconfigs_snapshot_emitter.sk.go | 3 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 158 ++++- test/mocks/v1/testing_snapshot_emitter.sk.go | 3 +- .../mocks/v1/testing_snapshot_emitter_test.go | 575 ++++++++++++++++-- .../v1alpha1/testing_snapshot_emitter.sk.go | 3 +- .../v1alpha1/testing_snapshot_emitter_test.go | 220 ++++++- .../v2alpha1/testing_snapshot_emitter.sk.go | 3 +- .../v2alpha1/testing_snapshot_emitter_test.go | 337 +++++++++- 10 files changed, 1433 insertions(+), 105 deletions(-) diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index 143ed327d..d9db181ea 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -299,6 +299,7 @@ type {{ lower_camel .Name }}ListWithNamespace struct { newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled for i, resourceNamespace := range namespacesResources { + c.namespacesWatching.Load(resourceNamespace) namespace := resourceNamespace.Name newlyRegisteredNamespaces[i] = namespace {{- range .Resources }} @@ -481,8 +482,8 @@ type {{ lower_camel .Name }}ListWithNamespace struct { } if len(newNamespaces) > 0 { contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) - c.updateNamespaces.Unlock() } + c.updateNamespaces.Unlock() } } }() diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 1d116c314..35696d699 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -34,6 +34,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients" "github.com/solo-io/solo-kit/pkg/api/v1/clients/factory" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/external/kubernetes/namespace" "github.com/solo-io/solo-kit/pkg/api/v1/resources" @@ -45,6 +46,7 @@ import ( "k8s.io/client-go/rest" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" // Needed to run tests in GKE @@ -59,6 +61,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func log.Printf("This test creates kubernetes resources and is disabled by default. To enable, set RUN_KUBE_TESTS=1 in your env.") return } + + type metadataGetter interface { + GetMetadata() *core.Metadata + } + var ( ctx context.Context namespace1, namespace2 string @@ -132,6 +139,90 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace2 = helpers.RandString(8) } + getMapOfNamespaceResources := func() map[string][]string { + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + namespaceResources := make(map[string][]string, len(namespaces)) + for _, ns := range namespaces { + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, snap := range list { + snapMeta := snap.GetMetadata() + if _, hit := namespaceResources[snapMeta.Namespace]; hit { + namespaceResources[snap.GetMetadata().Namespace] = make([]string, 1) + } + namespaceResources[snapMeta.Namespace] = append(namespaceResources[snapMeta.Namespace], snapMeta.Name) + } + } + return namespaceResources + } + + findNonMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + nonMatching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _,pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if ! matched { + if _, hit := nonMatching[snapMeta.Namespace]; hit { + nonMatching[snap.GetMetadata().Namespace] = make([]string, 1) + } + nonMatching[snapMeta.Namespace] = append(nonMatching[snapMeta.Namespace], snapMeta.Name) + } + } + return nonMatching + } + + findMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + matching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _,pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if matched { + if _, hit := matching[snapMeta.Namespace]; hit { + matching[snap.GetMetadata().Namespace] = make([]string, 1) + } + matching[snapMeta.Namespace] = append(matching[snapMeta.Namespace], snapMeta.Name) + } + } + return matching + } + + getMapOfResources := func(listOfResources []metadataGetter) map[string][]string { + resources := make(map[string][]string) + for _, snap := range listOfResources { + snapMeta := snap.GetMetadata() + if _, hit := resources[snapMeta.Namespace]; hit { + resources[snap.GetMetadata().Namespace] = make([]string, 1) + } + resources[snapMeta.Namespace] = append(resources[snapMeta.Namespace], snapMeta.Name) + } + return resources + } + +{{- range .Resources }} +{{- if not .ClusterScoped }} + convert{{ .PluralName }}ToMetadataGetter := func(rl {{ .ImportPrefix }}{{ .Name }}List) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } +{{- end }} +{{- end }} + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -146,6 +237,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) var snap *{{ .GoName }}Snapshot + var previous *{{ .GoName }}Snapshot {{- range .Resources }} @@ -157,6 +249,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func for { select { case snap = <-snapshots: + previous = snap for _, expected := range expect{{ .PluralName }} { if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -173,12 +266,21 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func case <-time.After(time.Second * 10): {{- if .ClusterScoped }} combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) {{- else }} - nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) - nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) + unexpectedResource = findMatchingResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) + } else { + expectedResources = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }})) + unexpectedResource = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }})) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) {{- end }} - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } } @@ -618,6 +720,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Expect(err).NotTo(HaveOccurred()) var snap *{{ .GoName }}Snapshot + var previous *{{ .GoName }}Snapshot {{- range .Resources }} @@ -630,6 +733,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func for { select { case snap = <-snapshots: + previous = snap for _, expected := range expect{{ .PluralName }} { if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -646,12 +750,38 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func case <-time.After(time.Second * 10): {{- if .ClusterScoped }} combined, _ := {{ lower_camel .Name }}Client.List(clients.ListOpts{}) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) {{- else }} - nsList1, _ := {{ lower_camel .Name }}Client.List(namespace1, clients.ListOpts{}) - nsList2, _ := {{ lower_camel .Name }}Client.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.{{ upper_camel .PluralName }} { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1,namespace2,namespace3,namespace4,namespace5,namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := {{ lower_camel .Name }}Client.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _,snap := range expect{{ .PluralName }} { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _,snap := range unexpect{{ .PluralName }}{ + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) {{- end }} - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) } } } @@ -952,6 +1082,93 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }}{{/* end of with */}} {{- end }}{{/* end of range */}} }) + + It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func () { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + var previous *TestingSnapshot + +{{- range .Resources }} +{{ if not .ClusterScoped }} +{{ if .HasStatus }} + +{{/* no need for anything else, this only works on clients that have kube resource factories, this will not work on clients that have memory resource factories.*/}} + + /* + {{ .Name }} + */ + assertSnapshot{{ .PluralName }} := func(expect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List, unexpect{{ .PluralName }} {{ .ImportPrefix }}{{ .Name }}List) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpect{{ .PluralName }} { + if _, err := snap.{{ upper_camel .PluralName }}.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) + unexpectedResource = findMatchingResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) + } else { + expectedResources = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }})) + unexpectedResource = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }})) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + {{ lower_camel .Name }}1a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a}, nil) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func () bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1 * time.Second).Should(BeTrue()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshot{{ .PluralName }}({{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}2a}, {{ .ImportPrefix }}{{ .Name }}List{ {{ lower_camel .Name }}1a}) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func () bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1 * time.Second).Should(BeTrue()) +{{- end }} +{{- end }} +{{- end }} + }) }) Context("use different resource namespace listers", func() { diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 19cebaf66..1ce33bae9 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -236,6 +236,7 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled for i, resourceNamespace := range namespacesResources { + c.namespacesWatching.Load(resourceNamespace) namespace := resourceNamespace.Name newlyRegisteredNamespaces[i] = namespace err = c.kubeConfig.RegisterNamespace(namespace) @@ -398,8 +399,8 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa } if len(newNamespaces) > 0 { contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) - c.updateNamespaces.Unlock() } + c.updateNamespaces.Unlock() } } }() diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 550951adc..ced7d3df0 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,12 +1,13 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 import ( + "bytes" "context" + "fmt" "os" "time" @@ -20,6 +21,7 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/resources" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" corev1 "k8s.io/api/core/v1" @@ -38,6 +40,11 @@ var _ = Describe("V1Emitter", func() { log.Printf("This test creates kubernetes resources and is disabled by default. To enable, set RUN_KUBE_TESTS=1 in your env.") return } + + type metadataGetter interface { + GetMetadata() *core.Metadata + } + var ( ctx context.Context namespace1, namespace2 string @@ -102,6 +109,85 @@ var _ = Describe("V1Emitter", func() { namespace2 = helpers.RandString(8) } + getMapOfNamespaceResources := func() map[string][]string { + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + namespaceResources := make(map[string][]string, len(namespaces)) + for _, ns := range namespaces { + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, snap := range list { + snapMeta := snap.GetMetadata() + if _, hit := namespaceResources[snapMeta.Namespace]; hit { + namespaceResources[snap.GetMetadata().Namespace] = make([]string, 1) + } + namespaceResources[snapMeta.Namespace] = append(namespaceResources[snapMeta.Namespace], snapMeta.Name) + } + } + return namespaceResources + } + + findNonMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + nonMatching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if !matched { + if _, hit := nonMatching[snapMeta.Namespace]; hit { + nonMatching[snap.GetMetadata().Namespace] = make([]string, 1) + } + nonMatching[snapMeta.Namespace] = append(nonMatching[snapMeta.Namespace], snapMeta.Name) + } + } + return nonMatching + } + + findMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + matching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if matched { + if _, hit := matching[snapMeta.Namespace]; hit { + matching[snap.GetMetadata().Namespace] = make([]string, 1) + } + matching[snapMeta.Namespace] = append(matching[snapMeta.Namespace], snapMeta.Name) + } + } + return matching + } + + getMapOfResources := func(listOfResources []metadataGetter) map[string][]string { + resources := make(map[string][]string) + for _, snap := range listOfResources { + snapMeta := snap.GetMetadata() + if _, hit := resources[snapMeta.Namespace]; hit { + resources[snap.GetMetadata().Namespace] = make([]string, 1) + } + resources[snapMeta.Namespace] = append(resources[snapMeta.Namespace], snapMeta.Name) + } + return resources + } + convertkubeconfigsToMetadataGetter := func(rl KubeConfigList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -116,6 +202,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *KubeconfigsSnapshot + var previous *KubeconfigsSnapshot /* KubeConfig @@ -125,6 +212,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectkubeconfigs { if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -139,10 +227,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertkubeconfigsToMetadataGetter(expectkubeconfigs), convertkubeconfigsToMetadataGetter(previous.Kubeconfigs)) + unexpectedResource = findMatchingResources(convertkubeconfigsToMetadataGetter(unexpectkubeconfigs), convertkubeconfigsToMetadataGetter(previous.Kubeconfigs)) + } else { + expectedResources = getMapOfResources(convertkubeconfigsToMetadataGetter(expectkubeconfigs)) + unexpectedResource = getMapOfResources(convertkubeconfigsToMetadataGetter(unexpectkubeconfigs)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -407,6 +503,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *KubeconfigsSnapshot + var previous *KubeconfigsSnapshot /* KubeConfig @@ -417,6 +514,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectkubeconfigs { if _, err := snap.Kubeconfigs.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -431,10 +529,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := kubeConfigClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := kubeConfigClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Kubeconfigs { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := kubeConfigClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectkubeconfigs { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectkubeconfigs { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -629,6 +752,23 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace4) getNewNamespaces() }) + + It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + var previous *TestingSnapshot + + }) }) Context("use different resource namespace listers", func() { diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index fa6bfb591..cbeeff3ac 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -479,6 +479,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled for i, resourceNamespace := range namespacesResources { + c.namespacesWatching.Load(resourceNamespace) namespace := resourceNamespace.Name newlyRegisteredNamespaces[i] = namespace err = c.simpleMockResource.RegisterNamespace(namespace) @@ -976,8 +977,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } if len(newNamespaces) > 0 { contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) - c.updateNamespaces.Unlock() } + c.updateNamespaces.Unlock() } } }() diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index 4351deb45..d602948c1 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -1,12 +1,13 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1 import ( + "bytes" "context" + "fmt" "os" "time" @@ -24,9 +25,11 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/resources" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" "github.com/solo-io/solo-kit/test/helpers" corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -43,6 +46,11 @@ var _ = Describe("V1Emitter", func() { log.Printf("This test creates kubernetes resources and is disabled by default. To enable, set RUN_KUBE_TESTS=1 in your env.") return } + + type metadataGetter interface { + GetMetadata() *core.Metadata + } + var ( ctx context.Context namespace1, namespace2 string @@ -145,6 +153,120 @@ var _ = Describe("V1Emitter", func() { namespace2 = helpers.RandString(8) } + getMapOfNamespaceResources := func() map[string][]string { + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + namespaceResources := make(map[string][]string, len(namespaces)) + for _, ns := range namespaces { + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, snap := range list { + snapMeta := snap.GetMetadata() + if _, hit := namespaceResources[snapMeta.Namespace]; hit { + namespaceResources[snap.GetMetadata().Namespace] = make([]string, 1) + } + namespaceResources[snapMeta.Namespace] = append(namespaceResources[snapMeta.Namespace], snapMeta.Name) + } + } + return namespaceResources + } + + findNonMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + nonMatching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if !matched { + if _, hit := nonMatching[snapMeta.Namespace]; hit { + nonMatching[snap.GetMetadata().Namespace] = make([]string, 1) + } + nonMatching[snapMeta.Namespace] = append(nonMatching[snapMeta.Namespace], snapMeta.Name) + } + } + return nonMatching + } + + findMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + matching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if matched { + if _, hit := matching[snapMeta.Namespace]; hit { + matching[snap.GetMetadata().Namespace] = make([]string, 1) + } + matching[snapMeta.Namespace] = append(matching[snapMeta.Namespace], snapMeta.Name) + } + } + return matching + } + + getMapOfResources := func(listOfResources []metadataGetter) map[string][]string { + resources := make(map[string][]string) + for _, snap := range listOfResources { + snapMeta := snap.GetMetadata() + if _, hit := resources[snapMeta.Namespace]; hit { + resources[snap.GetMetadata().Namespace] = make([]string, 1) + } + resources[snapMeta.Namespace] = append(resources[snapMeta.Namespace], snapMeta.Name) + } + return resources + } + convertSimplemocksToMetadataGetter := func(rl SimpleMockResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertMocksToMetadataGetter := func(rl MockResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertFakesToMetadataGetter := func(rl FakeResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertAnothermockresourcesToMetadataGetter := func(rl AnotherMockResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertmctsToMetadataGetter := func(rl MockCustomTypeList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertpodsToMetadataGetter := func(rl github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -159,6 +281,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* SimpleMockResource @@ -168,6 +291,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectSimplemocks { if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -182,10 +306,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertSimplemocksToMetadataGetter(expectSimplemocks), convertSimplemocksToMetadataGetter(previous.Simplemocks)) + unexpectedResource = findMatchingResources(convertSimplemocksToMetadataGetter(unexpectSimplemocks), convertSimplemocksToMetadataGetter(previous.Simplemocks)) + } else { + expectedResources = getMapOfResources(convertSimplemocksToMetadataGetter(expectSimplemocks)) + unexpectedResource = getMapOfResources(convertSimplemocksToMetadataGetter(unexpectSimplemocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -263,6 +395,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -277,10 +410,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -358,6 +499,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFakes { if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -372,10 +514,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertFakesToMetadataGetter(expectFakes), convertFakesToMetadataGetter(previous.Fakes)) + unexpectedResource = findMatchingResources(convertFakesToMetadataGetter(unexpectFakes), convertFakesToMetadataGetter(previous.Fakes)) + } else { + expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) + unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -453,6 +603,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectAnothermockresources { if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -467,10 +618,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) + unexpectedResource = findMatchingResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) + } else { + expectedResources = getMapOfResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources)) + unexpectedResource = getMapOfResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -548,6 +707,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectClusterresources { if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -618,6 +778,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectmcts { if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -632,10 +793,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertmctsToMetadataGetter(expectmcts), convertmctsToMetadataGetter(previous.Mcts)) + unexpectedResource = findMatchingResources(convertmctsToMetadataGetter(unexpectmcts), convertmctsToMetadataGetter(previous.Mcts)) + } else { + expectedResources = getMapOfResources(convertmctsToMetadataGetter(expectmcts)) + unexpectedResource = getMapOfResources(convertmctsToMetadataGetter(unexpectmcts)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -713,6 +882,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectpods { if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -727,10 +897,18 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertpodsToMetadataGetter(expectpods), convertpodsToMetadataGetter(previous.Pods)) + unexpectedResource = findMatchingResources(convertpodsToMetadataGetter(unexpectpods), convertpodsToMetadataGetter(previous.Pods)) + } else { + expectedResources = getMapOfResources(convertpodsToMetadataGetter(expectpods)) + unexpectedResource = getMapOfResources(convertpodsToMetadataGetter(unexpectpods)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -1712,6 +1890,7 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* SimpleMockResource @@ -1722,6 +1901,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectSimplemocks { if _, err := snap.Simplemocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -1736,10 +1916,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := simpleMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := simpleMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Simplemocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := simpleMockResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectSimplemocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectSimplemocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -1804,6 +2009,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -1818,10 +2024,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Mocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -1886,6 +2117,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFakes { if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -1900,10 +2132,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Fakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := fakeResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectFakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectFakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -1968,6 +2225,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectAnothermockresources { if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -1982,10 +2240,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := anotherMockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := anotherMockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Anothermockresources { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := anotherMockResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectAnothermockresources { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectAnothermockresources { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -2050,6 +2333,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectClusterresources { if _, err := snap.Clusterresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -2121,6 +2405,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectmcts { if _, err := snap.Mcts.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -2135,10 +2420,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockCustomTypeClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockCustomTypeClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Mcts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := mockCustomTypeClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectmcts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectmcts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -2203,6 +2513,7 @@ var _ = Describe("V1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectpods { if _, err := snap.Pods.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -2217,10 +2528,35 @@ var _ = Describe("V1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := podClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := podClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Pods { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := podClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectpods { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectpods { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -3053,6 +3389,147 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace4) getNewNamespaces() }) + + It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + var previous *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource1a}, nil) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource2a}, MockResourceList{mockResource1a}) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + + /* + AnotherMockResource + */ + assertSnapshotAnothermockresources := func(expectAnothermockresources AnotherMockResourceList, unexpectAnothermockresources AnotherMockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectAnothermockresources { + if _, err := snap.Anothermockresources.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectAnothermockresources { + if _, err := snap.Anothermockresources.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) + unexpectedResource = findMatchingResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) + } else { + expectedResources = getMapOfResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources)) + unexpectedResource = getMapOfResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + anotherMockResource1a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource1a}, nil) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotAnothermockresources(AnotherMockResourceList{anotherMockResource2a}, AnotherMockResourceList{anotherMockResource1a}) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + + }) }) Context("use different resource namespace listers", func() { diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index a9254c517..7296eba4e 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -236,6 +236,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled for i, resourceNamespace := range namespacesResources { + c.namespacesWatching.Load(resourceNamespace) namespace := resourceNamespace.Name newlyRegisteredNamespaces[i] = namespace err = c.mockResource.RegisterNamespace(namespace) @@ -398,8 +399,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } if len(newNamespaces) > 0 { contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) - c.updateNamespaces.Unlock() } + c.updateNamespaces.Unlock() } } }() diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 688cc6d05..66ab32597 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,12 +1,13 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v1alpha1 import ( + "bytes" "context" + "fmt" "os" "time" @@ -20,10 +21,12 @@ import ( kuberc "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube" "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/resources" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -40,6 +43,11 @@ var _ = Describe("V1Alpha1Emitter", func() { log.Printf("This test creates kubernetes resources and is disabled by default. To enable, set RUN_KUBE_TESTS=1 in your env.") return } + + type metadataGetter interface { + GetMetadata() *core.Metadata + } + var ( ctx context.Context namespace1, namespace2 string @@ -106,6 +114,85 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace2 = helpers.RandString(8) } + getMapOfNamespaceResources := func() map[string][]string { + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + namespaceResources := make(map[string][]string, len(namespaces)) + for _, ns := range namespaces { + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, snap := range list { + snapMeta := snap.GetMetadata() + if _, hit := namespaceResources[snapMeta.Namespace]; hit { + namespaceResources[snap.GetMetadata().Namespace] = make([]string, 1) + } + namespaceResources[snapMeta.Namespace] = append(namespaceResources[snapMeta.Namespace], snapMeta.Name) + } + } + return namespaceResources + } + + findNonMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + nonMatching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if !matched { + if _, hit := nonMatching[snapMeta.Namespace]; hit { + nonMatching[snap.GetMetadata().Namespace] = make([]string, 1) + } + nonMatching[snapMeta.Namespace] = append(nonMatching[snapMeta.Namespace], snapMeta.Name) + } + } + return nonMatching + } + + findMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + matching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if matched { + if _, hit := matching[snapMeta.Namespace]; hit { + matching[snap.GetMetadata().Namespace] = make([]string, 1) + } + matching[snapMeta.Namespace] = append(matching[snapMeta.Namespace], snapMeta.Name) + } + } + return matching + } + + getMapOfResources := func(listOfResources []metadataGetter) map[string][]string { + resources := make(map[string][]string) + for _, snap := range listOfResources { + snapMeta := snap.GetMetadata() + if _, hit := resources[snapMeta.Namespace]; hit { + resources[snap.GetMetadata().Namespace] = make([]string, 1) + } + resources[snapMeta.Namespace] = append(resources[snapMeta.Namespace], snapMeta.Name) + } + return resources + } + convertMocksToMetadataGetter := func(rl MockResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -120,6 +207,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* MockResource @@ -129,6 +217,7 @@ var _ = Describe("V1Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -143,10 +232,18 @@ var _ = Describe("V1Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -422,6 +519,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* MockResource @@ -432,6 +530,7 @@ var _ = Describe("V1Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -446,10 +545,35 @@ var _ = Describe("V1Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Mocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -644,6 +768,84 @@ var _ = Describe("V1Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace4) getNewNamespaces() }) + + It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + var previous *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource1a}, nil) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource2a}, MockResourceList{mockResource1a}) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }) }) Context("use different resource namespace listers", func() { diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index e3c1a07f2..03eceed39 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -330,6 +330,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO newlyRegisteredNamespaces := make([]string, len(namespacesResources)) // non watched namespaces that are labeled for i, resourceNamespace := range namespacesResources { + c.namespacesWatching.Load(resourceNamespace) namespace := resourceNamespace.Name newlyRegisteredNamespaces[i] = namespace err = c.mockResource.RegisterNamespace(namespace) @@ -626,8 +627,8 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO } if len(newNamespaces) > 0 { contextutils.LoggerFrom(ctx).Infof("registered the new namespace %v", newNamespaces) - c.updateNamespaces.Unlock() } + c.updateNamespaces.Unlock() } } }() diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 997c8e79f..befe5a99a 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,12 +1,13 @@ // Code generated by solo-kit. DO NOT EDIT. -//go:build solokit // +build solokit package v2alpha1 import ( + "bytes" "context" + "fmt" "os" "time" @@ -23,10 +24,12 @@ import ( "github.com/solo-io/solo-kit/pkg/api/v1/clients/kube/cache" "github.com/solo-io/solo-kit/pkg/api/v1/clients/memory" "github.com/solo-io/solo-kit/pkg/api/v1/resources" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" "github.com/solo-io/solo-kit/pkg/utils/statusutils" "github.com/solo-io/solo-kit/test/helpers" corev1 "k8s.io/api/core/v1" apiext "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -43,6 +46,11 @@ var _ = Describe("V2Alpha1Emitter", func() { log.Printf("This test creates kubernetes resources and is disabled by default. To enable, set RUN_KUBE_TESTS=1 in your env.") return } + + type metadataGetter interface { + GetMetadata() *core.Metadata + } + var ( ctx context.Context namespace1, namespace2 string @@ -121,6 +129,99 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace2 = helpers.RandString(8) } + getMapOfNamespaceResources := func() map[string][]string { + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + namespaceResources := make(map[string][]string, len(namespaces)) + for _, ns := range namespaces { + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, snap := range list { + snapMeta := snap.GetMetadata() + if _, hit := namespaceResources[snapMeta.Namespace]; hit { + namespaceResources[snap.GetMetadata().Namespace] = make([]string, 1) + } + namespaceResources[snapMeta.Namespace] = append(namespaceResources[snapMeta.Namespace], snapMeta.Name) + } + } + return namespaceResources + } + + findNonMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + nonMatching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if !matched { + if _, hit := nonMatching[snapMeta.Namespace]; hit { + nonMatching[snap.GetMetadata().Namespace] = make([]string, 1) + } + nonMatching[snapMeta.Namespace] = append(nonMatching[snapMeta.Namespace], snapMeta.Name) + } + } + return nonMatching + } + + findMatchingResources := func(matchList, findList []metadataGetter) map[string][]string { + matching := make(map[string][]string) + for _, snap := range matchList { + snapMeta := snap.GetMetadata() + matched := false + for _, pre := range findList { + preMeta := pre.GetMetadata() + if preMeta.Namespace == snapMeta.Namespace && preMeta.Name == snapMeta.Name { + matched = true + break + } + } + if matched { + if _, hit := matching[snapMeta.Namespace]; hit { + matching[snap.GetMetadata().Namespace] = make([]string, 1) + } + matching[snapMeta.Namespace] = append(matching[snapMeta.Namespace], snapMeta.Name) + } + } + return matching + } + + getMapOfResources := func(listOfResources []metadataGetter) map[string][]string { + resources := make(map[string][]string) + for _, snap := range listOfResources { + snapMeta := snap.GetMetadata() + if _, hit := resources[snapMeta.Namespace]; hit { + resources[snap.GetMetadata().Namespace] = make([]string, 1) + } + resources[snapMeta.Namespace] = append(resources[snapMeta.Namespace], snapMeta.Name) + } + return resources + } + convertMocksToMetadataGetter := func(rl MockResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertFcarsToMetadataGetter := func(rl FrequentlyChangingAnnotationsResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + convertFakesToMetadataGetter := func(rl testing_solo_io.FakeResourceList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } + runNamespacedSelectorsWithWatchNamespaces := func() { ctx := context.Background() err := emitter.Register() @@ -135,6 +236,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* MockResource @@ -144,6 +246,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -158,10 +261,18 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -239,6 +350,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFcars { if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -253,10 +365,18 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertFcarsToMetadataGetter(expectFcars), convertFcarsToMetadataGetter(previous.Fcars)) + unexpectedResource = findMatchingResources(convertFcarsToMetadataGetter(unexpectFcars), convertFcarsToMetadataGetter(previous.Fcars)) + } else { + expectedResources = getMapOfResources(convertFcarsToMetadataGetter(expectFcars)) + unexpectedResource = getMapOfResources(convertFcarsToMetadataGetter(unexpectFcars)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -334,6 +454,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFakes { if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -348,10 +469,18 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertFakesToMetadataGetter(expectFakes), convertFakesToMetadataGetter(previous.Fakes)) + unexpectedResource = findMatchingResources(convertFakesToMetadataGetter(unexpectFakes), convertFakesToMetadataGetter(previous.Fakes)) + } else { + expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) + unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -865,6 +994,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) var snap *TestingSnapshot + var previous *TestingSnapshot /* MockResource @@ -875,6 +1005,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectMocks { if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -889,10 +1020,35 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := mockResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := mockResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Mocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectMocks { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -957,6 +1113,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFcars { if _, err := snap.Fcars.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -971,10 +1128,35 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := frequentlyChangingAnnotationsResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := frequentlyChangingAnnotationsResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Fcars { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := frequentlyChangingAnnotationsResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectFcars { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectFcars { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -1039,6 +1221,7 @@ var _ = Describe("V2Alpha1Emitter", func() { for { select { case snap = <-snapshots: + previous = snap for _, expected := range expectFakes { if _, err := snap.Fakes.Find(expected.GetMetadata().Ref().Strings()); err != nil { continue drain @@ -1053,10 +1236,35 @@ var _ = Describe("V2Alpha1Emitter", func() { case err := <-errs: Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - nsList1, _ := fakeResourceClient.List(namespace1, clients.ListOpts{}) - nsList2, _ := fakeResourceClient.List(namespace2, clients.ListOpts{}) - combined := append(nsList1, nsList2...) - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Fakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := fakeResourceClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectFakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectFakes { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) } } } @@ -1467,6 +1675,85 @@ var _ = Describe("V2Alpha1Emitter", func() { deleteNamespaces(ctx, kube, namespace4) getNewNamespaces() }) + + It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func() { + ctx := context.Background() + err := emitter.Register() + Expect(err).NotTo(HaveOccurred()) + + snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ + Ctx: ctx, + RefreshRate: time.Second, + ExpressionSelector: labelExpression1, + }) + Expect(err).NotTo(HaveOccurred()) + + var snap *TestingSnapshot + var previous *TestingSnapshot + + /* + MockResource + */ + assertSnapshotMocks := func(expectMocks MockResourceList, unexpectMocks MockResourceList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectMocks { + if _, err := snap.Mocks.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectMocks { + if _, err := snap.Mocks.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) + } else { + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) + } + namespaceResources := getMapOfNamespaceResources() + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource1a, err := mockResourceClient.Write(NewMockResource(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource1a}, nil) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + + mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotMocks(MockResourceList{mockResource2a}, MockResourceList{mockResource1a}) + + deleteNamespaces(ctx, kube, namespace3) + Eventually(func() bool { + _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) + return apierrors.IsNotFound(err) + }, 10*time.Second, 1*time.Second).Should(BeTrue()) + + }) }) Context("use different resource namespace listers", func() { From 93642d130c948913b2fe283921b378eca83c4229 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Mon, 19 Sep 2022 16:21:38 -0500 Subject: [PATCH 73/98] fix error with kube emitter --- pkg/code-generator/codegen/templates/funcs.go | 6 +++ .../snapshot_emitter_test_template.go | 27 ++++++++-- .../v1/kubeconfigs_snapshot_emitter_test.go | 26 +++------- .../mocks/v1/testing_snapshot_emitter_test.go | 52 +++++++++++++++---- .../v1alpha1/testing_snapshot_emitter_test.go | 16 ++++-- .../v2alpha1/testing_snapshot_emitter_test.go | 28 +++++++--- 6 files changed, 112 insertions(+), 43 deletions(-) diff --git a/pkg/code-generator/codegen/templates/funcs.go b/pkg/code-generator/codegen/templates/funcs.go index 7ba2c556d..712fbabcf 100644 --- a/pkg/code-generator/codegen/templates/funcs.go +++ b/pkg/code-generator/codegen/templates/funcs.go @@ -91,6 +91,12 @@ var Funcs = template.FuncMap{ "minus": func(a, b int) int { return a - b }, + "inc": func(a int) int { + return a + 1 + }, + "ge": func(a, b int) bool { + return a >= b + }, } func printPointer(format string, p *string) string { diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 35696d699..841b4b611 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -139,11 +139,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func namespace2 = helpers.RandString(8) } - getMapOfNamespaceResources := func() map[string][]string { + getMapOfNamespaceResources := func(getList func(string) ([]metadataGetter,error)) map[string][]string { namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} namespaceResources := make(map[string][]string, len(namespaces)) for _, ns := range namespaces { - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + list, _ := getList(ns) for _, snap := range list { snapMeta := snap.GetMetadata() if _, hit := namespaceResources[snapMeta.Namespace]; hit { @@ -278,7 +278,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func expectedResources = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }})) unexpectedResource = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }})) } - namespaceResources := getMapOfNamespaceResources() + getList := func (ns string) ([]metadataGetter, error) { + l, err := {{ lower_camel .Name }}Client.List(ns, clients.ListOpts{}) + return convert{{ .PluralName }}ToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) {{- end }} } @@ -1083,6 +1087,16 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }}{{/* end of range */}} }) +{{- $num_of_clients_supported := 0 }} +{{- range .Resources }} +{{ if not .ClusterScoped }} +{{ if .HasStatus }} + {{ $num_of_clients_supported = inc $num_of_clients_supported }} +{{- end }} +{{- end }} +{{- end }} + +{{ if ge $num_of_clients_supported 1 }} It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func () { ctx := context.Background() err := emitter.Register() @@ -1137,7 +1151,11 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func expectedResources = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }})) unexpectedResource = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }})) } - namespaceResources := getMapOfNamespaceResources() + getList := func (ns string) ([]metadataGetter, error) { + l, err := {{ lower_camel .Name }}Client.List(ns, clients.ListOpts{}) + return convert{{ .PluralName }}ToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -1169,6 +1187,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func {{- end }} {{- end }} }) +{{- end }}{{/* if $num_of_clients_supported */}} }) Context("use different resource namespace listers", func() { diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index ced7d3df0..0615bbede 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -109,11 +109,11 @@ var _ = Describe("V1Emitter", func() { namespace2 = helpers.RandString(8) } - getMapOfNamespaceResources := func() map[string][]string { + getMapOfNamespaceResources := func(getList func(string) ([]metadataGetter, error)) map[string][]string { namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} namespaceResources := make(map[string][]string, len(namespaces)) for _, ns := range namespaces { - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + list, _ := getList(ns) for _, snap := range list { snapMeta := snap.GetMetadata() if _, hit := namespaceResources[snapMeta.Namespace]; hit { @@ -237,7 +237,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertkubeconfigsToMetadataGetter(expectkubeconfigs)) unexpectedResource = getMapOfResources(convertkubeconfigsToMetadataGetter(unexpectkubeconfigs)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := kubeConfigClient.List(ns, clients.ListOpts{}) + return convertkubeconfigsToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -753,22 +757,6 @@ var _ = Describe("V1Emitter", func() { getNewNamespaces() }) - It("should be able to return a resource from a deleted namespace, after the namespace is re-created", func() { - ctx := context.Background() - err := emitter.Register() - Expect(err).NotTo(HaveOccurred()) - - snapshots, errs, err := emitter.Snapshots([]string{""}, clients.WatchOpts{ - Ctx: ctx, - RefreshRate: time.Second, - ExpressionSelector: labelExpression1, - }) - Expect(err).NotTo(HaveOccurred()) - - var snap *TestingSnapshot - var previous *TestingSnapshot - - }) }) Context("use different resource namespace listers", func() { diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index d602948c1..b86c9ced3 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -153,11 +153,11 @@ var _ = Describe("V1Emitter", func() { namespace2 = helpers.RandString(8) } - getMapOfNamespaceResources := func() map[string][]string { + getMapOfNamespaceResources := func(getList func(string) ([]metadataGetter, error)) map[string][]string { namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} namespaceResources := make(map[string][]string, len(namespaces)) for _, ns := range namespaces { - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + list, _ := getList(ns) for _, snap := range list { snapMeta := snap.GetMetadata() if _, hit := namespaceResources[snapMeta.Namespace]; hit { @@ -316,7 +316,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertSimplemocksToMetadataGetter(expectSimplemocks)) unexpectedResource = getMapOfResources(convertSimplemocksToMetadataGetter(unexpectSimplemocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := simpleMockResourceClient.List(ns, clients.ListOpts{}) + return convertSimplemocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -420,7 +424,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -524,7 +532,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := fakeResourceClient.List(ns, clients.ListOpts{}) + return convertFakesToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -628,7 +640,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources)) unexpectedResource = getMapOfResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := anotherMockResourceClient.List(ns, clients.ListOpts{}) + return convertAnothermockresourcesToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -803,7 +819,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertmctsToMetadataGetter(expectmcts)) unexpectedResource = getMapOfResources(convertmctsToMetadataGetter(unexpectmcts)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockCustomTypeClient.List(ns, clients.ListOpts{}) + return convertmctsToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -907,7 +927,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertpodsToMetadataGetter(expectpods)) unexpectedResource = getMapOfResources(convertpodsToMetadataGetter(unexpectpods)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := podClient.List(ns, clients.ListOpts{}) + return convertpodsToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -3438,7 +3462,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -3500,7 +3528,11 @@ var _ = Describe("V1Emitter", func() { expectedResources = getMapOfResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources)) unexpectedResource = getMapOfResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := anotherMockResourceClient.List(ns, clients.ListOpts{}) + return convertAnothermockresourcesToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index 66ab32597..b706f6fb5 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -114,11 +114,11 @@ var _ = Describe("V1Alpha1Emitter", func() { namespace2 = helpers.RandString(8) } - getMapOfNamespaceResources := func() map[string][]string { + getMapOfNamespaceResources := func(getList func(string) ([]metadataGetter, error)) map[string][]string { namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} namespaceResources := make(map[string][]string, len(namespaces)) for _, ns := range namespaces { - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + list, _ := getList(ns) for _, snap := range list { snapMeta := snap.GetMetadata() if _, hit := namespaceResources[snapMeta.Namespace]; hit { @@ -242,7 +242,11 @@ var _ = Describe("V1Alpha1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -817,7 +821,11 @@ var _ = Describe("V1Alpha1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index befe5a99a..748928e5e 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -129,11 +129,11 @@ var _ = Describe("V2Alpha1Emitter", func() { namespace2 = helpers.RandString(8) } - getMapOfNamespaceResources := func() map[string][]string { + getMapOfNamespaceResources := func(getList func(string) ([]metadataGetter, error)) map[string][]string { namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} namespaceResources := make(map[string][]string, len(namespaces)) for _, ns := range namespaces { - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) + list, _ := getList(ns) for _, snap := range list { snapMeta := snap.GetMetadata() if _, hit := namespaceResources[snapMeta.Namespace]; hit { @@ -271,7 +271,11 @@ var _ = Describe("V2Alpha1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -375,7 +379,11 @@ var _ = Describe("V2Alpha1Emitter", func() { expectedResources = getMapOfResources(convertFcarsToMetadataGetter(expectFcars)) unexpectedResource = getMapOfResources(convertFcarsToMetadataGetter(unexpectFcars)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := frequentlyChangingAnnotationsResourceClient.List(ns, clients.ListOpts{}) + return convertFcarsToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -479,7 +487,11 @@ var _ = Describe("V2Alpha1Emitter", func() { expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := fakeResourceClient.List(ns, clients.ListOpts{}) + return convertFakesToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } @@ -1724,7 +1736,11 @@ var _ = Describe("V2Alpha1Emitter", func() { expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - namespaceResources := getMapOfNamespaceResources() + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } From a047e22a8fb59ae2d63f3a6f4d50e15de273118b Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 21 Sep 2022 14:05:30 +0000 Subject: [PATCH 74/98] Adding changelog file to new location --- changelog/v0.30.5/namespace-selectors.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 changelog/v0.30.5/namespace-selectors.yaml diff --git a/changelog/v0.30.5/namespace-selectors.yaml b/changelog/v0.30.5/namespace-selectors.yaml new file mode 100644 index 000000000..9e8920465 --- /dev/null +++ b/changelog/v0.30.5/namespace-selectors.yaml @@ -0,0 +1,9 @@ +changelog: + - type: NEW_FEATURE + issueLink: https://github.com/solo-io/gloo/issues/5868 + description: | + Added the ability to watch namespaces given be Expression Selectors in the Watch Opts + Watched Namespaces work as normally. When Expression Selectors the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set. \ No newline at end of file From 235307761406c13eb447429ed6ff40250edd57d9 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 21 Sep 2022 14:05:31 +0000 Subject: [PATCH 75/98] Deleting changelog file from old location --- changelog/v0.30.4/namespace-selectors.yaml | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 changelog/v0.30.4/namespace-selectors.yaml diff --git a/changelog/v0.30.4/namespace-selectors.yaml b/changelog/v0.30.4/namespace-selectors.yaml deleted file mode 100644 index 9e8920465..000000000 --- a/changelog/v0.30.4/namespace-selectors.yaml +++ /dev/null @@ -1,9 +0,0 @@ -changelog: - - type: NEW_FEATURE - issueLink: https://github.com/solo-io/gloo/issues/5868 - description: | - Added the ability to watch namespaces given be Expression Selectors in the Watch Opts - Watched Namespaces work as normally. When Expression Selectors the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set. \ No newline at end of file From f8e908c7f745b0ca7e39541ec6545726091346df Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 21 Sep 2022 15:34:32 -0500 Subject: [PATCH 76/98] Kube Cache now requires a boolean for creating the namespace lister --- .../configmap/configmap_base_client_test.go | 2 +- .../namespace/namespace_base_client_test.go | 2 +- .../external/kubernetes/pod/pod_base_client_test.go | 2 +- .../kubernetes/service/service_base_client_test.go | 2 +- .../tests/plain/plain_resource_client_test.go | 2 +- .../configmap/tests/struct/resource_client_test.go | 4 ++-- pkg/api/v1/clients/kube/cache/cache.go | 11 ++++++----- pkg/api/v1/clients/kube/cache/cache_test.go | 10 +++++----- .../tests/plain/plain_secret_resource_client_test.go | 2 +- pkg/multicluster/watch_kubeconfigs_test.go | 2 +- test/tests/typed/typed_rc_tester.go | 4 ++-- 11 files changed, 22 insertions(+), 21 deletions(-) diff --git a/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go b/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go index f725cd8ba..5fef9575b 100644 --- a/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go +++ b/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go @@ -38,7 +38,7 @@ var _ = Describe("Configmap base client", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client = NewConfigMapClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go b/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go index bf33cb220..36c5676cb 100644 --- a/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go +++ b/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go @@ -37,7 +37,7 @@ var _ = Describe("Namespace base client", func() { var err error namespace = helpers.RandString(8) kube = helpers.MustKubeClient() - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/pod/pod_base_client_test.go b/pkg/api/external/kubernetes/pod/pod_base_client_test.go index e1ebb7d53..25783f153 100644 --- a/pkg/api/external/kubernetes/pod/pod_base_client_test.go +++ b/pkg/api/external/kubernetes/pod/pod_base_client_test.go @@ -35,7 +35,7 @@ var _ = Describe("PodBaseClient", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/service/service_base_client_test.go b/pkg/api/external/kubernetes/service/service_base_client_test.go index 847d86a54..c3e80ad55 100644 --- a/pkg/api/external/kubernetes/service/service_base_client_test.go +++ b/pkg/api/external/kubernetes/service/service_base_client_test.go @@ -35,7 +35,7 @@ var _ = Describe("ServiceBaseClient", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go b/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go index ad39a674b..7edbcd613 100644 --- a/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go +++ b/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go @@ -44,7 +44,7 @@ var _ = Describe("PlainConfigmap", func() { kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, true) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go b/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go index 37f69e311..d217e545a 100644 --- a/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go +++ b/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go @@ -49,7 +49,7 @@ var _ = Describe("Base", func() { kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, false) Expect(err).NotTo(HaveOccurred()) @@ -101,7 +101,7 @@ var _ = Describe("Base", func() { err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns3) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, false) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index e36d44ed2..542904fbf 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -111,7 +111,7 @@ func NewCoreCacheForConfig(ctx context.Context, cluster string, restConfig *rest if err != nil { return nil } - c, err := NewKubeCoreCache(ctx, kubeClient) + c, err := NewKubeCoreCache(ctx, kubeClient, true) if err != nil { return nil } @@ -120,13 +120,14 @@ func NewCoreCacheForConfig(ctx context.Context, cluster string, restConfig *rest var _ clustercache.NewClusterCacheForConfig = NewCoreCacheForConfig +// creates the namespace lister. func NewFromConfigWithOptions(resyncDuration time.Duration, namesapcesToWatch []string) clustercache.NewClusterCacheForConfig { return func(ctx context.Context, cluster string, restConfig *rest.Config) clustercache.ClusterCache { kubeClient, err := kubernetes.NewForConfig(restConfig) if err != nil { return nil } - c, err := NewKubeCoreCacheWithOptions(ctx, kubeClient, resyncDuration, namesapcesToWatch) + c, err := NewKubeCoreCacheWithOptions(ctx, kubeClient, resyncDuration, namesapcesToWatch, true) if err != nil { return nil } @@ -136,12 +137,12 @@ func NewFromConfigWithOptions(resyncDuration time.Duration, namesapcesToWatch [] // This context should live as long as the cache is desired. i.e. if the cache is shared // across clients, it should get a context that has a longer lifetime than the clients themselves -func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface) (*kubeCoreCaches, error) { +func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface, createNamespaceLister bool) (*kubeCoreCaches, error) { resyncDuration := 12 * time.Hour - return NewKubeCoreCacheWithOptions(ctx, client, resyncDuration, []string{metav1.NamespaceAll}) + return NewKubeCoreCacheWithOptions(ctx, client, resyncDuration, []string{metav1.NamespaceAll}, createNamespaceLister) } -func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interface, resyncDuration time.Duration, namesapcesToWatch []string) (*kubeCoreCaches, error) { +func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interface, resyncDuration time.Duration, namesapcesToWatch []string, createNamespaceLister bool) (*kubeCoreCaches, error) { if len(namesapcesToWatch) == 0 { namesapcesToWatch = []string{metav1.NamespaceAll} diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index 77da276af..40b8c7f9e 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -67,7 +67,7 @@ var _ = Describe("kube core cache tests", func() { BeforeEach(func() { var err error - cache, err = NewKubeCoreCache(ctx, client) + cache, err = NewKubeCoreCache(ctx, client, true) Expect(err).NotTo(HaveOccurred()) }) @@ -104,7 +104,7 @@ var _ = Describe("kube core cache tests", func() { BeforeEach(func() { var err error - cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{"default"}) + cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{"default"}, true) Expect(err).NotTo(HaveOccurred()) }) @@ -133,7 +133,7 @@ var _ = Describe("kube core cache tests", func() { createNamespaceAndResource(ns) } var err error - cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{testns, testns2}) + cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{testns, testns2}, true) Expect(err).NotTo(HaveOccurred()) }) @@ -153,7 +153,7 @@ var _ = Describe("kube core cache tests", func() { Context("Invalid namespaces", func() { It("should error with invalid namespace config", func() { var err error - _, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{"default", ""}) + _, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{"default", ""}, true) Expect(err).To(HaveOccurred()) }) }) @@ -171,7 +171,7 @@ var _ = Describe("kube core cache tests", func() { createNamespaceAndResource(initialNs) var err error - cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{initialNs}) + cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{initialNs}, true) Expect(err).NotTo(HaveOccurred()) }) diff --git a/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go b/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go index 5d4a9a246..deb1eecf4 100644 --- a/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go +++ b/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go @@ -45,7 +45,7 @@ var _ = Describe("Kube Secret Client Plain=True", func() { err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kube) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kube, true) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, true, kcache) }) diff --git a/pkg/multicluster/watch_kubeconfigs_test.go b/pkg/multicluster/watch_kubeconfigs_test.go index ade59f623..f4a44bf23 100644 --- a/pkg/multicluster/watch_kubeconfigs_test.go +++ b/pkg/multicluster/watch_kubeconfigs_test.go @@ -72,7 +72,7 @@ var _ = Describe("WatchKubeconfigs", func() { kubeCfg2, err := secretconverter.KubeCfgFromSecret(kubeCfgSecret2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) Expect(err).NotTo(HaveOccurred()) kubeConfigs, errs, err := WatchKubeConfigs(context.TODO(), kubeClient, kubeCache) diff --git a/test/tests/typed/typed_rc_tester.go b/test/tests/typed/typed_rc_tester.go index 6b194229c..19fd52c6d 100644 --- a/test/tests/typed/typed_rc_tester.go +++ b/test/tests/typed/typed_rc_tester.go @@ -222,7 +222,7 @@ func (rct *KubeConfigMapRcTester) Setup(ctx context.Context, namespace string) f kubeClient := helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kubeClient, namespace) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) Expect(err).NotTo(HaveOccurred()) return &factory.KubeConfigMapClientFactory{ Clientset: kubeClient, @@ -255,7 +255,7 @@ func (rct *KubeSecretRcTester) Setup(ctx context.Context, namespace string) fact kubeClient := helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kubeClient, namespace) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) Expect(err).NotTo(HaveOccurred()) return &factory.KubeSecretClientFactory{ Clientset: kubeClient, From 8d91464d002df44b1ca7d8cf7260d1eead9a94ba Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 21 Sep 2022 16:12:04 -0500 Subject: [PATCH 77/98] update to fix merge conflict --- .../mock_custom_spec_hash_type_client.sk.go | 5 + .../mock_custom_spec_hash_type_client_test.go | 187 +++++++ test/mocks/v1/testing_event_loop_test.go | 2 + test/mocks/v1/testing_snapshot_emitter.sk.go | 123 ++++- .../mocks/v1/testing_snapshot_emitter_test.go | 504 +++++++++++++++++- 5 files changed, 778 insertions(+), 43 deletions(-) create mode 100644 test/mocks/v1/mock_custom_spec_hash_type_client_test.go diff --git a/test/mocks/v1/mock_custom_spec_hash_type_client.sk.go b/test/mocks/v1/mock_custom_spec_hash_type_client.sk.go index 42df31aba..22ed10bb1 100644 --- a/test/mocks/v1/mock_custom_spec_hash_type_client.sk.go +++ b/test/mocks/v1/mock_custom_spec_hash_type_client.sk.go @@ -19,6 +19,7 @@ type MockCustomSpecHashTypeWatcher interface { type MockCustomSpecHashTypeClient interface { BaseClient() clients.ResourceClient Register() error + RegisterNamespace(namespace string) error Read(namespace, name string, opts clients.ReadOpts) (*MockCustomSpecHashType, error) Write(resource *MockCustomSpecHashType, opts clients.WriteOpts) (*MockCustomSpecHashType, error) Delete(namespace, name string, opts clients.DeleteOpts) error @@ -59,6 +60,10 @@ func (client *mockCustomSpecHashTypeClient) Register() error { return client.rc.Register() } +func (client *mockCustomSpecHashTypeClient) RegisterNamespace(namespace string) error { + return client.rc.RegisterNamespace(namespace) +} + func (client *mockCustomSpecHashTypeClient) Read(namespace, name string, opts clients.ReadOpts) (*MockCustomSpecHashType, error) { opts = opts.WithDefaults() diff --git a/test/mocks/v1/mock_custom_spec_hash_type_client_test.go b/test/mocks/v1/mock_custom_spec_hash_type_client_test.go new file mode 100644 index 000000000..728f528bc --- /dev/null +++ b/test/mocks/v1/mock_custom_spec_hash_type_client_test.go @@ -0,0 +1,187 @@ +// Code generated by solo-kit. DO NOT EDIT. + +//go:build solokit +// +build solokit + +package v1 + +import ( + "context" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/solo-io/solo-kit/pkg/api/v1/clients" + "github.com/solo-io/solo-kit/pkg/api/v1/resources" + "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" + "github.com/solo-io/solo-kit/pkg/errors" + "github.com/solo-io/solo-kit/test/helpers" + "github.com/solo-io/solo-kit/test/tests/typed" +) + +var _ = Describe("MockCustomSpecHashTypeClient", func() { + var ctx context.Context + var ( + namespace string + ) + for _, test := range []typed.ResourceClientTester{ + &typed.ConsulRcTester{}, + &typed.FileRcTester{}, + &typed.MemoryRcTester{}, + &typed.VaultRcTester{}, + &typed.KubeSecretRcTester{}, + &typed.KubeConfigMapRcTester{}, + } { + Context("resource client backed by "+test.Description(), func() { + var ( + client MockCustomSpecHashTypeClient + err error + name1, name2, name3 = "foo" + helpers.RandString(3), "boo" + helpers.RandString(3), "goo" + helpers.RandString(3) + ) + + BeforeEach(func() { + namespace = helpers.RandString(6) + ctx = context.Background() + factory := test.Setup(ctx, namespace) + client, err = NewMockCustomSpecHashTypeClient(ctx, factory) + Expect(err).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + test.Teardown(ctx, namespace) + }) + + It("CRUDs MockCustomSpecHashTypes "+test.Description(), func() { + MockCustomSpecHashTypeClientTest(namespace, client, name1, name2, name3) + }) + }) + } +}) + +func MockCustomSpecHashTypeClientTest(namespace string, client MockCustomSpecHashTypeClient, name1, name2, name3 string) { + testOffset := 1 + + err := client.Register() + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + + name := name1 + input := NewMockCustomSpecHashType(namespace, name) + + r1, err := client.Write(input, clients.WriteOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + + _, err = client.Write(input, clients.WriteOpts{}) + ExpectWithOffset(testOffset, err).To(HaveOccurred()) + ExpectWithOffset(testOffset, errors.IsExist(err)).To(BeTrue()) + + ExpectWithOffset(testOffset, r1).To(BeAssignableToTypeOf(&MockCustomSpecHashType{})) + ExpectWithOffset(testOffset, r1.GetMetadata().Name).To(Equal(name)) + ExpectWithOffset(testOffset, r1.GetMetadata().Namespace).To(Equal(namespace)) + ExpectWithOffset(testOffset, r1.GetMetadata().ResourceVersion).NotTo(Equal(input.GetMetadata().ResourceVersion)) + ExpectWithOffset(testOffset, r1.GetMetadata().Ref()).To(Equal(input.GetMetadata().Ref())) + + _, err = client.Write(input, clients.WriteOpts{ + OverwriteExisting: true, + }) + ExpectWithOffset(testOffset, err).To(HaveOccurred()) + + resources.UpdateMetadata(input, func(meta *core.Metadata) { + meta.ResourceVersion = r1.GetMetadata().ResourceVersion + }) + r1, err = client.Write(input, clients.WriteOpts{ + OverwriteExisting: true, + }) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + read, err := client.Read(namespace, name, clients.ReadOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + ExpectWithOffset(testOffset, read).To(Equal(r1)) + _, err = client.Read("doesntexist", name, clients.ReadOpts{}) + ExpectWithOffset(testOffset, err).To(HaveOccurred()) + ExpectWithOffset(testOffset, errors.IsNotExist(err)).To(BeTrue()) + + name = name2 + input = &MockCustomSpecHashType{} + + input.SetMetadata(&core.Metadata{ + Name: name, + Namespace: namespace, + }) + + r2, err := client.Write(input, clients.WriteOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + list, err := client.List(namespace, clients.ListOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + ExpectWithOffset(testOffset, list).To(ContainElement(r1)) + ExpectWithOffset(testOffset, list).To(ContainElement(r2)) + err = client.Delete(namespace, "adsfw", clients.DeleteOpts{}) + ExpectWithOffset(testOffset, err).To(HaveOccurred()) + ExpectWithOffset(testOffset, errors.IsNotExist(err)).To(BeTrue()) + err = client.Delete(namespace, "adsfw", clients.DeleteOpts{ + IgnoreNotExist: true, + }) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + err = client.Delete(namespace, r2.GetMetadata().Name, clients.DeleteOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + + Eventually(func() MockCustomSpecHashTypeList { + list, err = client.List(namespace, clients.ListOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + return list + }, time.Second*10).Should(ContainElement(r1)) + Eventually(func() MockCustomSpecHashTypeList { + list, err = client.List(namespace, clients.ListOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + return list + }, time.Second*10).ShouldNot(ContainElement(r2)) + w, errs, err := client.Watch(namespace, clients.WatchOpts{ + RefreshRate: time.Hour, + }) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + + var r3 resources.Resource + wait := make(chan struct{}) + go func() { + defer close(wait) + defer GinkgoRecover() + + resources.UpdateMetadata(r2, func(meta *core.Metadata) { + meta.ResourceVersion = "" + }) + r2, err = client.Write(r2, clients.WriteOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + + name = name3 + input = &MockCustomSpecHashType{} + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + input.SetMetadata(&core.Metadata{ + Name: name, + Namespace: namespace, + }) + + r3, err = client.Write(input, clients.WriteOpts{}) + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + }() + <-wait + + select { + case err := <-errs: + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + case list = <-w: + case <-time.After(time.Millisecond * 5): + Fail("expected a message in channel") + } + + go func() { + defer GinkgoRecover() + for { + select { + case err := <-errs: + ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) + case <-time.After(time.Second / 4): + return + } + } + }() + + Eventually(w, time.Second*5, time.Second/10).Should(Receive(And(ContainElement(r1), ContainElement(r3), ContainElement(r3)))) +} diff --git a/test/mocks/v1/testing_event_loop_test.go b/test/mocks/v1/testing_event_loop_test.go index 353524c80..c6a2c26bf 100644 --- a/test/mocks/v1/testing_event_loop_test.go +++ b/test/mocks/v1/testing_event_loop_test.go @@ -101,6 +101,8 @@ var _ = Describe("TestingEventLoop", func() { Expect(err).NotTo(HaveOccurred()) _, err = emitter.MockCustomType().Write(NewMockCustomType(namespace, "jerry"), clients.WriteOpts{}) Expect(err).NotTo(HaveOccurred()) + _, err = emitter.MockCustomSpecHashType().Write(NewMockCustomSpecHashType(namespace, "jerry"), clients.WriteOpts{}) + Expect(err).NotTo(HaveOccurred()) _, err = emitter.Pod().Write(github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace, "jerry"), clients.WriteOpts{}) Expect(err).NotTo(HaveOccurred()) sync := &mockTestingSyncer{} diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 18516e88f..77e456699 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -93,7 +93,7 @@ type TestingEmitter interface { Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient } -func NewTestingEmitter(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, resourceNamespaceLister resources.ResourceNamespaceLister) TestingEmitter { +func NewTestingEmitter(simpleMockResourceClient SimpleMockResourceClient, mockResourceClient MockResourceClient, fakeResourceClient FakeResourceClient, anotherMockResourceClient AnotherMockResourceClient, clusterResourceClient ClusterResourceClient, mockCustomTypeClient MockCustomTypeClient, mockCustomSpecHashTypeClient MockCustomSpecHashTypeClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, resourceNamespaceLister resources.ResourceNamespaceLister) TestingEmitter { return NewTestingEmitterWithEmit(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, mockCustomSpecHashTypeClient, podClient, resourceNamespaceLister, make(chan struct{})) } @@ -105,8 +105,8 @@ func NewTestingEmitterWithEmit(simpleMockResourceClient SimpleMockResourceClient anotherMockResource: anotherMockResourceClient, clusterResource: clusterResourceClient, mockCustomType: mockCustomTypeClient, + mockCustomSpecHashType: mockCustomSpecHashTypeClient, pod: podClient, - mockCustomSpecHashType: mockCustomSpecHashTypeClient, resourceNamespaceLister: resourceNamespaceLister, forceEmit: emit, } @@ -263,7 +263,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO namespace string } mockCustomSpecHashTypeChan := make(chan mockCustomSpecHashTypeListWithNamespace) - var initialMockCustomSpecHashTypeList MockCustomSpecHashTypeList /* Create channel for Pod */ type podListWithNamespace struct { @@ -279,6 +278,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fakesByNamespace := sync.Map{} anothermockresourcesByNamespace := sync.Map{} mctsByNamespace := sync.Map{} + mcshtsByNamespace := sync.Map{} podsByNamespace := sync.Map{} if hasWatchedNamespaces || !watchingLabeledNamespaces { // then watch all resources on watch Namespaces @@ -295,18 +295,6 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO simplemocksByNamespace.Store(namespace, simplemocks) } simpleMockResourceNamespacesChan, simpleMockResourceErrs, err := c.simpleMockResource.Watch(namespace, watchedNamespacesWatchOptions) - simplemocksByNamespace := make(map[string]SimpleMockResourceList) - mocksByNamespace := make(map[string]MockResourceList) - fakesByNamespace := make(map[string]FakeResourceList) - anothermockresourcesByNamespace := make(map[string]AnotherMockResourceList) - mctsByNamespace := make(map[string]MockCustomTypeList) - mcshtsByNamespace := make(map[string]MockCustomSpecHashTypeList) - podsByNamespace := make(map[string]github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) - - for _, namespace := range watchNamespaces { - /* Setup namespaced watch for SimpleMockResource */ - { - simplemocks, err := c.simpleMockResource.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) if err != nil { return nil, nil, errors.Wrapf(err, "starting SimpleMockResource watch") } @@ -392,6 +380,25 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") }(namespace) + /* Setup namespaced watch for MockCustomSpecHashType */ + { + mcshts, err := c.mockCustomSpecHashType.List(namespace, watchedNamespacesListOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockCustomSpecHashType list") + } + initialMockCustomSpecHashTypeList = append(initialMockCustomSpecHashTypeList, mcshts...) + mcshtsByNamespace.Store(namespace, mcshts) + } + mockCustomSpecHashTypeNamespacesChan, mockCustomSpecHashTypeErrs, err := c.mockCustomSpecHashType.Watch(namespace, watchedNamespacesWatchOptions) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockCustomSpecHashType watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomSpecHashTypeErrs, namespace+"-mcshts") + }(namespace) /* Setup namespaced watch for Pod */ { pods, err := c.pod.List(namespace, watchedNamespacesListOptions) @@ -466,6 +473,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: } + case mockCustomSpecHashTypeList, ok := <-mockCustomSpecHashTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomSpecHashTypeChan <- mockCustomSpecHashTypeListWithNamespace{list: mockCustomSpecHashTypeList, namespace: namespace}: + } case podList, ok := <-podNamespacesChan: if !ok { return @@ -627,6 +643,29 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-mcts") }(namespace) + err = c.mockCustomSpecHashType.RegisterNamespace(namespace) + if err != nil { + return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the mockCustomSpecHashType") + } + /* Setup namespaced watch for MockCustomSpecHashType */ + { + mcshts, err := c.mockCustomSpecHashType.List(namespace, clients.ListOpts{Ctx: opts.Ctx}) + if err != nil { + return nil, nil, errors.Wrapf(err, "initial MockCustomSpecHashType list with new namespace") + } + initialMockCustomSpecHashTypeList = append(initialMockCustomSpecHashTypeList, mcshts...) + mcshtsByNamespace.Store(namespace, mcshts) + } + mockCustomSpecHashTypeNamespacesChan, mockCustomSpecHashTypeErrs, err := c.mockCustomSpecHashType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx}) + if err != nil { + return nil, nil, errors.Wrapf(err, "starting MockCustomSpecHashType watch") + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomSpecHashTypeErrs, namespace+"-mcshts") + }(namespace) err = c.pod.RegisterNamespace(namespace) if err != nil { return nil, nil, errors.Wrapf(err, "there was an error registering the namespace to the pod") @@ -701,6 +740,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: } + case mockCustomSpecHashTypeList, ok := <-mockCustomSpecHashTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomSpecHashTypeChan <- mockCustomSpecHashTypeListWithNamespace{list: mockCustomSpecHashTypeList, namespace: namespace}: + } case podList, ok := <-podNamespacesChan: if !ok { return @@ -782,6 +830,7 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO fakeResourceChan <- fakeResourceListWithNamespace{list: FakeResourceList{}, namespace: ns} anotherMockResourceChan <- anotherMockResourceListWithNamespace{list: AnotherMockResourceList{}, namespace: ns} mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: MockCustomTypeList{}, namespace: ns} + mockCustomSpecHashTypeChan <- mockCustomSpecHashTypeListWithNamespace{list: MockCustomSpecHashTypeList{}, namespace: ns} podChan <- podListWithNamespace{list: github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList{}, namespace: ns} } @@ -912,6 +961,31 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO defer done.Done() errutils.AggregateErrs(ctx, errs, mockCustomTypeErrs, namespace+"-new-namespace-mcts") }(namespace) + err = c.mockCustomSpecHashType.RegisterNamespace(namespace) + if err != nil { + errs <- errors.Wrapf(err, "there was an error registering the namespace to the mockCustomSpecHashType") + continue + } + /* Setup namespaced watch for MockCustomSpecHashType for new namespace */ + { + mcshts, err := c.mockCustomSpecHashType.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + if err != nil { + errs <- errors.Wrapf(err, "initial new namespace MockCustomSpecHashType list in namespace watch") + continue + } + mcshtsByNamespace.Store(namespace, mcshts) + } + mockCustomSpecHashTypeNamespacesChan, mockCustomSpecHashTypeErrs, err := c.mockCustomSpecHashType.Watch(namespace, clients.WatchOpts{Ctx: opts.Ctx, Selector: opts.Selector}) + if err != nil { + errs <- errors.Wrapf(err, "starting new namespace MockCustomSpecHashType watch") + continue + } + + done.Add(1) + go func(namespace string) { + defer done.Done() + errutils.AggregateErrs(ctx, errs, mockCustomSpecHashTypeErrs, namespace+"-new-namespace-mcshts") + }(namespace) err = c.pod.RegisterNamespace(namespace) if err != nil { errs <- errors.Wrapf(err, "there was an error registering the namespace to the pod") @@ -992,6 +1066,15 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO return case mockCustomTypeChan <- mockCustomTypeListWithNamespace{list: mockCustomTypeList, namespace: namespace}: } + case mockCustomSpecHashTypeList, ok := <-mockCustomSpecHashTypeNamespacesChan: + if !ok { + return + } + select { + case <-ctx.Done(): + return + case mockCustomSpecHashTypeChan <- mockCustomSpecHashTypeListWithNamespace{list: mockCustomSpecHashTypeList, namespace: namespace}: + } case podList, ok := <-podNamespacesChan: if !ok { return @@ -1246,11 +1329,13 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO ) // merge lists by namespace - mcshtsByNamespace[namespace] = mockCustomSpecHashTypeNamespacedList.list + mcshtsByNamespace.Store(namespace, mockCustomSpecHashTypeNamespacedList.list) var mockCustomSpecHashTypeList MockCustomSpecHashTypeList - for _, mcshts := range mcshtsByNamespace { - mockCustomSpecHashTypeList = append(mockCustomSpecHashTypeList, mcshts...) - } + mcshtsByNamespace.Range(func(key interface{}, value interface{}) bool { + mocks := value.(MockCustomSpecHashTypeList) + mockCustomSpecHashTypeList = append(mockCustomSpecHashTypeList, mocks...) + return true + }) currentSnapshot.Mcshts = mockCustomSpecHashTypeList.Sort() case podNamespacedList, ok := <-podChan: if !ok { diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index dc511af58..abe1ad588 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -53,28 +53,29 @@ var _ = Describe("V1Emitter", func() { } var ( - ctx context.Context - namespace1, namespace2 string - namespace3, namespace4 string - namespace5, namespace6 string - name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) - name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) - name5 = "melisa" + helpers.RandString(3) - labels1 = map[string]string{"env": "test"} - labelExpression1 = "env in (test)" - cfg *rest.Config - clientset *apiext.Clientset - kube kubernetes.Interface - emitter TestingEmitter - simpleMockResourceClient SimpleMockResourceClient - mockResourceClient MockResourceClient - fakeResourceClient FakeResourceClient - anotherMockResourceClient AnotherMockResourceClient - clusterResourceClient ClusterResourceClient - mockCustomTypeClient MockCustomTypeClient - podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient - resourceNamespaceLister resources.ResourceNamespaceLister - kubeCache cache.KubeCoreCache + ctx context.Context + namespace1, namespace2 string + namespace3, namespace4 string + namespace5, namespace6 string + name1, name2 = "angela" + helpers.RandString(3), "bob" + helpers.RandString(3) + name3, name4 = "susan" + helpers.RandString(3), "jim" + helpers.RandString(3) + name5 = "melisa" + helpers.RandString(3) + labels1 = map[string]string{"env": "test"} + labelExpression1 = "env in (test)" + cfg *rest.Config + clientset *apiext.Clientset + kube kubernetes.Interface + emitter TestingEmitter + simpleMockResourceClient SimpleMockResourceClient + mockResourceClient MockResourceClient + fakeResourceClient FakeResourceClient + anotherMockResourceClient AnotherMockResourceClient + clusterResourceClient ClusterResourceClient + mockCustomTypeClient MockCustomTypeClient + mockCustomSpecHashTypeClient MockCustomSpecHashTypeClient + podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient + resourceNamespaceLister resources.ResourceNamespaceLister + kubeCache cache.KubeCoreCache ) const ( TIME_BETWEEN_MESSAGES = 5 @@ -109,6 +110,11 @@ var _ = Describe("V1Emitter", func() { resource.GetMetadata().Labels = labels return resource } + NewMockCustomSpecHashTypeWithLabels := func(namespace, name string, labels map[string]string) *MockCustomSpecHashType { + resource := NewMockCustomSpecHashType(namespace, name) + resource.GetMetadata().Labels = labels + return resource + } NewPodWithLabels := func(namespace, name string, labels map[string]string) *github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.Pod { resource := github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPod(namespace, name) resource.GetMetadata().Labels = labels @@ -260,6 +266,13 @@ var _ = Describe("V1Emitter", func() { } return listConv } + convertmcshtsToMetadataGetter := func(rl MockCustomSpecHashTypeList) []metadataGetter { + listConv := make([]metadataGetter, len(rl)) + for i, r := range rl { + listConv[i] = r + } + return listConv + } convertpodsToMetadataGetter := func(rl github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList) []metadataGetter { listConv := make([]metadataGetter, len(rl)) for i, r := range rl { @@ -895,6 +908,114 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3, namespace4) getNewNamespaces() + /* + MockCustomSpecHashType + */ + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + var expectedResources map[string][]string + var unexpectedResource map[string][]string + + if previous != nil { + expectedResources = findNonMatchingResources(convertmcshtsToMetadataGetter(expectmcshts), convertmcshtsToMetadataGetter(previous.Mcshts)) + unexpectedResource = findMatchingResources(convertmcshtsToMetadataGetter(unexpectmcshts), convertmcshtsToMetadataGetter(previous.Mcshts)) + } else { + expectedResources = getMapOfResources(convertmcshtsToMetadataGetter(expectmcshts)) + unexpectedResource = getMapOfResources(convertmcshtsToMetadataGetter(unexpectmcshts)) + } + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockCustomSpecHashTypeClient.List(ns, clients.ListOpts{}) + return convertmcshtsToMetadataGetter(l), err + } + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) + } + } + } + + mockCustomSpecHashType1a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType1b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, nil) + + mockCustomSpecHashType3a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashTypeWithLabels(namespace1, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType3b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashTypeWithLabels(namespace2, name3, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched = append(mockCustomSpecHashTypeWatched, MockCustomSpecHashTypeList{mockCustomSpecHashType3a, mockCustomSpecHashType3b}...) + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, nil) + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaces(ctx, kube, namespace4) + + mockCustomSpecHashType4a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType4b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched = append(mockCustomSpecHashTypeWatched, mockCustomSpecHashType4a) + mockCustomSpecHashTypeNotWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType4b} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + mockCustomSpecHashType5a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashTypeWithLabels(namespace3, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType5b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashTypeWithLabels(namespace4, name2, labels1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched = append(mockCustomSpecHashTypeWatched, mockCustomSpecHashType5a) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, mockCustomSpecHashType5b) + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + for _, r := range mockCustomSpecHashTypeNotWatched { + err = mockCustomSpecHashTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1a.GetMetadata().Namespace, mockCustomSpecHashType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1b.GetMetadata().Namespace, mockCustomSpecHashType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b}...) + mockCustomSpecHashTypeWatched = MockCustomSpecHashTypeList{mockCustomSpecHashType3a, mockCustomSpecHashType3b, mockCustomSpecHashType4a, mockCustomSpecHashType5a} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType3a.GetMetadata().Namespace, mockCustomSpecHashType3a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType3b.GetMetadata().Namespace, mockCustomSpecHashType3b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, MockCustomSpecHashTypeList{mockCustomSpecHashType3a, mockCustomSpecHashType3b}...) + mockCustomSpecHashTypeWatched = MockCustomSpecHashTypeList{mockCustomSpecHashType4a, mockCustomSpecHashType5a} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType4a.GetMetadata().Namespace, mockCustomSpecHashType4a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType5a.GetMetadata().Namespace, mockCustomSpecHashType5a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, MockCustomSpecHashTypeList{mockCustomSpecHashType5a, mockCustomSpecHashType5b}...) + assertSnapshotmcshts(nil, mockCustomSpecHashTypeNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4) + getNewNamespaces() + /* Pod */ @@ -1085,6 +1206,13 @@ var _ = Describe("V1Emitter", func() { mockCustomTypeClient, err = NewMockCustomTypeClient(ctx, mockCustomTypeClientFactory) Expect(err).NotTo(HaveOccurred()) + // MockCustomSpecHashType Constructor + mockCustomSpecHashTypeClientFactory := &factory.MemoryResourceClientFactory{ + Cache: memory.NewInMemoryResourceCache(), + } + + mockCustomSpecHashTypeClient, err = NewMockCustomSpecHashTypeClient(ctx, mockCustomSpecHashTypeClientFactory) + Expect(err).NotTo(HaveOccurred()) // Pod Constructor podClientFactory := &factory.MemoryResourceClientFactory{ Cache: memory.NewInMemoryResourceCache(), @@ -1092,7 +1220,7 @@ var _ = Describe("V1Emitter", func() { podClient, err = github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.NewPodClient(ctx, podClientFactory) Expect(err).NotTo(HaveOccurred()) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, mockCustomSpecHashTypeClient, podClient, resourceNamespaceLister) }) AfterEach(func() { err := os.Unsetenv(statusutils.PodNamespaceEnvName) @@ -1449,6 +1577,63 @@ var _ = Describe("V1Emitter", func() { assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + /* + MockCustomSpecHashType + */ + + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomSpecHashTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomSpecHashTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + mockCustomSpecHashType1a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name5), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType1b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name5), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b}, nil) + mockCustomSpecHashType2a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType2b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b, mockCustomSpecHashType2a, mockCustomSpecHashType2b}, nil) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType2a.GetMetadata().Namespace, mockCustomSpecHashType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType2b.GetMetadata().Namespace, mockCustomSpecHashType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b}, MockCustomSpecHashTypeList{mockCustomSpecHashType2a, mockCustomSpecHashType2b}) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1a.GetMetadata().Namespace, mockCustomSpecHashType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1b.GetMetadata().Namespace, mockCustomSpecHashType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + assertSnapshotmcshts(nil, MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b, mockCustomSpecHashType2a, mockCustomSpecHashType2b}) + /* Pod */ @@ -1846,6 +2031,61 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) assertSnapshotmcts(nil, MockCustomTypeList{mockCustomType1a, mockCustomType1b, mockCustomType2a, mockCustomType2b}) + /* + MockCustomSpecHashType + */ + + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomSpecHashTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomSpecHashTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockCustomSpecHashType1a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType1b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b}, nil) + + mockCustomSpecHashType2a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType2b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b, mockCustomSpecHashType2a, mockCustomSpecHashType2b}, nil) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType2a.GetMetadata().Namespace, mockCustomSpecHashType2a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType2b.GetMetadata().Namespace, mockCustomSpecHashType2b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotmcshts(MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b}, MockCustomSpecHashTypeList{mockCustomSpecHashType2a, mockCustomSpecHashType2b}) + + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1a.GetMetadata().Namespace, mockCustomSpecHashType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1b.GetMetadata().Namespace, mockCustomSpecHashType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + assertSnapshotmcshts(nil, MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b, mockCustomSpecHashType2a, mockCustomSpecHashType2b}) + /* Pod */ @@ -2529,6 +2769,114 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) getNewNamespaces() + /* + MockCustomSpecHashType + */ + + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + previous = snap + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + + var buffer bytes.Buffer + if previous != nil { + for _, sn := range previous.Mcshts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } else { + buffer.WriteString("****** NO PREVIOUS SNAP ********") + } + namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} + for i, ns := range namespaces { + buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) + list, _ := mockCustomSpecHashTypeClient.List(ns, clients.ListOpts{}) + for _, sn := range list { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) + buffer.WriteByte('\n') + } + } + buffer.WriteString("********** EXPECTED *********") + for _, snap := range expectmcshts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + buffer.WriteString("********* UNEXPECTED ***********") + for _, snap := range unexpectmcshts { + buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + } + + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + } + } + } + + mockCustomSpecHashType1a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType1b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b} + + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockCustomSpecHashType2a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType2b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType2a, mockCustomSpecHashType2b} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + createNamespaces(ctx, kube, namespace5) + createNamespaceWithLabel(ctx, kube, namespace6, labels1) + + mockCustomSpecHashType5a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace5, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType5b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace6, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, mockCustomSpecHashType5a) + mockCustomSpecHashTypeWatched = append(mockCustomSpecHashTypeWatched, mockCustomSpecHashType5b) + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + mockCustomSpecHashType7a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace5, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType7b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace6, name4), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, mockCustomSpecHashType7a) + mockCustomSpecHashTypeWatched = append(mockCustomSpecHashTypeWatched, mockCustomSpecHashType7b) + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + for _, r := range mockCustomSpecHashTypeNotWatched { + err = mockCustomSpecHashTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + } + + for _, r := range mockCustomSpecHashTypeWatched { + err = mockCustomSpecHashTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, r) + } + assertSnapshotmcshts(nil, mockCustomSpecHashTypeNotWatched) + + // clean up environment + deleteNamespaces(ctx, kube, namespace3, namespace4, namespace5, namespace6) + getNewNamespaces() + /* Pod */ @@ -2945,6 +3293,55 @@ var _ = Describe("V1Emitter", func() { getNewNamespaces1and2() createNamespaces(ctx, kube, namespace1, namespace2) + /* + MockCustomSpecHashType + */ + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomSpecHashTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomSpecHashTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + mockCustomSpecHashType1a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace1, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType1b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace2, name2), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, nil) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1a.GetMetadata().Namespace, mockCustomSpecHashType1a.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + err = mockCustomSpecHashTypeClient.Delete(mockCustomSpecHashType1b.GetMetadata().Namespace, mockCustomSpecHashType1b.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + + mockCustomSpecHashTypeNotWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType1a, mockCustomSpecHashType1b} + assertSnapshotmcshts(nil, mockCustomSpecHashTypeNotWatched) + + deleteNamespaces(ctx, kube, namespace1, namespace2) + + getNewNamespaces1and2() + createNamespaces(ctx, kube, namespace1, namespace2) + /* Pod */ @@ -3355,6 +3752,65 @@ var _ = Describe("V1Emitter", func() { deleteNamespaces(ctx, kube, namespace4) getNewNamespaces() + /* + MockCustomSpecHashType + */ + + assertSnapshotmcshts := func(expectmcshts MockCustomSpecHashTypeList, unexpectmcshts MockCustomSpecHashTypeList) { + drain: + for { + select { + case snap = <-snapshots: + for _, expected := range expectmcshts { + if _, err := snap.Mcshts.Find(expected.GetMetadata().Ref().Strings()); err != nil { + continue drain + } + } + for _, unexpected := range unexpectmcshts { + if _, err := snap.Mcshts.Find(unexpected.GetMetadata().Ref().Strings()); err == nil { + continue drain + } + } + break drain + case err := <-errs: + Expect(err).NotTo(HaveOccurred()) + case <-time.After(time.Second * 10): + nsList1, _ := mockCustomSpecHashTypeClient.List(namespace1, clients.ListOpts{}) + nsList2, _ := mockCustomSpecHashTypeClient.List(namespace2, clients.ListOpts{}) + combined := append(nsList1, nsList2...) + Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) + } + } + } + + // create namespaces + createNamespaceWithLabel(ctx, kube, namespace3, labels1) + createNamespaceWithLabel(ctx, kube, namespace4, labels1) + + mockCustomSpecHashType2a, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace3, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashType2b, err := mockCustomSpecHashTypeClient.Write(NewMockCustomSpecHashType(namespace4, name1), clients.WriteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched := MockCustomSpecHashTypeList{} + mockCustomSpecHashTypeWatched := MockCustomSpecHashTypeList{mockCustomSpecHashType2a, mockCustomSpecHashType2b} + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + deleteNamespaces(ctx, kube, namespace3) + + mockCustomSpecHashTypeWatched = MockCustomSpecHashTypeList{mockCustomSpecHashType2b} + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, mockCustomSpecHashType2a) + assertSnapshotmcshts(mockCustomSpecHashTypeWatched, mockCustomSpecHashTypeNotWatched) + + for _, r := range mockCustomSpecHashTypeWatched { + err = mockCustomSpecHashTypeClient.Delete(r.GetMetadata().Namespace, r.GetMetadata().Name, clients.DeleteOpts{Ctx: ctx}) + Expect(err).NotTo(HaveOccurred()) + mockCustomSpecHashTypeNotWatched = append(mockCustomSpecHashTypeNotWatched, r) + } + assertSnapshotmcshts(nil, mockCustomSpecHashTypeNotWatched) + + deleteNamespaces(ctx, kube, namespace4) + getNewNamespaces() + /* Pod */ @@ -3568,7 +4024,7 @@ var _ = Describe("V1Emitter", func() { Context("use different resource namespace listers", func() { BeforeEach(func() { resourceNamespaceLister = namespace.NewKubeClientResourceNamespaceLister(kube) - emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, podClient, resourceNamespaceLister) + emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, mockCustomSpecHashTypeClient, podClient, resourceNamespaceLister) }) It("Should work with the Kube Client Namespace Lister", func() { From 4c7db7f5ee650d16808343d04df273a3421d25a5 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 21 Sep 2022 16:57:48 -0500 Subject: [PATCH 78/98] when namespaces is set to "" then create the namespace lister --- .../kubernetes/configmap/configmap_base_client_test.go | 2 +- .../kubernetes/namespace/namespace_base_client_test.go | 2 +- pkg/api/external/kubernetes/pod/pod_base_client_test.go | 2 +- .../external/kubernetes/service/service_base_client_test.go | 2 +- .../configmap/tests/plain/plain_resource_client_test.go | 2 +- .../clients/configmap/tests/struct/resource_client_test.go | 4 ++-- pkg/api/v1/clients/kube/cache/cache.go | 6 +++--- pkg/api/v1/clients/kube/cache/cache_test.go | 2 +- .../tests/plain/plain_secret_resource_client_test.go | 2 +- pkg/multicluster/watch_kubeconfigs_test.go | 2 +- test/tests/typed/typed_rc_tester.go | 4 ++-- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go b/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go index 5fef9575b..f725cd8ba 100644 --- a/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go +++ b/pkg/api/external/kubernetes/configmap/configmap_base_client_test.go @@ -38,7 +38,7 @@ var _ = Describe("Configmap base client", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client = NewConfigMapClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go b/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go index 36c5676cb..bf33cb220 100644 --- a/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go +++ b/pkg/api/external/kubernetes/namespace/namespace_base_client_test.go @@ -37,7 +37,7 @@ var _ = Describe("Namespace base client", func() { var err error namespace = helpers.RandString(8) kube = helpers.MustKubeClient() - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/pod/pod_base_client_test.go b/pkg/api/external/kubernetes/pod/pod_base_client_test.go index 25783f153..e1ebb7d53 100644 --- a/pkg/api/external/kubernetes/pod/pod_base_client_test.go +++ b/pkg/api/external/kubernetes/pod/pod_base_client_test.go @@ -35,7 +35,7 @@ var _ = Describe("PodBaseClient", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/external/kubernetes/service/service_base_client_test.go b/pkg/api/external/kubernetes/service/service_base_client_test.go index c3e80ad55..847d86a54 100644 --- a/pkg/api/external/kubernetes/service/service_base_client_test.go +++ b/pkg/api/external/kubernetes/service/service_base_client_test.go @@ -35,7 +35,7 @@ var _ = Describe("ServiceBaseClient", func() { namespace = helpers.RandString(8) kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, namespace) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client = newResourceClient(kube, kubeCache) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go b/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go index 7edbcd613..ad39a674b 100644 --- a/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go +++ b/pkg/api/v1/clients/configmap/tests/plain/plain_resource_client_test.go @@ -44,7 +44,7 @@ var _ = Describe("PlainConfigmap", func() { kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, true) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go b/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go index d217e545a..37f69e311 100644 --- a/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go +++ b/pkg/api/v1/clients/configmap/tests/struct/resource_client_test.go @@ -49,7 +49,7 @@ var _ = Describe("Base", func() { kube = helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, false) Expect(err).NotTo(HaveOccurred()) @@ -101,7 +101,7 @@ var _ = Describe("Base", func() { err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns3) Expect(err).NotTo(HaveOccurred()) - kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube, true) + kubeCache, err = cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, kubeCache, false) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index 542904fbf..e19292ecd 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -111,7 +111,7 @@ func NewCoreCacheForConfig(ctx context.Context, cluster string, restConfig *rest if err != nil { return nil } - c, err := NewKubeCoreCache(ctx, kubeClient, true) + c, err := NewKubeCoreCache(ctx, kubeClient) if err != nil { return nil } @@ -137,9 +137,9 @@ func NewFromConfigWithOptions(resyncDuration time.Duration, namesapcesToWatch [] // This context should live as long as the cache is desired. i.e. if the cache is shared // across clients, it should get a context that has a longer lifetime than the clients themselves -func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface, createNamespaceLister bool) (*kubeCoreCaches, error) { +func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface) (*kubeCoreCaches, error) { resyncDuration := 12 * time.Hour - return NewKubeCoreCacheWithOptions(ctx, client, resyncDuration, []string{metav1.NamespaceAll}, createNamespaceLister) + return NewKubeCoreCacheWithOptions(ctx, client, resyncDuration, []string{metav1.NamespaceAll}, true) } func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interface, resyncDuration time.Duration, namesapcesToWatch []string, createNamespaceLister bool) (*kubeCoreCaches, error) { diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index 40b8c7f9e..d91512d33 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -67,7 +67,7 @@ var _ = Describe("kube core cache tests", func() { BeforeEach(func() { var err error - cache, err = NewKubeCoreCache(ctx, client, true) + cache, err = NewKubeCoreCache(ctx, client) Expect(err).NotTo(HaveOccurred()) }) diff --git a/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go b/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go index deb1eecf4..5d4a9a246 100644 --- a/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go +++ b/pkg/api/v1/clients/kubesecret/tests/plain/plain_secret_resource_client_test.go @@ -45,7 +45,7 @@ var _ = Describe("Kube Secret Client Plain=True", func() { err := kubeutils.CreateNamespacesInParallel(ctx, kube, ns1, ns2) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kube, true) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kube) Expect(err).NotTo(HaveOccurred()) client, err = NewResourceClient(kube, &v1.MockResource{}, true, kcache) }) diff --git a/pkg/multicluster/watch_kubeconfigs_test.go b/pkg/multicluster/watch_kubeconfigs_test.go index f4a44bf23..ade59f623 100644 --- a/pkg/multicluster/watch_kubeconfigs_test.go +++ b/pkg/multicluster/watch_kubeconfigs_test.go @@ -72,7 +72,7 @@ var _ = Describe("WatchKubeconfigs", func() { kubeCfg2, err := secretconverter.KubeCfgFromSecret(kubeCfgSecret2) Expect(err).NotTo(HaveOccurred()) - kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) + kubeCache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) Expect(err).NotTo(HaveOccurred()) kubeConfigs, errs, err := WatchKubeConfigs(context.TODO(), kubeClient, kubeCache) diff --git a/test/tests/typed/typed_rc_tester.go b/test/tests/typed/typed_rc_tester.go index 19fd52c6d..6b194229c 100644 --- a/test/tests/typed/typed_rc_tester.go +++ b/test/tests/typed/typed_rc_tester.go @@ -222,7 +222,7 @@ func (rct *KubeConfigMapRcTester) Setup(ctx context.Context, namespace string) f kubeClient := helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kubeClient, namespace) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) Expect(err).NotTo(HaveOccurred()) return &factory.KubeConfigMapClientFactory{ Clientset: kubeClient, @@ -255,7 +255,7 @@ func (rct *KubeSecretRcTester) Setup(ctx context.Context, namespace string) fact kubeClient := helpers.MustKubeClient() err := kubeutils.CreateNamespacesInParallel(ctx, kubeClient, namespace) Expect(err).NotTo(HaveOccurred()) - kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient, true) + kcache, err := cache.NewKubeCoreCache(context.TODO(), kubeClient) Expect(err).NotTo(HaveOccurred()) return &factory.KubeSecretClientFactory{ Clientset: kubeClient, From 1c1fa6ca7864b049043f6c5da50d8fdfa911e7fd Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Wed, 21 Sep 2022 17:35:09 -0500 Subject: [PATCH 79/98] condition on namespace lister --- pkg/api/v1/clients/kube/cache/cache.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index e19292ecd..1cf68a13d 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -176,8 +176,9 @@ func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interfac k.addNewNamespace(nsToWatch) } - // since we now allow namespaces to be registered dynamically we will always create the namespace lister - k.addNamespaceLister() + if createNamespaceLister { + k.addNamespaceLister() + } k.kubeController = controller.NewController("kube-plugin-controller", controller.NewLockingSyncHandler(k.updatedOccured), k.informers..., From 66ddd96470813a5b333fcc50c93f96d2f960e44c Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 22 Sep 2022 10:46:23 -0500 Subject: [PATCH 80/98] delete failing mock_custom spec hash resource --- .../mock_custom_spec_hash_type_client_test.go | 187 ------------------ 1 file changed, 187 deletions(-) delete mode 100644 test/mocks/v1/mock_custom_spec_hash_type_client_test.go diff --git a/test/mocks/v1/mock_custom_spec_hash_type_client_test.go b/test/mocks/v1/mock_custom_spec_hash_type_client_test.go deleted file mode 100644 index 728f528bc..000000000 --- a/test/mocks/v1/mock_custom_spec_hash_type_client_test.go +++ /dev/null @@ -1,187 +0,0 @@ -// Code generated by solo-kit. DO NOT EDIT. - -//go:build solokit -// +build solokit - -package v1 - -import ( - "context" - "time" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "github.com/solo-io/solo-kit/pkg/api/v1/clients" - "github.com/solo-io/solo-kit/pkg/api/v1/resources" - "github.com/solo-io/solo-kit/pkg/api/v1/resources/core" - "github.com/solo-io/solo-kit/pkg/errors" - "github.com/solo-io/solo-kit/test/helpers" - "github.com/solo-io/solo-kit/test/tests/typed" -) - -var _ = Describe("MockCustomSpecHashTypeClient", func() { - var ctx context.Context - var ( - namespace string - ) - for _, test := range []typed.ResourceClientTester{ - &typed.ConsulRcTester{}, - &typed.FileRcTester{}, - &typed.MemoryRcTester{}, - &typed.VaultRcTester{}, - &typed.KubeSecretRcTester{}, - &typed.KubeConfigMapRcTester{}, - } { - Context("resource client backed by "+test.Description(), func() { - var ( - client MockCustomSpecHashTypeClient - err error - name1, name2, name3 = "foo" + helpers.RandString(3), "boo" + helpers.RandString(3), "goo" + helpers.RandString(3) - ) - - BeforeEach(func() { - namespace = helpers.RandString(6) - ctx = context.Background() - factory := test.Setup(ctx, namespace) - client, err = NewMockCustomSpecHashTypeClient(ctx, factory) - Expect(err).NotTo(HaveOccurred()) - }) - - AfterEach(func() { - test.Teardown(ctx, namespace) - }) - - It("CRUDs MockCustomSpecHashTypes "+test.Description(), func() { - MockCustomSpecHashTypeClientTest(namespace, client, name1, name2, name3) - }) - }) - } -}) - -func MockCustomSpecHashTypeClientTest(namespace string, client MockCustomSpecHashTypeClient, name1, name2, name3 string) { - testOffset := 1 - - err := client.Register() - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - - name := name1 - input := NewMockCustomSpecHashType(namespace, name) - - r1, err := client.Write(input, clients.WriteOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - - _, err = client.Write(input, clients.WriteOpts{}) - ExpectWithOffset(testOffset, err).To(HaveOccurred()) - ExpectWithOffset(testOffset, errors.IsExist(err)).To(BeTrue()) - - ExpectWithOffset(testOffset, r1).To(BeAssignableToTypeOf(&MockCustomSpecHashType{})) - ExpectWithOffset(testOffset, r1.GetMetadata().Name).To(Equal(name)) - ExpectWithOffset(testOffset, r1.GetMetadata().Namespace).To(Equal(namespace)) - ExpectWithOffset(testOffset, r1.GetMetadata().ResourceVersion).NotTo(Equal(input.GetMetadata().ResourceVersion)) - ExpectWithOffset(testOffset, r1.GetMetadata().Ref()).To(Equal(input.GetMetadata().Ref())) - - _, err = client.Write(input, clients.WriteOpts{ - OverwriteExisting: true, - }) - ExpectWithOffset(testOffset, err).To(HaveOccurred()) - - resources.UpdateMetadata(input, func(meta *core.Metadata) { - meta.ResourceVersion = r1.GetMetadata().ResourceVersion - }) - r1, err = client.Write(input, clients.WriteOpts{ - OverwriteExisting: true, - }) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - read, err := client.Read(namespace, name, clients.ReadOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - ExpectWithOffset(testOffset, read).To(Equal(r1)) - _, err = client.Read("doesntexist", name, clients.ReadOpts{}) - ExpectWithOffset(testOffset, err).To(HaveOccurred()) - ExpectWithOffset(testOffset, errors.IsNotExist(err)).To(BeTrue()) - - name = name2 - input = &MockCustomSpecHashType{} - - input.SetMetadata(&core.Metadata{ - Name: name, - Namespace: namespace, - }) - - r2, err := client.Write(input, clients.WriteOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - list, err := client.List(namespace, clients.ListOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - ExpectWithOffset(testOffset, list).To(ContainElement(r1)) - ExpectWithOffset(testOffset, list).To(ContainElement(r2)) - err = client.Delete(namespace, "adsfw", clients.DeleteOpts{}) - ExpectWithOffset(testOffset, err).To(HaveOccurred()) - ExpectWithOffset(testOffset, errors.IsNotExist(err)).To(BeTrue()) - err = client.Delete(namespace, "adsfw", clients.DeleteOpts{ - IgnoreNotExist: true, - }) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - err = client.Delete(namespace, r2.GetMetadata().Name, clients.DeleteOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - - Eventually(func() MockCustomSpecHashTypeList { - list, err = client.List(namespace, clients.ListOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - return list - }, time.Second*10).Should(ContainElement(r1)) - Eventually(func() MockCustomSpecHashTypeList { - list, err = client.List(namespace, clients.ListOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - return list - }, time.Second*10).ShouldNot(ContainElement(r2)) - w, errs, err := client.Watch(namespace, clients.WatchOpts{ - RefreshRate: time.Hour, - }) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - - var r3 resources.Resource - wait := make(chan struct{}) - go func() { - defer close(wait) - defer GinkgoRecover() - - resources.UpdateMetadata(r2, func(meta *core.Metadata) { - meta.ResourceVersion = "" - }) - r2, err = client.Write(r2, clients.WriteOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - - name = name3 - input = &MockCustomSpecHashType{} - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - input.SetMetadata(&core.Metadata{ - Name: name, - Namespace: namespace, - }) - - r3, err = client.Write(input, clients.WriteOpts{}) - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - }() - <-wait - - select { - case err := <-errs: - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - case list = <-w: - case <-time.After(time.Millisecond * 5): - Fail("expected a message in channel") - } - - go func() { - defer GinkgoRecover() - for { - select { - case err := <-errs: - ExpectWithOffset(testOffset, err).NotTo(HaveOccurred()) - case <-time.After(time.Second / 4): - return - } - } - }() - - Eventually(w, time.Second*5, time.Second/10).Should(Receive(And(ContainElement(r1), ContainElement(r3), ContainElement(r3)))) -} From eee5ed2fc55dfe67fe68847a92213e3343792895 Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 22 Sep 2022 13:03:04 -0500 Subject: [PATCH 81/98] some touch ups. Errors are now logged in the list and watch functions of the informers. added some comments, and some other things. --- README.md | 2 +- changelog/v0.30.5/namespace-selectors.yaml | 9 +- pkg/api/v1/clients/kube/cache/cache.go | 11 +- pkg/api/v1/clients/kube/cache/cache_test.go | 4 +- .../v1/clients/kube/controller/controller.go | 19 +- .../kube/controller/controller_test.go | 2 +- .../clients/kube/resource_client_factory.go | 25 +- .../templates/snapshot_emitter_template.go | 19 +- .../snapshot_emitter_test_template.go | 40 +-- .../v1/kubeconfigs_snapshot_emitter.sk.go | 9 +- .../v1/kubeconfigs_snapshot_emitter_test.go | 38 +-- test/mocks/v1/non_template_tests_test.go | 1 - test/mocks/v1/testing_snapshot_emitter.sk.go | 9 +- .../mocks/v1/testing_snapshot_emitter_test.go | 261 ++++++------------ .../v1alpha1/testing_snapshot_emitter.sk.go | 9 +- .../v1alpha1/testing_snapshot_emitter_test.go | 42 +-- .../v2alpha1/testing_snapshot_emitter.sk.go | 9 +- .../v2alpha1/testing_snapshot_emitter_test.go | 114 +++----- 18 files changed, 234 insertions(+), 389 deletions(-) diff --git a/README.md b/README.md index a210c99b5..f6eae39d3 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A collection of code generation and libraries to for API development. ### Testing To generate tests set `SkipGeneratedTests` to false in the `generate.go` file. This will use the test templates -to generate the code for the tests. Such as files like `pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go` +to generate the code for the tests. This will allow generation of templates such as `pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go` ### Description: - Define your declarative API in `.proto` files diff --git a/changelog/v0.30.5/namespace-selectors.yaml b/changelog/v0.30.5/namespace-selectors.yaml index 9e8920465..2bf84cbd9 100644 --- a/changelog/v0.30.5/namespace-selectors.yaml +++ b/changelog/v0.30.5/namespace-selectors.yaml @@ -1,9 +1,10 @@ changelog: - - type: NEW_FEATURE + - type: BREAKING_CHANGE issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false description: | - Added the ability to watch namespaces given be Expression Selectors in the Watch Opts - Watched Namespaces work as normally. When Expression Selectors the snapshot + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot emitter will watch, in addition to the watched namespaces, namespaces that are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set. \ No newline at end of file + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file diff --git a/pkg/api/v1/clients/kube/cache/cache.go b/pkg/api/v1/clients/kube/cache/cache.go index 1cf68a13d..1c682dbc6 100644 --- a/pkg/api/v1/clients/kube/cache/cache.go +++ b/pkg/api/v1/clients/kube/cache/cache.go @@ -96,10 +96,8 @@ type kubeCoreCaches struct { resyncDuration time.Duration // informers are the kube resources that provide events informers []cache.SharedIndexInformer - // registerNamespaceLock is a map string(namespace) -> sync.Once. Is used to register namespaces only once. - registerNamespaceLock sync.Map - + registerNamespaceLock sync.Map cacheUpdatedWatchers []chan struct{} cacheUpdatedWatchersMutex sync.Mutex } @@ -120,7 +118,7 @@ func NewCoreCacheForConfig(ctx context.Context, cluster string, restConfig *rest var _ clustercache.NewClusterCacheForConfig = NewCoreCacheForConfig -// creates the namespace lister. +// NewFromConfigWithOptions creates a new Cluster Cahce For Config function. The function will create the namespace lister by default. func NewFromConfigWithOptions(resyncDuration time.Duration, namesapcesToWatch []string) clustercache.NewClusterCacheForConfig { return func(ctx context.Context, cluster string, restConfig *rest.Config) clustercache.ClusterCache { kubeClient, err := kubernetes.NewForConfig(restConfig) @@ -135,6 +133,7 @@ func NewFromConfigWithOptions(resyncDuration time.Duration, namesapcesToWatch [] } } +// NewKubeCoreCache will create a new kube Core Caches. The namespace lister is created from this function. // This context should live as long as the cache is desired. i.e. if the cache is shared // across clients, it should get a context that has a longer lifetime than the clients themselves func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface) (*kubeCoreCaches, error) { @@ -142,6 +141,8 @@ func NewKubeCoreCache(ctx context.Context, client kubernetes.Interface) (*kubeCo return NewKubeCoreCacheWithOptions(ctx, client, resyncDuration, []string{metav1.NamespaceAll}, true) } +// NewKubeCoreCacheWithOptions will create a new kube Core Cache. By setting the +// createNamespaceLister the namespace lister will be created. func NewKubeCoreCacheWithOptions(ctx context.Context, client kubernetes.Interface, resyncDuration time.Duration, namesapcesToWatch []string, createNamespaceLister bool) (*kubeCoreCaches, error) { if len(namesapcesToWatch) == 0 { @@ -286,7 +287,7 @@ func (k *kubeCoreCaches) RegisterNewNamespaceCache(namespace string) error { onceFunc := once.(*onceAndSent) onceFunc.Once.Do(func() { informers := k.addNewNamespace(namespace) - if err := k.kubeController.AddNewListOfInformers(informers); err != nil { + if err := k.kubeController.AddNewOfInformers(informers...); err != nil { onceFunc.Err = errors.Wrapf(err, "failed to add new list of informers to kube controller") } }) diff --git a/pkg/api/v1/clients/kube/cache/cache_test.go b/pkg/api/v1/clients/kube/cache/cache_test.go index d91512d33..fc9401d95 100644 --- a/pkg/api/v1/clients/kube/cache/cache_test.go +++ b/pkg/api/v1/clients/kube/cache/cache_test.go @@ -133,7 +133,7 @@ var _ = Describe("kube core cache tests", func() { createNamespaceAndResource(ns) } var err error - cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{testns, testns2}, true) + cache, err = NewKubeCoreCacheWithOptions(ctx, client, time.Hour, []string{testns, testns2}, false) Expect(err).NotTo(HaveOccurred()) }) @@ -144,7 +144,7 @@ var _ = Describe("kube core cache tests", func() { }) It("can list resources for all listers", func() { - Expect(cache.NamespaceLister()).ToNot(BeNil()) + Expect(cache.NamespaceLister()).To(BeNil()) validateNamespaceResource(testns) validateNamespaceResource(testns2) }) diff --git a/pkg/api/v1/clients/kube/controller/controller.go b/pkg/api/v1/clients/kube/controller/controller.go index 36f39c97f..3a4ef8da6 100644 --- a/pkg/api/v1/clients/kube/controller/controller.go +++ b/pkg/api/v1/clients/kube/controller/controller.go @@ -108,6 +108,7 @@ func (c *Controller) waitForCacheToSync() error { return nil } +// setupInformer // 1. Get the function to tell if it has synced // 2. Register the event handler with the informer // 3. Run the informer @@ -117,24 +118,10 @@ func (c *Controller) setupInformer(informer cache.SharedIndexInformer) { go informer.Run(c.stopCh) } -// AddNewInformer will add a new informer to the already running controller -// if the controller is not running, it will just append the informer to the controllers -// list of informers -func (c *Controller) AddNewInformer(newInformer cache.SharedIndexInformer) error { - c.informers = append(c.informers, newInformer) - if c.isRunning { - c.setupInformer(newInformer) - if err := c.waitForCacheToSync(); err != nil { - return err - } - } - return nil -} - -// AddNewListOfInformers will add a list of new informers to the already running controller. +// AddNewOfInformers will add a list of new informers to the already running controller. // If the controller is not running, it will just append the informers to the controllers // list of informers -func (c *Controller) AddNewListOfInformers(newInformers []cache.SharedIndexInformer) error { +func (c *Controller) AddNewOfInformers(newInformers ...cache.SharedIndexInformer) error { c.informers = append(c.informers, newInformers...) if c.isRunning { for _, in := range newInformers { diff --git a/pkg/api/v1/clients/kube/controller/controller_test.go b/pkg/api/v1/clients/kube/controller/controller_test.go index 58ddbdeeb..8f23c5cc8 100644 --- a/pkg/api/v1/clients/kube/controller/controller_test.go +++ b/pkg/api/v1/clients/kube/controller/controller_test.go @@ -141,7 +141,7 @@ var _ = Describe("Test KubeController", func() { case <-time.After(100 * time.Millisecond): } - err = kubeController.AddNewInformer(newInformer) + err = kubeController.AddNewOfInformers(newInformer) Expect(err).NotTo(HaveOccurred()) getResultFromkMockResource(namespace2, name2, value2) diff --git a/pkg/api/v1/clients/kube/resource_client_factory.go b/pkg/api/v1/clients/kube/resource_client_factory.go index d295e0a9b..468290d7f 100644 --- a/pkg/api/v1/clients/kube/resource_client_factory.go +++ b/pkg/api/v1/clients/kube/resource_client_factory.go @@ -132,10 +132,8 @@ type ResourceClientSharedInformerFactory struct { // kubeController is the controller used to watch for events on the informers. It can be used to add new informers too. kubeController *controller.Controller - - // registerNamespaceLock is a map of string(namespace) -> sync.Once. It is used to register a new namespace. + // registerNamespaceLock is a map of string(namespace) -> Type -> sync.Once. It is used to register a new namespace Type. registerNamespaceLock sync.Map - // registryLock is used when adding or getting information from the registry registryLock sync.Mutex // startingLock is used when checking isRunning or to lock starting of the SharedInformer @@ -163,7 +161,11 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType } stats.Record(listCtx, MLists.M(1), MInFlight.M(1)) defer stats.Record(listCtx, MInFlight.M(-1)) - return listFunc(options) + listOfResources, err := listFunc(options) + if err != nil { + contextutils.LoggerFrom(ctx).Error(errors.Wrapf(err, "listing crs from the resource client factory")) + } + return listOfResources, err }, WatchFunc: func(options metav1.ListOptions) (kubewatch.Interface, error) { watchCtx := ctx @@ -173,7 +175,11 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType stats.Record(watchCtx, MWatches.M(1), MInFlight.M(1)) defer stats.Record(watchCtx, MInFlight.M(-1)) - return watchFunc(ctx, options) + watches, err := watchFunc(ctx, options) + if err != nil { + contextutils.LoggerFrom(ctx).Error(errors.Wrapf(err, "watching crs from the resource client factory")) + } + return watches, err }, }, objType, @@ -185,10 +191,9 @@ func NewSharedInformer(ctx context.Context, resyncPeriod time.Duration, objType // Creates a new SharedIndexInformer and adds it to the factory's informer registry. // This method is meant to be called once per rc namespace set, please call Register New Namespace when adding new namespaces. // NOTE: Currently we cannot share informers between resource clients, because the listWatch functions are configured -// with the client's specific token. Hence, we must enforce a one-to-one relationship between informers and clients. +// with the client's specific token(resource client type). Hence, we must enforce a one-to-one relationship between informers and clients. func (f *ResourceClientSharedInformerFactory) Register(rc *ResourceClient) error { // because we do not know that we have started yet or not, we need to make a lock - // once we know, we can proceed f.startingLock.Lock() ctx := f.ctx if f.started { @@ -315,8 +320,8 @@ func (f *ResourceClientSharedInformerFactory) Start() { // RegisterNewNamespace is used when the resource client is running. This will add a new namespace to the // kube controller so that events can be received. func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace string, rc *ResourceClient) error { - // because this is an exposed function, Register New Namespace could be called at any time - // in that event, we want to make sure that the cache has started, the reason is because + // because this is an exposed function, Register New Namespace could be called at any time. + // In that event, we want to make sure that the cache has started. The reason is because // we have to initiallize the default namespaces as well as this new namespace f.Start() @@ -337,7 +342,7 @@ func (f *ResourceClientSharedInformerFactory) RegisterNewNamespace(namespace str onceSent.Err = errors.Wrapf(err, "failed to add new namespace to registry:") return } - if err := f.kubeController.AddNewInformer(informer); err != nil { + if err := f.kubeController.AddNewOfInformers(informer); err != nil { onceSent.Err = errors.Wrapf(err, "failed to add new informer to kube controller") return } diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go index d9db181ea..fcdb17663 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_template.go @@ -9,9 +9,13 @@ import ( // the snapshot emitter when new resources have been created or updated. // Snapshot Emitters will delegate to Resource Clients to list and watch defined // resources. - -// ClusterScoped - without namespacing, get all the resources within the entire cluster. There is one watch per resource. -// this means that Expression Selectors will have no impact on namespaces. +// Non-ClusterScoped - gets all the resources within the watched set of namespaces. If Watched Namespaces is +// set to [""] then watches all namespaces. If the Expression Selector on the WatchOpts is set, then it will watch +// namespaces that meet the label criteria on the Expression Selector in addition to the watched namespaces. +// If watched Namespaces is set to [""] with the Expression Selectors, then it only watches the namespaces that meet the +// label criteria on the Expression Selectors. +// ClusterScoped -- there is no namespacing. Gets all the resources within the entire cluster. There is one watch per resource. +// Setting the Expression Selectors will have no impact on namespaces. // Not using ClusterScoped - allows for using namespacing, so that each namespace has it's own watch per resource. var ResourceGroupEmitterTemplate = template.Must(template.New("resource_group_emitter").Funcs(Funcs).Parse( `package {{ .Project.ProjectConfig.Version }} @@ -170,7 +174,8 @@ func (c *{{ lower_camel $.GoName }}Emitter) {{ .Name }}() {{ .ImportPrefix }}{{ // ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources // that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is // set, then all namespaces that meet the label criteria of the ExpressionSelector will -// also be watched. +// also be watched. If Expression Selector is set and watched namespaces is set to [""], then it +// will only watch namespaces that meet the label expression selector criteria. func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -391,17 +396,17 @@ type {{ lower_camel .Name }}ListWithNamespace struct { c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces - mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) + mapOfResourceNamespaces := make(map[string]struct{}, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) } - mapOfResourceNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = struct{}{} } for _, ns := range watchNamespaces { - mapOfResourceNamespaces[ns] = true + mapOfResourceNamespaces[ns] = struct{}{} } missingNamespaces := []string{} diff --git a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go index 841b4b611..169cdf94f 100644 --- a/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go +++ b/pkg/code-generator/codegen/templates/snapshot_emitter_test_template.go @@ -757,34 +757,22 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", combined)) {{- else }} - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.{{ upper_camel .PluralName }} { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) + unexpectedResource = findMatchingResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }}), convert{{ .PluralName }}ToMetadataGetter(previous.{{ upper_camel .PluralName }})) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1,namespace2,namespace3,namespace4,namespace5,namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := {{ lower_camel .Name }}Client.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _,snap := range expect{{ .PluralName }} { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(expect{{ .PluralName }})) + unexpectedResource = getMapOfResources(convert{{ .PluralName }}ToMetadataGetter(unexpect{{ .PluralName }})) } - buffer.WriteString("********* UNEXPECTED ***********") - for _,snap := range unexpect{{ .PluralName }}{ - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func (ns string) ([]metadataGetter, error) { + l, err := {{ lower_camel .Name }}Client.List(ns, clients.ListOpts{}) + return convert{{ .PluralName }}ToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) {{- end }} } } @@ -1171,7 +1159,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Eventually(func () bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1 * time.Second).Should(BeTrue()) + }, 15*time.Second, 1 * time.Second).Should(BeTrue()) createNamespaceWithLabel(ctx, kube, namespace3, labels1) {{ lower_camel .Name }}2a, err := {{ lower_camel .Name }}Client.Write({{ .ImportPrefix }}New{{ .Name }}(namespace3, name2), clients.WriteOpts{Ctx: ctx}) @@ -1182,7 +1170,7 @@ var _ = Describe("{{ upper_camel .Project.ProjectConfig.Version }}Emitter", func Eventually(func () bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1 * time.Second).Should(BeTrue()) + }, 15*time.Second, 1 * time.Second).Should(BeTrue()) {{- end }} {{- end }} {{- end }} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go index 1ce33bae9..41755ac43 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter.sk.go @@ -126,7 +126,8 @@ func (c *kubeconfigsEmitter) KubeConfig() KubeConfigClient { // ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources // that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is // set, then all namespaces that meet the label criteria of the ExpressionSelector will -// also be watched. +// also be watched. If Expression Selector is set and watched namespaces is set to [""], then it +// will only watch namespaces that meet the label expression selector criteria. func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *KubeconfigsSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -320,17 +321,17 @@ func (c *kubeconfigsEmitter) Snapshots(watchNamespaces []string, opts clients.Wa c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces - mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) + mapOfResourceNamespaces := make(map[string]struct{}, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) } - mapOfResourceNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = struct{}{} } for _, ns := range watchNamespaces { - mapOfResourceNamespaces[ns] = true + mapOfResourceNamespaces[ns] = struct{}{} } missingNamespaces := []string{} diff --git a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go index 0615bbede..60cb4fc5b 100644 --- a/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go +++ b/pkg/multicluster/v1/kubeconfigs_snapshot_emitter_test.go @@ -1,11 +1,11 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v1 import ( - "bytes" "context" "fmt" "os" @@ -534,34 +534,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Kubeconfigs { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertkubeconfigsToMetadataGetter(expectkubeconfigs), convertkubeconfigsToMetadataGetter(previous.Kubeconfigs)) + unexpectedResource = findMatchingResources(convertkubeconfigsToMetadataGetter(unexpectkubeconfigs), convertkubeconfigsToMetadataGetter(previous.Kubeconfigs)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := kubeConfigClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectkubeconfigs { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertkubeconfigsToMetadataGetter(expectkubeconfigs)) + unexpectedResource = getMapOfResources(convertkubeconfigsToMetadataGetter(unexpectkubeconfigs)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectkubeconfigs { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := kubeConfigClient.List(ns, clients.ListOpts{}) + return convertkubeconfigsToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } diff --git a/test/mocks/v1/non_template_tests_test.go b/test/mocks/v1/non_template_tests_test.go index 3ea9fa9f5..5b5499e23 100644 --- a/test/mocks/v1/non_template_tests_test.go +++ b/test/mocks/v1/non_template_tests_test.go @@ -146,7 +146,6 @@ var _ = Describe("V1Emitter", func() { resourceNamespaceLister := namespace.NewKubeClientResourceNamespaceLister(kube) emitter = NewTestingEmitter(simpleMockResourceClient, mockResourceClient, fakeResourceClient, anotherMockResourceClient, clusterResourceClient, mockCustomTypeClient, mockCustomSpecHashTypeClient, podClient, resourceNamespaceLister) - // create `FakeResource`s in "namespace1" and "slowWatchNamespace" _, err = fakeResourceClient.Write(NewFakeResource(namespace1, name1), clients.WriteOpts{Ctx: ctx}) Expect(err).NotTo(HaveOccurred()) diff --git a/test/mocks/v1/testing_snapshot_emitter.sk.go b/test/mocks/v1/testing_snapshot_emitter.sk.go index 77e456699..8315c2d4e 100644 --- a/test/mocks/v1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1/testing_snapshot_emitter.sk.go @@ -198,7 +198,8 @@ func (c *testingEmitter) Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_ // ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources // that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is // set, then all namespaces that meet the label criteria of the ExpressionSelector will -// also be watched. +// also be watched. If Expression Selector is set and watched namespaces is set to [""], then it +// will only watch namespaces that meet the label expression selector criteria. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -801,17 +802,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces - mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) + mapOfResourceNamespaces := make(map[string]struct{}, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) } - mapOfResourceNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = struct{}{} } for _, ns := range watchNamespaces { - mapOfResourceNamespaces[ns] = true + mapOfResourceNamespaces[ns] = struct{}{} } missingNamespaces := []string{} diff --git a/test/mocks/v1/testing_snapshot_emitter_test.go b/test/mocks/v1/testing_snapshot_emitter_test.go index abe1ad588..0b77bf73e 100644 --- a/test/mocks/v1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1/testing_snapshot_emitter_test.go @@ -6,7 +6,6 @@ package v1 import ( - "bytes" "context" "fmt" "os" @@ -2182,34 +2181,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Simplemocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertSimplemocksToMetadataGetter(expectSimplemocks), convertSimplemocksToMetadataGetter(previous.Simplemocks)) + unexpectedResource = findMatchingResources(convertSimplemocksToMetadataGetter(unexpectSimplemocks), convertSimplemocksToMetadataGetter(previous.Simplemocks)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := simpleMockResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertSimplemocksToMetadataGetter(expectSimplemocks)) + unexpectedResource = getMapOfResources(convertSimplemocksToMetadataGetter(unexpectSimplemocks)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectSimplemocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectSimplemocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := simpleMockResourceClient.List(ns, clients.ListOpts{}) + return convertSimplemocksToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2290,34 +2277,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Mocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2398,34 +2373,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Fakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertFakesToMetadataGetter(expectFakes), convertFakesToMetadataGetter(previous.Fakes)) + unexpectedResource = findMatchingResources(convertFakesToMetadataGetter(unexpectFakes), convertFakesToMetadataGetter(previous.Fakes)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := fakeResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) + unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectFakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectFakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := fakeResourceClient.List(ns, clients.ListOpts{}) + return convertFakesToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2506,34 +2469,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Anothermockresources { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) + unexpectedResource = findMatchingResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources), convertAnothermockresourcesToMetadataGetter(previous.Anothermockresources)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := anotherMockResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectAnothermockresources { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertAnothermockresourcesToMetadataGetter(expectAnothermockresources)) + unexpectedResource = getMapOfResources(convertAnothermockresourcesToMetadataGetter(unexpectAnothermockresources)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectAnothermockresources { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := anotherMockResourceClient.List(ns, clients.ListOpts{}) + return convertAnothermockresourcesToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2686,34 +2637,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Mcts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertmctsToMetadataGetter(expectmcts), convertmctsToMetadataGetter(previous.Mcts)) + unexpectedResource = findMatchingResources(convertmctsToMetadataGetter(unexpectmcts), convertmctsToMetadataGetter(previous.Mcts)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := mockCustomTypeClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertmctsToMetadataGetter(expectmcts)) + unexpectedResource = getMapOfResources(convertmctsToMetadataGetter(unexpectmcts)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectmcts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectmcts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockCustomTypeClient.List(ns, clients.ListOpts{}) + return convertmctsToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2794,34 +2733,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Mcshts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertmcshtsToMetadataGetter(expectmcshts), convertmcshtsToMetadataGetter(previous.Mcshts)) + unexpectedResource = findMatchingResources(convertmcshtsToMetadataGetter(unexpectmcshts), convertmcshtsToMetadataGetter(previous.Mcshts)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := mockCustomSpecHashTypeClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertmcshtsToMetadataGetter(expectmcshts)) + unexpectedResource = getMapOfResources(convertmcshtsToMetadataGetter(unexpectmcshts)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectmcshts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectmcshts { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockCustomSpecHashTypeClient.List(ns, clients.ListOpts{}) + return convertmcshtsToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -2902,34 +2829,22 @@ var _ = Describe("V1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Pods { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertpodsToMetadataGetter(expectpods), convertpodsToMetadataGetter(previous.Pods)) + unexpectedResource = findMatchingResources(convertpodsToMetadataGetter(unexpectpods), convertpodsToMetadataGetter(previous.Pods)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := podClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertpodsToMetadataGetter(expectpods)) + unexpectedResource = getMapOfResources(convertpodsToMetadataGetter(unexpectpods)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectpods { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectpods { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := podClient.List(ns, clients.ListOpts{}) + return convertpodsToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -3939,7 +3854,7 @@ var _ = Describe("V1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) createNamespaceWithLabel(ctx, kube, namespace3, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) @@ -3950,7 +3865,7 @@ var _ = Describe("V1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) /* AnotherMockResource @@ -4005,7 +3920,7 @@ var _ = Describe("V1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) createNamespaceWithLabel(ctx, kube, namespace3, labels1) anotherMockResource2a, err := anotherMockResourceClient.Write(NewAnotherMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) @@ -4016,7 +3931,7 @@ var _ = Describe("V1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) }) }) diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go index 7296eba4e..f01941dcf 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter.sk.go @@ -126,7 +126,8 @@ func (c *testingEmitter) MockResource() MockResourceClient { // ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources // that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is // set, then all namespaces that meet the label criteria of the ExpressionSelector will -// also be watched. +// also be watched. If Expression Selector is set and watched namespaces is set to [""], then it +// will only watch namespaces that meet the label expression selector criteria. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -320,17 +321,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces - mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) + mapOfResourceNamespaces := make(map[string]struct{}, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) } - mapOfResourceNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = struct{}{} } for _, ns := range watchNamespaces { - mapOfResourceNamespaces[ns] = true + mapOfResourceNamespaces[ns] = struct{}{} } missingNamespaces := []string{} diff --git a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go index b706f6fb5..cfbe39220 100644 --- a/test/mocks/v1alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v1alpha1/testing_snapshot_emitter_test.go @@ -1,11 +1,11 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v1alpha1 import ( - "bytes" "context" "fmt" "os" @@ -550,34 +550,22 @@ var _ = Describe("V1Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Mocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -841,7 +829,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) createNamespaceWithLabel(ctx, kube, namespace3, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) @@ -852,7 +840,7 @@ var _ = Describe("V1Alpha1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) }) }) diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go index 03eceed39..cf86b012b 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter.sk.go @@ -148,7 +148,8 @@ func (c *testingEmitter) FakeResource() testing_solo_io.FakeResourceClient { // ExpressionSelector of the WatchOpts. Setting watchNamespaces will watch for all resources // that are in the specified namespaces. In addition if ExpressionSelector of the WatchOpts is // set, then all namespaces that meet the label criteria of the ExpressionSelector will -// also be watched. +// also be watched. If Expression Selector is set and watched namespaces is set to [""], then it +// will only watch namespaces that meet the label expression selector criteria. func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *TestingSnapshot, <-chan error, error) { if len(watchNamespaces) == 0 { @@ -478,17 +479,17 @@ func (c *testingEmitter) Snapshots(watchNamespaces []string, opts clients.WatchO c.updateNamespaces.Lock() // get the new namespaces, and get a map of the namespaces - mapOfResourceNamespaces := make(map[string]bool, len(resourceNamespaces)) + mapOfResourceNamespaces := make(map[string]struct{}, len(resourceNamespaces)) newNamespaces := []string{} for _, ns := range resourceNamespaces { if _, hit := c.namespacesWatching.Load(ns.Name); !hit { newNamespaces = append(newNamespaces, ns.Name) } - mapOfResourceNamespaces[ns.Name] = true + mapOfResourceNamespaces[ns.Name] = struct{}{} } for _, ns := range watchNamespaces { - mapOfResourceNamespaces[ns] = true + mapOfResourceNamespaces[ns] = struct{}{} } missingNamespaces := []string{} diff --git a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go index 748928e5e..816b72eef 100644 --- a/test/mocks/v2alpha1/testing_snapshot_emitter_test.go +++ b/test/mocks/v2alpha1/testing_snapshot_emitter_test.go @@ -1,11 +1,11 @@ // Code generated by solo-kit. DO NOT EDIT. +//go:build solokit // +build solokit package v2alpha1 import ( - "bytes" "context" "fmt" "os" @@ -1033,34 +1033,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Mocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertMocksToMetadataGetter(expectMocks), convertMocksToMetadataGetter(previous.Mocks)) + unexpectedResource = findMatchingResources(convertMocksToMetadataGetter(unexpectMocks), convertMocksToMetadataGetter(previous.Mocks)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := mockResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertMocksToMetadataGetter(expectMocks)) + unexpectedResource = getMapOfResources(convertMocksToMetadataGetter(unexpectMocks)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectMocks { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := mockResourceClient.List(ns, clients.ListOpts{}) + return convertMocksToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -1141,34 +1129,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Fcars { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertFcarsToMetadataGetter(expectFcars), convertFcarsToMetadataGetter(previous.Fcars)) + unexpectedResource = findMatchingResources(convertFcarsToMetadataGetter(unexpectFcars), convertFcarsToMetadataGetter(previous.Fcars)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := frequentlyChangingAnnotationsResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = getMapOfResources(convertFcarsToMetadataGetter(expectFcars)) + unexpectedResource = getMapOfResources(convertFcarsToMetadataGetter(unexpectFcars)) } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectFcars { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) - } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectFcars { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := frequentlyChangingAnnotationsResourceClient.List(ns, clients.ListOpts{}) + return convertFcarsToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -1249,34 +1225,22 @@ var _ = Describe("V2Alpha1Emitter", func() { Expect(err).NotTo(HaveOccurred()) case <-time.After(time.Second * 10): - var buffer bytes.Buffer + var expectedResources map[string][]string + var unexpectedResource map[string][]string + if previous != nil { - for _, sn := range previous.Fakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } + expectedResources = findNonMatchingResources(convertFakesToMetadataGetter(expectFakes), convertFakesToMetadataGetter(previous.Fakes)) + unexpectedResource = findMatchingResources(convertFakesToMetadataGetter(unexpectFakes), convertFakesToMetadataGetter(previous.Fakes)) } else { - buffer.WriteString("****** NO PREVIOUS SNAP ********") - } - namespaces := []string{namespace1, namespace2, namespace3, namespace4, namespace5, namespace6} - for i, ns := range namespaces { - buffer.WriteString(fmt.Sprintf("*********** %d::%v ***********", i, ns)) - list, _ := fakeResourceClient.List(ns, clients.ListOpts{}) - for _, sn := range list { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", sn.GetMetadata().Namespace, sn.GetMetadata().Name)) - buffer.WriteByte('\n') - } - } - buffer.WriteString("********** EXPECTED *********") - for _, snap := range expectFakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + expectedResources = getMapOfResources(convertFakesToMetadataGetter(expectFakes)) + unexpectedResource = getMapOfResources(convertFakesToMetadataGetter(unexpectFakes)) } - buffer.WriteString("********* UNEXPECTED ***********") - for _, snap := range unexpectFakes { - buffer.WriteString(fmt.Sprintf("namespace: %v name: %v ", snap.GetMetadata().Namespace, snap.GetMetadata().Name)) + getList := func(ns string) ([]metadataGetter, error) { + l, err := fakeResourceClient.List(ns, clients.ListOpts{}) + return convertFakesToMetadataGetter(l), err } - - Fail("expected final snapshot before 10 seconds. expected " + log.Sprintf("%v", buffer.String())) + namespaceResources := getMapOfNamespaceResources(getList) + Fail(fmt.Sprintf("expected final snapshot before 10 seconds. expected \nExpected:\n%#v\n\nUnexpected:\n%#v\n\nnamespaces:\n%#v", expectedResources, unexpectedResource, namespaceResources)) } } } @@ -1756,7 +1720,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) createNamespaceWithLabel(ctx, kube, namespace3, labels1) mockResource2a, err := mockResourceClient.Write(NewMockResource(namespace3, name2), clients.WriteOpts{Ctx: ctx}) @@ -1767,7 +1731,7 @@ var _ = Describe("V2Alpha1Emitter", func() { Eventually(func() bool { _, err = kube.CoreV1().Namespaces().Get(ctx, namespace3, metav1.GetOptions{}) return apierrors.IsNotFound(err) - }, 10*time.Second, 1*time.Second).Should(BeTrue()) + }, 15*time.Second, 1*time.Second).Should(BeTrue()) }) }) From 53176bfeaa8bfe12e6bbf2d3b2ea835511b1a34f Mon Sep 17 00:00:00 2001 From: Jake Cukjati Date: Thu, 22 Sep 2022 13:05:54 -0500 Subject: [PATCH 82/98] move changelog --- changelog/{v0.30.5 => v0.31.0}/namespace-selectors.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{v0.30.5 => v0.31.0}/namespace-selectors.yaml (100%) diff --git a/changelog/v0.30.5/namespace-selectors.yaml b/changelog/v0.31.0/namespace-selectors.yaml similarity index 100% rename from changelog/v0.30.5/namespace-selectors.yaml rename to changelog/v0.31.0/namespace-selectors.yaml From c3b15100f0b272bb81c35732f4f85b0f6eced275 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Mon, 13 Feb 2023 15:43:31 +0000 Subject: [PATCH 83/98] Adding changelog file to new location --- changelog/v0.32.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.32.0/namespace-selectors.yaml diff --git a/changelog/v0.32.0/namespace-selectors.yaml b/changelog/v0.32.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.32.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 879f457bd443d7a92b528ddce902398bad613aaa Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Mon, 13 Feb 2023 15:43:32 +0000 Subject: [PATCH 84/98] Deleting changelog file from old location --- changelog/v0.31.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.31.0/namespace-selectors.yaml diff --git a/changelog/v0.31.0/namespace-selectors.yaml b/changelog/v0.31.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.31.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 3e2718b1d69b50eafa6b389d0cdd28c9306ecbda Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 19 May 2023 19:22:35 +0000 Subject: [PATCH 85/98] Adding changelog file to new location --- changelog/v0.33.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.33.0/namespace-selectors.yaml diff --git a/changelog/v0.33.0/namespace-selectors.yaml b/changelog/v0.33.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.33.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 26bac9dd76a64f5ef66bc7ac1423f4ec3a1ecda1 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 19 May 2023 19:22:36 +0000 Subject: [PATCH 86/98] Deleting changelog file from old location --- changelog/v0.32.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.32.0/namespace-selectors.yaml diff --git a/changelog/v0.32.0/namespace-selectors.yaml b/changelog/v0.32.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.32.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From e6f69d449b7c6c908fe4782631557fe61278d3a2 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 21 Jul 2023 19:18:13 +0000 Subject: [PATCH 87/98] Adding changelog file to new location --- changelog/v0.34.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.34.0/namespace-selectors.yaml diff --git a/changelog/v0.34.0/namespace-selectors.yaml b/changelog/v0.34.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.34.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From c0ea1741b1024707bb7ca43144d41999000f7d32 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 21 Jul 2023 19:18:14 +0000 Subject: [PATCH 88/98] Deleting changelog file from old location --- changelog/v0.33.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.33.0/namespace-selectors.yaml diff --git a/changelog/v0.33.0/namespace-selectors.yaml b/changelog/v0.33.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.33.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From f8db291ff8c0d5f2528bc1c3387fb9d929492bca Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Tue, 5 Dec 2023 16:41:08 +0000 Subject: [PATCH 89/98] Adding changelog file to new location --- changelog/v0.35.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.35.0/namespace-selectors.yaml diff --git a/changelog/v0.35.0/namespace-selectors.yaml b/changelog/v0.35.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.35.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From ebcbbd943413e1db1614fdb1826d2ac7fc343fba Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Tue, 5 Dec 2023 16:41:08 +0000 Subject: [PATCH 90/98] Deleting changelog file from old location --- changelog/v0.34.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.34.0/namespace-selectors.yaml diff --git a/changelog/v0.34.0/namespace-selectors.yaml b/changelog/v0.34.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.34.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 9f7df5e02e55f29f770c108e7830ee49022430ef Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Tue, 28 May 2024 15:48:56 +0000 Subject: [PATCH 91/98] Adding changelog file to new location --- changelog/v0.36.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.36.0/namespace-selectors.yaml diff --git a/changelog/v0.36.0/namespace-selectors.yaml b/changelog/v0.36.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.36.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 5100636f69252f522e9294d89223a735c3a7f28a Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Tue, 28 May 2024 15:48:56 +0000 Subject: [PATCH 92/98] Deleting changelog file from old location --- changelog/v0.35.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.35.0/namespace-selectors.yaml diff --git a/changelog/v0.35.0/namespace-selectors.yaml b/changelog/v0.35.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.35.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 9f20d56a75b781fb56c78778feccf62ee7d6af1b Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 11 Sep 2024 17:15:34 +0000 Subject: [PATCH 93/98] Adding changelog file to new location --- changelog/v0.37.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.37.0/namespace-selectors.yaml diff --git a/changelog/v0.37.0/namespace-selectors.yaml b/changelog/v0.37.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.37.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From b560d27b3bea680bbdca094be66bc557c3c19ee2 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Wed, 11 Sep 2024 17:15:34 +0000 Subject: [PATCH 94/98] Deleting changelog file from old location --- changelog/v0.36.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.36.0/namespace-selectors.yaml diff --git a/changelog/v0.36.0/namespace-selectors.yaml b/changelog/v0.36.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.36.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 12c10c146657ca17b344efac19940da6d4665e6b Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 14 Feb 2025 01:31:02 +0000 Subject: [PATCH 95/98] Adding changelog file to new location --- changelog/v0.38.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.38.0/namespace-selectors.yaml diff --git a/changelog/v0.38.0/namespace-selectors.yaml b/changelog/v0.38.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.38.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From cd9d37bef3d6772dbea182606b08ec409c0e52e4 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Fri, 14 Feb 2025 01:31:03 +0000 Subject: [PATCH 96/98] Deleting changelog file from old location --- changelog/v0.37.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.37.0/namespace-selectors.yaml diff --git a/changelog/v0.37.0/namespace-selectors.yaml b/changelog/v0.37.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.37.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 158a844cc1d74174a67af4dbc1cdae8a5de8cd92 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Thu, 20 Feb 2025 15:30:36 +0000 Subject: [PATCH 97/98] Adding changelog file to new location --- changelog/v0.39.0/namespace-selectors.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 changelog/v0.39.0/namespace-selectors.yaml diff --git a/changelog/v0.39.0/namespace-selectors.yaml b/changelog/v0.39.0/namespace-selectors.yaml new file mode 100644 index 000000000..2bf84cbd9 --- /dev/null +++ b/changelog/v0.39.0/namespace-selectors.yaml @@ -0,0 +1,10 @@ +changelog: + - type: BREAKING_CHANGE + issueLink: https://github.com/solo-io/gloo/issues/5868 + resolvesIssue: false + description: | + Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. + Watched Namespaces work as normally. When Expression Selectors are set the snapshot + emitter will watch, in addition to the watched namespaces, namespaces that + are labeled and meet the criteria of the Expression Selector. All resource + clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file From 03a3807df5ebb1abf4aa7c01a161594b0e04bd71 Mon Sep 17 00:00:00 2001 From: changelog-bot Date: Thu, 20 Feb 2025 15:30:37 +0000 Subject: [PATCH 98/98] Deleting changelog file from old location --- changelog/v0.38.0/namespace-selectors.yaml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 changelog/v0.38.0/namespace-selectors.yaml diff --git a/changelog/v0.38.0/namespace-selectors.yaml b/changelog/v0.38.0/namespace-selectors.yaml deleted file mode 100644 index 2bf84cbd9..000000000 --- a/changelog/v0.38.0/namespace-selectors.yaml +++ /dev/null @@ -1,10 +0,0 @@ -changelog: - - type: BREAKING_CHANGE - issueLink: https://github.com/solo-io/gloo/issues/5868 - resolvesIssue: false - description: | - Added the ability to watch namespaces given by Expression Selectors in the Watch Opts. - Watched Namespaces work as normally. When Expression Selectors are set the snapshot - emitter will watch, in addition to the watched namespaces, namespaces that - are labeled and meet the criteria of the Expression Selector. All resource - clients will watch these namespaces, if set, via the snapshot emitters. \ No newline at end of file