github.com/abayer/test-infra@v0.0.5/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  	"errors"
    22  	"flag"
    23  	"fmt"
    24  	"net/http"
    25  	"net/url"
    26  	"os/signal"
    27  	"strconv"
    28  	"syscall"
    29  
    30  	"github.com/sirupsen/logrus"
    31  
    32  	"k8s.io/test-infra/prow/config"
    33  	"k8s.io/test-infra/prow/flagutil"
    34  	"k8s.io/test-infra/prow/github"
    35  	"k8s.io/test-infra/prow/pluginhelp/externalplugins"
    36  )
    37  
    38  var (
    39  	configPath        = flag.String("config-path", "/etc/config/config.yaml", "Path to config.yaml.")
    40  	port              = flag.Int("port", 8888, "Port to listen on.")
    41  	dryRun            = flag.Bool("dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.")
    42  	webhookSecretFile = flag.String("hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.")
    43  	githubTokenFile   = flag.String("github-token-file", "/etc/github/oauth", "Path to the file containing the GitHub OAuth token.")
    44  	githubEndpoint    = flagutil.NewStrings("https://api.github.com")
    45  	prowURL           = flag.String("prow-url", "", "Prow frontend URL.")
    46  )
    47  
    48  func init() {
    49  	flag.Var(&githubEndpoint, "github-endpoint", "GitHub's API endpoint.")
    50  }
    51  
    52  func validateFlags() error {
    53  	if *prowURL == "" {
    54  		return errors.New("--prow-url needs to be specified")
    55  	}
    56  	for _, ep := range githubEndpoint.Strings() {
    57  		if _, err := url.Parse(ep); err != nil {
    58  			return fmt.Errorf("invalid --endpoint URL %q: %v", ep, err)
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func main() {
    65  	flag.Parse()
    66  	logrus.SetFormatter(&logrus.JSONFormatter{})
    67  	// TODO: Use global option from the prow config.
    68  	logrus.SetLevel(logrus.DebugLevel)
    69  	log := logrus.StandardLogger().WithField("plugin", "refresh")
    70  
    71  	// Ignore SIGTERM so that we don't drop hooks when the pod is removed.
    72  	// We'll get SIGTERM first and then SIGKILL after our graceful termination
    73  	// deadline.
    74  	signal.Ignore(syscall.SIGTERM)
    75  
    76  	if err := validateFlags(); err != nil {
    77  		log.WithError(err).Fatal("Error validating flags.")
    78  	}
    79  
    80  	configAgent := &config.Agent{}
    81  	if err := configAgent.Start(*configPath, ""); err != nil {
    82  		log.WithError(err).Fatal("Error starting config agent.")
    83  	}
    84  
    85  	secretAgent := &config.SecretAgent{}
    86  	if err := secretAgent.Start([]string{*githubTokenFile, *webhookSecretFile}); err != nil {
    87  		logrus.WithError(err).Fatal("Error starting secrets agent.")
    88  	}
    89  
    90  	ghc := github.NewClient(secretAgent.GetTokenGenerator(*githubTokenFile), githubEndpoint.Strings()...)
    91  	if *dryRun {
    92  		ghc = github.NewDryRunClient(secretAgent.GetTokenGenerator(*githubTokenFile), githubEndpoint.Strings()...)
    93  	}
    94  
    95  	serv := &server{
    96  		tokenGenerator: secretAgent.GetTokenGenerator(*webhookSecretFile),
    97  		prowURL:        *prowURL,
    98  		configAgent:    configAgent,
    99  		ghc:            ghc,
   100  		log:            log,
   101  	}
   102  
   103  	http.Handle("/", serv)
   104  	externalplugins.ServeExternalPluginHelp(http.DefaultServeMux, log, helpProvider)
   105  	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
   106  }