github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/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  	"os/signal"
    27  	"strconv"
    28  	"syscall"
    29  
    30  	"github.com/sirupsen/logrus"
    31  
    32  	"k8s.io/test-infra/pkg/flagutil"
    33  	"k8s.io/test-infra/prow/config"
    34  	"k8s.io/test-infra/prow/config/secret"
    35  	prowflagutil "k8s.io/test-infra/prow/flagutil"
    36  	"k8s.io/test-infra/prow/pluginhelp/externalplugins"
    37  )
    38  
    39  type options struct {
    40  	port int
    41  
    42  	configPath string
    43  	dryRun     bool
    44  	github     prowflagutil.GitHubOptions
    45  	prowURL    string
    46  
    47  	webhookSecretFile string
    48  }
    49  
    50  func (o *options) Validate() error {
    51  	for _, group := range []flagutil.OptionGroup{&o.github} {
    52  		if err := group.Validate(o.dryRun); err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	if _, err := url.ParseRequestURI(o.prowURL); err != nil {
    58  		return fmt.Errorf("invalid -prow-url URI: %q", o.prowURL)
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func gatherOptions() options {
    65  	o := options{}
    66  	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
    67  	fs.IntVar(&o.port, "port", 8888, "Port to listen on.")
    68  	fs.StringVar(&o.configPath, "config-path", "/etc/config/config.yaml", "Path to config.yaml.")
    69  	fs.BoolVar(&o.dryRun, "dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.")
    70  	fs.StringVar(&o.webhookSecretFile, "hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
    71  	fs.StringVar(&o.prowURL, "prow-url", "", "Prow frontend URL.")
    72  	for _, group := range []flagutil.OptionGroup{&o.github} {
    73  		group.AddFlags(fs)
    74  	}
    75  	fs.Parse(os.Args[1:])
    76  	return o
    77  }
    78  
    79  func main() {
    80  	o := gatherOptions()
    81  	if err := o.Validate(); err != nil {
    82  		logrus.Fatalf("Invalid options: %v", err)
    83  	}
    84  
    85  	logrus.SetFormatter(&logrus.JSONFormatter{})
    86  	// TODO: Use global option from the prow config.
    87  	logrus.SetLevel(logrus.DebugLevel)
    88  	log := logrus.StandardLogger().WithField("plugin", "refresh")
    89  
    90  	// Ignore SIGTERM so that we don't drop hooks when the pod is removed.
    91  	// We'll get SIGTERM first and then SIGKILL after our graceful termination
    92  	// deadline.
    93  	signal.Ignore(syscall.SIGTERM)
    94  
    95  	configAgent := &config.Agent{}
    96  	if err := configAgent.Start(o.configPath, ""); err != nil {
    97  		log.WithError(err).Fatal("Error starting config agent.")
    98  	}
    99  
   100  	secretAgent := &secret.Agent{}
   101  	if err := secretAgent.Start([]string{o.github.TokenPath, o.webhookSecretFile}); err != nil {
   102  		logrus.WithError(err).Fatal("Error starting secrets agent.")
   103  	}
   104  
   105  	githubClient, err := o.github.GitHubClient(secretAgent, o.dryRun)
   106  	if err != nil {
   107  		logrus.WithError(err).Fatal("Error getting GitHub client.")
   108  	}
   109  
   110  	serv := &server{
   111  		tokenGenerator: secretAgent.GetTokenGenerator(o.webhookSecretFile),
   112  		prowURL:        o.prowURL,
   113  		configAgent:    configAgent,
   114  		ghc:            githubClient,
   115  		log:            log,
   116  	}
   117  
   118  	http.Handle("/", serv)
   119  	externalplugins.ServeExternalPluginHelp(http.DefaultServeMux, log, helpProvider)
   120  	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(o.port), nil))
   121  }