github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/jenkins-operator/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/jenkins" 31 "k8s.io/test-infra/prow/kube" 32 ) 33 34 var ( 35 configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.") 36 37 jenkinsURL = flag.String("jenkins-url", "http://jenkins-proxy", "Jenkins URL") 38 jenkinsUserName = flag.String("jenkins-user", "jenkins-trigger", "Jenkins username") 39 jenkinsTokenFile = flag.String("jenkins-token-file", "", "Path to the file containing the Jenkins API token.") 40 jenkinsBearerTokenFile = flag.String("jenkins-bearer-token-file", "", "Path to the file containing the Jenkins API bearer token.") 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 dryRun = flag.Bool("dry-run", true, "Whether or not to make mutating API calls to GitHub.") 46 ) 47 48 func main() { 49 flag.Parse() 50 logrus.SetFormatter(&logrus.JSONFormatter{}) 51 52 configAgent := &config.Agent{} 53 if err := configAgent.Start(*configPath); err != nil { 54 logrus.WithError(err).Fatal("Error starting config agent.") 55 } 56 57 kc, err := kube.NewClientInCluster(configAgent.Config().ProwJobNamespace) 58 if err != nil { 59 logrus.WithError(err).Fatal("Error getting kube client.") 60 } 61 62 ac := &jenkins.AuthConfig{} 63 if *jenkinsTokenFile != "" { 64 token, err := loadToken(*jenkinsTokenFile) 65 if err != nil { 66 logrus.WithError(err).Fatalf("Could not read token file.") 67 } 68 ac.Basic = &jenkins.BasicAuthConfig{ 69 User: *jenkinsUserName, 70 Token: token, 71 } 72 } else if *jenkinsBearerTokenFile != "" { 73 token, err := loadToken(*jenkinsBearerTokenFile) 74 if err != nil { 75 logrus.WithError(err).Fatalf("Could not read bearer token file.") 76 } 77 ac.BearerToken = &jenkins.BearerTokenAuthConfig{ 78 Token: token, 79 } 80 } else { 81 logrus.Fatal("An auth token for basic or bearer token auth must be supplied.") 82 } 83 jc := jenkins.NewClient(*jenkinsURL, ac) 84 85 oauthSecretRaw, err := ioutil.ReadFile(*githubTokenFile) 86 if err != nil { 87 logrus.WithError(err).Fatalf("Could not read Github oauth secret file.") 88 } 89 oauthSecret := string(bytes.TrimSpace(oauthSecretRaw)) 90 91 _, err = url.Parse(*githubEndpoint) 92 if err != nil { 93 logrus.WithError(err).Fatal("Must specify a valid --github-endpoint URL.") 94 } 95 96 var ghc *github.Client 97 if *dryRun { 98 ghc = github.NewDryRunClient(oauthSecret, *githubEndpoint) 99 } else { 100 ghc = github.NewClient(oauthSecret, *githubEndpoint) 101 } 102 103 logger := logrus.StandardLogger() 104 kc.Logger = logger.WithField("client", "kube") 105 jc.Logger = logger.WithField("client", "jenkins") 106 ghc.Logger = logger.WithField("client", "github") 107 108 c := jenkins.NewController(kc, jc, ghc, configAgent) 109 110 for range time.Tick(30 * time.Second) { 111 start := time.Now() 112 if err := c.Sync(); err != nil { 113 logrus.WithError(err).Error("Error syncing.") 114 } 115 logrus.Infof("Sync time: %v", time.Since(start)) 116 } 117 } 118 119 func loadToken(file string) (string, error) { 120 raw, err := ioutil.ReadFile(file) 121 if err != nil { 122 return "", err 123 } 124 return string(bytes.TrimSpace(raw)), nil 125 }