github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/status-reconciler/main.go (about)

     1  /*
     2  Copyright 2018 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  package main
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"os/signal"
    23  	"syscall"
    24  
    25  	"github.com/sirupsen/logrus"
    26  
    27  	"k8s.io/test-infra/pkg/flagutil"
    28  	"k8s.io/test-infra/prow/config"
    29  	"k8s.io/test-infra/prow/config/secret"
    30  	prowflagutil "k8s.io/test-infra/prow/flagutil"
    31  	_ "k8s.io/test-infra/prow/hook"
    32  	"k8s.io/test-infra/prow/logrusutil"
    33  	"k8s.io/test-infra/prow/plugins"
    34  	"k8s.io/test-infra/prow/statusreconciler"
    35  )
    36  
    37  type options struct {
    38  	configPath    string
    39  	jobConfigPath string
    40  	pluginConfig  string
    41  
    42  	continueOnError bool
    43  	dryRun          bool
    44  	kubernetes      prowflagutil.KubernetesOptions
    45  	github          prowflagutil.GitHubOptions
    46  }
    47  
    48  func gatherOptions() options {
    49  	o := options{}
    50  	fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
    51  
    52  	fs.StringVar(&o.configPath, "config-path", "/etc/config/config.yaml", "Path to config.yaml.")
    53  	fs.StringVar(&o.jobConfigPath, "job-config-path", "", "Path to prow job configs.")
    54  	fs.StringVar(&o.pluginConfig, "plugin-config", "/etc/plugins/plugins.yaml", "Path to plugin config file.")
    55  
    56  	fs.BoolVar(&o.continueOnError, "continue-on-error", false, "Indicates that the migration should continue if context migration fails for an individual PR.")
    57  	fs.BoolVar(&o.dryRun, "dry-run", true, "Whether or not to make mutating API calls to GitHub.")
    58  	for _, group := range []flagutil.OptionGroup{&o.kubernetes, &o.github} {
    59  		group.AddFlags(fs)
    60  	}
    61  
    62  	fs.Parse(os.Args[1:])
    63  	return o
    64  }
    65  
    66  func (o *options) Validate() error {
    67  	for _, group := range []flagutil.OptionGroup{&o.kubernetes, &o.github} {
    68  		if err := group.Validate(o.dryRun); err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func main() {
    77  	o := gatherOptions()
    78  	if err := o.Validate(); err != nil {
    79  		logrus.WithError(err).Fatal("Invalid options")
    80  	}
    81  
    82  	logrus.SetFormatter(
    83  		logrusutil.NewDefaultFieldsFormatter(nil, logrus.Fields{"component": "status-reconciler"}),
    84  	)
    85  
    86  	configAgent := &config.Agent{}
    87  	if err := configAgent.Start(o.configPath, o.jobConfigPath); err != nil {
    88  		logrus.WithError(err).Fatal("Error starting config agent.")
    89  	}
    90  	changes := make(chan config.Delta)
    91  	configAgent.Subscribe(changes)
    92  
    93  	secretAgent := &secret.Agent{}
    94  	if o.github.TokenPath != "" {
    95  		if err := secretAgent.Start([]string{o.github.TokenPath}); err != nil {
    96  			logrus.WithError(err).Fatal("Error starting secrets agent.")
    97  		}
    98  	}
    99  
   100  	pluginAgent := &plugins.ConfigAgent{}
   101  	if err := pluginAgent.Start(o.pluginConfig); err != nil {
   102  		logrus.WithError(err).Fatal("Error starting plugin configuration 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  	kubeClient, err := o.kubernetes.Client(configAgent.Config().ProwJobNamespace, o.dryRun)
   111  	if err != nil {
   112  		logrus.WithError(err).Fatal("Error getting kube client.")
   113  	}
   114  
   115  	sig := make(chan os.Signal, 1)
   116  	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
   117  
   118  	c := statusreconciler.NewController(o.continueOnError, kubeClient, githubClient, configAgent, pluginAgent)
   119  	c.Run(sig, changes)
   120  }