Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't count Custom Resources if not enabled at NIC startup #5749

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/configs/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1567,10 +1567,47 @@ func TestGetMixedIngressAnnotations(t *testing.T) {
}
}

func TestGetVitualServerCountsReportsNumberOfVSAndVSR(t *testing.T) {
t.Parallel()

tcnf := createTestConfigurator(t)
tcnf.virtualServers = map[string]*VirtualServerEx{
"vs": validVirtualServerExWithUpstreams,
}

gotVS, gotVSRoutes := tcnf.GetVirtualServerCounts()
wantVS, wantVSRoutes := 1, 0

if gotVS != wantVS {
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
}
if gotVSRoutes != wantVSRoutes {
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
}
}

func TestGetVitualServerCountsNotExistingVS(t *testing.T) {
t.Parallel()

tcnf := createTestConfigurator(t)
tcnf.virtualServers = nil

gotVS, gotVSRoutes := tcnf.GetVirtualServerCounts()
wantVS, wantVSRoutes := 0, 0

if gotVS != wantVS {
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
}
if gotVSRoutes != wantVSRoutes {
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
}
}

var (
invalidVirtualServerEx = &VirtualServerEx{
VirtualServer: &conf_v1.VirtualServer{},
}

validVirtualServerExWithUpstreams = &VirtualServerEx{
VirtualServer: &conf_v1.VirtualServer{
ObjectMeta: meta_v1.ObjectMeta{
Expand All @@ -1587,6 +1624,7 @@ var (
},
},
}

validTransportServerExWithUpstreams = &TransportServerEx{
TransportServer: &conf_v1.TransportServer{
ObjectMeta: meta_v1.ObjectMeta{
Expand Down
5 changes: 3 additions & 2 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
Namespace: os.Getenv("POD_NAMESPACE"),
Name: os.Getenv("POD_NAME"),
},
Policies: lbc.getAllPolicies,
IsPlus: lbc.isNginxPlus,
Policies: lbc.getAllPolicies,
IsPlus: lbc.isNginxPlus,
CustomResourcesEnabled: lbc.areCustomResourcesEnabled,
}
collector, err := telemetry.NewCollector(
collectorConfig,
Expand Down
7 changes: 4 additions & 3 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ func (c *Collector) IngressClassCount(ctx context.Context) (int, error) {
// PolicyCount returns the count in each Policy
func (c *Collector) PolicyCount() map[string]int {
policyCounters := make(map[string]int)

if !c.Config.CustomResourcesEnabled {
return policyCounters
}
if c.Config.Policies == nil {
return policyCounters
}

policies := c.Config.Policies()
if policies == nil {
if len(policies) == 0 {
return policyCounters
}

Expand Down
42 changes: 27 additions & 15 deletions internal/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type CollectorConfig struct {

// InstallationFlags represents the list of set flags managed by NIC
InstallationFlags []string

// Indicates if using of Custom Resources is enabled.
CustomResourcesEnabled bool
}

// NewCollector takes 0 or more options and creates a new TraceReporter.
Expand Down Expand Up @@ -206,7 +209,8 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
vsrCount := 0
tsCount := 0

if c.Config.Configurator != nil {
// Collect Custom Resources only if CR enabled at startup.
if c.Config.Configurator != nil && c.Config.CustomResourcesEnabled {
vsCount, vsrCount = c.Config.Configurator.GetVirtualServerCounts()
tsCount = c.Config.Configurator.GetTransportServerCounts()
}
Expand Down Expand Up @@ -254,25 +258,33 @@ func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
glog.V(3).Infof("Unable to collect telemetry data: Ingress Classes: %v", err)
}

policies := c.PolicyCount()

accessControlCount := policies["AccessControl"]
rateLimitCount := policies["RateLimit"]
jwtAuthCount := policies["JWTAuth"]
basicAuthCount := policies["BasicAuth"]
ingressMTLSCount := policies["IngressMTLS"]
egressMTLSCount := policies["EgressMTLS"]
oidcCount := policies["OIDC"]
wafCount := policies["WAF"]
var (
accessControlCount int
rateLimitCount int
jwtAuthCount int
basicAuthCount int
ingressMTLSCount int
egressMTLSCount int
oidcCount int
wafCount int
)
// Collect Custom Resources (Policies) only if CR enabled at startup.
if c.Config.CustomResourcesEnabled {
policies := c.PolicyCount()
accessControlCount = policies["AccessControl"]
rateLimitCount = policies["RateLimit"]
jwtAuthCount = policies["JWTAuth"]
basicAuthCount = policies["BasicAuth"]
ingressMTLSCount = policies["IngressMTLS"]
egressMTLSCount = policies["EgressMTLS"]
oidcCount = policies["OIDC"]
wafCount = policies["WAF"]
}

ingressAnnotations := c.IngressAnnotations()

appProtectVersion := c.AppProtectVersion()

isPlus := c.IsPlusEnabled()

installationFlags := c.InstallationFlags()

serviceCounts, err := c.ServiceCounts()
if err != nil {
glog.V(3).Infof("Unable to collect telemetry data: Service Counts: %v", err)
Expand Down
Loading
Loading