-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
584 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
pkg/providers/ingress/translation/annotations/upstream/upstream.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations" | ||
apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1" | ||
) | ||
|
||
func NewParser() annotations.IngressAnnotationsParser { | ||
return &Upstream{} | ||
} | ||
|
||
type Upstream struct { | ||
Scheme string | ||
Retry int | ||
TimeoutRead int | ||
TimeoutConnect int | ||
TimeoutSend int | ||
} | ||
|
||
func (u *Upstream) Parse(e annotations.Extractor) (interface{}, error) { | ||
scheme := strings.ToLower(e.GetStringAnnotation(annotations.AnnotationsUpstreamScheme)) | ||
if scheme != "" { | ||
_, ok := apisixv1.ValidSchemes[scheme] | ||
if !ok { | ||
keys := make([]string, 0, len(apisixv1.ValidSchemes)) | ||
for key := range apisixv1.ValidSchemes { | ||
keys = append(keys, key) | ||
} | ||
return nil, fmt.Errorf("scheme %s is not supported, Only { %s } are supported", scheme, strings.Join(keys, ", ")) | ||
} | ||
u.Scheme = scheme | ||
} | ||
|
||
retry := e.GetStringAnnotation(annotations.AnnotationsUpstreamRetry) | ||
if retry != "" { | ||
t, err := strconv.Atoi(retry) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not parse retry as an integer: %s", err.Error()) | ||
} | ||
u.Retry = t | ||
} | ||
|
||
timeoutConnect := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutConnect), "s") | ||
if 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 | ||
} | ||
|
||
timeoutRead := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutRead), "s") | ||
if 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 | ||
} | ||
|
||
timeoutSend := strings.TrimSuffix(e.GetStringAnnotation(annotations.AnnotationsUpstreamTimeoutSend), "s") | ||
if 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
98
pkg/providers/ingress/translation/annotations/upstream/upstream_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations" | ||
"github.com/apache/apisix-ingress-controller/pkg/providers/ingress/translation/annotations/upstream" | ||
) | ||
|
||
func TestIPRestrictionHandler(t *testing.T) { | ||
anno := map[string]string{ | ||
annotations.AnnotationsUpstreamScheme: "grpcs", | ||
} | ||
u := upstream.NewParser() | ||
|
||
out, err := u.Parse(annotations.NewExtractor(anno)) | ||
ups, ok := out.(upstream.Upstream) | ||
if !ok { | ||
t.Fatalf("could not parse upstream") | ||
} | ||
assert.Nil(t, err, "checking given error") | ||
assert.Equal(t, "grpcs", ups.Scheme) | ||
|
||
anno[annotations.AnnotationsUpstreamScheme] = "gRPC" | ||
out, err = u.Parse(annotations.NewExtractor(anno)) | ||
ups, ok = out.(upstream.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 := upstream.NewParser() | ||
out, err := u.Parse(annotations.NewExtractor(anno)) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
ups, ok := out.(upstream.Upstream) | ||
if !ok { | ||
t.Fatalf("could not parse upstream") | ||
} | ||
assert.Nil(t, err, "checking given error") | ||
assert.Equal(t, 2, ups.Retry) | ||
|
||
anno[annotations.AnnotationsUpstreamRetry] = "asdf" | ||
out, err = u.Parse(annotations.NewExtractor(anno)) | ||
assert.NotNil(t, err, "checking given error") | ||
} | ||
|
||
func TestTimeoutParsing(t *testing.T) { | ||
anno := map[string]string{ | ||
annotations.AnnotationsUpstreamTimeoutConnect: "2s", | ||
annotations.AnnotationsUpstreamTimeoutRead: "3s", | ||
annotations.AnnotationsUpstreamTimeoutSend: "4s", | ||
} | ||
u := upstream.NewParser() | ||
out, err := u.Parse(annotations.NewExtractor(anno)) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
ups, ok := out.(upstream.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") | ||
} |
47 changes: 0 additions & 47 deletions
47
pkg/providers/ingress/translation/annotations/upstreamscheme/upstreamscheme.go
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.