deckhouse / k8s-image-availability-exporter

Alert if an image used in Kubernetes cannot be pulled from container registry
Apache License 2.0
211 stars 25 forks source link

Add Argo Rollouts support #134

Open tony2001 opened 6 months ago

tony2001 commented 6 months ago

We use Argo Rollouts for gradual deployment of our services, so we needed to detect missing images for them as well. The support is quite straightforward, but it requires its own client, which I kinda dislike. I would be grateful if you can think of any way to get rid of it.

nabokihms commented 5 months ago

@tony2001 thanks! The idea is excellent, but my only concern is importing the github.com/argoproj/argo-rollouts.

We can play around with it and use the dynamic client. This approach is more generic, and we will be able to add more resources in the future without importing the world.

Example:

var rolloutsResource = schema.GroupVersionResource{Group: "argoproj,io", Version: "v1alpha1", Resource: "rollouts"}

clusterClient, err := dynamic.NewForConfig(clusterConfig)
if err != nil {
    log.Fatalln(err)
}

informer = dynamicinformer.NewFilteredDynamicInformer(
    clusterClient,
    rolloutsResource,
    corev1.NamespaceAll,
    30*time.Second,
    cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
    nil,
)

To work with objects, there is the unstructured package.

containers, _, _ := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "containers")

res := make([]corev1.Container{}, 0, len(containers))

for _, c := range containers {
      if props, ok := c.(map[string]interface{}); ok {
            res := append(res, corev1.Container{
                Image: props["image"].(string),
                Name:  props["name"].(string),
             }) 
      }

}

return res 
tony2001 commented 5 months ago

Sounds reasonable indeed!