/
imagerepository_controller.go
139 lines (117 loc) · 4.34 KB
/
imagerepository_controller.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
/*
Copyright 2020 Michael Bridgen <mikeb@squaremobius.net>
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 controllers
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
imagev1alpha1 "github.com/squaremo/image-reflector-controller/api/v1alpha1"
)
const (
scanTimeout = 10 * time.Second
defaultScanInterval = 10 * time.Minute
)
type DatabaseWriter interface {
SetTags(repo string, tags []string)
}
// ImageRepositoryReconciler reconciles a ImageRepository object
type ImageRepositoryReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Database interface {
DatabaseWriter
DatabaseReader
}
}
// +kubebuilder:rbac:groups=image.toolkit.fluxcd.io,resources=imagerepositories,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=image.toolkit.fluxcd.io,resources=imagerepositories/status,verbs=get;update;patch
func (r *ImageRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("imagerepository", req.NamespacedName)
var imageRepo imagev1alpha1.ImageRepository
if err := r.Get(ctx, req.NamespacedName, &imageRepo); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
ref, err := name.ParseReference(imageRepo.Spec.Image)
if err != nil {
imageRepo.Status.LastError = err.Error()
if err := r.Status().Update(ctx, &imageRepo); err != nil {
return ctrl.Result{}, err
}
log.Error(err, "Unable to parse image name", "imageName", imageRepo.Spec.Image)
return ctrl.Result{}, nil
}
canonicalName := ref.Context().String()
imageRepo.Status.CanonicalImageName = canonicalName
now := time.Now()
ok, when := r.shouldScan(&imageRepo, now)
if ok {
ctx, cancel := context.WithTimeout(ctx, scanTimeout)
defer cancel()
tags, err := remote.ListWithContext(ctx, ref.Context()) // TODO: auth
if err != nil {
imageRepo.Status.LastError = err.Error()
if err = r.Status().Update(ctx, &imageRepo); err != nil {
return ctrl.Result{}, err
}
}
imageRepo.Status.LastScanTime = &metav1.Time{Time: now}
imageRepo.Status.LastScanResult.TagCount = len(tags)
imageRepo.Status.LastError = ""
// share the information in the database
r.Database.SetTags(canonicalName, tags)
log.Info("successful scan", "tag count", len(tags))
if err = r.Status().Update(ctx, &imageRepo); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{RequeueAfter: when}, nil
}
// shouldScan takes an image repo and the time now, and says whether
// the repository should be scanned now, and how long to wait for the
// next scan.
func (r *ImageRepositoryReconciler) shouldScan(repo *imagev1alpha1.ImageRepository, now time.Time) (bool, time.Duration) {
scanInterval := defaultScanInterval
if repo.Spec.ScanInterval != nil {
scanInterval = repo.Spec.ScanInterval.Duration
}
if repo.Status.LastScanTime == nil {
return true, scanInterval
}
// when recovering, it's possible that the resource has a last
// scan time, but there's no records because the database has been
// dropped and created again.
// FIXME If the repo exists, has been
// scanned, and doesn't have any tags, this will mean a scan every
// time the resource comes up for reconciliation.
if tags := r.Database.Tags(repo.Status.CanonicalImageName); len(tags) == 0 {
return true, scanInterval
}
when := scanInterval - now.Sub(repo.Status.LastScanTime.Time)
if when < time.Second {
return true, scanInterval
}
return false, when
}
func (r *ImageRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&imagev1alpha1.ImageRepository{}).
Complete(r)
}