Skip to content

Commit

Permalink
iterate over namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed committed Oct 27, 2024
1 parent b454809 commit 109aca7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cli/cmd/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"os"

"github.com/odigos-io/odigos/common/consts"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/odigos-io/odigos/cli/pkg/kube"
Expand Down Expand Up @@ -72,6 +74,7 @@ Odigos CLI and monitor the instrumentation status.`,
}

func instrumentCluster(ctx context.Context, client *kube.Client, excludedNs, excludedApps map[string]struct{}) {
systemNs := sliceToMap(consts.SystemNamespaces)
nsList, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
fmt.Printf("\033[31mERROR\033[0m Cannot list namespaces: %s\n", err)
Expand All @@ -80,6 +83,31 @@ func instrumentCluster(ctx context.Context, client *kube.Client, excludedNs, exc

for _, ns := range nsList.Items {
fmt.Printf("Instrumenting namespace: %s\n", ns.Name)
_, excluded := excludedNs[ns.Name]
_, system := systemNs[ns.Name]
if excluded || system {
fmt.Printf(" - Skipping namespace due to exclusion file or system namespace\n")
continue
}

instrumentNamespace(ctx, client, ns.Name, excludedApps)
}
}

func instrumentNamespace(ctx context.Context, client *kube.Client, ns string, excludedApps map[string]struct{}) {
deps, err := client.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{})
if err != nil {
fmt.Printf(" - \033[31mERROR\033[0m Cannot list deployments: %s\n", err)
return
}

for _, dep := range deps.Items {
fmt.Printf(" - Instrumenting deployment: %s\n", dep.Name)
_, excluded := excludedApps[dep.Name]
if excluded {
fmt.Printf(" - Skipping deployment due to exclusion file\n")
continue
}
}
}

Expand Down Expand Up @@ -135,3 +163,11 @@ func init() {
clusterCmd.Flags().String(excludeAppsFileFlag, "", "File containing applications to exclude from instrumentation. Each application should be on a new line.")
clusterCmd.Flags().Bool(skipPreflightCheckFlag, false, "Skip preflight checks")
}

func sliceToMap(slice []string) map[string]struct{} {
m := make(map[string]struct{})
for _, s := range slice {
m[s] = struct{}{}
}
return m
}

0 comments on commit 109aca7

Please sign in to comment.