support wildcards --kube-include-namespace/--kube-exclude-namespace

This commit is contained in:
Tomas Hruska 2025-12-18 00:00:30 +01:00
parent 65a4534be9
commit 263dd00c5b
4 changed files with 19 additions and 21 deletions

View file

@ -49,8 +49,8 @@ curl -fsSL http://<pulse-ip>:7655/install.sh | \
| `--disable-proxmox` | - | Disable Proxmox even if detected | - | | `--disable-proxmox` | - | Disable Proxmox even if detected | - |
| `--kubeconfig` | `PULSE_KUBECONFIG` | Kubeconfig path (optional) | *(auto)* | | `--kubeconfig` | `PULSE_KUBECONFIG` | Kubeconfig path (optional) | *(auto)* |
| `--kube-context` | `PULSE_KUBE_CONTEXT` | Kubeconfig context (optional) | *(auto)* | | `--kube-context` | `PULSE_KUBE_CONTEXT` | Kubeconfig context (optional) | *(auto)* |
| `--kube-include-namespace` | `PULSE_KUBE_INCLUDE_NAMESPACES` | Limit namespaces (repeatable or CSV) | *(all)* | | `--kube-include-namespace` | `PULSE_KUBE_INCLUDE_NAMESPACES` | Limit namespaces (repeatable or CSV, wildcards supported) | *(all)* |
| `--kube-exclude-namespace` | `PULSE_KUBE_EXCLUDE_NAMESPACES` | Exclude namespaces (repeatable or CSV) | *(none)* | | `--kube-exclude-namespace` | `PULSE_KUBE_EXCLUDE_NAMESPACES` | Exclude namespaces (repeatable or CSV, wildcards supported) | *(none)* |
| `--kube-include-all-pods` | `PULSE_KUBE_INCLUDE_ALL_PODS` | Include all non-succeeded pods | `false` | | `--kube-include-all-pods` | `PULSE_KUBE_INCLUDE_ALL_PODS` | Include all non-succeeded pods | `false` |
| `--kube-max-pods` | `PULSE_KUBE_MAX_PODS` | Max pods per report | `200` | | `--kube-max-pods` | `PULSE_KUBE_MAX_PODS` | Max pods per report | `200` |
| `--disable-auto-update` | `PULSE_DISABLE_AUTO_UPDATE` | Disable auto-updates | `false` | | `--disable-auto-update` | `PULSE_DISABLE_AUTO_UPDATE` | Disable auto-updates | `false` |

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.24.0
toolchain go1.24.7 toolchain go1.24.7
require ( require (
github.com/IGLOU-EU/go-wildcard/v2 v2.1.0
github.com/coreos/go-oidc/v3 v3.17.0 github.com/coreos/go-oidc/v3 v3.17.0
github.com/docker/docker v28.5.2+incompatible github.com/docker/docker v28.5.2+incompatible
github.com/fsnotify/fsnotify v1.9.0 github.com/fsnotify/fsnotify v1.9.0

BIN
go.sum

Binary file not shown.

View file

@ -15,6 +15,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/IGLOU-EU/go-wildcard/v2"
"github.com/rcourtman/pulse-go-rewrite/internal/buffer" "github.com/rcourtman/pulse-go-rewrite/internal/buffer"
agentsk8s "github.com/rcourtman/pulse-go-rewrite/pkg/agents/kubernetes" agentsk8s "github.com/rcourtman/pulse-go-rewrite/pkg/agents/kubernetes"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -69,8 +70,8 @@ type Agent struct {
clusterContext string clusterContext string
clusterVersion string clusterVersion string
includeNamespaces sets.Set[string] includeNamespaces []string
excludeNamespaces sets.Set[string] excludeNamespaces []string
reportBuffer *buffer.Queue[agentsk8s.Report] reportBuffer *buffer.Queue[agentsk8s.Report]
} }
@ -164,8 +165,8 @@ func New(cfg Config) (*Agent, error) {
clusterName: clusterName, clusterName: clusterName,
clusterServer: clusterServer, clusterServer: clusterServer,
clusterContext: clusterContext, clusterContext: clusterContext,
includeNamespaces: makeNamespaceSet(cfg.IncludeNamespaces), includeNamespaces: cfg.IncludeNamespaces,
excludeNamespaces: makeNamespaceSet(cfg.ExcludeNamespaces), excludeNamespaces: cfg.ExcludeNamespaces,
reportBuffer: buffer.New[agentsk8s.Report](60), reportBuffer: buffer.New[agentsk8s.Report](60),
} }
@ -241,17 +242,6 @@ func buildRESTConfig(kubeconfigPath, kubeContext string) (*rest.Config, string,
return restCfg, contextName, nil return restCfg, contextName, nil
} }
func makeNamespaceSet(values []string) sets.Set[string] {
set := sets.New[string]()
for _, v := range values {
v = strings.TrimSpace(v)
if v != "" {
set.Insert(v)
}
}
return set
}
func computeClusterID(server, context, name string) string { func computeClusterID(server, context, name string) string {
payload := strings.TrimSpace(server) + "|" + strings.TrimSpace(context) + "|" + strings.TrimSpace(name) payload := strings.TrimSpace(server) + "|" + strings.TrimSpace(context) + "|" + strings.TrimSpace(name)
sum := sha256.Sum256([]byte(payload)) sum := sha256.Sum256([]byte(payload))
@ -321,13 +311,20 @@ func (a *Agent) namespaceAllowed(ns string) bool {
if ns == "" { if ns == "" {
return false return false
} }
if a.excludeNamespaces.Has(ns) { for _, excludeNamespace := range a.excludeNamespaces {
return false if wildcard.Match(excludeNamespace, ns) {
return false
}
} }
if a.includeNamespaces.Len() == 0 { if len(a.includeNamespaces) == 0 {
return true return true
} }
return a.includeNamespaces.Has(ns) for _, includeNamespace := range a.includeNamespaces {
if wildcard.Match(includeNamespace, ns) {
return true
}
}
return false
} }
func (a *Agent) collectReport(ctx context.Context) (agentsk8s.Report, error) { func (a *Agent) collectReport(ctx context.Context) (agentsk8s.Report, error) {