Skip to content

Commit 46120dd

Browse files
authored
Don't count Custom Resources if not enabled at NIC startup (#5749)
1 parent bc19e2f commit 46120dd

File tree

5 files changed

+356
-157
lines changed

5 files changed

+356
-157
lines changed

internal/configs/configurator_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,10 +1567,47 @@ func TestGetMixedIngressAnnotations(t *testing.T) {
15671567
}
15681568
}
15691569

1570+
func TestGetVitualServerCountsReportsNumberOfVSAndVSR(t *testing.T) {
1571+
t.Parallel()
1572+
1573+
tcnf := createTestConfigurator(t)
1574+
tcnf.virtualServers = map[string]*VirtualServerEx{
1575+
"vs": validVirtualServerExWithUpstreams,
1576+
}
1577+
1578+
gotVS, gotVSRoutes := tcnf.GetVirtualServerCounts()
1579+
wantVS, wantVSRoutes := 1, 0
1580+
1581+
if gotVS != wantVS {
1582+
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
1583+
}
1584+
if gotVSRoutes != wantVSRoutes {
1585+
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
1586+
}
1587+
}
1588+
1589+
func TestGetVitualServerCountsNotExistingVS(t *testing.T) {
1590+
t.Parallel()
1591+
1592+
tcnf := createTestConfigurator(t)
1593+
tcnf.virtualServers = nil
1594+
1595+
gotVS, gotVSRoutes := tcnf.GetVirtualServerCounts()
1596+
wantVS, wantVSRoutes := 0, 0
1597+
1598+
if gotVS != wantVS {
1599+
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
1600+
}
1601+
if gotVSRoutes != wantVSRoutes {
1602+
t.Errorf("GetVirtualServerCounts() = %d, %d, want %d, %d", gotVS, gotVSRoutes, wantVS, wantVSRoutes)
1603+
}
1604+
}
1605+
15701606
var (
15711607
invalidVirtualServerEx = &VirtualServerEx{
15721608
VirtualServer: &conf_v1.VirtualServer{},
15731609
}
1610+
15741611
validVirtualServerExWithUpstreams = &VirtualServerEx{
15751612
VirtualServer: &conf_v1.VirtualServer{
15761613
ObjectMeta: meta_v1.ObjectMeta{
@@ -1587,6 +1624,7 @@ var (
15871624
},
15881625
},
15891626
}
1627+
15901628
validTransportServerExWithUpstreams = &TransportServerEx{
15911629
TransportServer: &conf_v1.TransportServer{
15921630
ObjectMeta: meta_v1.ObjectMeta{

internal/k8s/controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,9 @@ func NewLoadBalancerController(input NewLoadBalancerControllerInput) *LoadBalanc
375375
Namespace: os.Getenv("POD_NAMESPACE"),
376376
Name: os.Getenv("POD_NAME"),
377377
},
378-
Policies: lbc.getAllPolicies,
379-
IsPlus: lbc.isNginxPlus,
378+
Policies: lbc.getAllPolicies,
379+
IsPlus: lbc.isNginxPlus,
380+
CustomResourcesEnabled: lbc.areCustomResourcesEnabled,
380381
}
381382
collector, err := telemetry.NewCollector(
382383
collectorConfig,

internal/telemetry/cluster.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ func (c *Collector) IngressClassCount(ctx context.Context) (int, error) {
162162
// PolicyCount returns the count in each Policy
163163
func (c *Collector) PolicyCount() map[string]int {
164164
policyCounters := make(map[string]int)
165-
165+
if !c.Config.CustomResourcesEnabled {
166+
return policyCounters
167+
}
166168
if c.Config.Policies == nil {
167169
return policyCounters
168170
}
169-
170171
policies := c.Config.Policies()
171-
if policies == nil {
172+
if len(policies) == 0 {
172173
return policyCounters
173174
}
174175

internal/telemetry/collector.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ type CollectorConfig struct {
8080

8181
// InstallationFlags represents the list of set flags managed by NIC
8282
InstallationFlags []string
83+
84+
// Indicates if using of Custom Resources is enabled.
85+
CustomResourcesEnabled bool
8386
}
8487

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

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

257-
policies := c.PolicyCount()
258-
259-
accessControlCount := policies["AccessControl"]
260-
rateLimitCount := policies["RateLimit"]
261-
jwtAuthCount := policies["JWTAuth"]
262-
basicAuthCount := policies["BasicAuth"]
263-
ingressMTLSCount := policies["IngressMTLS"]
264-
egressMTLSCount := policies["EgressMTLS"]
265-
oidcCount := policies["OIDC"]
266-
wafCount := policies["WAF"]
261+
var (
262+
accessControlCount int
263+
rateLimitCount int
264+
jwtAuthCount int
265+
basicAuthCount int
266+
ingressMTLSCount int
267+
egressMTLSCount int
268+
oidcCount int
269+
wafCount int
270+
)
271+
// Collect Custom Resources (Policies) only if CR enabled at startup.
272+
if c.Config.CustomResourcesEnabled {
273+
policies := c.PolicyCount()
274+
accessControlCount = policies["AccessControl"]
275+
rateLimitCount = policies["RateLimit"]
276+
jwtAuthCount = policies["JWTAuth"]
277+
basicAuthCount = policies["BasicAuth"]
278+
ingressMTLSCount = policies["IngressMTLS"]
279+
egressMTLSCount = policies["EgressMTLS"]
280+
oidcCount = policies["OIDC"]
281+
wafCount = policies["WAF"]
282+
}
267283

268284
ingressAnnotations := c.IngressAnnotations()
269-
270285
appProtectVersion := c.AppProtectVersion()
271-
272286
isPlus := c.IsPlusEnabled()
273-
274287
installationFlags := c.InstallationFlags()
275-
276288
serviceCounts, err := c.ServiceCounts()
277289
if err != nil {
278290
glog.V(3).Infof("Unable to collect telemetry data: Service Counts: %v", err)

0 commit comments

Comments
 (0)