Skip to content

Commit 87e0889

Browse files
committed
feat(webhook): make improvements to the community pr by fixing issues with the git writeback method and making the webhook command more inline with the run command
Signed-off-by: Christopher Coco <[email protected]>
1 parent 9abea93 commit 87e0889

File tree

4 files changed

+207
-101
lines changed

4 files changed

+207
-101
lines changed

cmd/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ type ImageUpdaterConfig struct {
5151
GitCommitSignOff bool
5252
DisableKubeEvents bool
5353
GitCreds git.CredsStore
54-
WebhookPort int
5554
EnableWebhook bool
5655
}
5756

cmd/run.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
// newRunCommand implements "run" command
3333
func newRunCommand() *cobra.Command {
3434
var cfg *ImageUpdaterConfig = &ImageUpdaterConfig{}
35+
var webhookCfg *WebhookConfig = &WebhookConfig{}
3536
var once bool
3637
var kubeConfig string
3738
var disableKubernetes bool
@@ -183,7 +184,7 @@ func newRunCommand() *cobra.Command {
183184

184185
// Start the webhook server if enabled
185186
var webhookServer *webhook.WebhookServer
186-
if cfg.EnableWebhook && cfg.WebhookPort > 0 {
187+
if cfg.EnableWebhook && webhookCfg.Port > 0 {
187188
// Initialize the ArgoCD client for webhook server
188189
var argoClient argocd.ArgoCD
189190
switch cfg.ApplicationsAPIKind {
@@ -200,33 +201,36 @@ func newRunCommand() *cobra.Command {
200201
handler := webhook.NewWebhookHandler()
201202

202203
// Register supported webhook handlers with default empty secrets
203-
// In production, these would be configured via flags or environment variables
204-
dockerHandler := webhook.NewDockerHubWebhook("")
204+
dockerHandler := webhook.NewDockerHubWebhook(webhookCfg.DockerSecret)
205205
handler.RegisterHandler(dockerHandler)
206206

207-
ghcrHandler := webhook.NewGHCRWebhook("")
207+
ghcrHandler := webhook.NewGHCRWebhook(webhookCfg.GHCRSecret)
208208
handler.RegisterHandler(ghcrHandler)
209209

210-
harborHandler := webhook.NewHarborWebhook("")
210+
harborHandler := webhook.NewHarborWebhook(webhookCfg.HarborSecret)
211211
handler.RegisterHandler(harborHandler)
212212

213-
quayHandler := webhook.NewQuayWebhook("")
213+
quayHandler := webhook.NewQuayWebhook(webhookCfg.QuaySecret)
214214
handler.RegisterHandler(quayHandler)
215215

216-
log.Infof("Starting webhook server on port %d", cfg.WebhookPort)
217-
webhookServer = webhook.NewWebhookServer(cfg.WebhookPort, handler, cfg.KubeClient, argoClient)
216+
log.Infof("Starting webhook server on port %d", webhookCfg.Port)
217+
webhookServer = webhook.NewWebhookServer(webhookCfg.Port, handler, cfg.KubeClient, argoClient)
218218

219219
// Set updater config
220-
updaterConfig := &argocd.UpdaterConfig{
220+
webhookServer.UpdaterConfig = &argocd.UpdateConfiguration{
221+
NewRegFN: registry.NewClient,
222+
ArgoClient: cfg.ArgoClient,
223+
KubeClient: cfg.KubeClient,
221224
DryRun: cfg.DryRun,
222225
GitCommitUser: cfg.GitCommitUser,
223226
GitCommitEmail: cfg.GitCommitMail,
224-
GitCommitMessage: cfg.GitCommitMessage.Tree.Root.String(),
227+
GitCommitMessage: cfg.GitCommitMessage,
225228
GitCommitSigningKey: cfg.GitCommitSigningKey,
226229
GitCommitSigningMethod: cfg.GitCommitSigningMethod,
227230
GitCommitSignOff: cfg.GitCommitSignOff,
231+
DisableKubeEvents: cfg.DisableKubeEvents,
232+
GitCreds: cfg.GitCreds,
228233
}
229-
webhookServer.UpdaterConfig = updaterConfig
230234

231235
whErrCh = make(chan error, 1)
232236
go func() {
@@ -236,7 +240,7 @@ func newRunCommand() *cobra.Command {
236240
}
237241
}()
238242

239-
log.Infof("Webhook server started and listening on port %d", cfg.WebhookPort)
243+
log.Infof("Webhook server started and listening on port %d", webhookCfg.Port)
240244
}
241245

242246
// This is our main loop. We leave it only when our health probe server
@@ -326,9 +330,15 @@ func newRunCommand() *cobra.Command {
326330
runCmd.Flags().BoolVar(&cfg.GitCommitSignOff, "git-commit-sign-off", env.GetBoolVal("GIT_COMMIT_SIGN_OFF", false), "Whether to sign-off git commits")
327331
runCmd.Flags().StringVar(&commitMessagePath, "git-commit-message-path", defaultCommitTemplatePath, "Path to a template to use for Git commit messages")
328332
runCmd.Flags().BoolVar(&cfg.DisableKubeEvents, "disable-kube-events", env.GetBoolVal("IMAGE_UPDATER_KUBE_EVENTS", false), "Disable kubernetes events")
329-
runCmd.Flags().IntVar(&cfg.WebhookPort, "webhook-port", env.ParseNumFromEnv("WEBHOOK_PORT", 8082, 0, 65535), "Port to start the webhook server on, 0 to disable")
330333
runCmd.Flags().BoolVar(&cfg.EnableWebhook, "enable-webhook", env.GetBoolVal("ENABLE_WEBHOOK", false), "Enable webhook server for receiving registry events")
331334

335+
runCmd.Flags().IntVar(&webhookCfg.Port, "webhook-port", env.ParseNumFromEnv("WEBHOOK_PORT", 8082, 0, 65535), "Port to listen on for webhook events")
336+
runCmd.Flags().StringVar(&webhookCfg.DockerSecret, "docker-secret", env.GetStringVal("DOCKER_WEBHOOK_SECRET", ""), "Secret for validating Docker Hub webhooks")
337+
runCmd.Flags().StringVar(&webhookCfg.GHCRSecret, "ghcr-secret", env.GetStringVal("GHCR_WEBHOOK_SECRET", ""), "Secret for validating GitHub Container Registry webhooks")
338+
runCmd.Flags().StringVar(&webhookCfg.QuaySecret, "quay-secret", env.GetStringVal("QUAY_WEBHOOK_SECRET", ""), "Secret for validating Quay webhooks")
339+
runCmd.Flags().StringVar(&webhookCfg.HarborSecret, "harbor-secret", env.GetStringVal("HARBOR_WEBHOOK_SECRET", ""), "Secret for validating Harbor webhooks")
340+
runCmd.Flags().BoolVar(&webhookCfg.UpdateOnEvent, "update-on-event", true, "Whether to trigger image update checks when webhook events are received")
341+
332342
return runCmd
333343
}
334344

0 commit comments

Comments
 (0)