github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/tide/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  package main
    18  
    19  import (
    20  	"bytes"
    21  	"flag"
    22  	"io/ioutil"
    23  	"net/url"
    24  	"time"
    25  
    26  	"github.com/sirupsen/logrus"
    27  
    28  	"k8s.io/test-infra/prow/config"
    29  	"k8s.io/test-infra/prow/git"
    30  	"k8s.io/test-infra/prow/github"
    31  	"k8s.io/test-infra/prow/kube"
    32  	"k8s.io/test-infra/prow/tide"
    33  )
    34  
    35  var (
    36  	dryRun  = flag.Bool("dry-run", true, "Whether to mutate any real-world state.")
    37  	runOnce = flag.Bool("run-once", false, "If true, run only once then quit.")
    38  
    39  	configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.")
    40  	cluster    = flag.String("cluster", "", "Path to kube.Cluster YAML file. If empty, uses the local cluster.")
    41  
    42  	_               = flag.String("github-bot-name", "", "Deprecated.")
    43  	githubEndpoint  = flag.String("github-endpoint", "https://api.github.com", "GitHub's API endpoint.")
    44  	githubTokenFile = flag.String("github-token-file", "/etc/github/oauth", "Path to the file containing the GitHub OAuth token.")
    45  )
    46  
    47  func main() {
    48  	flag.Parse()
    49  	logrus.SetFormatter(&logrus.JSONFormatter{})
    50  
    51  	configAgent := &config.Agent{}
    52  	if err := configAgent.Start(*configPath); err != nil {
    53  		logrus.WithError(err).Fatal("Error starting config agent.")
    54  	}
    55  
    56  	oauthSecretRaw, err := ioutil.ReadFile(*githubTokenFile)
    57  	if err != nil {
    58  		logrus.WithError(err).Fatalf("Could not read oauth secret file.")
    59  	}
    60  	oauthSecret := string(bytes.TrimSpace(oauthSecretRaw))
    61  
    62  	_, err = url.Parse(*githubEndpoint)
    63  	if err != nil {
    64  		logrus.WithError(err).Fatalf("Must specify a valid --github-endpoint URL.")
    65  	}
    66  
    67  	ghc := github.NewClient(oauthSecret, *githubEndpoint)
    68  
    69  	var kc *kube.Client
    70  	if *cluster == "" {
    71  		kc, err = kube.NewClientInCluster(configAgent.Config().ProwJobNamespace)
    72  		if err != nil {
    73  			logrus.WithError(err).Fatal("Error getting kube client.")
    74  		}
    75  	} else {
    76  		kc, err = kube.NewClientFromFile(*cluster, configAgent.Config().ProwJobNamespace)
    77  		if err != nil {
    78  			logrus.WithError(err).Fatal("Error getting kube client.")
    79  		}
    80  	}
    81  
    82  	gc, err := git.NewClient()
    83  	if err != nil {
    84  		logrus.WithError(err).Fatal("Error getting git client.")
    85  	}
    86  	defer gc.Clean()
    87  
    88  	logger := logrus.StandardLogger()
    89  	ghc.Logger = logger.WithField("client", "github")
    90  	kc.Logger = logger.WithField("client", "kube")
    91  	gc.Logger = logger.WithField("client", "git")
    92  
    93  	c := tide.NewController(ghc, kc, configAgent, gc)
    94  	c.Logger = logger.WithField("controller", "tide")
    95  	c.DryRun = *dryRun
    96  
    97  	sync(c)
    98  	if *runOnce {
    99  		return
   100  	}
   101  	for range time.Tick(time.Minute) {
   102  		sync(c)
   103  	}
   104  }
   105  
   106  func sync(c *tide.Controller) {
   107  	start := time.Now()
   108  	if err := c.Sync(); err != nil {
   109  		logrus.WithError(err).Error("Error syncing.")
   110  	}
   111  	logrus.Infof("Sync time: %v", time.Since(start))
   112  }