-
Notifications
You must be signed in to change notification settings - Fork 49
DVO-114: Add watcher prototype to handle configuration changes #251
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
openshift-merge-robot
merged 6 commits into
app-sre:master
from
ncaak:DVO-114/configmap-controller
Jun 1, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb7c063
WIP watcher draft and minor unit test
ncaak 37cc939
Update documentation and Refacoring
ncaak 4ce63cb
Fix linting recommendations
ncaak bf1e0af
Fix watcher with alternative PoC using informers factory
ncaak 44c52e9
Fix linting
ncaak c09a314
Clean unneeded snippet
ncaak 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| apicorev1 "k8s.io/api/core/v1" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/informers" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/cache" | ||
| ) | ||
|
|
||
| type ConfigMapWatcher struct { | ||
| clientset kubernetes.Interface | ||
| ch chan struct{} // TODO - TBD configmap struct | ||
| } | ||
|
|
||
| var configMapName = "deployment-validation-operator-config" | ||
| var configMapNamespace = "deployment-validation-operator" | ||
|
|
||
| // NewConfigMapWatcher returns a watcher that can be used both: | ||
| // basic: with GetStaticDisabledChecks method, it returns an existent ConfigMap data's disabled check | ||
| // dynamic: with StartInformer it sets an Informer that will be triggered on ConfigMap update | ||
| func NewConfigMapWatcher(cfg *rest.Config) (ConfigMapWatcher, error) { | ||
| clientset, err := kubernetes.NewForConfig(cfg) | ||
| if err != nil { | ||
| return ConfigMapWatcher{}, fmt.Errorf("initializing clientset: %w", err) | ||
| } | ||
|
|
||
| return ConfigMapWatcher{ | ||
| clientset: clientset, | ||
| }, nil | ||
| } | ||
|
|
||
| // GetStaticDisabledChecks returns an existent ConfigMap data's disabled checks, if they exist | ||
| func (cmw *ConfigMapWatcher) GetStaticDisabledChecks(ctx context.Context) ([]string, error) { | ||
| cm, err := cmw.clientset.CoreV1(). | ||
| ConfigMaps(configMapNamespace).Get(ctx, configMapName, v1.GetOptions{}) | ||
| if err != nil { | ||
| return []string{}, fmt.Errorf("gathering starting configmap: %w", err) | ||
| } | ||
|
|
||
| // TODO - Fix dummy return based on data being check1,check2,check3... | ||
| return strings.Split(cm.Data["disabled-checks"], ","), nil | ||
| } | ||
|
|
||
| // StartInformer will update the channel structure with new configuration data from ConfigMap update event | ||
| func (cmw *ConfigMapWatcher) StartInformer(ctx context.Context) error { | ||
| factory := informers.NewSharedInformerFactoryWithOptions( | ||
| cmw.clientset, time.Second*30, informers.WithNamespace(configMapNamespace), | ||
| ) | ||
| informer := factory.Core().V1().ConfigMaps().Informer() | ||
|
|
||
| informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ // nolint:errcheck | ||
| UpdateFunc: func(oldObj, newObj interface{}) { | ||
| oldCm := oldObj.(*apicorev1.ConfigMap) | ||
| newCm := newObj.(*apicorev1.ConfigMap) | ||
|
|
||
| fmt.Printf("oldCm: %v\n", oldCm) | ||
| fmt.Printf("ConfigMap updated: %s/%s\n", newCm.Namespace, newCm.Name) | ||
|
|
||
| // TODO - Validate new configmap | ||
| cmw.ch <- struct{}{} | ||
| }, | ||
| }) | ||
|
|
||
| factory.Start(ctx.Done()) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ConfigChanged receives push notifications when the configuration is updated | ||
| func (cmw *ConfigMapWatcher) ConfigChanged() <-chan struct{} { | ||
| return cmw.ch | ||
| } | ||
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,31 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| kubefake "k8s.io/client-go/kubernetes/fake" | ||
| ) | ||
|
|
||
| func TestStaticConfigMapWatcher(t *testing.T) { | ||
| // Given | ||
| cm := &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{Namespace: configMapNamespace, Name: configMapName}, | ||
| Data: map[string]string{"disabled-checks": "check,check2"}, | ||
| } | ||
| client := kubefake.NewSimpleClientset([]runtime.Object{cm}...) | ||
| mock := ConfigMapWatcher{ | ||
| clientset: client, | ||
| } | ||
|
|
||
| // When | ||
| test, err := mock.GetStaticDisabledChecks(context.Background()) | ||
|
|
||
| // Assert | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, []string{"check", "check2"}, test) | ||
| } |
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.