github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/external-plugins/refresh/main.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Refresh retries GitHub status updates for stale PR statuses.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"net/http"
    24  	"net/url"
    25  	"os"
    26  	"strconv"
    27  	"time"
    28  
    29  	"github.com/sirupsen/logrus"
    30  	"sigs.k8s.io/prow/pkg/interrupts"
    31  	"sigs.k8s.io/prow/pkg/logrusutil"
    32  	"sigs.k8s.io/prow/pkg/pjutil"
    33  
    34  	"sigs.k8s.io/prow/pkg/config/secret"
    35  	"sigs.k8s.io/prow/pkg/flagutil"
    36  	prowflagutil "sigs.k8s.io/prow/pkg/flagutil"
    37  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    38  	"sigs.k8s.io/prow/pkg/pluginhelp/externalplugins"
    39  )
    40  
    41  type options struct {
    42  	port int
    43  
    44  	config                 configflagutil.ConfigOptions
    45  	dryRun                 bool
    46  	github                 prowflagutil.GitHubOptions
    47  	instrumentationOptions prowflagutil.InstrumentationOptions
    48  	prowURL                string
    49  
    50  	webhookSecretFile string
    51  }
    52  
    53  func (o *options) Validate() error {
    54  	for _, group := range []flagutil.OptionGroup{&o.github} {
    55  		if err := group.Validate(o.dryRun); err != nil {
    56  			return err
    57  		}
    58  	}
    59  
    60  	if _, err := url.ParseRequestURI(o.prowURL); err != nil {
    61  		return fmt.Errorf("invalid -prow-url URI: %q", o.prowURL)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func gatherOptions() options {
    68  	o := options{config: configflagutil.ConfigOptions{ConfigPath: "/etc/config/config.yaml"}}
    69  	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
    70  	fs.IntVar(&o.port, "port", 8888, "Port to listen on.")
    71  	fs.BoolVar(&o.dryRun, "dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.")
    72  	fs.StringVar(&o.webhookSecretFile, "hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
    73  	fs.StringVar(&o.prowURL, "prow-url", "", "Prow frontend URL.")
    74  	for _, group := range []flagutil.OptionGroup{&o.github, &o.instrumentationOptions, &o.config} {
    75  		group.AddFlags(fs)
    76  	}
    77  	fs.Parse(os.Args[1:])
    78  	return o
    79  }
    80  
    81  func main() {
    82  	o := gatherOptions()
    83  	if err := o.Validate(); err != nil {
    84  		logrus.Fatalf("Invalid options: %v", err)
    85  	}
    86  
    87  	logrusutil.ComponentInit()
    88  	log := logrus.StandardLogger().WithField("plugin", pluginName)
    89  
    90  	configAgent, err := o.config.ConfigAgent()
    91  	if err != nil {
    92  		log.WithError(err).Fatal("Error starting config agent.")
    93  	}
    94  
    95  	if err := secret.Add(o.webhookSecretFile); err != nil {
    96  		logrus.WithError(err).Fatal("Error starting secrets agent.")
    97  	}
    98  
    99  	githubClient, err := o.github.GitHubClient(o.dryRun)
   100  	if err != nil {
   101  		logrus.WithError(err).Fatal("Error getting GitHub client.")
   102  	}
   103  
   104  	serv := &server{
   105  		tokenGenerator: secret.GetTokenGenerator(o.webhookSecretFile),
   106  		prowURL:        o.prowURL,
   107  		configAgent:    configAgent,
   108  		ghc:            githubClient,
   109  		log:            log,
   110  	}
   111  
   112  	health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort)
   113  	health.ServeReady()
   114  
   115  	mux := http.NewServeMux()
   116  	mux.Handle("/", serv)
   117  	externalplugins.ServeExternalPluginHelp(mux, log, helpProvider)
   118  	httpServer := &http.Server{Addr: ":" + strconv.Itoa(o.port), Handler: mux}
   119  	defer interrupts.WaitForGracefulShutdown()
   120  	interrupts.ListenAndServe(httpServer, 5*time.Second)
   121  }