Skip to content

Commit 8619847

Browse files
committed
fix: image status
1 parent a300d04 commit 8619847

File tree

9 files changed

+66
-25
lines changed

9 files changed

+66
-25
lines changed

.changelog/101.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:bug
2+
`image` - Now the image is displayed correctly the `last-result`.
3+
```
4+
5+
```release-note:enhancement
6+
`image/status` - Fix the documentation of the `image/status.
7+
```

api/v1alpha1/image_models.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type (
44
ImageStatusLastSync string
55
)
66

7-
var (
7+
const (
88
// Status of the image when it is last sync success.
99
ImageStatusLastSyncSuccess ImageStatusLastSync = "Success"
1010

cmd/kimup/event.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,23 @@ func refreshIfRequired(an annotations.Annotation, image v1alpha1.Image) {
6565
"namespace": image.Namespace,
6666
"name": image.Name,
6767
}).Info("Annotation trigger refresh")
68-
_, err := triggers.Trigger(triggers.RefreshImage, image.Namespace, image.Name)
69-
if err != nil {
70-
log.
71-
WithFields(logrus.Fields{
72-
"namespace": image.Namespace,
73-
"name": image.Name,
74-
}).
75-
Error("Error triggering event")
76-
}
68+
refresh(image)
7769
an.Remove(annotations.KeyAction)
7870
}
7971
}
8072

73+
func refresh(image v1alpha1.Image) {
74+
_, err := triggers.Trigger(triggers.RefreshImage, image.Namespace, image.Name)
75+
if err != nil {
76+
log.
77+
WithFields(logrus.Fields{
78+
"namespace": image.Namespace,
79+
"name": image.Name,
80+
}).
81+
Error("Error triggering event")
82+
}
83+
}
84+
8185
func setTagIfNotExists(ctx context.Context, kubeClient kubeclient.Interface, an annotations.Annotation, image *v1alpha1.Image) error {
8286
if an.Tag().IsNull() {
8387
a, err := actions.GetAction(actions.Apply)

cmd/kimup/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func main() {
119119
}
120120
}
121121

122+
refresh(event.Value)
123+
122124
// Remove the annotation annotations.AnnotationActionKey in the map[string]string
123125
an.Remove(annotations.KeyAction)
124126
}

cmd/kimup/scheduler.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,25 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
5757

5858
image, err := k.Image().Get(ctx, e.Data()["namespace"].(string), e.Data()["image"].(string))
5959
defer func() {
60+
// Add delay to avoid conflicts
61+
time.Sleep(500 * time.Millisecond)
62+
6063
// update the status of the image
6164
image.SetStatusTime(time.Now().Format(time.RFC3339))
62-
err := k.Image().UpdateStatus(ctx, image)
65+
66+
// Need to get image again to avoid conflicts
67+
imageRefreshed, err := k.Image().Get(ctx, e.Data()["namespace"].(string), e.Data()["image"].(string))
6368
if err != nil {
69+
log.WithError(err).
70+
WithFields(log.Fields{
71+
"Namespace": e.Data()["namespace"],
72+
"Image": e.Data()["image"],
73+
}).Error("Error getting image")
74+
return
75+
}
76+
imageRefreshed.Status = image.Status
77+
78+
if err := k.Image().UpdateStatus(ctx, imageRefreshed); err != nil {
6479
log.WithError(err).
6580
WithFields(log.Fields{
6681
"Namespace": e.Data()["namespace"],
@@ -77,6 +92,9 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
7792
}
7893
k.Image().Event(&image, corev1.EventTypeNormal, "Image update triggered", "")
7994

95+
// Set Status to Scheduled permit in the execution of the refresh if the image have a error or not
96+
image.SetStatusResult(v1alpha1.ImageStatusLastSyncScheduled)
97+
8098
var auths kubeclient.K8sDockerRegistrySecretData
8199

82100
if image.Spec.ImagePullSecrets != nil {
@@ -125,6 +143,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
125143
if err != nil {
126144
// Prometheus metrics - Increment the counter for the tags with error
127145
metrics.Tags().RequestErrorTotal.Inc()
146+
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorTags)
128147
k.Image().Event(&image, corev1.EventTypeWarning, "Fetch image tags", fmt.Sprintf("Error fetching tags: %v", err))
129148
log.WithError(err).Error("Error fetching tags")
130149
return err
@@ -173,8 +192,9 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
173192
if match {
174193
for _, action := range rule.Actions {
175194
a, err := actions.GetActionWithUntypedName(action.Type)
176-
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorAction)
177195
if err != nil {
196+
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorAction)
197+
k.Image().Event(&image, corev1.EventTypeWarning, "Get action", fmt.Sprintf("Error getting action %s: %v", action.Type, err))
178198
log.Errorf("Error getting action: %v", err)
179199
continue
180200
}
@@ -208,7 +228,9 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
208228
}
209229
}
210230

211-
image.SetStatusResult(v1alpha1.ImageStatusLastSyncSuccess)
231+
if image.Status.Result == v1alpha1.ImageStatusLastSyncScheduled {
232+
image.SetStatusResult(v1alpha1.ImageStatusLastSyncSuccess)
233+
}
212234
return k.Image().Update(ctx, image)
213235
})
214236

