github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/horologium/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  	"flag"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/sirupsen/logrus"
    25  
    26  	"k8s.io/test-infra/prow/config"
    27  	"k8s.io/test-infra/prow/kube"
    28  	"k8s.io/test-infra/prow/pjutil"
    29  )
    30  
    31  var configPath = flag.String("config-path", "/etc/config/config", "Path to config.yaml.")
    32  
    33  func main() {
    34  	flag.Parse()
    35  	logrus.SetFormatter(&logrus.JSONFormatter{})
    36  
    37  	configAgent := config.Agent{}
    38  	if err := configAgent.Start(*configPath); err != nil {
    39  		logrus.WithError(err).Fatal("Error starting config agent.")
    40  	}
    41  
    42  	kc, err := kube.NewClientInCluster(configAgent.Config().ProwJobNamespace)
    43  	if err != nil {
    44  		logrus.WithError(err).Fatal("Error getting kube client.")
    45  	}
    46  
    47  	logger := logrus.StandardLogger()
    48  	kc.Logger = logger.WithField("client", "kube")
    49  
    50  	for now := range time.Tick(1 * time.Minute) {
    51  		start := time.Now()
    52  		if err := sync(kc, configAgent.Config(), now); err != nil {
    53  			logrus.WithError(err).Error("Error syncing periodic jobs.")
    54  		}
    55  		logrus.Infof("Sync time: %v", time.Since(start))
    56  	}
    57  }
    58  
    59  type kubeClient interface {
    60  	ListProwJobs(map[string]string) ([]kube.ProwJob, error)
    61  	CreateProwJob(kube.ProwJob) (kube.ProwJob, error)
    62  }
    63  
    64  func sync(kc kubeClient, cfg *config.Config, now time.Time) error {
    65  	jobs, err := kc.ListProwJobs(nil)
    66  	if err != nil {
    67  		return fmt.Errorf("error listing prow jobs: %v", err)
    68  	}
    69  	latestJobs := pjutil.GetLatestPeriodics(jobs)
    70  
    71  	for _, p := range cfg.Periodics {
    72  		j, ok := latestJobs[p.Name]
    73  		if !ok || (j.Complete() && now.Sub(j.Status.StartTime) > p.GetInterval()) {
    74  			if _, err := kc.CreateProwJob(pjutil.NewProwJob(pjutil.PeriodicSpec(p))); err != nil {
    75  				return fmt.Errorf("error creating prow job: %v", err)
    76  			}
    77  		}
    78  	}
    79  	return nil
    80  }