mirror of
https://github.com/lukaszraczylo/kubernetes-images-sync-operator.git
synced 2026-07-08 03:54:26 +00:00
Resolve race condition for images names being mixed up.
This commit is contained in:
+15
-3
@@ -63,16 +63,18 @@ func ProcessContainerName(containerName string) (Container, error) {
|
|||||||
return cnt, nil
|
return cnt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cnt := Container{}
|
cnt := Container{FullName: containerName}
|
||||||
parts := strings.Split(containerName, "@")
|
parts := strings.Split(containerName, "@")
|
||||||
if len(parts) > 2 {
|
if len(parts) > 2 {
|
||||||
return cnt, fmt.Errorf("invalid container name format: %s", containerName)
|
return cnt, fmt.Errorf("invalid container name format: %s", containerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
imageAndTag := strings.SplitN(parts[0], ":", 2)
|
imageAndTag := strings.SplitN(parts[0], ":", 2)
|
||||||
cnt.Image = imageAndTag[0]
|
cnt.Image = imageAndTag[0]
|
||||||
if len(imageAndTag) == 2 {
|
if len(imageAndTag) == 2 {
|
||||||
cnt.Tag = imageAndTag[1]
|
cnt.Tag = imageAndTag[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
shaParts := strings.SplitN(parts[1], ":", 2)
|
shaParts := strings.SplitN(parts[1], ":", 2)
|
||||||
if len(shaParts) != 2 || (shaParts[0] != "sha" && shaParts[0] != "sha256") {
|
if len(shaParts) != 2 || (shaParts[0] != "sha" && shaParts[0] != "sha256") {
|
||||||
@@ -80,10 +82,11 @@ func ProcessContainerName(containerName string) (Container, error) {
|
|||||||
}
|
}
|
||||||
cnt.Sha = parts[1]
|
cnt.Sha = parts[1]
|
||||||
}
|
}
|
||||||
cnt.FullName = containerName
|
|
||||||
if cnt.Sha == "" && cnt.Tag == "" {
|
if cnt.Sha == "" && cnt.Tag == "" {
|
||||||
cnt.Tag = "latest"
|
cnt.Tag = "latest"
|
||||||
}
|
}
|
||||||
|
|
||||||
if cnt.Image == "" {
|
if cnt.Image == "" {
|
||||||
return cnt, fmt.Errorf("image name is required")
|
return cnt, fmt.Errorf("image name is required")
|
||||||
}
|
}
|
||||||
@@ -137,17 +140,26 @@ func ListAndProcessResources[T K8sResource, L client.ObjectList](ctx context.Con
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
semaphore := make(chan struct{}, 10) // Limit concurrent goroutines
|
semaphore := make(chan struct{}, 10) // Limit concurrent goroutines
|
||||||
|
var mu sync.Mutex // Mutex to protect containersList
|
||||||
|
|
||||||
processItem := func(item K8sResource, namespace string) {
|
processItem := func(item K8sResource, namespace string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
semaphore <- struct{}{}
|
semaphore <- struct{}{}
|
||||||
defer func() { <-semaphore }()
|
defer func() { <-semaphore }()
|
||||||
if err := processContainers(ctx, item, namespace, containersList); err != nil {
|
|
||||||
|
localContainersList := &ContainersList{}
|
||||||
|
if err := processContainers(ctx, item, namespace, localContainersList); err != nil {
|
||||||
select {
|
select {
|
||||||
case errChan <- err:
|
case errChan <- err:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Safely append the local results to the main containersList
|
||||||
|
mu.Lock()
|
||||||
|
containersList.Containers = append(containersList.Containers, localContainersList.Containers...)
|
||||||
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch typedList := any(list).(type) {
|
switch typedList := any(list).(type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user