Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout of creation Service Binding #1388

Merged
merged 22 commits into from
Oct 31, 2024
19 changes: 19 additions & 0 deletions cmd/broker/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -261,3 +262,21 @@ func TestRemoveBindingsFromSuspended(t *testing.T) {
response = suite.CallAPI(http.MethodDelete, fmt.Sprintf("oauth/v2/service_instances/%s/service_bindings/%s?plan_id=361c511f-f939-4621-b228-d0fb79a1fe15&service_id=47c9dcbf-ff30-448e-ab36-d3bad66ba281", iid, bindingID2), "")
assert.Equal(t, http.StatusOK, response.StatusCode)
}

func TestBindingCreationTimeout(t *testing.T) {
// given
cfg := fixConfig()
cfg.Broker.Binding.CreateBindingTimeout = 1 * time.Nanosecond
suite := NewBrokerSuiteTestWithConfig(t, cfg)
defer suite.TearDown()
iid := uuid.New().String()
bid := uuid.New().String()

resp := suite.CallAPI(http.MethodPut, fmt.Sprintf("oauth/v2/service_instances/%s/service_bindings/%s", iid, bid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "361c511f-f939-4621-b228-d0fb79a1fe15",
"parameters": {}
}`)
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
}
2 changes: 1 addition & 1 deletion cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func createAPI(router *mux.Router, servicesConfig broker.ServicesConfig, planVal
"/oauth/{region}/", // oauth2 handled by Ory with region
} {
route := router.PathPrefix(prefix).Subrouter()
broker.AttachRoutes(route, kymaEnvBroker, logger)
broker.AttachRoutes(route, kymaEnvBroker, logger, cfg.Broker.Binding.CreateBindingTimeout)
}

respWriter := httputil.NewResponseWriter(logs, cfg.DevelopmentMode)
Expand Down
1 change: 1 addition & 0 deletions cmd/broker/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ func fixConfig() *Config {
MaxExpirationSeconds: 7200,
MinExpirationSeconds: 600,
MaxBindingsCount: 10,
CreateBindingTimeout: 15 * time.Second,
},
AllowUpdateExpiredInstanceWithContext: true,
KimConfig: broker.KimConfig{
Expand Down
13 changes: 7 additions & 6 deletions internal/broker/bind_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ const (
)

type BindingConfig struct {
Enabled bool `envconfig:"default=false"`
BindablePlans EnablePlans `envconfig:"default=aws"`
ExpirationSeconds int `envconfig:"default=600"`
MaxExpirationSeconds int `envconfig:"default=7200"`
MinExpirationSeconds int `envconfig:"default=600"`
MaxBindingsCount int `envconfig:"default=10"`
Enabled bool `envconfig:"default=false"`
BindablePlans EnablePlans `envconfig:"default=aws"`
ExpirationSeconds int `envconfig:"default=600"`
MaxExpirationSeconds int `envconfig:"default=7200"`
MinExpirationSeconds int `envconfig:"default=600"`
MaxBindingsCount int `envconfig:"default=10"`
CreateBindingTimeout time.Duration `envconfig:"default=15s"`
}

type BindEndpoint struct {
Expand Down
14 changes: 12 additions & 2 deletions internal/broker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package broker

import (
"context"
"fmt"
"net/http"
"time"

"code.cloudfoundry.org/lager"
"github.com/gorilla/mux"
Expand All @@ -11,8 +13,16 @@ import (
"github.com/pivotal-cf/brokerapi/v8/middlewares"
)

type CreateBindingHandler struct {
handler func(w http.ResponseWriter, req *http.Request)
}

func (h CreateBindingHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
h.handler(rw, r)
}

// copied from github.com/pivotal-cf/brokerapi/api.go
func AttachRoutes(router *mux.Router, serviceBroker domain.ServiceBroker, logger lager.Logger) *mux.Router {
func AttachRoutes(router *mux.Router, serviceBroker domain.ServiceBroker, logger lager.Logger, createBindingTimeout time.Duration) *mux.Router {
apiHandler := handlers.NewApiHandler(serviceBroker, logger)
deprovision := func(w http.ResponseWriter, req *http.Request) {
req2 := req.WithContext(context.WithValue(req.Context(), "User-Agent", req.Header.Get("User-Agent")))
Expand All @@ -27,7 +37,7 @@ func AttachRoutes(router *mux.Router, serviceBroker domain.ServiceBroker, logger
router.HandleFunc("/v2/service_instances/{instance_id}", apiHandler.Update).Methods("PATCH")

router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.GetBinding).Methods("GET")
router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.Bind).Methods("PUT")
router.Handle("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", http.TimeoutHandler(CreateBindingHandler{apiHandler.Bind}, createBindingTimeout, fmt.Sprintf("request timeout: time exceeded %s", createBindingTimeout))).Methods("PUT")
router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}", apiHandler.Unbind).Methods("DELETE")

router.HandleFunc("/v2/service_instances/{instance_id}/service_bindings/{binding_id}/last_operation", apiHandler.LastBindingOperation).Methods("GET")
Expand Down
2 changes: 2 additions & 0 deletions resources/keb/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ spec:
value: "{{ .Values.binding.maxExpirationSeconds}}"
- name: APP_BROKER_BINDING_MAX_BINDINGS_COUNT
value: "{{ .Values.binding.maxBindingsCount}}"
- name: APP_BROKER_BINDING_CREATE_BINDING_TIMEOUT
value: "{{ .Values.binding.createBindingTimeout}}"
- name: APP_BROKER_ONLY_SINGLE_TRIAL_PER_GA
value: "{{ .Values.onlySingleTrialPerGA }}"
- name: APP_BROKER_URL
Expand Down
1 change: 1 addition & 0 deletions resources/keb/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ binding:
# minExpirationSeconds can't be lower than 600 seconds. Forced by Gardener
minExpirationSeconds: 600
maxBindingsCount: 10
createBindingTimeout: 15s

service:
type: ClusterIP
Expand Down
Loading