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

Fix ratelimit-scaling for mergable ingresses #5727 #5728

Merged
merged 2 commits into from
Jun 11, 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: 20 additions & 18 deletions internal/configs/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,16 @@ func generateNginxCfgForMergeableIngresses(p NginxCfgParams) (version1.IngressNg
isMinion := false

masterNginxCfg, warnings := generateNginxCfg(NginxCfgParams{
staticParams: p.staticParams,
ingEx: p.mergeableIngs.Master,
apResources: p.apResources,
dosResource: p.dosResource,
isMinion: isMinion,
isPlus: p.isPlus,
baseCfgParams: p.baseCfgParams,
isResolverConfigured: p.isResolverConfigured,
isWildcardEnabled: p.isWildcardEnabled,
staticParams: p.staticParams,
ingEx: p.mergeableIngs.Master,
apResources: p.apResources,
dosResource: p.dosResource,
isMinion: isMinion,
isPlus: p.isPlus,
baseCfgParams: p.baseCfgParams,
isResolverConfigured: p.isResolverConfigured,
isWildcardEnabled: p.isWildcardEnabled,
ingressControllerReplicas: p.ingressControllerReplicas,
})

// because p.mergeableIngs.Master.Ingress is a deepcopy of the original master
Expand Down Expand Up @@ -690,15 +691,16 @@ func generateNginxCfgForMergeableIngresses(p NginxCfgParams) (version1.IngressNg
dummyApResources := &AppProtectResources{}
dummyDosResource := &appProtectDosResource{}
nginxCfg, minionWarnings := generateNginxCfg(NginxCfgParams{
staticParams: p.staticParams,
ingEx: minion,
apResources: dummyApResources,
dosResource: dummyDosResource,
isMinion: isMinion,
isPlus: p.isPlus,
baseCfgParams: p.baseCfgParams,
isResolverConfigured: p.isResolverConfigured,
isWildcardEnabled: p.isWildcardEnabled,
staticParams: p.staticParams,
ingEx: minion,
apResources: dummyApResources,
dosResource: dummyDosResource,
isMinion: isMinion,
isPlus: p.isPlus,
baseCfgParams: p.baseCfgParams,
isResolverConfigured: p.isResolverConfigured,
isWildcardEnabled: p.isWildcardEnabled,
ingressControllerReplicas: p.ingressControllerReplicas,
})
warnings.Add(minionWarnings)

Expand Down
89 changes: 89 additions & 0 deletions internal/configs/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,95 @@ func TestGenerateNginxCfgForLimitReqWithScaling(t *testing.T) {
}
}

func TestGenerateNginxCfgForMergeableIngressesForLimitReqWithScaling(t *testing.T) {
t.Parallel()
mergeableIngresses := createMergeableCafeIngress()

mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-rate"] = "200r/s"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-key"] = "${request_uri}"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-burst"] = "100"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-delay"] = "80"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-no-delay"] = "true"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-reject-code"] = "429"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-zone-size"] = "11m"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-dry-run"] = "true"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-log-level"] = "info"
mergeableIngresses.Minions[0].Ingress.Annotations["nginx.org/limit-req-scale"] = "true"

mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-rate"] = "400r/s"
mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-burst"] = "200"
mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-delay"] = "160"
mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-reject-code"] = "503"
mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-zone-size"] = "12m"
mergeableIngresses.Minions[1].Ingress.Annotations["nginx.org/limit-req-scale"] = "true"

expectedZones := []version1.LimitReqZone{
{
Name: "default/cafe-ingress-coffee-minion",
Key: "${request_uri}",
Size: "11m",
Rate: "100r/s",
},
{
Name: "default/cafe-ingress-tea-minion",
Key: "${binary_remote_addr}",
Size: "12m",
Rate: "200r/s",
},
}

expectedReqs := map[string]*version1.LimitReq{
"cafe-ingress-coffee-minion": {
Zone: "default/cafe-ingress-coffee-minion",
Burst: 100,
Delay: 80,
LogLevel: "info",
RejectCode: 429,
NoDelay: true,
DryRun: true,
},
"cafe-ingress-tea-minion": {
Zone: "default/cafe-ingress-tea-minion",
Burst: 200,
Delay: 160,
LogLevel: "error",
RejectCode: 503,
},
}

isPlus := false

configParams := NewDefaultConfigParams(isPlus)

result, warnings := generateNginxCfgForMergeableIngresses(NginxCfgParams{
mergeableIngs: mergeableIngresses,
baseCfgParams: configParams,
isPlus: isPlus,
staticParams: &StaticConfigParams{},
ingressControllerReplicas: 2,
})

if !reflect.DeepEqual(result.LimitReqZones, expectedZones) {
t.Errorf("generateNginxCfg returned \n%v, but expected \n%v", result.LimitReqZones, expectedZones)
}

for _, server := range result.Servers {
for _, location := range server.Locations {
expectedLimitReq := expectedReqs[location.MinionIngress.Name]
if !reflect.DeepEqual(location.LimitReq, expectedLimitReq) {
t.Errorf("generateNginxCfg returned \n%v, but expected \n%v", location.LimitReq, expectedLimitReq)
}
}
}

if !reflect.DeepEqual(result.LimitReqZones, expectedZones) {
t.Errorf("generateNginxCfg returned \n%v, but expected \n%v", result.LimitReqZones, expectedZones)
}
if len(warnings) != 0 {
t.Errorf("generateNginxCfg returned warnings: %v", warnings)
}
}

func createMergeableCafeIngress() *MergeableIngresses {
master := networking.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Expand Down
Loading