Skip to content
Open
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
69 changes: 69 additions & 0 deletions internal/adc/translator/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 translator

import (
"errors"
"fmt"

"github.com/imdario/mergo"

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

// Structure extracted by Ingress Resource
type IngressConfig struct {
Upstream upstream.Upstream
}

// parsers registered for ingress annotations
var ingressAnnotationParsers = map[string]annotations.IngressAnnotationsParser{
"upstream": upstream.NewParser(),
}

func (t *Translator) TranslateIngressAnnotations(anno map[string]string) *IngressConfig {
if len(anno) == 0 {
return nil
}
ing := &IngressConfig{}
if err := translateAnnotations(anno, ing); err != nil {
t.Log.Error(err, "failed to translate ingress annotations", "annotations", anno)
}
return ing
}

func translateAnnotations(anno map[string]string, dst any) error {
extractor := annotations.NewExtractor(anno)
data := make(map[string]any)
var errs []error

for name, parser := range ingressAnnotationParsers {
out, err := parser.Parse(extractor)
if err != nil {
errs = append(errs, fmt.Errorf("parse %s: %w", name, err))
continue
}
if out != nil {
data[name] = out
}
}

if err := mergo.MapWithOverwrite(dst, data); err != nil {
errs = append(errs, fmt.Errorf("merge: %w", err))
}
return errors.Join(errs...)
}
88 changes: 88 additions & 0 deletions internal/adc/translator/annotations/upstream/upstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 upstream

import (
"fmt"
"strconv"
"strings"

apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations"
)

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

type Upstream struct {
Scheme string
Retries int
TimeoutRead int
TimeoutConnect int
TimeoutSend int
}

var validSchemes = map[string]struct{}{
apiv2.SchemeHTTP: {},
apiv2.SchemeHTTPS: {},
apiv2.SchemeGRPC: {},
apiv2.SchemeGRPCS: {},
}

func (u Upstream) Parse(e annotations.Extractor) (any, error) {
if scheme := strings.ToLower(e.GetStringAnnotation(annotations.AnnotationsUpstreamScheme)); scheme != "" {
if _, ok := validSchemes[scheme]; ok {
u.Scheme = scheme
} else {
return nil, fmt.Errorf("invalid upstream scheme: %s", scheme)
}
}

if retry := e.GetStringAnnotation(annotations.AnnotationsUpstreamRetry); retry != "" {
t, err := strconv.Atoi(retry)
if err != nil {
return nil, fmt.Errorf("could not parse retry as an integer: %s", err.Error())
}
u.Retries = t
}

if timeoutConnect := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutConnect), "s"); timeoutConnect != "" {
t, err := strconv.Atoi(timeoutConnect)
if err != nil {
return nil, fmt.Errorf("could not parse timeout as an integer: %s", err.Error())
}
u.TimeoutConnect = t
}

if timeoutRead := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutRead), "s"); timeoutRead != "" {
t, err := strconv.Atoi(timeoutRead)
if err != nil {
return nil, fmt.Errorf("could not parse timeout as an integer: %s", err.Error())
}
u.TimeoutRead = t
}

if timeoutSend := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutSend), "s"); timeoutSend != "" {
t, err := strconv.Atoi(timeoutSend)
if err != nil {
return nil, fmt.Errorf("could not parse timeout as an integer: %s", err.Error())
}
u.TimeoutSend = t
}

return u, nil
}
98 changes: 98 additions & 0 deletions internal/adc/translator/annotations/upstream/upstream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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 upstream

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestIPRestrictionHandler(t *testing.T) {
anno := map[string]string{
annotations.AnnotationsUpstreamScheme: "grpcs",
}
u := NewParser()

out, err := u.Parse(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")

ups, ok := out.(Upstream)
if !ok {
t.Fatalf("could not parse upstream")
}
assert.Equal(t, "grpcs", ups.Scheme)

anno[annotations.AnnotationsUpstreamScheme] = "gRPC"
out, err = u.Parse(annotations.NewExtractor(anno))
ups, ok = out.(Upstream)
if !ok {
t.Fatalf("could not parse upstream")
}
assert.Nil(t, err, "checking given error")
assert.Equal(t, "grpc", ups.Scheme)

anno[annotations.AnnotationsUpstreamScheme] = "nothing"
out, err = u.Parse(annotations.NewExtractor(anno))
assert.NotNil(t, err, "checking given error")
assert.Nil(t, out, "checking given output")
}

func TestRetryParsing(t *testing.T) {
anno := map[string]string{
annotations.AnnotationsUpstreamRetry: "2",
}
u := NewParser()
out, err := u.Parse(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")
ups, ok := out.(Upstream)
if !ok {
t.Fatalf("could not parse upstream")
}
assert.Nil(t, err, "checking given error")
assert.Equal(t, 2, ups.Retries)

anno[annotations.AnnotationsUpstreamRetry] = "asdf"
out, err = u.Parse(annotations.NewExtractor(anno))
assert.NotNil(t, err, "checking given error")
assert.Nil(t, out, "checking given output")
}

func TestTimeoutParsing(t *testing.T) {
anno := map[string]string{
annotations.AnnotationsUpstreamTimeoutConnect: "2s",
annotations.AnnotationsUpstreamTimeoutRead: "3s",
annotations.AnnotationsUpstreamTimeoutSend: "4s",
}
u := NewParser()
out, err := u.Parse(annotations.NewExtractor(anno))
assert.Nil(t, err, "checking given error")

ups, ok := out.(Upstream)
if !ok {
t.Fatalf("could not parse upstream")
}
assert.Nil(t, err, "checking given error")
assert.Equal(t, 2, ups.TimeoutConnect)
assert.Equal(t, 3, ups.TimeoutRead)
assert.Equal(t, 4, ups.TimeoutSend)
anno[annotations.AnnotationsUpstreamRetry] = "asdf"
out, err = u.Parse(annotations.NewExtractor(anno))
assert.NotNil(t, err, "checking given error")
assert.Nil(t, out, "checking given output")
}
Loading