-
Notifications
You must be signed in to change notification settings - Fork 153
feat(mgmt-agent): add configmap controller to watch swift router cm #6023
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
bennerv
wants to merge
2
commits into
Azure:main
Choose a base branch
from
bennerv:mgmt-agent-watch-swift-cm
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
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright 2025 Microsoft Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| coreinformers "k8s.io/client-go/informers/core/v1" | ||
| "k8s.io/client-go/tools/cache" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // The HyperShift router ConfigMap has no distinguishing labels, so we select by name. | ||
| const RouterConfigMapName = "router" | ||
|
|
||
| // ConfigMapWatcher watches ConfigMap resources using a typed informer and logs | ||
| // create, update, and delete events via structured logging. It is intended to | ||
| // be used with a field-selector-scoped informer factory so that only ConfigMaps | ||
| // with a specific name (e.g. "router") are watched. | ||
| type ConfigMapWatcher struct { | ||
| cmSynced cache.InformerSynced | ||
| } | ||
|
|
||
| // NewConfigMapWatcher creates a new ConfigMapWatcher. It registers event handlers | ||
| // on the given ConfigMap informer to log ConfigMap lifecycle events. | ||
| func NewConfigMapWatcher(cmInformer coreinformers.ConfigMapInformer) (*ConfigMapWatcher, error) { | ||
| w := &ConfigMapWatcher{ | ||
| cmSynced: cmInformer.Informer().HasSynced, | ||
| } | ||
|
|
||
| if _, err := cmInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
| AddFunc: func(obj interface{}) { | ||
| cm, ok := obj.(*corev1.ConfigMap) | ||
| if !ok { | ||
| return | ||
| } | ||
| logConfigMapEvent("Add", cm) | ||
| }, | ||
| UpdateFunc: func(_, newObj interface{}) { | ||
| cm, ok := newObj.(*corev1.ConfigMap) | ||
| if !ok { | ||
| return | ||
| } | ||
| logConfigMapEvent("Update", cm) | ||
| }, | ||
| DeleteFunc: func(obj interface{}) { | ||
| if tombstone, ok := obj.(cache.DeletedFinalStateUnknown); ok { | ||
| obj = tombstone.Obj | ||
| } | ||
| cm, ok := obj.(*corev1.ConfigMap) | ||
| if !ok { | ||
| return | ||
| } | ||
| logConfigMapEvent("Delete", cm) | ||
| }, | ||
| }); err != nil { | ||
| return nil, fmt.Errorf("failed to add event handler: %w", err) | ||
| } | ||
|
|
||
| return w, nil | ||
| } | ||
|
|
||
| // Run waits for the ConfigMap informer cache to sync and blocks until the | ||
| // context is cancelled. | ||
| func (w *ConfigMapWatcher) Run(ctx context.Context) error { | ||
| logger := klog.FromContext(ctx) | ||
| logger.Info("Starting ConfigMap watcher") | ||
|
|
||
| logger.Info("Waiting for ConfigMap informer cache to sync") | ||
| if ok := cache.WaitForCacheSync(ctx.Done(), w.cmSynced); !ok { | ||
| return fmt.Errorf("failed to wait for ConfigMap informer cache to sync") | ||
| } | ||
|
|
||
| logger.Info("ConfigMap watcher informer synced and running") | ||
| <-ctx.Done() | ||
| logger.Info("Shutting down ConfigMap watcher") | ||
| return nil | ||
| } | ||
|
|
||
| func logConfigMapEvent(eventType string, cm *corev1.ConfigMap) { | ||
| cm.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap")) | ||
| klog.InfoS("configmap event", | ||
| "event", eventType, | ||
| "namespace", cm.Namespace, | ||
| "name", cm.Name, | ||
| "object", cm, | ||
| ) | ||
| } | ||
|
Comment on lines
+94
to
+102
|
||
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,88 @@ | ||
| // Copyright 2025 Microsoft Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| kubeinformers "k8s.io/client-go/informers" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestLogConfigMapEvent(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| eventType string | ||
| cm *corev1.ConfigMap | ||
| }{ | ||
| { | ||
| name: "Add event", | ||
| eventType: "Add", | ||
| cm: &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: RouterConfigMapName, | ||
| Namespace: "ocm-hcp-test", | ||
| }, | ||
| Data: map[string]string{ | ||
| "haproxy.cfg": "global\n maxconn 4096", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Update event", | ||
| eventType: "Update", | ||
| cm: &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: RouterConfigMapName, | ||
| Namespace: "ocm-hcp-test", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Delete event", | ||
| eventType: "Delete", | ||
| cm: &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: RouterConfigMapName, | ||
| Namespace: "ocm-hcp-test", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| logConfigMapEvent(tt.eventType, tt.cm) | ||
| }) | ||
| } | ||
|
Comment on lines
+67
to
+71
|
||
| } | ||
|
|
||
| func TestNewConfigMapWatcher(t *testing.T) { | ||
| clientset := fake.NewSimpleClientset() | ||
| factory := kubeinformers.NewSharedInformerFactory(clientset, 0) | ||
|
|
||
| w, err := NewConfigMapWatcher(factory.Core().V1().ConfigMaps()) | ||
| if err != nil { | ||
| t.Fatalf("NewConfigMapWatcher() returned error: %v", err) | ||
| } | ||
| if w == nil { | ||
| t.Fatal("NewConfigMapWatcher() returned nil") | ||
| } | ||
| if w.cmSynced == nil { | ||
| t.Fatal("NewConfigMapWatcher() did not set cmSynced") | ||
| } | ||
| } | ||
|
Comment on lines
+74
to
+88
|
||
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.
can you add
by default the serialization of
cmhere won't have apiVersion * kind fieldsThere 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.
thanks fixed.