Newer
Older
EnvoyControlPlane / internal / snapshot / manager.go
package snapshot

import (
	"context"
	"envoy-control-plane/internal/storage"
	"fmt"

	// Ensure all standard filters are imported for proto unmarshalling
	_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/jwt_authn/v3"
	_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/lua/v3"
	_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/oauth2/v3"
	_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3"
	_ "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/listener/tls_inspector/v3"
	_ "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"

	"github.com/envoyproxy/go-control-plane/pkg/cache/types"
	cachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
	resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
)

// ResourceNamer is an interface implemented by all xDS resources with a GetName() method.
type ResourceNamer interface {
	GetName() string
}

// SnapshotManager wraps a SnapshotCache and provides file loading/modifying
// and DB synchronization functionality for Envoy xDS resources.
type SnapshotManager struct {
	Cache  cachev3.SnapshotCache
	NodeID string
	DB     *storage.Storage // Assuming Storage is defined elsewhere (DB persistence layer)
}

// NewSnapshotManager creates a new instance of SnapshotManager.
func NewSnapshotManager(cache cachev3.SnapshotCache, nodeID string, db *storage.Storage) *SnapshotManager {
	return &SnapshotManager{
		Cache:  cache,
		NodeID: nodeID,
		DB:     db,
	}
}

// SetSnapshot sets a full snapshot (utility method used by others).
func (sm *SnapshotManager) SetSnapshot(ctx context.Context, version string, resources map[resourcev3.Type][]types.Resource) error {
	snap, err := cachev3.NewSnapshot(version, resources)
	if err != nil {
		return fmt.Errorf("failed to create snapshot: %w", err)
	}
	return sm.Cache.SetSnapshot(ctx, sm.NodeID, snap)
}