github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/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 "syscall" 26 "time" 27 28 "github.com/sirupsen/logrus" 29 30 "k8s.io/test-infra/prow/config" 31 "k8s.io/test-infra/prow/gerrit/adapter" 32 "k8s.io/test-infra/prow/gerrit/client" 33 "k8s.io/test-infra/prow/kube" 34 "k8s.io/test-infra/prow/logrusutil" 35 ) 36 37 type options struct { 38 cookiefilePath string 39 configPath string 40 jobConfigPath string 41 projects client.ProjectsFlag 42 lastSyncFallback string 43 } 44 45 func (o *options) Validate() error { 46 if len(o.projects) == 0 { 47 return errors.New("--gerrit-projects must be set") 48 } 49 50 if o.cookiefilePath == "" { 51 logrus.Info("--cookiefile is not set, using anonymous authentication") 52 } 53 54 if o.configPath == "" { 55 return errors.New("--config-path must be set") 56 } 57 58 if o.lastSyncFallback == "" { 59 return errors.New("--last-sync-fallback must be set") 60 } 61 62 return nil 63 } 64 65 func gatherOptions() options { 66 o := options{ 67 projects: client.ProjectsFlag{}, 68 } 69 flag.StringVar(&o.configPath, "config-path", "", "Path to config.yaml.") 70 flag.StringVar(&o.jobConfigPath, "job-config-path", "", "Path to prow job configs") 71 flag.StringVar(&o.cookiefilePath, "cookiefile", "", "Path to git http.cookiefile, leave empty for anonymous") 72 flag.Var(&o.projects, "gerrit-projects", "Set of gerrit repos to monitor on a host example: --gerrit-host=https://android.googlesource.com=platform/build,toolchain/llvm, repeat flag for each host") 73 flag.StringVar(&o.lastSyncFallback, "last-sync-fallback", "", "Path to persistent volume to load the last sync time") 74 flag.Parse() 75 return o 76 } 77 78 func main() { 79 logrus.SetFormatter(logrusutil.NewDefaultFieldsFormatter(nil, logrus.Fields{"component": "gerrit"})) 80 o := gatherOptions() 81 if err := o.Validate(); err != nil { 82 logrus.Fatalf("Invalid options: %v", err) 83 } 84 85 ca := &config.Agent{} 86 if err := ca.Start(o.configPath, o.jobConfigPath); err != nil { 87 logrus.WithError(err).Fatal("Error starting config agent.") 88 } 89 90 kc, err := kube.NewClientInCluster(ca.Config().ProwJobNamespace) 91 if err != nil { 92 logrus.WithError(err).Fatal("Error getting kube client.") 93 } 94 95 c, err := adapter.NewController(o.lastSyncFallback, o.cookiefilePath, o.projects, kc, ca) 96 if err != nil { 97 logrus.WithError(err).Fatal("Error creating gerrit client.") 98 } 99 100 logrus.Infof("Starting gerrit fetcher") 101 102 tick := time.Tick(ca.Config().Gerrit.TickInterval) 103 sig := make(chan os.Signal, 1) 104 signal.Notify(sig, os.Interrupt, syscall.SIGTERM) 105 106 for { 107 select { 108 case <-tick: 109 start := time.Now() 110 if err := c.Sync(); err != nil { 111 logrus.WithError(err).Error("Error syncing.") 112 } 113 logrus.WithField("duration", fmt.Sprintf("%v", time.Since(start))).Info("Synced") 114 case <-sig: 115 logrus.Info("gerrit fetcher is shutting down...") 116 return 117 } 118 } 119 }