Skip to content

Commit

Permalink
chore: extract starting telemetry from internal/manager (#7100)
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 authored Feb 11, 2025
1 parent b74c3e1 commit 1e9406f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 119 deletions.
37 changes: 33 additions & 4 deletions internal/cmd/rootcmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/logging"
"github.com/kong/kubernetes-ingress-controller/v3/pkg/manager"
managercfg "github.com/kong/kubernetes-ingress-controller/v3/pkg/manager/config"
"github.com/kong/kubernetes-ingress-controller/v3/pkg/telemetry"
)

// Run sets up a default stderr logger and starts the controller manager.
Expand All @@ -27,10 +28,9 @@ func Run(ctx context.Context, c managercfg.Config, output io.Writer) error {
}
defer signal.Ignore(shutdownSignals...)

mid, err := manager.NewID("kic")
if err != nil {
return fmt.Errorf("failed to create manager ID: %w", err)
}
// Single manager with the same ID.
mid := manager.NewRandomID()

m, err := manager.NewManager(ctx, mid, logger, c)
if err != nil {
return fmt.Errorf("failed to create manager: %w", err)
Expand All @@ -41,5 +41,34 @@ func Run(ctx context.Context, c managercfg.Config, output io.Writer) error {
healthz.Ping, health.NewHealthCheckerFromFunc(m.IsReady),
).Start(ctx, c.ProbeAddr, logger.WithName("health-check"))

if c.AnonymousReports {
stopAnonymousReports, err := telemetry.SetupAnonymousReports(
ctx,
m.GetKubeconfig(),
m.GetClientsManager(),
telemetry.ReportConfig{
SplunkEndpoint: c.SplunkEndpoint,
SplunkEndpointInsecureSkipVerify: c.SplunkEndpointInsecureSkipVerify,
TelemetryPeriod: c.TelemetryPeriod,
ReportValues: telemetry.ReportValues{
PublishServiceNN: c.PublishService.OrEmpty(),
FeatureGates: c.FeatureGates,
MeshDetection: len(c.WatchNamespaces) == 0,
KonnectSyncEnabled: c.Konnect.ConfigSynchronizationEnabled,
GatewayServiceDiscoveryEnabled: c.KongAdminSvc.IsPresent(),
},
},
mid,
)
if err != nil {
logger.Error(err, "Failed setting up anonymous reports, continuing without telemetry")
} else {
defer stopAnonymousReports()
logger.Info("Anonymous reports enabled")
}
} else {
logger.Info("Anonymous reports disabled, skipping")
}

return m.Run(ctx)
}
13 changes: 6 additions & 7 deletions internal/konnect/node_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/google/uuid"

"github.com/kong/kubernetes-ingress-controller/v3/internal/clients"
"github.com/kong/kubernetes-ingress-controller/v3/internal/konnect/nodes"
Expand Down Expand Up @@ -39,8 +38,8 @@ type GatewayClientsChangesNotifier interface {
SubscribeToGatewayClientsChanges() (<-chan struct{}, bool)
}

