Skip to content

Commit 9a2fffa

Browse files
committed
fix: don't use generatedNames in RegistryMonitors as it still can produce duplicates specs
1 parent 5a27928 commit 9a2fffa

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

internal/controller/core/pod_controller.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,30 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
105105

106106
for registry := range registries {
107107
var registryMonitors kuikv1alpha1.RegistryMonitorList
108-
if err := r.List(ctx, &registryMonitors); err != nil {
108+
if err := r.List(ctx, &registryMonitors, client.MatchingFields{
109+
RegistryIndexKey: registry,
110+
}); err != nil {
109111
return ctrl.Result{}, err
110112
}
113+
if len(registryMonitors.Items) > 0 {
114+
continue
115+
}
111116

112-
found := false
113-
for _, registryMonitor := range registryMonitors.Items {
114-
if registryMonitor.Spec.Registry == registry {
115-
found = true
116-
break
117-
}
117+
registryMonitor := &kuikv1alpha1.RegistryMonitor{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Name: fmt.Sprintf("%016x", xxhash.Sum64String(registry)),
120+
},
121+
Spec: *r.defaultRegistryMonitorSpec.DeepCopy(),
118122
}
123+
registryMonitor.Spec.Registry = registry
119124

120-
if !found {
121-
log.Info("no registry monitor found for image, creating one with default values", "registry", registry)
122-
spec := r.defaultRegistryMonitorSpec.DeepCopy()
123-
spec.Registry = registry
124-
err := r.Create(ctx, &kuikv1alpha1.RegistryMonitor{
125-
ObjectMeta: metav1.ObjectMeta{
126-
GenerateName: fmt.Sprintf("%016x-", xxhash.Sum64String(spec.Registry)),
127-
},
128-
Spec: *spec,
129-
})
130-
if err != nil {
131-
return ctrl.Result{}, err
125+
if err := r.Create(ctx, registryMonitor); err != nil {
126+
if apierrors.IsAlreadyExists(err) {
127+
return ctrl.Result{}, nil
132128
}
129+
return ctrl.Result{}, err
130+
} else {
131+
log.Info("no registry monitor found for image, created one with default values", "registry", registry)
133132
}
134133
}
135134

0 commit comments

Comments
 (0)