Skip to content

Commit

Permalink
add custom-http-errors field to crd (Azure#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
aamgayle authored Sep 3, 2024
1 parent 6d1a4b7 commit 473378a
Show file tree
Hide file tree
Showing 21 changed files with 3,552 additions and 112 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha1/nginxingresscontroller_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type NginxIngressControllerSpec struct {
// +optional
DefaultBackendService *NICNamespacedName `json:"defaultBackendService,omitempty"`

// CustomHTTPErrors defines the error codes that the NginxIngressController should send to its default-backend in case of error.
// +optional
CustomHTTPErrors []int32 `json:"customHTTPErrors,omitempty"`

// Scaling defines configuration options for how the Ingress Controller scales
// +optional
Scaling *Scaling `json:"scaling,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ spec:
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
customHTTPErrors:
description: CustomHTTPErrors defines the error codes that the NginxIngressController
should send to its default-backend in case of error.
items:
format: int32
type: integer
type: array
defaultBackendService:
description: DefaultBackendService defines the service that the NginxIngressController
should default to when given HTTP traffic with not matching known
Expand Down
12 changes: 12 additions & 0 deletions pkg/controller/nginxingress/nginx_ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/Azure/aks-app-routing-operator/pkg/controller/keyvault"
"net/url"
"strconv"
"time"

approutingv1alpha1 "github.com/Azure/aks-app-routing-operator/api/v1alpha1"
Expand Down Expand Up @@ -581,6 +582,17 @@ func ToNginxIngressConfig(nic *approutingv1alpha1.NginxIngressController, defaul
nginxIng.DefaultBackendService = nic.Spec.DefaultBackendService.Namespace + "/" + nic.Spec.DefaultBackendService.Name
}

if len(nic.Spec.CustomHTTPErrors) != 0 {
errStr := ""
for i, errCode := range nic.Spec.CustomHTTPErrors {
errStr += strconv.Itoa(int(errCode))
if i+1 < len(nic.Spec.CustomHTTPErrors) {
errStr += ","
}
}
nginxIng.CustomHTTPErrors = errStr
}

return nginxIng
}

Expand Down
90 changes: 90 additions & 0 deletions pkg/controller/nginxingress/nginx_ingress_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ func TestToNginxIngressConfig(t *testing.T) {
FakeDefaultSSLCertNoNamespace := getFakeDefaultSSLCert("fake", "")

FakeDefaultBackend := approutingv1alpha1.NICNamespacedName{"fakename", "fakenamespace"}
FakeCustomErrors := []int32{404, 503}
SingleCustomError := []int32{404}
EmptyCustomErrors := []int32{}

FakeCertWithForceSSLRedirectTrue := getFakeDefaultSSLCert("fake", "fakenamespace")
FakeCertWithForceSSLRedirectTrue.ForceSSLRedirect = true
Expand Down Expand Up @@ -1333,6 +1336,93 @@ func TestToNginxIngressConfig(t *testing.T) {
TargetCPUUtilizationPercentage: balancedTargetCPUUtilization,
},
},
{
name: "default controller class with DefaultBackendService and CustomHTTPErrors",
nic: &approutingv1alpha1.NginxIngressController{
TypeMeta: metav1.TypeMeta{
APIVersion: approutingv1alpha1.GroupVersion.String(),
Kind: "NginxIngressController",
},
ObjectMeta: metav1.ObjectMeta{
Name: DefaultNicName,
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
ControllerNamePrefix: DefaultNicResourceName,
IngressClassName: DefaultIcName,
DefaultBackendService: &FakeDefaultBackend,
CustomHTTPErrors: FakeCustomErrors,
},
},
want: manifests.NginxIngressConfig{
ControllerClass: defaultCc,
ResourceName: DefaultNicResourceName,
IcName: DefaultIcName,
ServiceConfig: &manifests.ServiceConfig{},
DefaultBackendService: FakeDefaultBackend.Namespace + "/" + FakeDefaultBackend.Name,
CustomHTTPErrors: "404,503",
MaxReplicas: defaultMaxReplicas,
MinReplicas: defaultMinReplicas,
TargetCPUUtilizationPercentage: balancedTargetCPUUtilization,
},
},
{
name: "default controller class with DefaultBackendService and a single CustomHTTPError",
nic: &approutingv1alpha1.NginxIngressController{
TypeMeta: metav1.TypeMeta{
APIVersion: approutingv1alpha1.GroupVersion.String(),
Kind: "NginxIngressController",
},
ObjectMeta: metav1.ObjectMeta{
Name: DefaultNicName,
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
ControllerNamePrefix: DefaultNicResourceName,
IngressClassName: DefaultIcName,
DefaultBackendService: &FakeDefaultBackend,
CustomHTTPErrors: SingleCustomError,
},
},
want: manifests.NginxIngressConfig{
ControllerClass: defaultCc,
ResourceName: DefaultNicResourceName,
IcName: DefaultIcName,
ServiceConfig: &manifests.ServiceConfig{},
DefaultBackendService: FakeDefaultBackend.Namespace + "/" + FakeDefaultBackend.Name,
CustomHTTPErrors: "404",
MaxReplicas: defaultMaxReplicas,
MinReplicas: defaultMinReplicas,
TargetCPUUtilizationPercentage: balancedTargetCPUUtilization,
},
},
{
name: "default controller class with DefaultBackendService and empty custom errors",
nic: &approutingv1alpha1.NginxIngressController{
TypeMeta: metav1.TypeMeta{
APIVersion: approutingv1alpha1.GroupVersion.String(),
Kind: "NginxIngressController",
},
ObjectMeta: metav1.ObjectMeta{
Name: DefaultNicName,
},
Spec: approutingv1alpha1.NginxIngressControllerSpec{
ControllerNamePrefix: DefaultNicResourceName,
IngressClassName: DefaultIcName,
DefaultBackendService: &FakeDefaultBackend,
CustomHTTPErrors: EmptyCustomErrors,
},
},
want: manifests.NginxIngressConfig{
ControllerClass: defaultCc,
ResourceName: DefaultNicResourceName,
IcName: DefaultIcName,
ServiceConfig: &manifests.ServiceConfig{},
DefaultBackendService: FakeDefaultBackend.Namespace + "/" + FakeDefaultBackend.Name,
CustomHTTPErrors: "",
MaxReplicas: defaultMaxReplicas,
MinReplicas: defaultMinReplicas,
TargetCPUUtilizationPercentage: balancedTargetCPUUtilization,
},
},
}

for _, c := range cases {
Expand Down
Loading

0 comments on commit 473378a

Please sign in to comment.