-
Notifications
You must be signed in to change notification settings - Fork 172
Log 9591 Do not overwrite ConfigMaps owned by other resources #3381
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
stilwank
wants to merge
3
commits into
openshift:master
Choose a base branch
from
stilwank:LOG-9591-do-not-overwrite-foreign-configmap
base: master
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
3 commits
Select commit
Hold shift + click to select a range
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,153 @@ | ||
| package reconcile_test | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/openshift/cluster-logging-operator/internal/reconcile" | ||
| "github.com/openshift/cluster-logging-operator/internal/runtime" | ||
| "github.com/openshift/cluster-logging-operator/internal/utils" | ||
| "github.com/openshift/cluster-logging-operator/internal/utils/comparators" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| k8sruntime "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| var _ = Describe("reconciling ConfigMap", func() { | ||
|
|
||
| const ( | ||
| namespace = "openshift-logging" | ||
| name = "lokistack-config" | ||
| ) | ||
|
|
||
| var ( | ||
| clfOwner = metav1.OwnerReference{ | ||
| APIVersion: "observability.openshift.io/v1", | ||
| Kind: "ClusterLogForwarder", | ||
| Name: "lokistack", | ||
| UID: "clf-uid", | ||
| Controller: utils.GetPtr(true), | ||
| } | ||
| lokiOwner = metav1.OwnerReference{ | ||
| APIVersion: "loki.grafana.com/v1", | ||
| Kind: "LokiStack", | ||
| Name: "lokistack", | ||
| UID: "loki-uid", | ||
| Controller: utils.GetPtr(true), | ||
| } | ||
| ) | ||
|
|
||
| newClient := func(objs ...client.Object) client.Client { | ||
| globalScheme := k8sruntime.NewScheme() | ||
| Expect(scheme.AddToScheme(globalScheme)).To(Succeed()) | ||
| return fake.NewClientBuilder().WithScheme(globalScheme).WithObjects(objs...).Build() | ||
| } | ||
|
|
||
| getConfigMap := func(k8sClient client.Client) *corev1.ConfigMap { | ||
| result := &corev1.ConfigMap{} | ||
| Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, result)).To(Succeed()) | ||
| return result | ||
| } | ||
|
|
||
| desiredCollectorCM := func() *corev1.ConfigMap { | ||
| cm := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "vector.toml": "data_dir = \"/var/lib/vector\"", | ||
| }) | ||
| utils.AddOwnerRefToObject(cm, clfOwner) | ||
| return cm | ||
| } | ||
|
|
||
| It("should create the ConfigMap when it does not exist", func() { | ||
| k8sClient := newClient() | ||
| desired := desiredCollectorCM() | ||
|
|
||
| Expect(reconcile.Configmap(k8sClient, k8sClient, desired, comparators.CompareLabels)).To(Succeed()) | ||
|
|
||
| result := getConfigMap(k8sClient) | ||
| Expect(result.Data).To(HaveKey("vector.toml")) | ||
| Expect(result.OwnerReferences).To(Equal([]metav1.OwnerReference{clfOwner})) | ||
| }) | ||
|
|
||
| It("should update the ConfigMap when owned by the ClusterLogForwarder", func() { | ||
| existing := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "vector.toml": "old", | ||
| }) | ||
| utils.AddOwnerRefToObject(existing, clfOwner) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| desired := desiredCollectorCM() | ||
| Expect(reconcile.Configmap(k8sClient, k8sClient, desired, comparators.CompareLabels)).To(Succeed()) | ||
|
|
||
| result := getConfigMap(k8sClient) | ||
| Expect(result.Data["vector.toml"]).To(Equal("data_dir = \"/var/lib/vector\"")) | ||
| Expect(result.OwnerReferences).To(Equal([]metav1.OwnerReference{clfOwner})) | ||
| }) | ||
|
|
||
| It("should refuse to overwrite a ConfigMap owned by another resource", func() { | ||
| existing := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "config.yaml": "auth_enabled: false", | ||
| }) | ||
| utils.AddOwnerRefToObject(existing, lokiOwner) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| desired := desiredCollectorCM() | ||
| err := reconcile.Configmap(k8sClient, k8sClient, desired, comparators.CompareLabels) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("refusing to overwrite")) | ||
|
|
||
| result := getConfigMap(k8sClient) | ||
| Expect(result.Data).To(HaveKey("config.yaml")) | ||
| Expect(result.Data).NotTo(HaveKey("vector.toml")) | ||
| Expect(result.OwnerReferences).To(Equal([]metav1.OwnerReference{lokiOwner})) | ||
| }) | ||
|
|
||
| It("should refuse an identical ConfigMap owned by another resource", func() { | ||
| existing := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "vector.toml": "data_dir = \"/var/lib/vector\"", | ||
| }) | ||
| utils.AddOwnerRefToObject(existing, lokiOwner) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| err := reconcile.Configmap(k8sClient, k8sClient, desiredCollectorCM(), comparators.CompareLabels) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("refusing to overwrite")) | ||
| }) | ||
|
|
||
| It("should refuse to take ownership of an existing ConfigMap with no owner", func() { | ||
| existing := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "config.yaml": "auth_enabled: false", | ||
| }) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| desired := desiredCollectorCM() | ||
| err := reconcile.Configmap(k8sClient, k8sClient, desired, comparators.CompareLabels) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("refusing to overwrite")) | ||
|
|
||
| result := getConfigMap(k8sClient) | ||
| Expect(result.Data).To(HaveKey("config.yaml")) | ||
| Expect(result.OwnerReferences).To(BeEmpty()) | ||
| }) | ||
|
|
||
| It("should allow update when the desired owner UID is present among additional owners", func() { | ||
| existing := runtime.NewConfigMap(namespace, name, map[string]string{ | ||
| "vector.toml": "old", | ||
| }) | ||
| utils.AddOwnerRefToObject(existing, clfOwner) | ||
| utils.AddOwnerRefToObject(existing, metav1.OwnerReference{ | ||
| APIVersion: "v1", | ||
| Kind: "ConfigMap", | ||
| Name: "extra", | ||
| UID: "extra-uid", | ||
| }) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| Expect(reconcile.Configmap(k8sClient, k8sClient, desiredCollectorCM(), comparators.CompareLabels)).To(Succeed()) | ||
| result := getConfigMap(k8sClient) | ||
| Expect(result.Data["vector.toml"]).To(Equal("data_dir = \"/var/lib/vector\"")) | ||
| }) | ||
| }) |
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,73 @@ | ||
| package reconcile_test | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/openshift/cluster-logging-operator/internal/reconcile" | ||
| "github.com/openshift/cluster-logging-operator/internal/runtime" | ||
| "github.com/openshift/cluster-logging-operator/internal/utils" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| k8sruntime "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| var _ = Describe("reconciling DaemonSet ownership", func() { | ||
| const ( | ||
| namespace = "openshift-logging" | ||
| name = "lokistack" | ||
| ) | ||
|
|
||
| var ( | ||
| clfOwner = metav1.OwnerReference{ | ||
| APIVersion: "observability.openshift.io/v1", | ||
| Kind: "ClusterLogForwarder", | ||
| Name: "lokistack", | ||
| UID: "clf-uid", | ||
| Controller: utils.GetPtr(true), | ||
| } | ||
| lokiOwner = metav1.OwnerReference{ | ||
| APIVersion: "loki.grafana.com/v1", | ||
| Kind: "LokiStack", | ||
| Name: "lokistack", | ||
| UID: "loki-uid", | ||
| Controller: utils.GetPtr(true), | ||
| } | ||
| ) | ||
|
|
||
| newClient := func(objs ...client.Object) client.Client { | ||
| globalScheme := k8sruntime.NewScheme() | ||
| Expect(scheme.AddToScheme(globalScheme)).To(Succeed()) | ||
| Expect(appsv1.AddToScheme(globalScheme)).To(Succeed()) | ||
| return fake.NewClientBuilder().WithScheme(globalScheme).WithObjects(objs...).Build() | ||
| } | ||
|
|
||
| desiredDS := func() *appsv1.DaemonSet { | ||
| ds := runtime.NewDaemonSet(namespace, name) | ||
| ds.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"app": "collector"}} | ||
| ds.Spec.Template.Labels = map[string]string{"app": "collector"} | ||
| utils.AddOwnerRefToObject(ds, clfOwner) | ||
| return ds | ||
| } | ||
|
|
||
| It("should refuse to overwrite a DaemonSet owned by another resource", func() { | ||
| existing := runtime.NewDaemonSet(namespace, name) | ||
| existing.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"app": "loki"}} | ||
| existing.Spec.Template.Labels = map[string]string{"app": "loki"} | ||
| utils.AddOwnerRefToObject(existing, lokiOwner) | ||
| k8sClient := newClient(existing) | ||
|
|
||
| err := reconcile.DaemonSet(k8sClient, desiredDS()) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("refusing to overwrite")) | ||
|
|
||
| got := &appsv1.DaemonSet{} | ||
| Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, got)).To(Succeed()) | ||
| Expect(got.Spec.Selector.MatchLabels["app"]).To(Equal("loki")) | ||
| Expect(got.OwnerReferences).To(Equal([]metav1.OwnerReference{lokiOwner})) | ||
| }) | ||
| }) |
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.
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.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Keep unrelated OwnerReferences intact when the ownership guard allows a mutation.
The new contract explicitly allows a desired owner to coexist with additional OwnerReferences, but these reconciliation paths later replace the entire list with
desired.OwnerReferences. That can remove another owner’s garbage-collection relationship. Preserve existing non-target references while ensuring desired references, and assert this behavior in the additional-owner test.internal/reconcile/configmaps.go#L30-L31: preserve unrelated references when updating the ConfigMap.internal/reconcile/configmaps_test.go#L136-L152: assert thatextra-uidremains after reconciliation.internal/reconcile/deployment.go#L20-L22: preserve unrelated Deployment references.internal/reconcile/networkpolicy.go#L20-L22: preserve unrelated NetworkPolicy references.internal/reconcile/rbac.go#L19-L21: preserve unrelated Role references.internal/reconcile/rbac.go#L44-L46: preserve unrelated RoleBinding references.internal/reconcile/rbac.go#L75-L77: preserve unrelated ClusterRoleBinding references before roleRef handling.internal/reconcile/rbac.go#L88-L90: preserve unrelated ClusterRoleBinding references during update.📍 Affects 5 files
internal/reconcile/configmaps.go#L30-L31(this comment)internal/reconcile/configmaps_test.go#L136-L152internal/reconcile/deployment.go#L20-L22internal/reconcile/networkpolicy.go#L20-L22internal/reconcile/rbac.go#L19-L21internal/reconcile/rbac.go#L44-L46internal/reconcile/rbac.go#L75-L77internal/reconcile/rbac.go#L88-L90🤖 Prompt for AI Agents