-
Notifications
You must be signed in to change notification settings - Fork 55
/
configManager.go
175 lines (139 loc) · 4.84 KB
/
configManager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright The Ratify Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"context"
"os"
"time"
ef "github.com/deislabs/ratify/pkg/executor/core"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type GetExecutor func(context.Context) *ef.Executor
var (
configHash string
executor ef.Executor
)
// Create a executor from configurationFile and setup config file watcher
func GetExecutorAndWatchForUpdate(configFilePath string) (GetExecutor, error) {
configFilePath = getConfigurationFile(configFilePath)
cf, err := Load(configFilePath)
if err != nil {
return func(context.Context) *ef.Executor { return &ef.Executor{} }, err
}
configHash = cf.fileHash
stores, verifiers, policyEnforcer, err := CreateFromConfig(cf)
if err != nil {
return func(context.Context) *ef.Executor { return &ef.Executor{} }, err
}
executor = ef.Executor{
Verifiers: verifiers,
ReferrerStores: stores,
PolicyEnforcer: policyEnforcer,
Config: &cf.ExecutorConfig,
}
err = watchForConfigurationChange(configFilePath)
if err != nil {
return func(context.Context) *ef.Executor { return &ef.Executor{} }, err
}
logrus.Info("configuration successfully loaded.")
return func(context.Context) *ef.Executor { return &executor }, nil
}
func reloadExecutor(configFilePath string) {
cf, err := Load(configFilePath)
if err != nil {
logrus.Warnf("failed to load from config file , err: %v", err)
return
}
if configHash != cf.fileHash {
stores, verifiers, policyEnforcer, err := CreateFromConfig(cf)
newExecutor := ef.Executor{
Verifiers: verifiers,
ReferrerStores: stores,
PolicyEnforcer: policyEnforcer,
Config: &cf.ExecutorConfig,
}
if err != nil {
logrus.Warnf("failed to store/verifier/policy objects from config, no updates loaded. err: %v", err)
return
}
executor = newExecutor
configHash = cf.fileHash
logrus.Infof("configuration file has been updated, reloading executor succeeded")
} else {
logrus.Infof("no change found in config file, no executor update needed")
}
}
// Setup a watcher on file at configFilePath, reload executor on file change
func watchForConfigurationChange(configFilePath string) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "new file watcher on configuration file failed ")
}
if err = watcher.Add(configFilePath); err != nil {
logrus.Errorf("adding configuration file watcher failed, err: %v", err)
return err
}
logrus.Infof("watcher added on configuration file %v", configFilePath)
// setup for loop to listen for events
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
logrus.Warnf("no longer watching configuration file changes, file watcher event channel closed")
return
}
logrus.Infof("file watcher event detected %v", event)
// In a cluster scenario, a configMap will recreate the config file
// after the remove event, the watcher will also be removed
// since a watcher on a non existent file is not supported, we sleep until the file exist add the watcher back
if event.Name == configFilePath && event.Op&fsnotify.Remove == fsnotify.Remove {
logrus.Infof("config file remove event detected")
sleepTime := 1 * time.Second
waitTime := 60 //1min
time.Sleep(sleepTime)
_, err := os.Stat(configFilePath)
for err != nil {
if waitTime < 0 {
logrus.Warnf("config file not found after waiting for %v sec, os.Stat error %v", waitTime, err)
return
}
logrus.Infof("config file does not exist yet, sleeping again")
_, err = os.Stat(configFilePath)
time.Sleep(sleepTime)
waitTime--
}
reloadExecutor(configFilePath)
err = watcher.Add(configFilePath)
if err != nil {
logrus.Errorf("adding configuration file watcher failed, err: %v", err)
continue
}
logrus.Infof("watcher added on configuration directory %v", configFilePath)
}
// In a local scenario, the configuration will be updated through a write event
if event.Name == configFilePath && event.Op&fsnotify.Write == fsnotify.Write {
reloadExecutor(configFilePath)
}
case err, ok := <-watcher.Errors:
if !ok {
logrus.Errorf("configuration file watcher returned error : %v, watcher will be closed.", err)
return
}
}
}
}()
return nil
}