-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
184 lines (161 loc) · 5.63 KB
/
image.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
// Copyright 2020 The Shipwright 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 services
import (
"context"
"fmt"
"sort"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
imgv1b1 "github.com/shipwright-io/image/infra/images/v1beta1"
imgclient "github.com/shipwright-io/image/infra/images/v1beta1/gen/clientset/versioned"
imginform "github.com/shipwright-io/image/infra/images/v1beta1/gen/informers/externalversions"
imglist "github.com/shipwright-io/image/infra/images/v1beta1/gen/listers/images/v1beta1"
)
// Image gather all actions related to image img objects.
type Image struct {
imgcli imgclient.Interface
imglis imglist.ImageLister
implis imglist.ImageImportLister
imginf imginform.SharedInformerFactory
syssvc *SysContext
}
// NewImage returns a handler for all image img related services. I have chosen to go with a lazy
// approach here, you can pass or omit (nil) any parameter, it is up to the caller to decide what
// is needed for each specific case.
func NewImage(
corinf informers.SharedInformerFactory,
imgcli imgclient.Interface,
imginf imginform.SharedInformerFactory,
) *Image {
var imglis imglist.ImageLister
var implis imglist.ImageImportLister
if imginf != nil {
imglis = imginf.Shipwright().V1beta1().Images().Lister()
implis = imginf.Shipwright().V1beta1().ImageImports().Lister()
}
return &Image{
imginf: imginf,
imgcli: imgcli,
imglis: imglis,
implis: implis,
syssvc: NewSysContext(corinf),
}
}
// RecentlyFinishedImports return all processed ImageImport objects that refer to provided Image
// and have not being yet marked as already consumed. They are returned in a sorted (from oldest
// to newest) slice.
func (t *Image) RecentlyFinishedImports(
ctx context.Context, img *imgv1b1.Image,
) ([]imgv1b1.ImageImport, error) {
imports, err := t.implis.ImageImports(img.Namespace).List(labels.Everything())
if err != nil {
return nil, fmt.Errorf("unable to list images: %w", err)
}
var sortme []imgv1b1.ImageImport
for _, imp := range imports {
if !imp.AlreadyImported() || !imp.OwnedByImage(img) || imp.FlaggedAsConsumed() {
continue
}
impptr := imp.DeepCopy()
sortme = append(sortme, *impptr)
}
sort.SliceStable(
sortme,
func(i, j int) bool {
first := sortme[i].Status.HashReference.ImportedAt.Time
second := sortme[j].Status.HashReference.ImportedAt.Time
return second.After(first)
},
)
sorted := sortme // :-X
return sorted, nil
}
// Sync manages image updates, assuring we have the image imported. Beware that we change Image
// in place before updating it on api server, i.e. use DeepCopy() before passing the image object
// in.
func (t *Image) Sync(ctx context.Context, img *imgv1b1.Image) error {
var err error
imports, err := t.RecentlyFinishedImports(ctx, img)
if err != nil {
return fmt.Errorf("unable to read image imports: %w", err)
}
img.PrependFinishedImports(imports)
if _, err = t.imgcli.ShipwrightV1beta1().Images(img.Namespace).UpdateStatus(
ctx, img, metav1.UpdateOptions{},
); err != nil {
return fmt.Errorf("error updating image: %w", err)
}
// Now that we have successfully saved the ImageImports inside the Image object we
// can flag them for deletion. We ignore any errors here, the flagging process will
// be retried during next Sync call.
for _, imp := range imports {
imp.FlagAsConsumed()
if _, err := t.imgcli.ShipwrightV1beta1().ImageImports(img.Namespace).Update(
ctx, &imp, metav1.UpdateOptions{},
); err != nil {
klog.V(5).Infof(
"unable to flag image import for deletion %s/%s: %s",
imp.Namespace,
imp.Name,
err,
)
}
}
return nil
}
// Get returns a Image object. Returned object is already a copy of the cached object and may be
// modified by caller as needed.
func (t *Image) Get(ctx context.Context, ns, name string) (*imgv1b1.Image, error) {
img, err := t.imglis.Images(ns).Get(name)
if err != nil {
return nil, fmt.Errorf("unable to get image: %w", err)
}
return img.DeepCopy(), nil
}
// Validate checks if provided Image contains all mandatory fields. At this stage we only verify
// if it contain the necessary fields.
func (t *Image) Validate(ctx context.Context, img *imgv1b1.Image) error {
return img.Validate()
}
// AddEventHandler adds a handler to Image related events.
func (t *Image) AddEventHandler(handler cache.ResourceEventHandler) {
t.imginf.Shipwright().V1beta1().Images().Informer().AddEventHandler(handler)
}
// NewImageOpts holds the options necessary to call Image.NewImage().
type NewImageOpts struct {
Namespace string
Name string
From string
Mirror bool
Insecure bool
}
// NewImage creates and saves a new Image object. Saves it to kubernetes api before returning.
func (t *Image) NewImage(ctx context.Context, o NewImageOpts) (*imgv1b1.Image, error) {
img := &imgv1b1.Image{
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
},
Spec: imgv1b1.ImageSpec{
From: o.From,
Mirror: o.Mirror,
Insecure: o.Insecure,
},
}
opts := metav1.CreateOptions{}
return t.imgcli.ShipwrightV1beta1().Images(o.Namespace).Create(ctx, img, opts)
}