github.com/abayer/test-infra@v0.0.5/prow/cmd/gerrit/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  	"errors"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"os/signal"
    25  	"strings"
    26  	"syscall"
    27  	"time"
    28  
    29  	"github.com/sirupsen/logrus"
    30  
    31  	"k8s.io/test-infra/prow/config"
    32  	"k8s.io/test-infra/prow/gerrit"
    33  	"k8s.io/test-infra/prow/kube"
    34  	"k8s.io/test-infra/prow/logrusutil"
    35  )
    36  
    37  type options struct {
    38  	configPath string
    39  	instance   string
    40  	projects   string
    41  	storage    string
    42  }
    43  
    44  func (o *options) Validate() error {
    45  	if o.instance == "" {
    46  		return errors.New("--gerrit-instance must set")
    47  	}
    48  	return nil
    49  }
    50  
    51  func gatherOptions() options {
    52  	o := options{}
    53  	flag.StringVar(&o.configPath, "config-path", "/etc/config/config.yaml", "Path to config.yaml.")
    54  	flag.StringVar(&o.instance, "gerrit-instance", "", "URL to gerrit instance")
    55  	flag.StringVar(&o.projects, "gerrit-projects", "", "comma separated gerrit projects to fetch from the gerrit instance")
    56  	flag.StringVar(&o.storage, "storage", "", "Path to persistent volume to load the last sync time")
    57  	flag.Parse()
    58  	return o
    59  }
    60  
    61  func main() {
    62  	o := gatherOptions()
    63  	if err := o.Validate(); err != nil {
    64  		logrus.Fatalf("Invalid options: %v", err)
    65  	}
    66  
    67  	logrus.SetFormatter(
    68  		logrusutil.NewDefaultFieldsFormatter(nil, logrus.Fields{"component": "gerrit"}),
    69  	)
    70  
    71  	projs := strings.Split(o.projects, ",")
    72  	if len(projs) == 0 {
    73  		logrus.Fatal("must have one or more target gerrit project")
    74  	}
    75  
    76  	ca := &config.Agent{}
    77  	if err := ca.Start(o.configPath, ""); err != nil {
    78  		logrus.WithError(err).Fatal("Error starting config agent.")
    79  	}
    80  
    81  	kc, err := kube.NewClientInCluster(ca.Config().ProwJobNamespace)
    82  	if err != nil {
    83  		logrus.WithError(err).Fatal("Error getting kube client.")
    84  	}
    85  
    86  	c, err := gerrit.NewController(o.instance, o.storage, projs, kc, ca)
    87  	if err != nil {
    88  		logrus.WithError(err).Fatal("Error creating gerrit client.")
    89  	}
    90  
    91  	if err := c.Auth(); err != nil {
    92  		logrus.WithError(err).Fatal("Error auth gerrit client.")
    93  	}
    94  
    95  	logrus.Infof("Starting gerrit fetcher")
    96  
    97  	tick := time.Tick(ca.Config().Gerrit.TickInterval)
    98  	auth := time.Tick(time.Minute * 10)
    99  	sig := make(chan os.Signal, 1)
   100  	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
   101  
   102  	for {
   103  		select {
   104  		case <-tick:
   105  			start := time.Now()
   106  			if err := c.Sync(); err != nil {
   107  				logrus.WithError(err).Error("Error syncing.")
   108  			}
   109  			logrus.WithField("duration", fmt.Sprintf("%v", time.Since(start))).Info("Synced")
   110  		case <-auth:
   111  			if err := c.Auth(); err != nil {
   112  				logrus.WithError(err).Error("Error auth to gerrit... (continue)")
   113  			}
   114  		case <-sig:
   115  			logrus.Info("gerrit fetcher is shutting down...")
   116  			return
   117  		}
   118  	}
   119  }