Skip to content

Commit 3d2c010

Browse files
gcp-cherry-pick-bot[bot]alexymanthadaengdaengLee
authored
feat(hydrator): handle sourceHydrator fields from webhook (argoproj#19397) (cherry-pick argoproj#22485) (argoproj#22753)
Signed-off-by: daengdaengLee <[email protected]> Signed-off-by: Alexy Mantha <[email protected]> Co-authored-by: Alexy Mantha <[email protected]> Co-authored-by: Kunho Lee <[email protected]>
1 parent b6e6104 commit 3d2c010

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
@@ -307,6 +307,22 @@ func (a *ArgoCDWebhookHandler) HandleEvent(payload any) {
307307
continue
308308
}
309309
for _, app := range filteredApps {
310+
if app.Spec.SourceHydrator != nil {
311+
drySource := app.Spec.SourceHydrator.GetDrySource()
312+
if sourceRevisionHasChanged(drySource, revision, touchedHead) && sourceUsesURL(drySource, webURL, repoRegexp) {
313+
refreshPaths := path.GetAppRefreshPaths(&app)
314+
if path.AppFilesHaveChanged(refreshPaths, changedFiles) {
315+
namespacedAppInterface := a.appClientset.ArgoprojV1alpha1().Applications(app.ObjectMeta.Namespace)
316+
log.Infof("webhook trigger refresh app to hydrate '%s'", app.ObjectMeta.Name)
317+
_, err = argo.RefreshApp(namespacedAppInterface, app.ObjectMeta.Name, v1alpha1.RefreshTypeNormal, true)
318+
if err != nil {
319+
log.Warnf("Failed to hydrate app '%s' for controller reprocessing: %v", app.ObjectMeta.Name, err)
320+
continue
321+
}
322+
}
323+
}
324+
}
325+
310326
for _, source := range app.Spec.GetSources() {
311327
if sourceRevisionHasChanged(source, revision, touchedHead) && sourceUsesURL(source, webURL, repoRegexp) {
312328
refreshPaths := path.GetAppRefreshPaths(&app)

util/webhook/webhook_test.go

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

279+
// TestGitHubCommitEvent_Hydrate makes sure that a webhook will hydrate an app when dry source changed.
280+
func TestGitHubCommitEvent_Hydrate(t *testing.T) {
281+
hook := test.NewGlobal()
282+
var patched bool
283+
reaction := func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
284+
patchAction := action.(kubetesting.PatchAction)
285+
assert.Equal(t, "app-to-hydrate", patchAction.GetName())
286+
patched = true
287+
return true, nil, nil
288+
}
289+
h := NewMockHandler(&reactorDef{"patch", "applications", reaction}, []string{}, &v1alpha1.Application{
290+
ObjectMeta: metav1.ObjectMeta{
291+
Name: "app-to-hydrate",
292+
Namespace: "argocd",
293+
},
294+
Spec: v1alpha1.ApplicationSpec{
295+
SourceHydrator: &v1alpha1.SourceHydrator{
296+
DrySource: v1alpha1.DrySource{
297+
RepoURL: "https://github.com/jessesuen/test-repo",
298+
TargetRevision: "HEAD",
299+
Path: ".",
300+
},
301+
SyncSource: v1alpha1.SyncSource{
302+
TargetBranch: "environments/dev",
303+
Path: ".",
304+
},
305+
HydrateTo: nil,
306+
},
307+
},
308+
}, &v1alpha1.Application{
309+
ObjectMeta: metav1.ObjectMeta{
310+
Name: "app-to-ignore",
311+
},
312+
Spec: v1alpha1.ApplicationSpec{
313+
Sources: v1alpha1.ApplicationSources{
314+
{
315+
RepoURL: "https://github.com/some/unrelated-repo",
316+
Path: ".",
317+
},
318+
},
319+
},
320+
},
321+
)
322+
req := httptest.NewRequest(http.MethodPost, "/api/webhook", nil)
323+
req.Header.Set("X-GitHub-Event", "push")
324+
eventJSON, err := os.ReadFile("testdata/github-commit-event.json")
325+
require.NoError(t, err)
326+
req.Body = io.NopCloser(bytes.NewReader(eventJSON))
327+
w := httptest.NewRecorder()
328+
h.Handler(w, req)
329+
close(h.queue)
330+
h.Wait()
331+
assert.Equal(t, http.StatusOK, w.Code)
332+
assert.True(t, patched)
333+
334+
logMessages := make([]string, 0, len(hook.Entries))
335+
for _, entry := range hook.Entries {
336+
logMessages = append(logMessages, entry.Message)
337+
}
338+
339+
assert.Contains(t, logMessages, "webhook trigger refresh app to hydrate 'app-to-hydrate'")
340+
assert.NotContains(t, logMessages, "webhook trigger refresh app to hydrate 'app-to-ignore'")
341+
342+
hook.Reset()
343+
}
344+
279345
func TestGitHubTagEvent(t *testing.T) {
280346
hook := test.NewGlobal()
281347
h := NewMockHandler(nil, []string{})

0 commit comments

Comments
 (0)