github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/config/agent.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 config 18 19 import ( 20 "os" 21 "sync" 22 "time" 23 24 "github.com/sirupsen/logrus" 25 ) 26 27 // Agent watches a path and automatically loads the config stored 28 // therein. 29 type Agent struct { 30 sync.Mutex 31 c *Config 32 } 33 34 // Start will begin polling the config file at the path. If the first load 35 // fails, Start with return the error and abort. Future load failures will log 36 // the failure message but continue attempting to load. 37 func (ca *Agent) Start(prowConfig, jobConfig string) error { 38 c, err := Load(prowConfig, jobConfig) 39 if err != nil { 40 return err 41 } 42 ca.c = c 43 go func() { 44 var lastModTime time.Time 45 // Rarely, if two changes happen in the same second, mtime will 46 // be the same for the second change, and an mtime-based check would 47 // fail. Reload periodically just in case. 48 skips := 0 49 for range time.Tick(1 * time.Second) { 50 if skips < 600 { 51 // Check if the file changed to see if it needs to be re-read. 52 // os.Stat follows symbolic links, which is how ConfigMaps work. 53 prowStat, err := os.Stat(prowConfig) 54 if err != nil { 55 logrus.WithField("prowConfig", prowConfig).WithError(err).Error("Error loading prow config.") 56 continue 57 } 58 59 recentModTime := prowStat.ModTime() 60 61 // TODO(krzyzacy): allow empty jobConfig till fully migrate config to subdirs 62 if jobConfig != "" { 63 jobConfigStat, err := os.Stat(jobConfig) 64 if err != nil { 65 logrus.WithField("jobConfig", jobConfig).WithError(err).Error("Error loading job configs.") 66 continue 67 } 68 69 if jobConfigStat.ModTime().After(recentModTime) { 70 recentModTime = jobConfigStat.ModTime() 71 } 72 } 73 74 if !recentModTime.After(lastModTime) { 75 skips++ 76 continue // file hasn't been modified 77 } 78 lastModTime = recentModTime 79 } 80 if c, err := Load(prowConfig, jobConfig); err != nil { 81 logrus.WithField("prowConfig", prowConfig). 82 WithField("jobConfig", jobConfig). 83 WithError(err).Error("Error loading config.") 84 } else { 85 skips = 0 86 ca.Lock() 87 ca.c = c 88 ca.Unlock() 89 } 90 } 91 }() 92 return nil 93 } 94 95 // Config returns the latest config. Do not modify the config. 96 func (ca *Agent) Config() *Config { 97 ca.Lock() 98 defer ca.Unlock() 99 return ca.c 100 } 101 102 // Set sets the config. Useful for testing. 103 func (ca *Agent) Set(c *Config) { 104 ca.Lock() 105 defer ca.Unlock() 106 ca.c = c 107 }