-
Notifications
You must be signed in to change notification settings - Fork 6.7k
fix: resolve argocdService initialization issue in notifications CLI #24664
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
blakepettersson
merged 5 commits into
argoproj:master
from
puretension:fix/notifications-service-initialization-24196
Sep 22, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
55ec556
fix: resolve argocdService initialization issue in notifications CLI
puretension b9042a1
fix: resolve golangci-lint issues in settings_test.go
puretension 915a071
fix: modify GetFactorySettingsForCLI to use deferred pattern
puretension 15a50cc
revert: restore original settings_test.go
puretension 9330fac
Merge branch 'master' into fix/notifications-service-initialization-2…
puretension 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
Next
Next commit
fix: resolve argocdService initialization issue in notifications CLI
The notifications CLI commands were failing with 'argocdService is not initialized' error in v3.1.0 due to incorrect initialization order. This fix introduces GetFactorySettingsDeferred that uses a closure to defer service access until it's actually needed, ensuring the service is properly initialized when accessed. Changes: - Add GetFactorySettingsDeferred function with simplified naming - Add unit test to verify error handling for uninitialized service - Update notifications command to use deferred initialization pattern Fixes #24196 Signed-off-by: puretension <[email protected]>
- Loading branch information
commit 55ec5568109b4f9cb2650294ea1caf4efa3ae4e6
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 |
|---|---|---|
| @@ -1,95 +1,30 @@ | ||
| package settings | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/argoproj/notifications-engine/pkg/api" | ||
| "github.com/argoproj/notifications-engine/pkg/services" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
|
|
||
| "github.com/argoproj/argo-cd/v3/reposerver/apiclient/mocks" | ||
| "github.com/argoproj/notifications-engine/pkg/api" | ||
| service "github.com/argoproj/argo-cd/v3/util/notification/argocd" | ||
| ) | ||
|
|
||
| const ( | ||
| testNamespace = "default" | ||
| testContextKey = "test-context-key" | ||
| testContextKeyValue = "test-context-key-value" | ||
| ) | ||
| func TestGetFactorySettingsDeferred_ServiceNotInitialized(t *testing.T) { | ||
| var argocdService service.Service // nil service | ||
|
|
||
| func TestInitGetVars(t *testing.T) { | ||
| notificationsCm := corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Namespace: testNamespace, | ||
| Name: "argocd-notifications-cm", | ||
| }, | ||
| Data: map[string]string{ | ||
| "context": fmt.Sprintf("%s: %s", testContextKey, testContextKeyValue), | ||
| "service.webhook.test": "url: https://test.example.com", | ||
| "template.app-created": "email:\n subject: Application {{.app.metadata.name}} has been created.\nmessage: Application {{.app.metadata.name}} has been created.\nteams:\n title: Application {{.app.metadata.name}} has been created.\n", | ||
| "trigger.on-created": "- description: Application is created.\n oncePer: app.metadata.name\n send:\n - app-created\n when: \"true\"\n", | ||
| }, | ||
| } | ||
| notificationsSecret := corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "argocd-notifications-secret", | ||
| Namespace: testNamespace, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| "notification-secret": []byte("secret-value"), | ||
| }, | ||
| } | ||
| kubeclientset := fake.NewClientset(&corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Namespace: testNamespace, | ||
| Name: "argocd-notifications-cm", | ||
| }, | ||
| Data: notificationsCm.Data, | ||
| }, | ||
| &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "argocd-notifications-secret", | ||
| Namespace: testNamespace, | ||
| }, | ||
| Data: notificationsSecret.Data, | ||
| }) | ||
| mockRepoClient := &mocks.Clientset{RepoServerServiceClient: &mocks.RepoServerServiceClient{}} | ||
| argocdService, err := service.NewArgoCDService(kubeclientset, testNamespace, mockRepoClient) | ||
| require.NoError(t, err) | ||
| defer argocdService.Close() | ||
| config := api.Config{} | ||
| testDestination := services.Destination{ | ||
| Service: "webhook", | ||
| } | ||
| emptyAppData := map[string]any{} | ||
| settings := GetFactorySettingsDeferred( | ||
| func() service.Service { return argocdService }, | ||
| "test-secret", | ||
| "test-configmap", | ||
| false, | ||
| ) | ||
|
|
||
| varsProvider, _ := initGetVars(argocdService, &config, ¬ificationsCm, ¬ificationsSecret) | ||
| cfg := &api.Config{} | ||
| configMap := &corev1.ConfigMap{} | ||
| secret := &corev1.Secret{} | ||
|
|
||
| t.Run("Vars provider serves Application data on app key", func(t *testing.T) { | ||
| appData := map[string]any{ | ||
| "name": "app-name", | ||
| } | ||
| result := varsProvider(appData, testDestination) | ||
| assert.NotNil(t, t, result["app"]) | ||
| assert.Equal(t, result["app"], appData) | ||
| }) | ||
| t.Run("Vars provider serves notification context data on context key", func(t *testing.T) { | ||
| expectedContext := map[string]string{ | ||
| testContextKey: testContextKeyValue, | ||
| "notificationType": testDestination.Service, | ||
| } | ||
| result := varsProvider(emptyAppData, testDestination) | ||
| assert.NotNil(t, result["context"]) | ||
| assert.Equal(t, expectedContext, result["context"]) | ||
| }) | ||
| t.Run("Vars provider serves notification secrets on secrets key", func(t *testing.T) { | ||
| result := varsProvider(emptyAppData, testDestination) | ||
| assert.NotNil(t, result["secrets"]) | ||
| assert.Equal(t, result["secrets"], notificationsSecret.Data) | ||
| }) | ||
| _, err := settings.InitGetVars(cfg, configMap, secret) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "argocdService is not initialized") | ||
| } | ||
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.
Hi
Claude@puretension!Could you not modify the existing
GetFactorySettingsForCLIinstead, since this is the only place where that function is used? Another option is to revert #23424, but it's perhaps more clear with a deferred function.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.
Hi @blakepettersson!
You're absolutely right! I've modified the existing GetFactorySettingsForCLI
function to use the deferred pattern instead of creating a duplicate function.
This approach is much cleaner and eliminates the code duplication while still
solving the initialization issue. I also updated the test to match the changes.
Thanks for pointing this out. it's a much better solution!
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.
@blakepettersson Thanks for the review!
I've addressed the feedback and all tests are now passing.
Ready for another look!