-
Notifications
You must be signed in to change notification settings - Fork 191
fix(kubernetes)!: Provider updated for each watcher callback #532
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
Merged
Merged
Changes from all commits
Commits
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,180 @@ | ||
| package kubernetes | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "reflect" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/internal/test" | ||
| "github.com/containers/kubernetes-mcp-server/pkg/config" | ||
| "github.com/stretchr/testify/suite" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
| clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
| ) | ||
|
|
||
| type ProviderWatchTargetsTestSuite struct { | ||
| suite.Suite | ||
| mockServer *test.MockServer | ||
| discoveryClientHandler *test.DiscoveryClientHandler | ||
| kubeconfig *clientcmdapi.Config | ||
| staticConfig *config.StaticConfig | ||
| } | ||
|
|
||
| func (s *ProviderWatchTargetsTestSuite) SetupTest() { | ||
| s.mockServer = test.NewMockServer() | ||
| s.discoveryClientHandler = &test.DiscoveryClientHandler{} | ||
| s.mockServer.Handle(s.discoveryClientHandler) | ||
|
|
||
| s.T().Setenv("CLUSTER_STATE_POLL_INTERVAL_MS", "100") | ||
| s.T().Setenv("CLUSTER_STATE_DEBOUNCE_WINDOW_MS", "50") | ||
|
|
||
| // Add multiple fake contexts to allow testing of context changes | ||
| s.kubeconfig = s.mockServer.Kubeconfig() | ||
| for i := 0; i < 10; i++ { | ||
| name := fmt.Sprintf("context-%d", i) | ||
| s.kubeconfig.Contexts[name] = clientcmdapi.NewContext() | ||
| s.kubeconfig.Contexts[name].Cluster = s.kubeconfig.Contexts[s.kubeconfig.CurrentContext].Cluster | ||
| s.kubeconfig.Contexts[name].AuthInfo = s.kubeconfig.Contexts[s.kubeconfig.CurrentContext].AuthInfo | ||
| } | ||
|
|
||
| s.staticConfig = &config.StaticConfig{KubeConfig: test.KubeconfigFile(s.T(), s.kubeconfig)} | ||
| } | ||
|
|
||
| func (s *ProviderWatchTargetsTestSuite) TearDownTest() { | ||
| s.mockServer.Close() | ||
| } | ||
|
|
||
| func (s *ProviderWatchTargetsTestSuite) TestClusterStateChanges() { | ||
| testCases := []func() (Provider, error){ | ||
| func() (Provider, error) { return newKubeConfigClusterProvider(s.staticConfig) }, | ||
| func() (Provider, error) { | ||
| return newSingleClusterProvider(config.ClusterProviderDisabled)(s.staticConfig) | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| provider, err := tc() | ||
| s.Require().NoError(err, "Expected no error from provider creation") | ||
|
|
||
| s.Run("With provider "+reflect.TypeOf(provider).String(), func() { | ||
| callback, waitForCallback := CallbackWaiter() | ||
| provider.WatchTargets(callback) | ||
| s.Run("Reloads provider on cluster changes", func() { | ||
| s.discoveryClientHandler.Groups = append(s.discoveryClientHandler.Groups, `{"name":"alex.example.com","versions":[{"groupVersion":"alex.example.com/v1","version":"v1"}],"preferredVersion":{"groupVersion":"alex.example.com/v1","version":"v1"}}`) | ||
|
|
||
| s.Require().NoError(waitForCallback(5 * time.Second)) | ||
| // Provider-wise the watcher.ClusterState which triggers the callback has no effect. | ||
| // We might consider removing it at some point? (20251202) | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s *ProviderWatchTargetsTestSuite) TestKubeConfigClusterProvider() { | ||
| provider, err := newKubeConfigClusterProvider(s.staticConfig) | ||
| s.Require().NoError(err, "Expected no error from provider creation") | ||
|
|
||
| callback, waitForCallback := CallbackWaiter() | ||
| provider.WatchTargets(callback) | ||
|
|
||
| s.Run("KubeConfigClusterProvider updates targets (reset) on kubeconfig change", func() { | ||
| s.kubeconfig.CurrentContext = "context-1" | ||
| s.Require().NoError(clientcmd.WriteToFile(*s.kubeconfig, s.staticConfig.KubeConfig)) | ||
| s.Require().NoError(waitForCallback(5 * time.Second)) | ||
|
|
||
| s.Run("Replaces default target with new context", func() { | ||
| s.Equal("context-1", provider.GetDefaultTarget(), "Expected default target context to be updated") | ||
| }) | ||
| s.Run("Adds new context to targets", func() { | ||
| targets, err := provider.GetTargets(s.T().Context()) | ||
| s.Require().NoError(err, "Expected no error from GetTargets") | ||
| s.Contains(targets, "context-1") | ||
| }) | ||
| s.Run("Has derived Kubernetes for new context", func() { | ||
| k, err := provider.GetDerivedKubernetes(s.T().Context(), "context-1") | ||
| s.Require().NoError(err, "Expected no error from GetDerivedKubernetes for context-1") | ||
| s.NotNil(k, "Expected Kubernetes from GetDerivedKubernetes for context-1") | ||
| s.Run("Derived Kubernetes points to correct context", func() { | ||
| cfg, err := k.AccessControlClientset().ToRawKubeConfigLoader().RawConfig() | ||
| s.Require().NoError(err, "Expected no error from ToRawKubeConfigLoader") | ||
| s.Equal("context-1", cfg.CurrentContext, "Expected Kubernetes to point to changed-context") | ||
| }) | ||
| }) | ||
|
|
||
| s.Run("Keeps watching for further changes", func() { | ||
| s.kubeconfig.CurrentContext = "context-2" | ||
| s.Require().NoError(clientcmd.WriteToFile(*s.kubeconfig, s.staticConfig.KubeConfig)) | ||
| s.Require().NoError(waitForCallback(5 * time.Second)) | ||
|
|
||
| s.Run("Replaces default target with new context", func() { | ||
| s.Equal("context-2", provider.GetDefaultTarget(), "Expected default target context to be updated") | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func (s *ProviderWatchTargetsTestSuite) TestSingleClusterProvider() { | ||
| provider, err := newSingleClusterProvider(config.ClusterProviderDisabled)(s.staticConfig) | ||
| s.Require().NoError(err, "Expected no error from provider creation") | ||
|
|
||
| callback, waitForCallback := CallbackWaiter() | ||
| provider.WatchTargets(callback) | ||
|
|
||
| s.Run("SingleClusterProvider reloads/resets on kubeconfig change", func() { | ||
| s.kubeconfig.CurrentContext = "context-1" | ||
| s.Require().NoError(clientcmd.WriteToFile(*s.kubeconfig, s.staticConfig.KubeConfig)) | ||
| s.Require().NoError(waitForCallback(5 * time.Second)) | ||
|
|
||
| s.Run("Derived Kubernetes points to updated context", func() { | ||
| k, err := provider.GetDerivedKubernetes(s.T().Context(), "") | ||
| s.Require().NoError(err, "Expected no error from GetDerivedKubernetes for context-1") | ||
| s.NotNil(k, "Expected Kubernetes from GetDerivedKubernetes for context-1") | ||
| s.Run("Derived Kubernetes points to correct context", func() { | ||
| cfg, err := k.AccessControlClientset().ToRawKubeConfigLoader().RawConfig() | ||
| s.Require().NoError(err, "Expected no error from ToRawKubeConfigLoader") | ||
| s.Equal("context-1", cfg.CurrentContext, "Expected Kubernetes to point to changed-context") | ||
| }) | ||
| }) | ||
|
|
||
| s.Run("Keeps watching for further changes", func() { | ||
| s.kubeconfig.CurrentContext = "context-2" | ||
| s.Require().NoError(clientcmd.WriteToFile(*s.kubeconfig, s.staticConfig.KubeConfig)) | ||
| s.Require().NoError(waitForCallback(5 * time.Second)) | ||
|
|
||
| s.Run("Derived Kubernetes points to updated context", func() { | ||
| k, err := provider.GetDerivedKubernetes(s.T().Context(), "") | ||
| s.Require().NoError(err, "Expected no error from GetDerivedKubernetes for context-2") | ||
| s.NotNil(k, "Expected Kubernetes from GetDerivedKubernetes for context-2") | ||
| cfg, err := k.AccessControlClientset().ToRawKubeConfigLoader().RawConfig() | ||
| s.Require().NoError(err, "Expected no error from ToRawKubeConfigLoader") | ||
| s.Equal("context-2", cfg.CurrentContext, "Expected Kubernetes to point to changed-context") | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // CallbackWaiter returns a callback and wait function that can be used multiple times. | ||
| func CallbackWaiter() (callback func() error, waitFunc func(timeout time.Duration) error) { | ||
| signal := make(chan struct{}, 1) | ||
| callback = func() error { | ||
| select { | ||
| case signal <- struct{}{}: | ||
| default: | ||
| } | ||
| return nil | ||
| } | ||
| waitFunc = func(timeout time.Duration) error { | ||
| select { | ||
| case <-signal: | ||
| case <-time.After(timeout): | ||
| return errors.New("timeout waiting for callback") | ||
| } | ||
| return nil | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func TestProviderWatchTargetsTestSuite(t *testing.T) { | ||
| suite.Run(t, new(ProviderWatchTargetsTestSuite)) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would these managers have any resources that should be cleaned up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question.
We don't provide a
kubernetes.Manager.Clean method.The manager itself has a pointer reference to the global
config.StaticConfigand an instance ofkubernetes.AccessControlClientset.The clientset has instances of the discovery client, dynamic client, and kubernetes interface. But AFAICT, none of those clients provide clean up methods.
I might have missed something, but I'd say that there's no clean up needed.