diff --git a/cmd/broker/main.go b/cmd/broker/main.go index fe811e8fb..c7d95bfca 100644 --- a/cmd/broker/main.go +++ b/cmd/broker/main.go @@ -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.CreateBindTimeout) } respWriter := httputil.NewResponseWriter(logs, cfg.DevelopmentMode) diff --git a/internal/broker/bind_create.go b/internal/broker/bind_create.go index 43a50a048..ddef4ac63 100644 --- a/internal/broker/bind_create.go +++ b/internal/broker/bind_create.go @@ -13,8 +13,8 @@ import ( broker "github.com/kyma-project/kyma-environment-broker/internal/broker/bindings" "github.com/kyma-project/kyma-environment-broker/internal/storage" "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" - "github.com/pivotal-cf/brokerapi/v8/domain/apiresponses" + "github.com/pivotal-cf/brokerapi/domain/apiresponses" "github.com/pivotal-cf/brokerapi/v8/domain" "github.com/sirupsen/logrus" ) @@ -30,7 +30,7 @@ type BindingConfig struct { MaxExpirationSeconds int `envconfig:"default=7200"` MinExpirationSeconds int `envconfig:"default=600"` MaxBindingsCount int `envconfig:"default=10"` - Timeout time.Duration `envconfig:"default=15s"` + CreateBindTimeout time.Duration `envconfig:"default=15s"` } type BindEndpoint struct { @@ -82,36 +82,7 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d b.log.Infof("Bind parameters: %s", string(details.RawParameters)) b.log.Infof("Bind context: %s", string(details.RawContext)) b.log.Infof("Bind asyncAllowed: %v", asyncAllowed) - ctx, cancel := context.WithCancel(ctx) - timer := time.NewTimer(b.config.Timeout) - defer timer.Stop() - - execResult := make(chan domain.Binding) - execError := make(chan error) - go func() { - defer cancel() - result, err := b.execute(ctx, instanceID, bindingID, details, asyncAllowed) - if err != nil { - execError <- err - } - execResult <- result - }() - - select { - case <-timer.C: - return domain.Binding{}, fmt.Errorf("timeout") - case result := <-execResult: - return result, nil - case err := <-execError: - if err != nil { - return domain.Binding{}, err - } - } - return domain.Binding{}, nil -} - -func (b *BindEndpoint) execute(ctx context.Context, instanceID, bindingID string, details domain.BindDetails, asyncAllowed bool) (domain.Binding, error) { if !b.config.Enabled { return domain.Binding{}, fmt.Errorf("not supported") } @@ -249,6 +220,8 @@ func (b *BindEndpoint) execute(ctx context.Context, instanceID, bindingID string ExpiresAt: binding.ExpiresAt.Format(expiresAtLayout), }, }, nil + + return domain.Binding{}, nil } func (b *BindEndpoint) IsPlanBindable(planName string) bool { diff --git a/internal/broker/bind_create_test.go b/internal/broker/bind_create_test.go index 08865c05e..0967a3e36 100644 --- a/internal/broker/bind_create_test.go +++ b/internal/broker/bind_create_test.go @@ -231,7 +231,7 @@ func TestCreateBindingTimeoutHappen(t *testing.T) { MaxExpirationSeconds: 7200, MinExpirationSeconds: 600, MaxBindingsCount: 10, - Timeout: time.Duration(1 * time.Nanosecond), + CreateBindTimeout: time.Duration(1 * time.Nanosecond), } svc := NewBind(*bindingCfg, st.Instances(), st.Bindings(), logrus.New(), nil, nil) result, err := svc.Bind(context.Background(), instanceID, bindingID, domain.BindDetails{}, false) diff --git a/internal/broker/server.go b/internal/broker/server.go index 3aabcd28e..3fa2e1336 100644 --- a/internal/broker/server.go +++ b/internal/broker/server.go @@ -3,6 +3,7 @@ package broker import ( "context" "net/http" + "time" "code.cloudfoundry.org/lager" "github.com/gorilla/mux" @@ -11,8 +12,16 @@ import ( "github.com/pivotal-cf/brokerapi/v8/middlewares" ) +type createBindApiHandler struct { + handler handlers.APIHandler +} + +func (h createBindApiHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + h.handler.Bind(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, createBindTimeout 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"))) @@ -27,7 +36,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(createBindApiHandler{}, createBindTimeout, "request timeout: time exceeded %s")).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")