Skip to content
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
11 changes: 7 additions & 4 deletions internal/adc/translator/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ import (
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations"
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations/plugins"
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations/upstream"
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations/websocket"
)

// Structure extracted by Ingress Resource
type IngressConfig struct {
Upstream upstream.Upstream
Plugins adctypes.Plugins
Upstream upstream.Upstream
Plugins adctypes.Plugins
EnableWebsocket bool
}

var ingressAnnotationParsers = map[string]annotations.IngressAnnotationsParser{
"upstream": upstream.NewParser(),
"plugins": plugins.NewParser(),
"upstream": upstream.NewParser(),
"plugins": plugins.NewParser(),
"EnableWebsocket": websocket.NewParser(),
}

func (t *Translator) TranslateIngressAnnotations(anno map[string]string) *IngressConfig {
Expand Down
30 changes: 30 additions & 0 deletions internal/adc/translator/annotations/websocket/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package websocket

import (
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations"
)

type WebSocket struct{}

func NewParser() annotations.IngressAnnotationsParser {
return &WebSocket{}
}

func (w *WebSocket) Parse(e annotations.Extractor) (any, error) {
return e.GetBoolAnnotation(annotations.AnnotationsEnableWebSocket), nil
}
9 changes: 9 additions & 0 deletions internal/adc/translator/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ func TestTranslateIngressAnnotations(t *testing.T) {
},
},
},
{
name: "enable websocket",
anno: map[string]string{
annotations.AnnotationsEnableWebSocket: "true",
},
expected: &IngressConfig{
EnableWebsocket: true,
},
},
}

for _, tt := range tests {
Expand Down
5 changes: 4 additions & 1 deletion internal/adc/translator/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ func (t *Translator) buildServiceFromIngressPath(
service.Upstream = upstream

route := buildRouteFromIngressPath(obj, path, config, index, labels)
if protocol == internaltypes.AppProtocolWS || protocol == internaltypes.AppProtocolWSS {
// Check if websocket is enabled via annotation first, then fall back to appProtocol detection
if config != nil && config.EnableWebsocket {
route.EnableWebsocket = ptr.To(true)
} else if protocol == internaltypes.AppProtocolWS || protocol == internaltypes.AppProtocolWSS {
route.EnableWebsocket = ptr.To(true)
}
service.Routes = []*adctypes.Route{route}
Expand Down
1 change: 0 additions & 1 deletion internal/webhook/v1/ingress_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var ingresslog = logf.Log.WithName("ingress-resource")
// ref: https://apisix.apache.org/docs/ingress-controller/upgrade-guide/#limited-support-for-ingress-annotations
var unsupportedAnnotations = []string{
"k8s.apisix.apache.org/use-regex",
"k8s.apisix.apache.org/enable-websocket",
"k8s.apisix.apache.org/plugin-config-name",
"k8s.apisix.apache.org/enable-csrf",
"k8s.apisix.apache.org/csrf-key",
Expand Down
24 changes: 0 additions & 24 deletions internal/webhook/v1/ingress_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -64,29 +63,6 @@ func buildIngressValidator(t *testing.T, objects ...runtime.Object) *IngressCust
return NewIngressCustomValidator(builder.Build())
}

func TestIngressCustomValidator_ValidateCreate_UnsupportedAnnotations(t *testing.T) {
validator := buildIngressValidator(t)
obj := &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "default",
Annotations: map[string]string{
"k8s.apisix.apache.org/use-regex": "true",
"k8s.apisix.apache.org/enable-websocket": "true",
},
},
}

warnings, err := validator.ValidateCreate(context.TODO(), obj)
assert.NoError(t, err)
assert.Len(t, warnings, 2)

// Check that warnings contain the expected unsupported annotations
warningsStr := strings.Join(warnings, " ")
assert.Contains(t, warningsStr, "k8s.apisix.apache.org/use-regex")
assert.Contains(t, warningsStr, "k8s.apisix.apache.org/enable-websocket")
}

func TestIngressCustomValidator_ValidateCreate_SupportedAnnotations(t *testing.T) {
validator := buildIngressValidator(t)
obj := &networkingv1.Ingress{
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/ingress/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ spec:
port:
number: 80
`

ingressWebSocket = `
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: websocket
annotations:
k8s.apisix.apache.org/enable-websocket: "true"
spec:
ingressClassName: %s
rules:
- host: nginx.example
http:
paths:
- path: /ws
pathType: Exact
backend:
service:
name: nginx
port:
number: 80
`
)
BeforeEach(func() {
s.DeployNginx(framework.NginxOptions{
Expand Down Expand Up @@ -240,6 +262,15 @@ spec:
Expect(corsConfig["allow_methods"]).To(Equal("GET,POST"), "checking cors allow methods")
Expect(corsConfig["allow_headers"]).To(Equal("Origin,Authorization"), "checking cors allow headers")
})

It("websocket", func() {
Expect(s.CreateResourceFromString(fmt.Sprintf(ingressWebSocket, s.Namespace()))).ShouldNot(HaveOccurred(), "creating Ingress")

routes, err := s.DefaultDataplaneResource().Route().List(context.Background())
Expect(err).NotTo(HaveOccurred(), "listing Route")
Expect(routes).To(HaveLen(1), "checking Route length")
Expect(routes[0].EnableWebsocket).To(Equal(ptr.To(true)), "checking Route EnableWebsocket")
})
})

Context("Plugins", func() {
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/webhook/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ metadata:
namespace: %s
annotations:
k8s.apisix.apache.org/use-regex: "true"
k8s.apisix.apache.org/enable-websocket: "true"
spec:
ingressClassName: %s
rules:
Expand All @@ -76,7 +75,6 @@ spec:

output, err := s.CreateResourceFromStringAndGetOutput(ingressYAML)
Expect(err).ShouldNot(HaveOccurred())
Expect(output).To(ContainSubstring(`Warning: Annotation 'k8s.apisix.apache.org/enable-websocket' is not supported`))
Expect(output).To(ContainSubstring(`Warning: Annotation 'k8s.apisix.apache.org/use-regex' is not supported`))

s.RequestAssert(&scaffold.RequestAssert{
Expand Down
Loading