Skip to content

Commit d9ef10a

Browse files
feat(hydrator): handle sourceHydrator fields from webhook (#19397) (#22485)
Signed-off-by: daengdaengLee <[email protected]> Signed-off-by: Alexy Mantha <[email protected]> Co-authored-by: Kunho Lee <[email protected]>
1 parent 06dd876 commit d9ef10a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

util/webhook/webhook.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ func (a *ArgoCDWebhookHandler) HandleEvent(payload any) {
351351
continue
352352
}
353353
for _, app := range filteredApps {
354+
if app.Spec.SourceHydrator != nil {
355+
drySource := app.Spec.SourceHydrator.GetDrySource()
356+
if sourceRevisionHasChanged(drySource, revision, touchedHead) && sourceUsesURL(drySource, webURL, repoRegexp) {
357+
refreshPaths := path.GetAppRefreshPaths(&app)
358+
if path.AppFilesHaveChanged(refreshPaths, changedFiles) {
359+
namespacedAppInterface := a.appClientset.ArgoprojV1alpha1().Applications(app.ObjectMeta.Namespace)
360+
log.Infof("webhook trigger refresh app to hydrate '%s'", app.ObjectMeta.Name)
361+
_, err = argo.RefreshApp(namespacedAppInterface, app.ObjectMeta.Name, v1alpha1.RefreshTypeNormal, true)
362+
if err != nil {
363+
log.Warnf("Failed to hydrate app '%s' for controller reprocessing: %v", app.ObjectMeta.Name, err)
364+
continue
365+
}
366+
}
367+
}
368+
}
369+
354370
for _, source := range app.Spec.GetSources() {
355371
if sourceRevisionHasChanged(source, revision, touchedHead) && sourceUsesURL(source, webURL, repoRegexp) {
356372
refreshPaths := path.GetAppRefreshPaths(&app)

util/webhook/webhook_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,72 @@ func TestGitHubCommitEvent_AppsInOtherNamespaces(t *testing.T) {
310310
hook.Reset()
311311
}
312312

313+
// TestGitHubCommitEvent_Hydrate makes sure that a webhook will hydrate an app when dry source changed.
314+
func TestGitHubCommitEvent_Hydrate(t *testing.T) {
315+
hook := test.NewGlobal()
316+
var patched bool
317+
reaction := func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
318+
patchAction := action.(kubetesting.PatchAction)
319+
assert.Equal(t, "app-to-hydrate", patchAction.GetName())
320+
patched = true
321+
return true, nil, nil
322+
}
323+
h := NewMockHandler(&reactorDef{"patch", "applications", reaction}, []string{}, &v1alpha1.Application{
324+
ObjectMeta: metav1.ObjectMeta{
325+
Name: "app-to-hydrate",
326+
Namespace: "argocd",
327+
},
328+
Spec: v1alpha1.ApplicationSpec{
329+
SourceHydrator: &v1alpha1.SourceHydrator{
330+
DrySource: v1alpha1.DrySource{
331+
RepoURL: "https://github.com/jessesuen/test-repo",
332+
TargetRevision: "HEAD",
333+
Path: ".",
334+
},
335+
SyncSource: v1alpha1.SyncSource{
336+
TargetBranch: "environments/dev",
337+
Path: ".",
338+
},
339+
HydrateTo: nil,
340+
},
341+
},
342+
}, &v1alpha1.Application{
343+
ObjectMeta: metav1.ObjectMeta{
344+
Name: "app-to-ignore",
345+
},
346+
Spec: v1alpha1.ApplicationSpec{
347+
Sources: v1alpha1.ApplicationSources{
348+
{
349+
RepoURL: "https://github.com/some/unrelated-repo",
350+
Path: ".",
351+
},
352+
},
353+
},
354+
},
355+
)
356+
req := httptest.NewRequest(http.MethodPost, "/api/webhook", nil)
357+
req.Header.Set("X-GitHub-Event", "push")
358+
eventJSON, err := os.ReadFile("testdata/github-commit-event.json")
359+
require.NoError(t, err)
360+
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
361+
w := httptest.NewRecorder()
362+
h.Handler(w, req)
363+
close(h.queue)
364+
h.Wait()
365+
assert.Equal(t, http.StatusOK, w.Code)
366+
assert.True(t, patched)
367+
368+
logMessages := make([]string, 0, len(hook.Entries))
369+
for _, entry := range hook.Entries {
370+
logMessages = append(logMessages, entry.Message)
371+
}
372+
373+
assert.Contains(t, logMessages, "webhook trigger refresh app to hydrate 'app-to-hydrate'")
374+
assert.NotContains(t, logMessages, "webhook trigger refresh app to hydrate 'app-to-ignore'")
375+
376+
hook.Reset()
377+
}
378+
313379
func TestGitHubTagEvent(t *testing.T) {
314380
hook := test.NewGlobal()
315381
h := NewMockHandler(nil, []string{})

0 commit comments

Comments
 (0)