|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/signal" |
| 7 | + "syscall" |
| 8 | + |
| 9 | + "github.com/argoproj-labs/argocd-image-updater/pkg/argocd" |
| 10 | + "github.com/argoproj-labs/argocd-image-updater/pkg/kube" |
| 11 | + "github.com/argoproj-labs/argocd-image-updater/pkg/webhook" |
| 12 | + "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +// WebhookOptions holds the options for the webhook server |
| 18 | +type WebhookOptions struct { |
| 19 | + Port int |
| 20 | + DockerSecret string |
| 21 | + GHCRSecret string |
| 22 | + UpdateOnEvent bool |
| 23 | + ApplicationsAPIKind string |
| 24 | + AppNamespace string |
| 25 | + ServerAddr string |
| 26 | + Insecure bool |
| 27 | + Plaintext bool |
| 28 | + GRPCWeb bool |
| 29 | + AuthToken string |
| 30 | +} |
| 31 | + |
| 32 | +var webhookOpts WebhookOptions |
| 33 | + |
| 34 | +// NewWebhookCommand creates a new webhook command |
| 35 | +func NewWebhookCommand() *cobra.Command { |
| 36 | + var webhookCmd = &cobra.Command{ |
| 37 | + Use: "webhook", |
| 38 | + Short: "Start webhook server to receive registry events", |
| 39 | + Long: ` |
| 40 | +The webhook command starts a server that listens for webhook events from |
| 41 | +container registries. When an event is received, it can trigger an image |
| 42 | +update check for the affected images. |
| 43 | +
|
| 44 | +Supported registries: |
| 45 | +- Docker Hub |
| 46 | +- GitHub Container Registry (GHCR) |
| 47 | +`, |
| 48 | + Run: func(cmd *cobra.Command, args []string) { |
| 49 | + runWebhook() |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + webhookCmd.Flags().IntVar(&webhookOpts.Port, "port", 8080, "Port to listen on for webhook events") |
| 54 | + webhookCmd.Flags().StringVar(&webhookOpts.DockerSecret, "docker-secret", "", "Secret for validating Docker Hub webhooks") |
| 55 | + webhookCmd.Flags().StringVar(&webhookOpts.GHCRSecret, "ghcr-secret", "", "Secret for validating GitHub Container Registry webhooks") |
| 56 | + webhookCmd.Flags().BoolVar(&webhookOpts.UpdateOnEvent, "update-on-event", true, "Whether to trigger image update checks when webhook events are received") |
| 57 | + webhookCmd.Flags().StringVar(&webhookOpts.ApplicationsAPIKind, "applications-api", applicationsAPIKindK8S, "API kind that is used to manage Argo CD applications ('kubernetes' or 'argocd')") |
| 58 | + webhookCmd.Flags().StringVar(&webhookOpts.AppNamespace, "application-namespace", "", "namespace where Argo Image Updater will manage applications") |
| 59 | + webhookCmd.Flags().StringVar(&webhookOpts.ServerAddr, "argocd-server-addr", "", "address of ArgoCD API server") |
| 60 | + webhookCmd.Flags().BoolVar(&webhookOpts.Insecure, "argocd-insecure", false, "(INSECURE) ignore invalid TLS certs for ArgoCD server") |
| 61 | + webhookCmd.Flags().BoolVar(&webhookOpts.Plaintext, "argocd-plaintext", false, "(INSECURE) connect without TLS to ArgoCD server") |
| 62 | + webhookCmd.Flags().BoolVar(&webhookOpts.GRPCWeb, "argocd-grpc-web", false, "use grpc-web for connection to ArgoCD") |
| 63 | + webhookCmd.Flags().StringVar(&webhookOpts.AuthToken, "argocd-auth-token", "", "use token for authenticating to ArgoCD") |
| 64 | + |
| 65 | + return webhookCmd |
| 66 | +} |
| 67 | + |
| 68 | +// runWebhook starts the webhook server |
| 69 | +func runWebhook() { |
| 70 | + log.Infof("Starting webhook server on port %d", webhookOpts.Port) |
| 71 | + |
| 72 | + // Initialize the ArgoCD client |
| 73 | + var argoClient argocd.ArgoCD |
| 74 | + var err error |
| 75 | + |
| 76 | + // Create Kubernetes client |
| 77 | + var kubeClient *kube.ImageUpdaterKubernetesClient |
| 78 | + kubeClient, err = getKubeConfig(context.TODO(), "", "") |
| 79 | + if err != nil { |
| 80 | + log.Fatalf("Could not create Kubernetes client: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Set up based on application API kind |
| 84 | + if webhookOpts.ApplicationsAPIKind == applicationsAPIKindK8S { |
| 85 | + argoClient, err = argocd.NewK8SClient(kubeClient, &argocd.K8SClientOptions{AppNamespace: webhookOpts.AppNamespace}) |
| 86 | + } else { |
| 87 | + // Use defaults if not specified |
| 88 | + serverAddr := webhookOpts.ServerAddr |
| 89 | + if serverAddr == "" { |
| 90 | + serverAddr = defaultArgoCDServerAddr |
| 91 | + } |
| 92 | + |
| 93 | + // Check for auth token from environment if not provided |
| 94 | + authToken := webhookOpts.AuthToken |
| 95 | + if authToken == "" { |
| 96 | + if token := os.Getenv("ARGOCD_TOKEN"); token != "" { |
| 97 | + authToken = token |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + clientOpts := argocd.ClientOptions{ |
| 102 | + ServerAddr: serverAddr, |
| 103 | + Insecure: webhookOpts.Insecure, |
| 104 | + Plaintext: webhookOpts.Plaintext, |
| 105 | + GRPCWeb: webhookOpts.GRPCWeb, |
| 106 | + AuthToken: authToken, |
| 107 | + } |
| 108 | + argoClient, err = argocd.NewAPIClient(&clientOpts) |
| 109 | + } |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + log.Fatalf("Could not create ArgoCD client: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + // Create webhook handler |
| 116 | + handler := webhook.NewWebhookHandler() |
| 117 | + |
| 118 | + // Register supported webhook handlers |
| 119 | + dockerHandler := webhook.NewDockerHubWebhook(webhookOpts.DockerSecret) |
| 120 | + handler.RegisterHandler(dockerHandler) |
| 121 | + |
| 122 | + ghcrHandler := webhook.NewGHCRWebhook(webhookOpts.GHCRSecret) |
| 123 | + handler.RegisterHandler(ghcrHandler) |
| 124 | + |
| 125 | + // Create webhook server |
| 126 | + server := webhook.NewWebhookServer(webhookOpts.Port, handler, kubeClient, argoClient) |
| 127 | + |
| 128 | + // Set up graceful shutdown |
| 129 | + stop := make(chan os.Signal, 1) |
| 130 | + signal.Notify(stop, os.Interrupt, syscall.SIGTERM) |
| 131 | + |
| 132 | + // Start the server in a separate goroutine |
| 133 | + go func() { |
| 134 | + if err := server.Start(); err != nil { |
| 135 | + log.Fatalf("Failed to start webhook server: %v", err) |
| 136 | + } |
| 137 | + }() |
| 138 | + |
| 139 | + // Wait for interrupt signal |
| 140 | + <-stop |
| 141 | + |
| 142 | + // Gracefully shut down the server |
| 143 | + log.Infof("Shutting down webhook server") |
| 144 | + if err := server.Stop(); err != nil { |
| 145 | + log.Errorf("Error stopping webhook server: %v", err) |
| 146 | + } |
| 147 | +} |
0 commit comments