-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DomainMapping Overriding the default HTTP behavior (#12786)
* DomainMapping Overriding the default HTTP behavior * fix lint issue * del usless domainmapping from ut * add comment * add log in http-protocol disabled and invalid case * reuse GetHTTPOption func * move GetHTTPOption to pkg/networking and add comment
- Loading branch information
Showing
4 changed files
with
144 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package networking | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
networkingpkg "knative.dev/networking/pkg" | ||
"knative.dev/networking/pkg/apis/networking" | ||
netv1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
// GetHTTPOption get http-protocol from resource annotations if not, get it from configmap config-network | ||
func GetHTTPOption(ctx context.Context, networkConfig *networkingpkg.Config, annotations map[string]string) (netv1alpha1.HTTPOption, error) { | ||
// Get HTTPOption via annotations. | ||
if len(annotations) != 0 && networking.GetHTTPProtocol(annotations) != "" { | ||
protocol := strings.ToLower(networking.GetHTTPProtocol(annotations)) | ||
switch networkingpkg.HTTPProtocol(protocol) { | ||
case networkingpkg.HTTPEnabled: | ||
return netv1alpha1.HTTPOptionEnabled, nil | ||
case networkingpkg.HTTPRedirected: | ||
return netv1alpha1.HTTPOptionRedirected, nil | ||
default: | ||
return "", fmt.Errorf("incorrect http-protocol annotation: " + protocol) | ||
} | ||
} | ||
|
||
// Get logger from context | ||
logger := logging.FromContext(ctx) | ||
|
||
// Get HTTPOption via config-network. | ||
switch httpProtocol := networkConfig.HTTPProtocol; httpProtocol { | ||
case networkingpkg.HTTPEnabled: | ||
return netv1alpha1.HTTPOptionEnabled, nil | ||
case networkingpkg.HTTPRedirected: | ||
return netv1alpha1.HTTPOptionRedirected, nil | ||
// This will be deprecated soon | ||
case networkingpkg.HTTPDisabled: | ||
logger.Warnf("http-protocol %s in config-network ConfigMap will be deprecated soon", httpProtocol) | ||
return "", nil | ||
default: | ||
logger.Warnf("http-protocol %s in config-network ConfigMap is not supported", httpProtocol) | ||
return "", nil | ||
} | ||
} |
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,90 @@ | ||
package networking | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
network "knative.dev/networking/pkg" | ||
networkingpkg "knative.dev/networking/pkg" | ||
"knative.dev/networking/pkg/apis/networking" | ||
netv1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1" | ||
logtesting "knative.dev/pkg/logging/testing" | ||
"knative.dev/serving/pkg/reconciler/domainmapping/config" | ||
) | ||
|
||
func TestGetHTTPOption(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
configHTTPProtocol network.HTTPProtocol | ||
annotationHTTPProtocol network.HTTPProtocol | ||
wantHTTPOption netv1alpha1.HTTPOption | ||
wantError error | ||
}{{ | ||
name: "HTTPProtocol enabled by config, enabled by annotation", | ||
configHTTPProtocol: network.HTTPEnabled, | ||
annotationHTTPProtocol: network.HTTPEnabled, | ||
wantHTTPOption: netv1alpha1.HTTPOptionEnabled, | ||
}, { | ||
name: "HTTPProtocol enabled by config, redirected by annotation", | ||
configHTTPProtocol: network.HTTPEnabled, | ||
annotationHTTPProtocol: network.HTTPRedirected, | ||
wantHTTPOption: netv1alpha1.HTTPOptionRedirected, | ||
}, { | ||
name: "HTTPProtocol enabled by config, invalid by annotation", | ||
configHTTPProtocol: network.HTTPEnabled, | ||
annotationHTTPProtocol: "foo", | ||
wantError: errors.New("incorrect http-protocol annotation: foo"), | ||
}, { | ||
name: "HTTPProtocol redirected by config, enabled by annotation", | ||
configHTTPProtocol: network.HTTPRedirected, | ||
annotationHTTPProtocol: network.HTTPEnabled, | ||
wantHTTPOption: netv1alpha1.HTTPOptionEnabled, | ||
}, { | ||
name: "HTTPProtocol redirected by config, redirected by annotation", | ||
configHTTPProtocol: network.HTTPRedirected, | ||
annotationHTTPProtocol: network.HTTPRedirected, | ||
wantHTTPOption: netv1alpha1.HTTPOptionRedirected, | ||
}, { | ||
name: "HTTPProtocol redirected by config, invalid by annotation", | ||
configHTTPProtocol: network.HTTPRedirected, | ||
annotationHTTPProtocol: "foo", | ||
wantError: errors.New("incorrect http-protocol annotation: foo"), | ||
}, { | ||
name: "HTTPProtocol enabled by config, nil annotations", | ||
configHTTPProtocol: network.HTTPEnabled, | ||
wantHTTPOption: netv1alpha1.HTTPOptionEnabled, | ||
}, { | ||
name: "HTTPProtocol redirected by config, nil annotations", | ||
configHTTPProtocol: network.HTTPRedirected, | ||
wantHTTPOption: netv1alpha1.HTTPOptionRedirected, | ||
}, { | ||
name: "HTTPProtocol disabled by config, nil annotations", | ||
configHTTPProtocol: network.HTTPDisabled, | ||
wantHTTPOption: "", | ||
}} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := logtesting.TestContextWithLogger(t) | ||
ctx = config.ToContext(ctx, &config.Config{ | ||
Network: &network.Config{ | ||
HTTPProtocol: tc.configHTTPProtocol, | ||
}, | ||
}) | ||
|
||
var annotations map[string]string | ||
if tc.annotationHTTPProtocol != "" { | ||
annotations = map[string]string{ | ||
networking.HTTPProtocolAnnotationKey: string(tc.annotationHTTPProtocol), | ||
} | ||
} | ||
|
||
got, err := GetHTTPOption(ctx, &networkingpkg.Config{HTTPProtocol: tc.configHTTPProtocol}, annotations) | ||
if tc.wantError != nil && fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.wantError) { | ||
t.Errorf("err = %s, want %v", err, tc.wantError) | ||
} | ||
if tc.wantError == nil && got != tc.wantHTTPOption { | ||
t.Errorf("GetHTTPOption = %s, want %s", got, tc.wantHTTPOption) | ||
} | ||
}) | ||
} | ||
} |
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