feat: kubedog integration with unified resource handling (#2383)

* feat: add kubedog-based resource tracking integration

Add kubedog tracking as an alternative to Helm's --wait flag with:
- Real-time deployment progress tracking
- Container log streaming
- Fine-grained resource filtering (trackKinds/skipKinds/trackResources)

Features:
- New pkg/resource package for unified manifest parsing and filtering
- New pkg/kubedog package wrapping kubedog library
- CLI flags: --track-mode, --track-timeout, --track-logs
- Helmfile YAML support for trackMode, trackTimeout, trackLogs, trackKinds, skipKinds, trackResources
- Case-insensitive kind matching for filtering
- Multi-context support with proper kubeconfig/kubeContext handling

Tracking supports: Deployment, StatefulSet, DaemonSet, Job

Resource filtering priority (highest to lowest):
1. trackResources - explicit resource whitelist
2. skipKinds - blacklist specific kinds
3. trackKinds - whitelist specific kinds

Integration:
- Disable Helm --wait when using kubedog tracking
- Track after successful Helm sync/apply
- Respect release.Namespace as fallback for resources without namespace
- Use getKubeContext() for correct cluster targeting

Tests:
- Unit tests for resource filtering and kubedog options
- Integration test with httpbin chart
- E2E snapshot tests for YAML serialization
- Documentation in docs/advanced-features.md

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix: address PR #2383 review comments (round 4)

1. resource/filter.go: Skip empty whitelist entries in matchWhitelist
   - At least one field (kind/name/namespace) must be specified
   - Prevents matching all resources with empty TrackResources entries

2. config/apply.go: Add ValidateConfig for track-mode validation
   - Validate --track-mode must be 'helm' or 'kubedog'
   - Reject invalid values like --track-mode foo

3. config/sync.go: Add ValidateConfig for track-mode validation
   - Same validation as apply command
   - Ensures consistent behavior across commands

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2026-03-02 17:15:12 +08:00
committed by GitHub
parent c70bd04f3a
commit 6e21671228
29 changed files with 1550 additions and 17 deletions
+94
View File
@@ -0,0 +1,94 @@
package resource
import (
"strings"
"go.uber.org/zap"
)
type ResourceFilter struct {
logger *zap.SugaredLogger
config *FilterConfig
skipKinds map[string]bool
trackKinds map[string]bool
}
func NewResourceFilter(config *FilterConfig, logger *zap.SugaredLogger) *ResourceFilter {
f := &ResourceFilter{
config: config,
logger: logger,
}
if config != nil {
f.skipKinds = make(map[string]bool)
for _, kind := range config.SkipKinds {
f.skipKinds[strings.ToLower(kind)] = true
}
f.trackKinds = make(map[string]bool)
for _, kind := range config.TrackKinds {
f.trackKinds[strings.ToLower(kind)] = true
}
}
return f
}
func (f *ResourceFilter) Filter(resources []Resource) []Resource {
if f.config == nil {
return resources
}
var filtered []Resource
for i := range resources {
if f.ShouldTrack(&resources[i]) {
filtered = append(filtered, resources[i])
} else if f.logger != nil {
res := resources[i]
f.logger.Debugf("Skipping resource %s/%s (kind: %s) based on configuration", res.Namespace, res.Name, res.Kind)
}
}
return filtered
}
func (f *ResourceFilter) ShouldTrack(r *Resource) bool {
if f.config == nil {
return true
}
if len(f.config.TrackResources) > 0 {
return f.matchWhitelist(r)
}
kindLower := strings.ToLower(r.Kind)
if f.skipKinds[kindLower] {
return false
}
if len(f.trackKinds) > 0 {
return f.trackKinds[kindLower]
}
return true
}
func (f *ResourceFilter) matchWhitelist(r *Resource) bool {
for _, tr := range f.config.TrackResources {
// At least one field must be specified for a match
if tr.Kind == "" && tr.Name == "" && tr.Namespace == "" {
continue
}
if tr.Kind != "" && !strings.EqualFold(tr.Kind, r.Kind) {
continue
}
if tr.Name != "" && tr.Name != r.Name {
continue
}
if tr.Namespace != "" && tr.Namespace != r.Namespace {
continue
}
return true
}
return false
}
+169
View File
@@ -0,0 +1,169 @@
package resource
import (
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func TestResourceFilter_Filter(t *testing.T) {
resources := []Resource{
{Kind: "Deployment", Name: "app1", Namespace: "default"},
{Kind: "StatefulSet", Name: "db1", Namespace: "default"},
{Kind: "ConfigMap", Name: "cm1", Namespace: "default"},
{Kind: "Secret", Name: "sec1", Namespace: "kube-system"},
}
tests := []struct {
name string
config *FilterConfig
expected int
}{
{
name: "nil filter returns all",
config: nil,
expected: 4,
},
{
name: "TrackKinds whitelist",
config: &FilterConfig{
TrackKinds: []string{"Deployment", "StatefulSet"},
},
expected: 2,
},
{
name: "SkipKinds blacklist",
config: &FilterConfig{
SkipKinds: []string{"ConfigMap", "Secret"},
},
expected: 2,
},
{
name: "TrackResources whitelist by kind and namespace",
config: &FilterConfig{
TrackResources: []Resource{
{Kind: "Deployment", Namespace: "default"},
},
},
expected: 1,
},
{
name: "TrackResources whitelist by name",
config: &FilterConfig{
TrackResources: []Resource{
{Name: "app1"},
{Name: "db1"},
},
},
expected: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := NewResourceFilter(tt.config, zap.NewNop().Sugar())
filtered := filter.Filter(resources)
assert.Equal(t, tt.expected, len(filtered))
})
}
}
func TestResourceFilter_ShouldTrack(t *testing.T) {
logger := zap.NewNop().Sugar()
tests := []struct {
name string
config *FilterConfig
resource *Resource
expected bool
}{
{
name: "nil config tracks all",
config: nil,
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: true,
},
{
name: "TrackKinds matches",
config: &FilterConfig{
TrackKinds: []string{"Deployment"},
},
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: true,
},
{
name: "TrackKinds no match",
config: &FilterConfig{
TrackKinds: []string{"StatefulSet"},
},
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: false,
},
{
name: "SkipKinds matches",
config: &FilterConfig{
SkipKinds: []string{"ConfigMap"},
},
resource: &Resource{Kind: "ConfigMap", Name: "cm1"},
expected: false,
},
{
name: "SkipKinds no match",
config: &FilterConfig{
SkipKinds: []string{"ConfigMap"},
},
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: true,
},
{
name: "TrackResources whitelist matches all criteria",
config: &FilterConfig{
TrackResources: []Resource{
{Kind: "Deployment", Name: "app1", Namespace: "default"},
},
},
resource: &Resource{Kind: "Deployment", Name: "app1", Namespace: "default"},
expected: true,
},
{
name: "TrackResources whitelist partial match",
config: &FilterConfig{
TrackResources: []Resource{
{Kind: "Deployment", Name: "app1"},
},
},
resource: &Resource{Kind: "Deployment", Name: "app1", Namespace: "other"},
expected: true,
},
{
name: "TrackResources whitelist no match",
config: &FilterConfig{
TrackResources: []Resource{
{Kind: "Deployment", Name: "app2"},
},
},
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: false,
},
{
name: "TrackResources takes precedence over TrackKinds",
config: &FilterConfig{
TrackResources: []Resource{
{Kind: "Deployment", Name: "app1"},
},
TrackKinds: []string{"StatefulSet"},
},
resource: &Resource{Kind: "Deployment", Name: "app1"},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filter := NewResourceFilter(tt.config, logger)
result := filter.ShouldTrack(tt.resource)
assert.Equal(t, tt.expected, result)
})
}
}
+62
View File
@@ -0,0 +1,62 @@
package resource
import (
"bytes"
"errors"
"fmt"
"io"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)
func ParseManifest(manifest []byte, defaultNamespace string, logger *zap.SugaredLogger) ([]Resource, error) {
var resources []Resource
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(manifest), 4096)
for {
var obj unstructured.Unstructured
err := decoder.Decode(&obj)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("failed to decode manifest: %w", err)
}
if len(obj.Object) == 0 {
continue
}
kind := obj.GetKind()
if kind == "" {
if logger != nil {
logger.Debugf("Skipping resource without kind")
}
continue
}
name := obj.GetName()
if name == "" {
if logger != nil {
logger.Debugf("Skipping %s resource without name", kind)
}
continue
}
namespace := obj.GetNamespace()
if namespace == "" {
namespace = defaultNamespace
}
resources = append(resources, Resource{
Kind: kind,
Name: name,
Namespace: namespace,
})
}
return resources, nil
}
+13
View File
@@ -0,0 +1,13 @@
package resource
type Resource struct {
Kind string
Name string
Namespace string
}
type FilterConfig struct {
TrackKinds []string
SkipKinds []string
TrackResources []Resource
}