github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/plank/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/github" 30 "k8s.io/test-infra/prow/kube" 31 "k8s.io/test-infra/prow/plank" 32 ) 33 34 var ( 35 totURL = flag.String("tot-url", "", "Tot URL") 36 37 configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.") 38 buildCluster = flag.String("build-cluster", "", "Path to file containing a YAML-marshalled kube.Cluster object. If empty, uses the local cluster.") 39 40 _ = flag.String("github-bot-name", "", "Deprecated.") 41 githubEndpoint = flag.String("github-endpoint", "https://api.github.com", "GitHub's API endpoint.") 42 githubTokenFile = flag.String("github-token-file", "/etc/github/oauth", "Path to the file containing the GitHub OAuth token.") 43 dryRun = flag.Bool("dry-run", true, "Whether or not to make mutating API calls to GitHub.") 44 ) 45 46 func main() { 47 flag.Parse() 48 logrus.SetFormatter(&logrus.JSONFormatter{}) 49 50 configAgent := &config.Agent{} 51 if err := configAgent.Start(*configPath); err != nil { 52 logrus.WithError(err).Fatal("Error starting config agent.") 53 } 54 55 kc, err := kube.NewClientInCluster(configAgent.Config().ProwJobNamespace) 56 if err != nil { 57 logrus.WithError(err).Fatal("Error getting kube client.") 58 } 59 var pkc *kube.Client 60 if *buildCluster == "" { 61 pkc = kc.Namespace(configAgent.Config().PodNamespace) 62 } else { 63 pkc, err = kube.NewClientFromFile(*buildCluster, configAgent.Config().PodNamespace) 64 if err != nil { 65 logrus.WithError(err).Fatal("Error getting kube client to build cluster.") 66 } 67 } 68 69 oauthSecretRaw, err := ioutil.ReadFile(*githubTokenFile) 70 if err != nil { 71 logrus.WithError(err).Fatalf("Could not read oauth secret file.") 72 } 73 74 _, err = url.Parse(*githubEndpoint) 75 if err != nil { 76 logrus.WithError(err).Fatalf("Must specify a valid --github-endpoint URL.") 77 } 78 79 oauthSecret := string(bytes.TrimSpace(oauthSecretRaw)) 80 81 var ghc *github.Client 82 if *dryRun { 83 ghc = github.NewDryRunClient(oauthSecret, *githubEndpoint) 84 } else { 85 ghc = github.NewClient(oauthSecret, *githubEndpoint) 86 } 87 88 logger := logrus.StandardLogger() 89 kc.Logger = logger.WithField("client", "kube") 90 pkc.Logger = logger.WithField("client", "kube") 91 ghc.Logger = logger.WithField("client", "github") 92 93 c, err := plank.NewController(kc, pkc, ghc, configAgent, *totURL) 94 if err != nil { 95 logrus.WithError(err).Fatal("Error creating plank controller.") 96 } 97 for range time.Tick(30 * time.Second) { 98 start := time.Now() 99 if err := c.Sync(); err != nil { 100 logrus.WithError(err).Error("Error syncing.") 101 } 102 logrus.Infof("Sync time: %v", time.Since(start)) 103 } 104 }