-
Notifications
You must be signed in to change notification settings - Fork 15
Non Watched Namespace Labeled selectors #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jackstine
wants to merge
112
commits into
main
Choose a base branch
from
labeled-selectors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
112 commits
Select commit
Hold shift + click to select a range
c3c1bfa
initial commit before code generation
jackstine 9d2a486
solving generator problems
jackstine a8a66dd
updated the snahost emitter template, it now generates code.
jackstine ed99470
comment with added
jackstine acebcb8
update to testing snapshot emitter
jackstine c466f4d
added code for testing snapshot emitter
jackstine 0f6b483
updated so that ClusterScoped resource snapshot emitters will work
jackstine 16b5232
update the snapshot_emitter to call out to kube for the namespaces
jackstine e64c8f1
updates to testing and snapshot emitter
jackstine 670cf7e
update for interface for ResourceNamespaces
jackstine 95d8548
updated snapshot_emitter to filter namespaces by labels, and not reso…
jackstine 86bca00
added client interface
jackstine 90b86d2
left out some things
jackstine 26216b5
get interface to work with expression selectors
jackstine 0ef7be9
Merge refs/heads/master into labeled-selectors
soloio-bulldozer[bot] c829254
got it passing tests for namespaced labels
jackstine afc7fd2
Merge branch 'labeled-selectors' of ssh://github.com/solo-io/solo-kit…
jackstine b2ce0cf
snapshot does not contain deleted namespace resources
jackstine 4cf797d
pass CI for non ClusterScoped
jackstine 765bfc5
adding kube client resource namespace lister
jackstine 36bccb7
added in map for watched namespaces
jackstine af6eb5d
updated snapshot emitter, delt with err channels and with deleting na…
jackstine 644a6f9
clean up and reviews
jackstine 4c4192f
add missing file generated
jackstine 5c53707
change variable name
jackstine ac6124f
update with comments, and nits
jackstine 3f60056
moving some functions
jackstine 24ded9e
small change
jackstine 8bd5ec5
closing the channels
jackstine 2a9cf1c
updated the tests
jackstine 20770ed
fix kube event loop error
jackstine 835b2f9
update changelog
jackstine 4c86983
to pass CI retro 1
jackstine 62de6b8
fix
jackstine 0d847d1
fix
jackstine feb1c79
race conditions, still working on fixing issues with deleted namespaces
jackstine c2377cf
fix issue with non Expression Selector emitters updating when namespa…
jackstine b806cfc
update fix problem with namespaces getting terminated, but not deleted
jackstine 8587388
refactored the test code, to make it more easier to manage
jackstine 148dc61
missing build solokit
jackstine 576f2aa
fix issue with name5
jackstine 1a0989a
fix name5
jackstine 3aa046c
v1 alpha
jackstine f09e7e6
fix problems with parallel tests
jackstine 217108b
update to fix notifications about snapshots
jackstine a1fb027
missing file updates
jackstine f1db36e
add updated go build comments
jackstine f6d374f
Merge ssh://github.com/solo-io/solo-kit into labeledSelectors
jackstine 9aafe60
updated the controller to allow adding informers
jackstine e7371ca
register new namespace to cache
jackstine d3c5196
register new namespaces for the Resoruce Client Shared Informer Factory
jackstine ce03b28
Merge branch 'cache-changes' into labeled-selectors
jackstine 1ae4371
added in Register Namespace to the Resouce client interface.
jackstine 5e9f466
update resource clients
jackstine b72998b
Merge refs/heads/master into labeled-selectors
soloio-bulldozer[bot] cb7159b
add rc template
jackstine e2b5868
Merge branch 'labeled-selectors' of ssh://github.com/solo-io/solo-kit…
jackstine d05edec
add in once functionality to register calls
jackstine 0e2db70
rcf must start() if we are registering namespaces to it
jackstine 2ad5399
possible race condition fix
jackstine e9c3539
fix registering issue
jackstine 7af3b64
fix reconciler
jackstine 7b1e280
change test so that the kube cache does not panic
jackstine b334e28
update the client interface
jackstine f3597e0
better mutex control
jackstine 6988153
fix race condition in test
jackstine 0641785
add comment
jackstine 2f06f41
allow new namespaces in the kube resouce client
jackstine 57873d6
clean up
jackstine b9cbb91
added in a test for when namespaces are deleted
jackstine 8bb2c03
fix cache test
jackstine c074097
fix cache test again
jackstine f3aa04a
Adding changelog file to new location
ea8b754
Deleting changelog file from old location
9894a78
Merge branch 'master' into labeled-selectors
jackstine 39072d5
fix merge conflict issue
jackstine 50578c7
finish todos, add logs for registered namespaces, fixed issue with Re…
jackstine 7b30788
update tests
jackstine 24c3941
fixed issue with test, and cleaned up tests a little
jackstine 93642d1
fix error with kube emitter
jackstine a047e22
Adding changelog file to new location
2353077
Deleting changelog file from old location
f8e908c
Kube Cache now requires a boolean for creating the namespace lister
jackstine ed44661
Merge branch 'labeled-selectors' of ssh://github.com/solo-io/solo-kit…
jackstine 57e4976
Merge branch 'master' into labeled-selectors
jackstine 8d91464
update to fix merge conflict
jackstine 4c7db7f
when namespaces is set to "" then create the namespace lister
jackstine 1c1fa6c
condition on namespace lister
jackstine 66ddd96
delete failing mock_custom spec hash resource
jackstine eee5ed2
some touch ups. Errors are now logged in the list and watch functions…
jackstine 53176bf
move changelog
jackstine b6afc93
Merge refs/heads/master into labeled-selectors
soloio-bulldozer[bot] 548a0c9
Merge refs/heads/master into labeled-selectors
soloio-bulldozer[bot] d2f6b4d
Merge refs/heads/master into labeled-selectors
soloio-bulldozer[bot] c3b1510
Adding changelog file to new location
879f457
Deleting changelog file from old location
3e2718b
Adding changelog file to new location
26bac9d
Deleting changelog file from old location
e6f69d4
Adding changelog file to new location
c0ea174
Deleting changelog file from old location
f8db291
Adding changelog file to new location
ebcbbd9
Deleting changelog file from old location
9f7df5e
Adding changelog file to new location
5100636
Deleting changelog file from old location
9f20d56
Adding changelog file to new location
b560d27
Deleting changelog file from old location
12c10c1
Adding changelog file to new location
cd9d37b
Deleting changelog file from old location
158a844
Adding changelog file to new location
03a3807
Deleting changelog file from old location
71b9752
Adding changelog file to new location
d12e7d9
Deleting changelog file from old location
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
pkg/api/external/kubernetes/namespace/resource_namespace.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package namespace | ||
|
||
import ( | ||
"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 = &kubeClientResourceNamespaceLister{} | ||
|
||
// 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{ | ||
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 &kubeClientResourceNamespaceLister{ | ||
kube: kube, | ||
} | ||
} | ||
|
||
type kubeResourceNamespaceLister struct { | ||
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.client.List(clients.TranslateResourceNamespaceListToListOptions(opts)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
converted := convertNamespaceListToResourceNamespace(namespaces) | ||
return kns.filter(converted, filtered), nil | ||
} | ||
|
||
// 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.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(): | ||
return | ||
} | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
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 := make(resources.ResourceNamespaceList, len(namespaces)) | ||
for i, ns := range namespaces { | ||
l[i] = resources.ResourceNamespace{Name: ns.ObjectMeta.Name} | ||
} | ||
return l | ||
} | ||
|
||
type kubeClientResourceNamespaceLister struct { | ||
kube kubernetes.Interface | ||
} | ||
|
||
// GetResourceNamespaceList is the kubernetes implementation that returns the list of namespaces | ||
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 { | ||
return nil, err | ||
} | ||
return convertNamespaceListToResourceNamespaceList(namespaceList), nil | ||
} | ||
|
||
// GetResourceNamespaceWatch returns a watch for events that occur on kube namespaces returning a list of all the namespaces | ||
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 { | ||
return nil, nil, err | ||
} | ||
namespaceChan := namespaceWatcher.ResultChan() | ||
resourceNamespaceChan := make(chan resources.ResourceNamespaceList) | ||
errorChannel := make(chan error) | ||
go func() { | ||
defer func() { | ||
close(resourceNamespaceChan) | ||
close(errorChannel) | ||
}() | ||
Comment on lines
+128
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing the channels here when an error occurs. |
||
for { | ||
select { | ||
case <-opts.Ctx.Done(): | ||
return | ||
case event, ok := <-namespaceChan: | ||
if !ok { | ||
return | ||
} | ||
switch event.Type { | ||
case kubewatch.Error: | ||
errorChannel <- errors.Errorf("error with the event from watching namespaces: %v", event) | ||
return | ||
default: | ||
resourceNamespaceList, err := client.GetResourceNamespaceList(resources.ResourceNamespaceListOptions{ | ||
Ctx: opts.Ctx, | ||
ExpressionSelector: opts.ExpressionSelector, | ||
}, filtered) | ||
if err != nil { | ||
errorChannel <- errors.Wrap(err, "error getting the list of resource namespaces while watching") | ||
return | ||
} | ||
resourceNamespaceChan <- resourceNamespaceList | ||
} | ||
} | ||
} | ||
}() | ||
return resourceNamespaceChan, errorChannel, nil | ||
} | ||
|
||
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 | ||
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 := make(resources.ResourceNamespaceList, len(namespaceList.Items)) | ||
for i, item := range namespaceList.Items { | ||
ns := item.Name | ||
resourceNamespaces[i] = resources.ResourceNamespace{Name: ns} | ||
} | ||
return resourceNamespaces | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.