docs/crd/image.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ The following status can be set on an image:
7979

8080
| Last-Sync state | Description |
8181
| --------------- | ----------- |
82-
| `ImageStatusLastSyncErrorAction` | Status of the image when it is last sync error action. |
83-
| `ImageStatusLastSyncErrorGetImage` | Status of the image when it is last sync get error. |
84-
| `ImageStatusLastSyncErrorGetRule` | Status of the image when it is last sync error get rule. |
85-
| `ImageStatusLastSyncErrorPullSecrets` | Status of the image when it is last sync error secrets. |
86-
| `ImageStatusLastSyncErrorRegistry` | Status of the image when it is last sync error registry. |
87-
| `ImageStatusLastSyncErrorTags` | Status of the image when it is last sync error tags. |
88-
| `ImageStatusLastSyncError` | Status of the image when an error occurred. |
89-
| `ImageStatusLastSyncScheduled` | Status of the image when it is last sync is scheduled. |
90-
| `ImageStatusLastSyncSuccess` | Status of the image when it is last sync success. |
82+
| "ActionError" | Status of the image when it is last sync error action. |
83+
| "Error" | Status of the image when an error occurred. |
84+
| "GetImageError" | Status of the image when it is last sync get error. |
85+
| "GetRuleError" | Status of the image when it is last sync error get rule. |
86+
| "PullSecretsError" | Status of the image when it is last sync error secrets. |
87+
| "RegistryError" | Status of the image when it is last sync error registry. |
88+
| "Scheduled" | Status of the image when it is last sync is scheduled. |
89+
| "Success" | Status of the image when it is last sync success. |
90+
| "TagsError" | Status of the image when it is last sync error tags. |
9191

9292

internal/controller/image_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (r *ImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
9090
r.Recorder.Event(&image, "Warning", string(ImageUpdate), fmt.Sprintf("Failed to set checksum: %v", err))
9191
return ctrl.Result{}, err
9292
}
93+
9394
if err := r.Client.Update(ctx, &image); err != nil {
9495
xlog.WithError(err).Error("unable to update Image")
9596
r.Recorder.Event(&image, "Warning", string(ImageUpdate), fmt.Sprintf("Failed to update image: %v", err))

tools/doc/generate-doc-crd-image.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"go/ast"
65
"go/parser"
76
"go/token"
@@ -29,7 +28,13 @@ func generateDocImageCRD() {
2928
for _, spec := range decl.(*ast.GenDecl).Specs {
3029
if _, ok := spec.(*ast.ValueSpec); ok {
3130
for _, ident := range spec.(*ast.ValueSpec).Names {
32-
imgStatusSlice = append(imgStatusSlice, []string{fmt.Sprintf("`%s`", ident.Name), strings.TrimSuffix(ident.Obj.Decl.(*ast.ValueSpec).Doc.Text(), "\n")})
31+
// Get value of const
32+
if ident.Obj.Kind == ast.Con {
33+
value := ident.Obj.Decl.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
34+
description := ident.Obj.Decl.(*ast.ValueSpec).Doc.Text()
35+
36+
imgStatusSlice = append(imgStatusSlice, []string{strings.TrimSuffix(value, "\n"), strings.TrimSuffix(description, "\n")})
37+
}
3338
}
3439
}
3540
}

tools/env-dev/whoami-deployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ metadata:
3838
name: traefik-whoami
3939
namespace: dev-kube-image-updater
4040
spec:
41-
image: traefik/whoami
41+
image: whoami
4242
baseTag: v1.10.0
4343
triggers:
4444
- type: crontab

0 commit comments

Comments
 (0)