-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR: add annotation on Service and Ingress for timeout server
- Loading branch information
1 parent
5da8480
commit e2f797c
Showing
7 changed files
with
309 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2023 HAProxy Technologies LLC | ||
// | ||
// Licensed 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, softwarehaproxyConfig | ||
// 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 timeoutserver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/haproxytech/kubernetes-ingress/deploy/tests/integration" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/k8s" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/store" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
var ( | ||
appNs = "appNs" | ||
serviceName = "appSvcName" | ||
ingressName = "appIngName" | ||
configMapNamespace = "haproxy-controller" | ||
configMapName = "haproxy-kubernetes-ingress" | ||
) | ||
|
||
type TimeoutServerSuite struct { | ||
integration.BaseSuite | ||
} | ||
|
||
func TestTimeoutServer(t *testing.T) { | ||
t.Parallel() | ||
suite.Run(t, new(TimeoutServerSuite)) | ||
} | ||
|
||
func (suite *TimeoutServerSuite) BeforeTest(suiteName, testName string) { | ||
suite.BaseSuite.BeforeTest(suiteName, testName) | ||
// Add any needed update to the controller setting | ||
// by updating suite.TestControllers[suite.T().Name()].XXXXX | ||
testController := suite.TestControllers[suite.T().Name()] | ||
testController.OSArgs.ConfigMap.Name = configMapName | ||
testController.OSArgs.ConfigMap.Namespace = configMapNamespace | ||
} | ||
|
||
func newConfigMap() *store.ConfigMap { | ||
return &store.ConfigMap{ | ||
Annotations: map[string]string{}, | ||
Namespace: configMapNamespace, | ||
Name: configMapName, | ||
Status: store.ADDED, | ||
} | ||
} | ||
|
||
func (suite *TimeoutServerSuite) setupTest() *store.ConfigMap { | ||
testController := suite.TestControllers[suite.T().Name()] | ||
|
||
ns := store.Namespace{Name: appNs, Status: store.ADDED} | ||
cm := newConfigMap() | ||
testController.EventChan <- k8s.SyncDataEvent{SyncType: k8s.NAMESPACE, Namespace: ns.Name, Data: &ns} | ||
testController.EventChan <- k8s.SyncDataEvent{ | ||
SyncType: k8s.CONFIGMAP, Namespace: configMapNamespace, Name: configMapName, Data: newConfigMap(), | ||
} | ||
testController.EventChan <- k8s.SyncDataEvent{SyncType: k8s.COMMAND} | ||
controllerHasWorked := make(chan struct{}) | ||
testController.EventChan <- k8s.SyncDataEvent{SyncType: k8s.COMMAND, EventProcessed: controllerHasWorked} | ||
<-controllerHasWorked | ||
return cm | ||
} | ||
|
||
func (suite *TimeoutServerSuite) fixture(events ...k8s.SyncDataEvent) { | ||
testController := suite.TestControllers[suite.T().Name()] | ||
|
||
// Now sending store events for test setup | ||
for _, e := range events { | ||
testController.EventChan <- e | ||
} | ||
testController.EventChan <- k8s.SyncDataEvent{SyncType: k8s.COMMAND} | ||
controllerHasWorked := make(chan struct{}) | ||
testController.EventChan <- k8s.SyncDataEvent{SyncType: k8s.COMMAND, EventProcessed: controllerHasWorked} | ||
<-controllerHasWorked | ||
} |
166 changes: 166 additions & 0 deletions
166
deploy/tests/integration/timeout-server/timeoutserver_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,166 @@ | ||
// Copyright 2019 HAProxy Technologies LLC | ||
// | ||
// Licensed 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 timeoutserver | ||
|
||
import ( | ||
"github.com/haproxytech/kubernetes-ingress/pkg/k8s" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/store" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
) | ||
|
||
func (suite *TimeoutServerSuite) TestTimeoutServerConfigMap() { | ||
// name := suite.T().Name() | ||
// testController := suite.TestControllers[name] | ||
|
||
suite.StartController() | ||
cm := suite.setupTest() | ||
|
||
/////////////////////////////////////// | ||
// timeout server setup in: | ||
// - configmap only | ||
cm.Status = store.MODIFIED | ||
cm.Annotations["timeout-server"] = "77000" | ||
svc := newAppSvc() | ||
ing := newAppIngress() | ||
events := []k8s.SyncDataEvent{ | ||
{SyncType: k8s.CONFIGMAP, Namespace: configMapNamespace, Name: configMapName, Data: cm}, | ||
{SyncType: k8s.SERVICE, Namespace: appNs, Name: serviceName, Data: svc}, | ||
{SyncType: k8s.INGRESS, Namespace: appNs, Name: ingressName, Data: ing}, | ||
} | ||
suite.fixture(events...) | ||
// Expected occurrences of "timeout server 77000" | ||
// 1- defaults | ||
// 2- backend haproxy-controller_default-local-service_http | ||
// 3- backend appNs_appSvcName_https | ||
suite.ExpectHaproxyConfigContains("timeout server 77000", 3) | ||
|
||
suite.StopController() | ||
} | ||
|
||
func (suite *TimeoutServerSuite) TestTimeoutServerService() { | ||
// name := suite.T().Name() | ||
// testController := suite.TestControllers[name] | ||
|
||
suite.StartController() | ||
cm := suite.setupTest() | ||
|
||
/////////////////////////////////////// | ||
// timeout server setup in: | ||
// - configmap (77000) | ||
// - ingress (76000) | ||
// - app svc (75000) (appNs_appSvcName_https) | ||
cm.Status = store.MODIFIED | ||
cm.Annotations["timeout-server"] = "77000" | ||
ing := newAppIngress() | ||
ing.Annotations["timeout-server"] = "76000" | ||
svc := newAppSvc() | ||
svc.Annotations["timeout-server"] = "75000" | ||
|
||
events := []k8s.SyncDataEvent{ | ||
{SyncType: k8s.CONFIGMAP, Namespace: configMapNamespace, Name: configMapName, Data: cm}, | ||
{SyncType: k8s.SERVICE, Namespace: appNs, Name: serviceName, Data: svc}, | ||
{SyncType: k8s.INGRESS, Namespace: appNs, Name: ingressName, Data: ing}, | ||
} | ||
suite.fixture(events...) | ||
// Expected occurrences of "timeout server 77000": #2 | ||
// 1- defaults | ||
// 2- backend haproxy-controller_default-local-service_http | ||
// Expected occurrences of "timeout server 75000": #1 (from service) | ||
// 1- backend appNs_appSvcName_https | ||
// Expected occurrences of "timeout server 76000": #0 (from ingress) | ||
suite.ExpectHaproxyConfigContains("timeout server 77000", 2) | ||
suite.ExpectHaproxyConfigContains("timeout server 75000", 1) | ||
suite.ExpectHaproxyConfigContains("timeout server 76000", 0) | ||
|
||
suite.StopController() | ||
} | ||
|
||
func (suite *TimeoutServerSuite) TestTimeoutServerIngress() { | ||
// name := suite.T().Name() | ||
// testController := suite.TestControllers[name] | ||
|
||
suite.StartController() | ||
cm := suite.setupTest() | ||
|
||
/////////////////////////////////////// | ||
// timeout server setup in: | ||
// - configmap (77000) | ||
// - ingress (76000) | ||
cm.Status = store.MODIFIED | ||
cm.Annotations["timeout-server"] = "77000" | ||
ing := newAppIngress() | ||
ing.Annotations["timeout-server"] = "76000" | ||
svc := newAppSvc() | ||
|
||
events := []k8s.SyncDataEvent{ | ||
{SyncType: k8s.CONFIGMAP, Namespace: configMapNamespace, Name: configMapName, Data: cm}, | ||
{SyncType: k8s.SERVICE, Namespace: appNs, Name: serviceName, Data: svc}, | ||
{SyncType: k8s.INGRESS, Namespace: appNs, Name: ingressName, Data: ing}, | ||
} | ||
suite.fixture(events...) | ||
// Expected occurrences of "timeout server 77000": #2 | ||
// 1- defaults | ||
// 2- backend haproxy-controller_default-local-service_http | ||
// Expected occurrences of "timeout server 75000": #0 (from service) | ||
// Expected occurrences of "timeout server 76000": #1 (from ingress) | ||
// 1- backend appNs_appSvcName_https | ||
suite.ExpectHaproxyConfigContains("timeout server 77000", 2) | ||
suite.ExpectHaproxyConfigContains("timeout server 75000", 0) | ||
suite.ExpectHaproxyConfigContains("timeout server 76000", 1) | ||
|
||
suite.StopController() | ||
} | ||
|
||
func newAppSvc() *store.Service { | ||
return &store.Service{ | ||
Annotations: map[string]string{}, | ||
Name: serviceName, | ||
Namespace: appNs, | ||
Ports: []store.ServicePort{ | ||
{ | ||
Name: "https", | ||
Protocol: "TCP", | ||
Port: 443, | ||
Status: store.ADDED, | ||
}, | ||
}, | ||
Status: store.ADDED, | ||
} | ||
} | ||
|
||
func newAppIngress() *store.Ingress { | ||
return &store.Ingress{ | ||
IngressCore: store.IngressCore{ | ||
APIVersion: store.NETWORKINGV1, | ||
Name: ingressName, | ||
Namespace: appNs, | ||
Annotations: map[string]string{}, | ||
Rules: map[string]*store.IngressRule{ | ||
"": { | ||
Paths: map[string]*store.IngressPath{ | ||
string(networkingv1.PathTypePrefix) + "-/": { | ||
Path: "/", | ||
PathTypeMatch: string(networkingv1.PathTypePrefix), | ||
SvcNamespace: appNs, | ||
SvcPortString: "https", | ||
SvcName: serviceName, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Status: store.ADDED, | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/haproxytech/client-native/v3/models" | ||
|
||
"github.com/haproxytech/kubernetes-ingress/pkg/annotations/common" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/store" | ||
"github.com/haproxytech/kubernetes-ingress/pkg/utils" | ||
) | ||
|
||
type TimeoutServer struct { | ||
backend *models.Backend | ||
name string | ||
} | ||
|
||
func NewTimeoutServer(n string, b *models.Backend) *TimeoutServer { | ||
return &TimeoutServer{name: n, backend: b} | ||
} | ||
|
||
func (a *TimeoutServer) GetName() string { | ||
return a.name | ||
} | ||
|
||
func (a *TimeoutServer) Process(k store.K8s, annotations ...map[string]string) error { | ||
input := common.GetValue(a.GetName(), annotations...) | ||
if input == "" { | ||
a.backend.ServerTimeout = nil | ||
return nil | ||
} | ||
timeout, err := utils.ParseTime(input) | ||
if err != nil { | ||
return err | ||
} | ||
a.backend.ServerTimeout = timeout | ||
return nil | ||
} |