mirror of
https://github.com/lukaszraczylo/kubernetes-images-sync-operator.git
synced 2026-06-09 23:19:15 +00:00
Add ability to include/exclude namespaces.
This commit is contained in:
@@ -5,10 +5,9 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// JOB IMAGES
|
||||
BACKUP_JOB_IMAGE = "ghcr.io/lukaszraczylo/kubernetes-images-sync-worker:1.0.2"
|
||||
var BACKUP_JOB_IMAGE = "ghcr.io/lukaszraczylo/kubernetes-images-sync-worker:1.0.2"
|
||||
|
||||
const (
|
||||
// AVAILABLE STATUSES
|
||||
STATUS_PENDING = "PENDING"
|
||||
STATUS_STARTING = "STARTING"
|
||||
@@ -24,10 +23,11 @@ const (
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
Image string `json:"image"`
|
||||
Tag string `json:"tag"`
|
||||
Sha string `json:"sha"`
|
||||
FullName string `json:"fullName"`
|
||||
Image string `json:"image"`
|
||||
Tag string `json:"tag"`
|
||||
Sha string `json:"sha"`
|
||||
FullName string `json:"fullName"`
|
||||
ImageNamespace string `json:"imageNamespace"`
|
||||
}
|
||||
|
||||
type ContainersList struct {
|
||||
@@ -96,3 +96,34 @@ func NormalizeImageName(name string) string {
|
||||
// Trim leading and trailing hyphens
|
||||
return strings.Trim(normalized, "-")
|
||||
}
|
||||
|
||||
// filterOnlyFromNamespaces filters out containers from namespaces that are not in the list
|
||||
func FilterOnlyFromNamespaces(containers ContainersList, namespaces []string) ContainersList {
|
||||
result := ContainersList{}
|
||||
for _, container := range containers.Containers {
|
||||
for _, namespace := range namespaces {
|
||||
if container.ImageNamespace == namespace {
|
||||
result.Containers = append(result.Containers, container)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// filterOutWholeNamespaces filters out containers from namespaces that are in the list
|
||||
func FilterOutWholeNamespaces(containers ContainersList, namespaces []string) ContainersList {
|
||||
result := ContainersList{}
|
||||
for _, container := range containers.Containers {
|
||||
excluded := false
|
||||
for _, namespace := range namespaces {
|
||||
if container.ImageNamespace == namespace {
|
||||
excluded = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !excluded {
|
||||
result.Containers = append(result.Containers, container)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func processContainerName(containerName string) (Container, error) {
|
||||
return cnt, nil
|
||||
}
|
||||
|
||||
func processContainers[T K8sResource](resource T, containersList *ContainersList) error {
|
||||
func processContainers[T K8sResource](resource T, namespace string, containersList *ContainersList) error {
|
||||
podSpec := resource.GetPodSpec()
|
||||
if podSpec == nil {
|
||||
return fmt.Errorf("nil PodSpec")
|
||||
@@ -72,13 +72,13 @@ func processContainers[T K8sResource](resource T, containersList *ContainersList
|
||||
|
||||
allContainers := append(podSpec.Containers, podSpec.InitContainers...)
|
||||
for _, container := range allContainers {
|
||||
if err := processContainer(container.Image, containersList); err != nil {
|
||||
if err := processContainer(container.Image, namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, container := range podSpec.EphemeralContainers {
|
||||
if err := processContainer(container.EphemeralContainerCommon.Image, containersList); err != nil {
|
||||
if err := processContainer(container.EphemeralContainerCommon.Image, namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -87,11 +87,12 @@ func processContainers[T K8sResource](resource T, containersList *ContainersList
|
||||
}
|
||||
|
||||
// processContainer handles the processing of a single container image
|
||||
func processContainer(image string, containersList *ContainersList) error {
|
||||
func processContainer(image string, containerNamespace string, containersList *ContainersList) error {
|
||||
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
|
||||
}
|
||||
@@ -105,25 +106,25 @@ func ListAndProcessResources[T K8sResource, L client.ObjectList](ctx context.Con
|
||||
switch typedList := any(list).(type) {
|
||||
case *appsv1.DeploymentList:
|
||||
for i := range typedList.Items {
|
||||
if err := processContainers((*DeploymentWrapper)(&typedList.Items[i]), containersList); err != nil {
|
||||
if err := processContainers((*DeploymentWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *batchv1.JobList:
|
||||
for i := range typedList.Items {
|
||||
if err := processContainers((*JobWrapper)(&typedList.Items[i]), containersList); err != nil {
|
||||
if err := processContainers((*JobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *appsv1.DaemonSetList:
|
||||
for i := range typedList.Items {
|
||||
if err := processContainers((*DaemonSetWrapper)(&typedList.Items[i]), containersList); err != nil {
|
||||
if err := processContainers((*DaemonSetWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case *batchv1.CronJobList:
|
||||
for i := range typedList.Items {
|
||||
if err := processContainers((*CronJobWrapper)(&typedList.Items[i]), containersList); err != nil {
|
||||
if err := processContainers((*CronJobWrapper)(&typedList.Items[i]), typedList.Items[i].Namespace, containersList); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user