Skip to content

Commit 70e8756

Browse files
committed
feat: add timeout to monitoring tasks (active and passive)
1 parent 1c71b82 commit 70e8756

File tree

13 files changed

+87
-22
lines changed

13 files changed

+87
-22
lines changed

api/kuik/v1alpha1/imagemonitor_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"strings"
9+
"time"
910

1011
"github.com/cespare/xxhash"
1112
"github.com/enix/kube-image-keeper/internal/registry"
@@ -132,7 +133,7 @@ func (i *ImageMonitor) GetPullSecrets(ctx context.Context, c client.Client) (sec
132133
return image.GetPullSecrets(ctx, c)
133134
}
134135

135-
func (i *ImageMonitor) Monitor(ctx context.Context, k8sClient client.Client, httpMethod string) error {
136+
func (i *ImageMonitor) Monitor(ctx context.Context, k8sClient client.Client, httpMethod string, timeout time.Duration) error {
136137
patch := client.MergeFrom(i.DeepCopy())
137138
i.Status.Upstream.LastMonitor = metav1.Now()
138139
if err := k8sClient.Status().Patch(ctx, i, patch); err != nil {
@@ -141,9 +142,10 @@ func (i *ImageMonitor) Monitor(ctx context.Context, k8sClient client.Client, htt
141142

142143
patch = client.MergeFrom(i.DeepCopy())
143144
pullSecrets, pullSecretsErr := i.GetPullSecrets(ctx, k8sClient)
145+
client := registry.NewClient(nil, nil).WithPullSecrets(pullSecrets)
144146

145147
var lastErr error
146-
if desc, err := registry.ReadDescriptor(httpMethod, i.Reference(), pullSecrets, nil, nil); err != nil {
148+
if desc, err := client.ReadDescriptor(httpMethod, i.Reference(), timeout); err != nil {
147149
i.Status.Upstream.LastError = err.Error()
148150
lastErr = err
149151
var te *transport.Error

api/kuik/v1alpha1/registrymonitor_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type RegistryMonitorSpec struct {
2424
// +kubebuilder:validation:Enum=HEAD;GET
2525
// +default:value="HEAD"
2626
Method string `json:"method"`
27+
// Timeout is the maximum duration of a monitoring task
28+
// +default:value="30s"
29+
Timeout metav1.Duration `json:"timeout"`
2730
}
2831

2932
// RegistryMonitorStatus defines the observed state of RegistryMonitor.

api/kuik/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/kuik.enix.io_registrymonitors.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@ spec:
8686
description: Registry is the registry to monitor for image updates,
8787
it filters local image to check upstream
8888
type: string
89+
timeout:
90+
default: 30s
91+
description: Timeout is the maximum duration of a monitoring task
92+
type: string
8993
required:
9094
- interval
9195
- maxPerInterval
9296
- method
9397
- parallel
9498
- registry
99+
- timeout
95100
type: object
96101
status:
97102
description: RegistryMonitorStatus defines the observed state of RegistryMonitor.

helm/kube-image-keeper/crds/kuik.enix.io_registrymonitors.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@ spec:
8585
description: Registry is the registry to monitor for image updates,
8686
it filters local image to check upstream
8787
type: string
88+
timeout:
89+
default: 30s
90+
description: Timeout is the maximum duration of a monitoring task
91+
type: string
8892
required:
8993
- interval
9094
- maxPerInterval
9195
- method
9296
- parallel
9397
- registry
98+
- timeout
9499
type: object
95100
status:
96101
description: RegistryMonitorStatus defines the observed state of RegistryMonitor.

helm/kube-image-keeper/templates/mutatingwebhookconfiguration.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- if gt (len .Values.configuration.routing.strategies) 0 }}
12
apiVersion: admissionregistration.k8s.io/v1
23
kind: MutatingWebhookConfiguration
34
metadata:
@@ -34,4 +35,4 @@ webhooks:
3435
resources:
3536
- pods
3637
sideEffects: None
37-
38+
{{- end -}}

helm/kube-image-keeper/values.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ registryMonitors:
103103
parallel: 1
104104
# -- HTTP method to use to monitor an image of this registry
105105
method: HEAD
106+
# -- Maximum duration of a monitoring task
107+
timeout: 30s
106108
# -- RegistryMonitors to create on install and upgrade, if name is not provided, defaults to registry value.
107109
items:
108110
docker.io:
@@ -139,7 +141,9 @@ configuration:
139141
monitoring:
140142
enabled: true
141143
routing:
142-
activeCheck: false
144+
activeCheck:
145+
enabled: true
146+
timeout: 1s
143147
strategies: []
144148
# - paths:
145149
# - enix/x509-certificate-exporter

internal/config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func load(provider koanf.Provider, parser koanf.Parser) (*Config, error) {
3636

3737
k.UnmarshalWithConf("", config, koanf.UnmarshalConf{
3838
DecoderConfig: &mapstructure.DecoderConfig{
39-
DecodeHook: mapstructure.ComposeDecodeHookFunc(stringToRegexp),
39+
DecodeHook: mapstructure.ComposeDecodeHookFunc(
40+
mapstructure.StringToTimeDurationHookFunc(),
41+
stringToRegexp,
42+
),
4043
},
4144
})
4245

internal/controller/core/pod_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
155155
MaxPerInterval: 1,
156156
Parallel: 1,
157157
Method: http.MethodHead,
158+
Timeout: metav1.Duration{Duration: 5 * time.Second},
158159
}
159160

160161
// TODO: read this from config instead

internal/controller/kuik/registrymonitor_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (r *RegistryMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
174174
logImageMonitor.Info("monitoring image")
175175
// TODO: add to SetupWithManager Watches(&source.Channel{Source: eventChannel}, &handler.EnqueueRequestForObject{})
176176
// push an event in the channel when this function is done
177-
if err := imageMonitor.Monitor(logf.IntoContext(ctx, logImageMonitor), r.Client, registryMonitor.Spec.Method); err != nil {
177+
if err := imageMonitor.Monitor(logf.IntoContext(ctx, logImageMonitor), r.Client, registryMonitor.Spec.Method, registryMonitor.Spec.Timeout.Duration); err != nil {
178178
logImageMonitor.Info("failed to monitor image", "error", err.Error())
179179
}
180180
logImageMonitor.Info("image monitored with success")

0 commit comments

Comments
 (0)