mirror of
https://github.com/lukaszraczylo/kubernetes-images-sync-operator.git
synced 2026-07-11 04:11:44 +00:00
Add ability to process additional images
This commit is contained in:
@@ -49,6 +49,9 @@ spec:
|
|||||||
# excludedNamespaces:
|
# excludedNamespaces:
|
||||||
# - my-awesome-namespace
|
# - my-awesome-namespace
|
||||||
|
|
||||||
|
additionalImages:
|
||||||
|
- minio/minio:RELEASE.2024-09-09T16-59-28Z
|
||||||
|
|
||||||
basePath: /images # base path in the target directory
|
basePath: /images # base path in the target directory
|
||||||
storage:
|
storage:
|
||||||
target: S3 # file backup is not ready yet
|
target: S3 # file backup is not ready yet
|
||||||
|
|||||||
@@ -69,7 +69,8 @@ type ClusterImageExportSpec struct {
|
|||||||
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
|
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
|
||||||
// +kubebuilder:validation.Minimum=1
|
// +kubebuilder:validation.Minimum=1
|
||||||
// +kubebuilder:validation.Maximum=100
|
// +kubebuilder:validation.Maximum=100
|
||||||
MaxConcurrentJobs int `json:"maxConcurrentJobs"`
|
MaxConcurrentJobs int `json:"maxConcurrentJobs"`
|
||||||
|
AdditionalImages []string `json:"additionalImages,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClusterImageExportStatus defines the observed state of ClusterImageExport
|
// ClusterImageExportStatus defines the observed state of ClusterImageExport
|
||||||
|
|||||||
+2
-2
@@ -10,9 +10,9 @@ description: |
|
|||||||
|
|
||||||
type: application
|
type: application
|
||||||
|
|
||||||
version: 0.1.27
|
version: 0.1.28
|
||||||
|
|
||||||
appVersion: "0.1.27"
|
appVersion: "0.1.28"
|
||||||
|
|
||||||
home: https://github.com/lukaszraczylo/kubernetes-images-sync-operator
|
home: https://github.com/lukaszraczylo/kubernetes-images-sync-operator
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ sa:
|
|||||||
- ALL
|
- ALL
|
||||||
image:
|
image:
|
||||||
repository: ghcr.io/lukaszraczylo/kubernetes-images-sync-operator
|
repository: ghcr.io/lukaszraczylo/kubernetes-images-sync-operator
|
||||||
tag: 0.1.27
|
tag: 0.1.28
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 500m
|
cpu: 500m
|
||||||
|
|||||||
@@ -53,6 +53,10 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
description: ClusterImageExportSpec defines the desired state of ClusterImageExport
|
description: ClusterImageExportSpec defines the desired state of ClusterImageExport
|
||||||
properties:
|
properties:
|
||||||
|
additionalImages:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
basePath:
|
basePath:
|
||||||
description: Base path for the export - both file and S3
|
description: Base path for the export - both file and S3
|
||||||
maxLength: 255
|
maxLength: 255
|
||||||
|
|||||||
@@ -87,6 +87,17 @@ func (r *ClusterImageExportReconciler) Reconcile(ctx context.Context, req ctrl.R
|
|||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(clusterImageExport.Spec.AdditionalImages) > 0 {
|
||||||
|
for _, image := range clusterImageExport.Spec.AdditionalImages {
|
||||||
|
img, err := shared.ProcessContainerName(image)
|
||||||
|
if err != nil {
|
||||||
|
l.Error(err, "unable to process additional image", "image", image)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fullImagesList.Containers = append(fullImagesList.Containers, img)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clusterImageExport.Status.Progress = shared.STATUS_RUNNING
|
clusterImageExport.Status.Progress = shared.STATUS_RUNNING
|
||||||
if err := r.Status().Update(ctx, clusterImageExport); err != nil {
|
if err := r.Status().Update(ctx, clusterImageExport); err != nil {
|
||||||
l.Error(err, "unable to update ClusterImageExport status to RUNNING")
|
l.Error(err, "unable to update ClusterImageExport status to RUNNING")
|
||||||
|
|||||||
+106
-38
@@ -4,11 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
raczylocomv1 "github.com/lukaszraczylo/kubernetes-images-sync-operator/api/raczylo.com/v1"
|
raczylocomv1 "github.com/lukaszraczylo/kubernetes-images-sync-operator/api/raczylo.com/v1"
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
batchv1 "k8s.io/api/batch/v1"
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||||
)
|
)
|
||||||
@@ -18,10 +21,12 @@ type K8sResource interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wrapper types
|
// Wrapper types
|
||||||
type DeploymentWrapper appsv1.Deployment
|
type (
|
||||||
type JobWrapper batchv1.Job
|
DeploymentWrapper appsv1.Deployment
|
||||||
type DaemonSetWrapper appsv1.DaemonSet
|
JobWrapper batchv1.Job
|
||||||
type CronJobWrapper batchv1.CronJob
|
DaemonSetWrapper appsv1.DaemonSet
|
||||||
|
CronJobWrapper batchv1.CronJob
|
||||||
|
)
|
||||||
|
|
||||||
// Implement the K8sResource interface for wrapper types
|
// Implement the K8sResource interface for wrapper types
|
||||||
func (d *DeploymentWrapper) GetPodSpec() *corev1.PodSpec { return &d.Spec.Template.Spec }
|
func (d *DeploymentWrapper) GetPodSpec() *corev1.PodSpec { return &d.Spec.Template.Spec }
|
||||||
@@ -31,17 +36,40 @@ func (cj *CronJobWrapper) GetPodSpec() *corev1.PodSpec {
|
|||||||
return &cj.Spec.JobTemplate.Spec.Template.Spec
|
return &cj.Spec.JobTemplate.Spec.Template.Spec
|
||||||
}
|
}
|
||||||
|
|
||||||
func processContainerName(containerName string) (Container, error) {
|
type ContainerCache struct {
|
||||||
|
sync.RWMutex
|
||||||
|
cache map[string]Container
|
||||||
|
}
|
||||||
|
|
||||||
|
var containerCache = &ContainerCache{
|
||||||
|
cache: make(map[string]Container),
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *ContainerCache) Get(key string) (Container, bool) {
|
||||||
|
cc.RLock()
|
||||||
|
defer cc.RUnlock()
|
||||||
|
c, ok := cc.cache[key]
|
||||||
|
return c, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *ContainerCache) Set(key string, value Container) {
|
||||||
|
cc.Lock()
|
||||||
|
defer cc.Unlock()
|
||||||
|
cc.cache[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProcessContainerName(containerName string) (Container, error) {
|
||||||
|
if cnt, ok := containerCache.Get(containerName); ok {
|
||||||
|
return cnt, nil
|
||||||
|
}
|
||||||
|
|
||||||
cnt := Container{}
|
cnt := Container{}
|
||||||
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.Split(parts[0], ":")
|
imageAndTag := strings.SplitN(parts[0], ":", 2)
|
||||||
cnt.Image = imageAndTag[0]
|
cnt.Image = imageAndTag[0]
|
||||||
if len(imageAndTag) > 2 {
|
|
||||||
return cnt, fmt.Errorf("invalid image:tag format: %s", parts[0])
|
|
||||||
}
|
|
||||||
if len(imageAndTag) == 2 {
|
if len(imageAndTag) == 2 {
|
||||||
cnt.Tag = imageAndTag[1]
|
cnt.Tag = imageAndTag[1]
|
||||||
}
|
}
|
||||||
@@ -53,18 +81,18 @@ func processContainerName(containerName string) (Container, error) {
|
|||||||
cnt.Sha = parts[1]
|
cnt.Sha = parts[1]
|
||||||
}
|
}
|
||||||
cnt.FullName = containerName
|
cnt.FullName = containerName
|
||||||
// if tag is empty and sha is empty - use tag 'latest'
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
containerCache.Set(containerName, cnt)
|
||||||
return cnt, nil
|
return cnt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processContainers[T K8sResource](resource T, namespace string, containersList *ContainersList) error {
|
func processContainers(ctx context.Context, resource K8sResource, namespace string, containersList *ContainersList) error {
|
||||||
podSpec := resource.GetPodSpec()
|
podSpec := resource.GetPodSpec()
|
||||||
if podSpec == nil {
|
if podSpec == nil {
|
||||||
return fmt.Errorf("nil PodSpec")
|
return fmt.Errorf("nil PodSpec")
|
||||||
@@ -72,13 +100,13 @@ func processContainers[T K8sResource](resource T, namespace string, containersLi
|
|||||||
|
|
||||||
allContainers := append(podSpec.Containers, podSpec.InitContainers...)
|
allContainers := append(podSpec.Containers, podSpec.InitContainers...)
|
||||||
for _, container := range allContainers {
|
for _, container := range allContainers {
|
||||||
if err := processContainer(container.Image, namespace, containersList); err != nil {
|
if err := processContainer(ctx, container.Image, namespace, containersList); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range podSpec.EphemeralContainers {
|
for _, container := range podSpec.EphemeralContainers {
|
||||||
if err := processContainer(container.EphemeralContainerCommon.Image, namespace, containersList); err != nil {
|
if err := processContainer(ctx, container.EphemeralContainerCommon.Image, namespace, containersList); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,58 +114,98 @@ func processContainers[T K8sResource](resource T, namespace string, containersLi
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// processContainer handles the processing of a single container image
|
func processContainer(ctx context.Context, image string, containerNamespace string, containersList *ContainersList) error {
|
||||||
func processContainer(image string, containerNamespace string, containersList *ContainersList) error {
|
select {
|
||||||
cnt, err := processContainerName(image)
|
case <-ctx.Done():
|
||||||
if err != nil {
|
return ctx.Err()
|
||||||
return fmt.Errorf("failed to process container name: %s - %w", image, err)
|
default:
|
||||||
|
cnt, err := ProcessContainerName(image)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to process container name: %s - %w", image, err)
|
||||||
|
}
|
||||||
|
cnt.ImageNamespace = containerNamespace
|
||||||
|
containersList.Containers = append(containersList.Containers, cnt)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
cnt.ImageNamespace = containerNamespace
|
|
||||||
containersList.Containers = append(containersList.Containers, cnt)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// listAndProcessResources is a generic function to list and process K8s resources
|
|
||||||
func ListAndProcessResources[T K8sResource, L client.ObjectList](ctx context.Context, r client.Client, list L, containersList *ContainersList) error {
|
func ListAndProcessResources[T K8sResource, L client.ObjectList](ctx context.Context, r client.Client, list L, containersList *ContainersList) error {
|
||||||
if err := r.List(ctx, list, &client.ListOptions{}); err != nil {
|
if err := r.List(ctx, list, &client.ListOptions{}); err != nil {
|
||||||
return fmt.Errorf("failed to list resources: %w", err)
|
return fmt.Errorf("failed to list resources: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
errChan := make(chan error, 1)
|
||||||
|
semaphore := make(chan struct{}, 10) // Limit concurrent goroutines
|
||||||
|
|
||||||
|
processItem := func(item K8sResource, namespace string) {
|
||||||
|
defer wg.Done()
|
||||||
|
semaphore <- struct{}{}
|
||||||
|
defer func() { <-semaphore }()
|
||||||
|
if err := processContainers(ctx, item, namespace, containersList); err != nil {
|
||||||
|
select {
|
||||||
|
case errChan <- err:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch typedList := any(list).(type) {
|
switch typedList := any(list).(type) {
|
||||||
case *appsv1.DeploymentList:
|
case *appsv1.DeploymentList:
|
||||||
for i := range typedList.Items {
|
for i := range typedList.Items {
|
||||||
if err := processContainers((*DeploymentWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
wg.Add(1)
|
||||||
return err
|
go processItem((*DeploymentWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case *batchv1.JobList:
|
case *batchv1.JobList:
|
||||||
for i := range typedList.Items {
|
for i := range typedList.Items {
|
||||||
if err := processContainers((*JobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
wg.Add(1)
|
||||||
return err
|
go processItem((*JobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case *appsv1.DaemonSetList:
|
case *appsv1.DaemonSetList:
|
||||||
for i := range typedList.Items {
|
for i := range typedList.Items {
|
||||||
if err := processContainers((*DaemonSetWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
wg.Add(1)
|
||||||
return err
|
go processItem((*DaemonSetWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case *batchv1.CronJobList:
|
case *batchv1.CronJobList:
|
||||||
for i := range typedList.Items {
|
for i := range typedList.Items {
|
||||||
if err := processContainers((*CronJobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
wg.Add(1)
|
||||||
return err
|
go processItem((*CronJobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported list type: %T", list)
|
return fmt.Errorf("unsupported list type: %T", list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(errChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-errChan:
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupIndexers(mgr manager.Manager) error {
|
func SetupIndexers(mgr manager.Manager) error {
|
||||||
return mgr.GetFieldIndexer().IndexField(context.Background(), &raczylocomv1.ClusterImage{}, "spec.exportName", func(rawObj client.Object) []string {
|
return wait.ExponentialBackoff(wait.Backoff{
|
||||||
clusterImage := rawObj.(*raczylocomv1.ClusterImage)
|
Duration: 1 * time.Second,
|
||||||
return []string{clusterImage.Spec.ExportName}
|
Factor: 2,
|
||||||
|
Jitter: 0.1,
|
||||||
|
Steps: 5,
|
||||||
|
}, func() (bool, error) {
|
||||||
|
err := mgr.GetFieldIndexer().IndexField(context.Background(), &raczylocomv1.ClusterImage{}, "spec.exportName", func(rawObj client.Object) []string {
|
||||||
|
clusterImage := rawObj.(*raczylocomv1.ClusterImage)
|
||||||
|
return []string{clusterImage.Spec.ExportName}
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user