-
Notifications
You must be signed in to change notification settings - Fork 58
feat: support disabling monitoring for RootSync and RepoSync #2233
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
cowsking
wants to merge
1
commit into
GoogleContainerTools:main
Choose a base branch
from
cowsking:feat/disable-monitoring-1-25
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
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
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,205 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // 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 e2e | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| "k8s.io/utils/ptr" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/GoogleContainerTools/config-sync/e2e/nomostest" | ||
| testmetrics "github.com/GoogleContainerTools/config-sync/e2e/nomostest/metrics" | ||
| "github.com/GoogleContainerTools/config-sync/e2e/nomostest/ntopts" | ||
| nomostesting "github.com/GoogleContainerTools/config-sync/e2e/nomostest/testing" | ||
| "github.com/GoogleContainerTools/config-sync/e2e/nomostest/testpredicates" | ||
| "github.com/GoogleContainerTools/config-sync/e2e/nomostest/testwatcher" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/api/configsync" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/api/configsync/v1beta1" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/core" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/core/k8sobjects" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/kinds" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/metrics" | ||
| "github.com/GoogleContainerTools/config-sync/pkg/reconcilermanager" | ||
| ) | ||
|
|
||
| func deploymentMissingVolume(volumeName string) testpredicates.Predicate { | ||
|
Contributor
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. Suggest moving the predicates to e2e/nomostest/testpredicates/predicates.go |
||
| return func(o client.Object) error { | ||
| if o == nil { | ||
| return testpredicates.ErrObjectNotFound | ||
| } | ||
| d, ok := o.(*appsv1.Deployment) | ||
| if !ok { | ||
| return testpredicates.WrongTypeErr(o, d) | ||
| } | ||
| for _, volume := range d.Spec.Template.Spec.Volumes { | ||
| if volume.Name == volumeName { | ||
| return fmt.Errorf("Deployment %s should not have volume %s", core.ObjectNamespacedName(o), volumeName) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func deploymentHasVolume(volumeName string) testpredicates.Predicate { | ||
| return func(o client.Object) error { | ||
| if o == nil { | ||
| return testpredicates.ErrObjectNotFound | ||
| } | ||
| d, ok := o.(*appsv1.Deployment) | ||
| if !ok { | ||
| return testpredicates.WrongTypeErr(o, d) | ||
| } | ||
| for _, volume := range d.Spec.Template.Spec.Volumes { | ||
| if volume.Name == volumeName { | ||
| return nil | ||
| } | ||
| } | ||
| return fmt.Errorf("Deployment %s should have volume %s", core.ObjectNamespacedName(o), volumeName) | ||
| } | ||
| } | ||
|
|
||
| func TestDisableMonitoringRootSync(t *testing.T) { | ||
| rootSyncID := nomostest.DefaultRootSyncID | ||
| nt := nomostest.New(t, nomostesting.OverrideAPI, | ||
| ntopts.SyncWithGitSource(rootSyncID, ntopts.Unstructured)) | ||
|
|
||
| rootReconcilerName := core.RootReconcilerObjectKey(rootSyncID.Name) | ||
| rootSyncV1 := k8sobjects.RootSyncObjectV1Beta1(configsync.RootSyncName) | ||
|
|
||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| // validate initial monitoring enabled (default state) | ||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| rootReconcilerName.Name, rootReconcilerName.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentHasContainer(metrics.OtelAgentName), | ||
| deploymentHasVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentMissingEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING"), | ||
| ), | ||
| )) | ||
| err := nomostest.ValidateStandardMetricsForRootSync(nt, testmetrics.Summary{Sync: rootSyncID.ObjectKey}) | ||
| if err != nil { | ||
| t.Fatalf("Expected standard metrics to be present when monitoring is enabled, but got: %v", err) | ||
| } | ||
|
|
||
| // disable monitoring | ||
| nt.MustMergePatch(rootSyncV1, `{"spec": {"monitoring": {"enabled": false}}}`) | ||
|
|
||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| rootReconcilerName.Name, rootReconcilerName.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentMissingContainer(metrics.OtelAgentName), | ||
| deploymentMissingVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentHasEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING", "true"), | ||
| ), | ||
| )) | ||
|
cowsking marked this conversation as resolved.
|
||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| err = nomostest.ValidateStandardMetricsForRootSync(nt, testmetrics.Summary{Sync: rootSyncID.ObjectKey}) | ||
| if err == nil { | ||
| t.Fatal("Expected an error when validating metrics for RootSync with disabled monitoring, but got nil") | ||
| } | ||
|
|
||
| // re-enable monitoring | ||
| nt.MustMergePatch(rootSyncV1, `{"spec": {"monitoring": {"enabled": true}}}`) | ||
|
|
||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| rootReconcilerName.Name, rootReconcilerName.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentHasContainer(metrics.OtelAgentName), | ||
| deploymentHasVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentMissingEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING"), | ||
| ), | ||
| )) | ||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| err = nomostest.ValidateStandardMetricsForRootSync(nt, testmetrics.Summary{Sync: rootSyncID.ObjectKey}) | ||
| if err != nil { | ||
| t.Fatalf("Expected standard metrics to be present after re-enabling monitoring, but got: %v", err) | ||
| } | ||
| } | ||
|
cowsking marked this conversation as resolved.
|
||
|
|
||
| func TestDisableMonitoringRepoSync(t *testing.T) { | ||
| repoSyncID := core.RepoSyncID(configsync.RepoSyncName, frontendNamespace) | ||
| nt := nomostest.New(t, nomostesting.OverrideAPI, | ||
| ntopts.SyncWithGitSource(nomostest.DefaultRootSyncID, ntopts.Unstructured), | ||
| ntopts.SyncWithGitSource(repoSyncID)) | ||
| rootSyncGitRepo := nt.SyncSourceGitReadWriteRepository(nomostest.DefaultRootSyncID) | ||
| repoSyncKey := repoSyncID.ObjectKey | ||
|
|
||
| frontendReconcilerNN := core.NsReconcilerObjectKey(repoSyncID.Namespace, repoSyncID.Name) | ||
| repoSyncFrontend := nomostest.RepoSyncObjectV1Beta1FromNonRootRepo(nt, repoSyncKey) | ||
|
|
||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| // Verify ns-reconciler-frontend uses the default monitoring state (enabled) | ||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| frontendReconcilerNN.Name, frontendReconcilerNN.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentHasContainer(metrics.OtelAgentName), | ||
| deploymentHasVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentMissingEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING"), | ||
| ), | ||
| )) | ||
| err := nomostest.ValidateStandardMetricsForRepoSync(nt, testmetrics.Summary{Sync: repoSyncID.ObjectKey}) | ||
| if err != nil { | ||
| t.Fatalf("Expected standard metrics to be present when monitoring is enabled, but got: %v", err) | ||
| } | ||
|
|
||
| // Disable monitoring | ||
| repoSyncFrontend.Spec.Monitoring = &v1beta1.MonitoringSpec{Enabled: ptr.To(false)} | ||
| nt.Must(rootSyncGitRepo.Add(nomostest.StructuredNSPath(repoSyncID.Namespace, repoSyncID.Name), repoSyncFrontend)) | ||
| nt.Must(rootSyncGitRepo.CommitAndPush("Disable monitoring of frontend Reposync")) | ||
|
|
||
| // validate override and make sure otel-agent is missing | ||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| frontendReconcilerNN.Name, frontendReconcilerNN.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentMissingContainer(metrics.OtelAgentName), | ||
| deploymentMissingVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentHasEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING", "true"), | ||
| ), | ||
| )) | ||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| err = nomostest.ValidateStandardMetricsForRepoSync(nt, testmetrics.Summary{Sync: repoSyncID.ObjectKey}) | ||
| if err == nil { | ||
| t.Fatal("Expected an error when validating metrics for RepoSync with disabled monitoring, but got nil") | ||
| } | ||
|
|
||
| // Re-enable monitoring | ||
| repoSyncFrontend.Spec.Monitoring = &v1beta1.MonitoringSpec{Enabled: ptr.To(true)} | ||
| nt.Must(rootSyncGitRepo.Add(nomostest.StructuredNSPath(repoSyncID.Namespace, repoSyncID.Name), repoSyncFrontend)) | ||
| nt.Must(rootSyncGitRepo.CommitAndPush("Re-enable monitoring of frontend RepoSync")) | ||
|
|
||
| // validate container comes back | ||
| nt.Must(nt.Watcher.WatchObject(kinds.Deployment(), | ||
| frontendReconcilerNN.Name, frontendReconcilerNN.Namespace, | ||
| testwatcher.WatchPredicates( | ||
| testpredicates.DeploymentHasContainer(metrics.OtelAgentName), | ||
| deploymentHasVolume("otel-agent-config-reconciler-vol"), | ||
| testpredicates.DeploymentMissingEnvVar(reconcilermanager.Reconciler, "DISABLE_MONITORING"), | ||
| ), | ||
| )) | ||
| nt.Must(nt.WatchForAllSyncs()) | ||
|
|
||
| err = nomostest.ValidateStandardMetricsForRepoSync(nt, testmetrics.Summary{Sync: repoSyncID.ObjectKey}) | ||
| if err != nil { | ||
| t.Fatalf("Expected standard metrics to be present after re-enabling monitoring, but got: %v", err) | ||
| } | ||
| } | ||
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.
Notice that the CRD API uses positive naming (spec.monitoring.enabled: false) while the internal environment variable uses negative opt-out naming (DISABLE_MONITORING=true). While spec.monitoring.enabled follows K8s API conventions and DISABLE_MONITORING=true provides backward-compatible defaults when unset, the difference might confuse contributors. Could we add a short code comment in pkg/reconcilermanager/controllers/util.go explaining this naming rationale?