type ManagerInstanceIDProvider interface {
GetID() uuid.UUID
type ManagerInstanceID interface {
String() string
}

// NodeClient is the interface to Konnect Control Plane Node API.
Expand Down Expand Up @@ -76,7 +75,7 @@ type NodeAgent struct {

gatewayInstanceGetter GatewayInstanceGetter
gatewayClientsChangesNotifier GatewayClientsChangesNotifier
managerInstanceIDProvider ManagerInstanceIDProvider
managerInstanceID ManagerInstanceID
}

type NodeAgentOpt func(*NodeAgent)
Expand All @@ -99,7 +98,7 @@ func NewNodeAgent(
configStatusSubscriber clients.ConfigStatusSubscriber,
gatewayGetter GatewayInstanceGetter,
gatewayClientsChangesNotifier GatewayClientsChangesNotifier,
managerInstanceIDProvider ManagerInstanceIDProvider,
managerInstanceID ManagerInstanceID,
opts ...NodeAgentOpt,
) *NodeAgent {
if refreshPeriod < MinRefreshNodePeriod {
Expand All @@ -115,7 +114,7 @@ func NewNodeAgent(
configStatusSubscriber: configStatusSubscriber,
gatewayInstanceGetter: gatewayGetter,
gatewayClientsChangesNotifier: gatewayClientsChangesNotifier,
managerInstanceIDProvider: managerInstanceIDProvider,
managerInstanceID: managerInstanceID,
}
a.configStatus.Store(clients.ConfigStatusOK)

Expand Down Expand Up @@ -290,7 +289,7 @@ func (a *NodeAgent) updateKICNode(ctx context.Context, existingNodes []*nodes.No
if len(nodesWithSameName) == 0 {
a.logger.V(logging.DebugLevel).Info("No nodes found for KIC pod, should create one", "hostname", a.hostname)
createNodeReq := &nodes.CreateNodeRequest{
ID: a.managerInstanceIDProvider.GetID().String(),
ID: a.managerInstanceID.String(),
Hostname: a.hostname,
Version: a.version,
Type: nodes.NodeTypeIngressController,
Expand Down
24 changes: 6 additions & 18 deletions internal/konnect/node_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,6 @@ func (m *mockGatewayClientsNotifier) Notify() {
m.ch <- struct{}{}
}

type mockManagerInstanceIDProvider struct {
instanceID uuid.UUID
}

func newMockManagerInstanceIDProvider(instanceID uuid.UUID) *mockManagerInstanceIDProvider {
return &mockManagerInstanceIDProvider{instanceID: instanceID}
}

func (m *mockManagerInstanceIDProvider) GetID() uuid.UUID {
return m.instanceID
}

type mockConfigStatusQueue struct {
gatewayStatusCh chan clients.GatewayConfigApplyStatus
konnetStatusCh chan clients.KonnectConfigUploadStatus
Expand Down Expand Up @@ -287,7 +275,7 @@ func TestNodeAgentUpdateNodes(t *testing.T) {
configStatusQueue,
newMockGatewayInstanceGetter(tc.gatewayInstances),
gatewayClientsChangesNotifier,
newMockManagerInstanceIDProvider(testManagerID),
testManagerID,
)

runAgent(t, nodeAgent)
Expand Down Expand Up @@ -355,7 +343,7 @@ func TestNodeAgent_StartDoesntReturnUntilContextGetsCancelled(t *testing.T) {
newMockConfigStatusNotifier(),
newMockGatewayInstanceGetter(nil),
newMockGatewayClientsNotifier(),
newMockManagerInstanceIDProvider(uuid.New()),
uuid.New(),
)

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -400,7 +388,7 @@ func TestNodeAgent_ControllerNodeStatusGetsUpdatedOnStatusNotification(t *testin
configStatusQueue,
newMockGatewayInstanceGetter(nil),
gatewayClientsChangesNotifier,
newMockManagerInstanceIDProvider(uuid.New()),
uuid.New(),
)

runAgent(t, nodeAgent)
Expand Down Expand Up @@ -502,7 +490,7 @@ func TestNodeAgent_ControllerNodeStatusGetsUpdatedOnlyWhenItChanges(t *testing.T
configStatusQueue,
newMockGatewayInstanceGetter(nil),
gatewayClientsChangesNotifier,
newMockManagerInstanceIDProvider(uuid.New()),
uuid.New(),
)

runAgent(t, nodeAgent)
Expand Down Expand Up @@ -552,7 +540,7 @@ func TestNodeAgent_TickerResetsOnEveryNodesUpdate(t *testing.T) {
configStatusQueue,
newMockGatewayInstanceGetter(nil),
gatewayClientsChangesNotifier,
newMockManagerInstanceIDProvider(uuid.New()),
uuid.New(),
konnect.WithRefreshTicker(ticker),
)

Expand Down Expand Up @@ -589,7 +577,7 @@ func TestNodeAgent_TickerResetsOnEveryNodesUpdate(t *testing.T) {
configStatusQueue,
newMockGatewayInstanceGetter(nil),
gatewayClientsChangesNotifier,
newMockManagerInstanceIDProvider(uuid.New()),
uuid.New(),
konnect.WithRefreshTicker(ticker),
)

Expand Down
21 changes: 0 additions & 21 deletions internal/manager/id.go

This file was deleted.

75 changes: 27 additions & 48 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/google/uuid"
"github.com/samber/mo"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -45,27 +46,31 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/pkg/manager/config"
managercfg "github.com/kong/kubernetes-ingress-controller/v3/pkg/manager/config"
"github.com/kong/kubernetes-ingress-controller/v3/pkg/metadata"
"github.com/kong/kubernetes-ingress-controller/v3/pkg/telemetry"
)

// -----------------------------------------------------------------------------
// Controller Manager - Setup & Run
// -----------------------------------------------------------------------------

type Manager struct {
cfg managercfg.Config
type InstanceID interface {
String() string
}

type Manager struct {
m manager.Manager
synchronizer *dataplane.Synchronizer
diagnosticsServer mo.Option[diagnostics.Server]
stopAnonymousReports func()
diagnosticsCollector mo.Option[*diagnostics.Collector]
diagnosticsHandler mo.Option[*diagnostics.HTTPHandler]

kubeconfig *rest.Config
clientsManager *clients.AdminAPIClientsManager
}

// New configures the controller manager call Start.
func New(
ctx context.Context,
instanceID InstanceID,
c managercfg.Config,
logger logr.Logger,
) (*Manager, error) {
Expand All @@ -85,10 +90,7 @@ func New(
}
}

m := &Manager{
cfg: c,
}

m := &Manager{}
diagnosticsClient := m.setupDiagnostics(ctx, c)
setupLog := logger.WithName("setup")
setupLog.Info("Starting controller manager", "release", metadata.Release, "repo", metadata.Repo, "commit", metadata.Commit)
Expand All @@ -101,6 +103,7 @@ func New(
if err != nil {
return nil, fmt.Errorf("get kubeconfig from file %q: %w", c.KubeconfigPath, err)
}
m.kubeconfig = kubeconfig

adminAPIsDiscoverer, err := adminapi.NewDiscoverer(sets.New(c.KongAdminSvcPortNames...))
if err != nil {
Expand Down Expand Up @@ -191,6 +194,7 @@ func New(
}
clientsManager = clientsManager.WithDBMode(dbMode)
clientsManager = clientsManager.WithReconciliationInterval(c.GatewayDiscoveryReadinessCheckInterval)
m.clientsManager = clientsManager

if c.KongAdminSvc.IsPresent() {
setupLog.Info("Running AdminAPIClientsManager loop")
Expand Down Expand Up @@ -295,8 +299,6 @@ func New(
// See https://github.com/kubernetes-sigs/kubebuilder/issues/932
// +kubebuilder:scaffold:builder

instanceIDProvider := NewInstanceIDProvider()

if c.Konnect.ConfigSynchronizationEnabled {
// In case of failures when building Konnect related objects, we're not returning errors as Konnect is not
// considered critical feature, and it should not break the basic functionality of the controller.
Expand Down Expand Up @@ -329,7 +331,7 @@ func New(
configStatusNotifier,
clientsManager,
setupLog,
instanceIDProvider,
instanceID,
); err != nil {
setupLog.Error(err, "Failed to setup Konnect NodeAgent with manager, skipping")
}
Expand All @@ -354,36 +356,7 @@ func New(
kongConfigFetcher.InjectLicenseGetter(licenseGetter)
}

stopAnonymousReports := func() {}
if c.AnonymousReports {
stopAnonymousReports, err = telemetry.SetupAnonymousReports(
ctx,
kubeconfig,
clientsManager,
telemetry.ReportConfig{
SplunkEndpoint: c.SplunkEndpoint,
SplunkEndpointInsecureSkipVerify: c.SplunkEndpointInsecureSkipVerify,
TelemetryPeriod: c.TelemetryPeriod,
ReportValues: telemetry.ReportValues{
PublishServiceNN: c.PublishService.OrEmpty(),
FeatureGates: c.FeatureGates,
MeshDetection: len(c.WatchNamespaces) == 0,
KonnectSyncEnabled: c.Konnect.ConfigSynchronizationEnabled,
GatewayServiceDiscoveryEnabled: c.KongAdminSvc.IsPresent(),
},
},
instanceIDProvider,
)
if err != nil {
setupLog.Error(err, "Failed setting up anonymous reports")
}
setupLog.Info("Anonymous reports enabled")
} else {
setupLog.Info("Anonymous reports disabled, skipping")
}

m.m = mgr
m.stopAnonymousReports = stopAnonymousReports
m.synchronizer = synchronizer

setupLog.Info("Finished setting up the controller manager")
Expand Down Expand Up @@ -438,7 +411,7 @@ func setupKonnectNodeAgentWithMgr(
configStatusSubscriber clients.ConfigStatusSubscriber,
clientsManager *clients.AdminAPIClientsManager,
logger logr.Logger,
instanceIDProvider *InstanceIDProvider,
instanceID InstanceID,
) error {
konnectNodesAPIClient, err := nodes.NewClient(c.Konnect)
if err != nil {
Expand All @@ -453,7 +426,7 @@ func setupKonnectNodeAgentWithMgr(
configStatusSubscriber,
konnect.NewGatewayClientGetter(logger, clientsManager),
clientsManager,
instanceIDProvider,
instanceID,
)
if err := mgr.Add(agent); err != nil {
return fmt.Errorf("failed adding konnect.NodeAgent runnable to the manager: %w", err)
Expand Down Expand Up @@ -526,12 +499,6 @@ func (m *Manager) Run(ctx context.Context) error {

logger.Info("Starting manager")

defer func() {
if m.stopAnonymousReports != nil {
m.stopAnonymousReports()
}
}()

if ds, ok := m.diagnosticsServer.Get(); ok {
go func() {
logger.Info("Starting diagnostics server")
Expand Down Expand Up @@ -577,3 +544,15 @@ func (m *Manager) DiagnosticsHandler() http.Handler {
}
return nil
}

// GetKubeconfig returns the Kubernetes client configuration used by the manager.
func (m *Manager) GetKubeconfig() *rest.Config {
return m.kubeconfig
}

// GetClientsManager returns the AdminAPIClientsManager used by the manager.
// TODO: It is used by telemetry to calculate kongVersion, DB mode and router
// flavor. It shouldn't be exposed so wide, only relevant stuff.
func (m *Manager) GetClientsManager() *clients.AdminAPIClientsManager {
return m.clientsManager
}
1 change: 1 addition & 0 deletions pkg/manager/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewID(s string) (ID, error) {
return ID{id: s}, nil
}

// String returns the string representation (usable for various APIs) of the manager ID.
func (id ID) String() string {
return id.id
}
Loading

0 comments on commit 1e9406f

Please sign in to comment.