/
repocontroller.go
332 lines (280 loc) · 8.8 KB
/
repocontroller.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gitops
import (
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/go-git/go-git/v5"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/jlewi/hydros/api/v1alpha1"
"github.com/jlewi/hydros/pkg/files"
"github.com/jlewi/hydros/pkg/github"
"github.com/jlewi/hydros/pkg/images"
"github.com/jlewi/hydros/pkg/util"
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/net/context"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// RepoController is a controller for a repo.
// The controller will periodically checkout the repository and search it for resources.
// It will then sync those resources
type RepoController struct {
// directory where repositories should be checked out
workDir string
config *v1alpha1.RepoConfig
cloner *github.ReposCloner
imageController *images.Controller
gitRepo *git.Repository
manager *github.TransportManager
selectors []labels.Selector
}
func NewRepoController(config *v1alpha1.RepoConfig, workDir string) (*RepoController, error) {
log := zapr.NewLogger(zap.L())
if config == nil {
return nil, errors.New("config must be non nil")
}
if errs, ok := config.IsValid(); !ok {
return nil, errors.New(errs)
}
privateKey, err := files.Read(config.Spec.GitHubAppConfig.PrivateKey)
if err != nil {
return nil, errors.Wrapf(err, "Could not read private key %v", config.Spec.GitHubAppConfig.PrivateKey)
}
manager, err := github.NewTransportManager(config.Spec.GitHubAppConfig.AppID, privateKey, log)
if err != nil {
return nil, err
}
imageController, err := images.NewController()
if err != nil {
return nil, errors.Wrap(err, "Failed to create image controller")
}
cloner := &github.ReposCloner{
URIs: []string{config.Spec.Repo},
Manager: manager,
BaseDir: workDir,
}
selectors := make([]labels.Selector, 0, len(config.Spec.Selectors))
for _, s := range config.Spec.Selectors {
k8sS, err := s.ToK8s()
if err != nil {
return nil, errors.Wrapf(err, "Error converting selector; %v", util.PrettyString(s))
}
k8sSelector, err := meta.LabelSelectorAsSelector(k8sS)
if err != nil {
return nil, errors.Wrapf(err, "Failed to convert selector to k8s selector; %v", util.PrettyString(s))
}
selectors = append(selectors, k8sSelector)
}
return &RepoController{
workDir: workDir,
config: config,
cloner: cloner,
imageController: imageController,
manager: manager,
selectors: selectors,
}, nil
}
func (c *RepoController) Reconcile(ctx context.Context) error {
log := util.LogFromContext(ctx)
log = log.WithValues("repoConfig", c.config.Metadata.Name)
ctx = logr.NewContext(ctx, log)
if err := c.cloner.Run(ctx); err != nil {
return err
}
repoDir, err := c.cloner.GetRepoDir(c.config.Spec.Repo)
if err != nil {
return err
}
c.gitRepo, err = git.PlainOpenWithOptions(repoDir, &git.PlainOpenOptions{})
if err != nil {
return errors.Wrapf(err, "Error opening git repo")
}
resources, err := c.findResources(ctx)
if err != nil {
return err
}
// Update the image controller with the current repo
if err := c.imageController.SetLocalRepos([]images.GitRepoRef{
{
Repo: c.gitRepo,
},
}); err != nil {
return err
}
// Apply all the resources in parallel
// https://github.com/jlewi/hydros/issues/60 is tracking properly ordering dependencies.
var wg sync.WaitGroup
for _, r := range resources {
wg.Add(1)
go func(rNode *resource) {
if err := c.applyResource(ctx, rNode); err != nil {
log.Error(err, "Error applying resource", "path", rNode.path, "name", rNode.node.GetName())
}
wg.Done()
}(r)
}
wg.Wait()
return nil
}
func (c *RepoController) RunPeriodically(ctx context.Context, period time.Duration) error {
log := util.LogFromContext(ctx)
log = log.WithValues("repoConfig", c.config.Metadata.Name)
ctx = logr.NewContext(ctx, log)
for {
err := c.Reconcile(ctx)
if err != nil {
log.Error(err, "Error reconciling")
}
if period == 0 {
return err
}
log.Info("Sleeping", "period", period)
time.Sleep(period)
}
return nil
}
func (c *RepoController) findResources(ctx context.Context) ([]*resource, error) {
log := util.LogFromContext(ctx)
repoDir, err := c.cloner.GetRepoDir(c.config.Spec.Repo)
if err != nil {
return nil, err
}
yamlFiles := make([]string, 0, 10)
// Match globs matches all the globs
for _, glob := range c.config.Spec.Globs {
dirFs := os.DirFS(repoDir)
matches, err := doublestar.Glob(dirFs, glob)
if err != nil {
log.Error(err, "Error matching glob", "glob", glob)
continue
}
for _, m := range matches {
yamlFiles = append(yamlFiles, m)
}
}
resources := make([]*resource, 0, len(yamlFiles))
allowedKinds := map[string]bool{
v1alpha1.ImageGVK.Kind: true,
v1alpha1.ManifestSyncGVK.Kind: true,
}
for _, yamlFile := range yamlFiles {
log.V(util.Debug).Info("Reading YAML file", "yamlFile", yamlFile)
fullpath := filepath.Join(repoDir, yamlFile)
nodes, err := util.ReadYaml(fullpath)
if err != nil {
log.Error(err, "Error reading YAML file", "yamlFile", fullpath)
continue
}
seen := map[string]bool{}
for _, node := range nodes {
s := schema.FromAPIVersionAndKind(node.GetApiVersion(), node.GetKind())
if s.Group != v1alpha1.Group {
log.V(util.Debug).Info("Skipping resource with non hydros group", "group", s.Group)
continue
}
if !allowedKinds[s.Kind] {
log.V(util.Debug).Info("Skipping resource with kind", "kind", s.Kind)
continue
}
// Check it matches a selector
isMatch := false
labelsMap := labels.Set(node.GetLabels())
for _, s := range c.selectors {
if s.Matches(labelsMap) {
isMatch = true
break
}
}
if !isMatch {
log.V(util.Debug).Info("Skipping resource because it doesn't match any selectors", "kind", s.Kind, "name", node.GetName(), "path", fullpath, "labels", labelsMap)
continue
}
// Ensure the resource has a name that is unique at least within the file.
if seen[node.GetName()] {
err := errors.New("Duplicate resource")
log.Error(err, "Skipping duplicate resource. Each resource in the file should be uniquely named", "kind", s.Kind, "name", node.GetName(), "path", fullpath)
continue
}
seen[node.GetName()] = true
log.Info("Adding resource", "kind", s.Kind, "name", node.GetName(), "path", fullpath)
resources = append(resources, &resource{
node: node,
path: fullpath,
rPath: yamlFile,
})
}
}
return resources, nil
}
func (c *RepoController) applyResource(ctx context.Context, r *resource) error {
log := util.LogFromContext(ctx)
log = log.WithValues("path", r.path, "name", r.node.GetName())
ctx = logr.NewContext(ctx, log)
switch r.node.GetKind() {
case v1alpha1.ImageGVK.Kind:
return c.applyImage(ctx, r)
case v1alpha1.ManifestSyncGVK.Kind:
return c.applyManifest(ctx, r)
default:
return errors.Errorf("Unknown kind %v", r.node.GetKind())
}
return nil
}
func (c *RepoController) applyImage(ctx context.Context, r *resource) error {
image := &v1alpha1.Image{}
if err := r.node.YNode().Decode(image); err != nil {
return errors.Wrapf(err, "Error decoding image")
}
headRef, err := c.gitRepo.Head()
if err != nil {
return errors.Wrapf(err, "Error getting head ref")
}
image.Status.SourceCommit += headRef.Hash().String()
return c.imageController.Reconcile(ctx, image)
}
func (c *RepoController) applyManifest(ctx context.Context, r *resource) error {
log := util.LogFromContext(ctx)
log = log.WithValues("path", r.path, "name", r.node.GetName())
manifest := &v1alpha1.ManifestSync{}
if err := r.node.YNode().Decode(manifest); err != nil {
return errors.Wrapf(err, "Error decoding manifest")
}
// Rewrite the source repo if necessary
if err := rewriteRepos(ctx, manifest, c.config.Spec.RepoMappings); err != nil {
return err
}
pause := c.config.Spec.Pause
if pause != "" {
pauseDur, err := time.ParseDuration(pause)
if err != nil {
return errors.Wrapf(err, "Error parsing pause duration %v", pause)
}
if err := SetTakeOverAnnotations(manifest, pauseDur); err != nil {
return errors.Wrapf(err, "Failed to set takeover annotations")
}
log.Info("Pausing automatic syncs; doing a takeover")
}
// Create a workDir for this syncer
// Each ManifestSync should get its own workDir
// This should be stable names so that they get reused on each sync
dirname := strings.Replace(r.rPath, "/", "_", -1) + "_" + r.node.GetName()
workDir := filepath.Join(c.workDir, dirname)
syncer, err := NewSyncer(manifest, c.manager, SyncWithWorkDir(workDir), SyncWithLogger(log))
if err != nil {
log.Error(err, "Failed to create syncer")
return err
}
return syncer.RunOnce(false)
}
type resource struct {
node *yaml.RNode
path string
rPath string
}