Skip to content

Commit

Permalink
Get Binding Endpoint (#1262)
Browse files Browse the repository at this point in the history
* Initial Test and Implementation

* Added Missing Instance ID in Get Binding Method

* Removed Unnecessary Field

* Added Missing Instance ID in Get Binding Method

* Get Binding Implementation Correction

* Insert Binding in Bind Create Endpoint

* Removed Unnecessary Field

* Added Missing Instance ID in Get Binding Method

* Added Missing Instance ID in Get Binding Method

* Get Binding Memory Implementation

* Missing Not Found Response

* Get Endpoint Test

* Linter and Missing Error Check

* Reverted Invalid Test Change

* Corrected Kubeconfig Assignment & Test Correction

* Removed TODO

* Linter

* Review Correction

#1262 (comment)

* Review Corrections

#1262 (comment)

* Code Review

#1262 (comment)

* Update internal/broker/bind_create.go

Co-authored-by: Jarosław Pieszka <[email protected]>

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

#1262 (comment)

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

* Review Remarks

#1262 (comment)

* Mock DB Correction

* Review Remarks - Additional Test Case

#1262 (comment)

* Linter

* Review Remarks

* Linter

* Review Remarks

* Linter

* Assertions Corrections

* Assertions Corrections

* Assertion Corrections

* Review Remarks

* Incorporate DB in Binding Selection E2E Test

---------

Co-authored-by: Jarosław Pieszka <[email protected]>
  • Loading branch information
ralikio and jaroslaw-pieszka authored Oct 10, 2024
1 parent f8cb6dd commit b75f66b
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 97 deletions.
4 changes: 2 additions & 2 deletions cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,9 @@ func createAPI(router *mux.Router, servicesConfig broker.ServicesConfig, planVal
planDefaults, logs, cfg.KymaDashboardConfig, kcBuilder, convergedCloudRegionProvider, kcpK8sClient),
GetInstanceEndpoint: broker.NewGetInstance(cfg.Broker, db.Instances(), db.Operations(), kcBuilder, logs),
LastOperationEndpoint: broker.NewLastOperation(db.Operations(), db.InstancesArchived(), logs),
BindEndpoint: broker.NewBind(cfg.Broker.Binding, db.Instances(), logs, clientProvider, kubeconfigProvider, gardenerClient),
BindEndpoint: broker.NewBind(cfg.Broker.Binding, db.Instances(), db.Bindings(), logs, clientProvider, kubeconfigProvider, gardenerClient),
UnbindEndpoint: broker.NewUnbind(logs),
GetBindingEndpoint: broker.NewGetBinding(logs),
GetBindingEndpoint: broker.NewGetBinding(logs, db.Bindings()),
LastBindingOperationEndpoint: broker.NewLastBindingOperation(logs),
}

Expand Down
34 changes: 30 additions & 4 deletions internal/broker/bind_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/kyma-project/kyma-environment-broker/internal"
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"
Expand All @@ -29,6 +31,7 @@ type BindingConfig struct {
type BindEndpoint struct {
config BindingConfig
instancesStorage storage.Instances
bindingsStorage storage.Bindings

serviceAccountBindingManager broker.BindingsManager
gardenerBindingsManager broker.BindingsManager
Expand All @@ -45,8 +48,11 @@ type Credentials struct {
Kubeconfig string `json:"kubeconfig"`
}

func NewBind(cfg BindingConfig, instanceStorage storage.Instances, log logrus.FieldLogger, clientProvider broker.ClientProvider, kubeconfigProvider broker.KubeconfigProvider, gardenerClient client.Client) *BindEndpoint {
return &BindEndpoint{config: cfg, instancesStorage: instanceStorage, log: log.WithField("service", "BindEndpoint"),
func NewBind(cfg BindingConfig, instanceStorage storage.Instances, bindingsStorage storage.Bindings, log logrus.FieldLogger, clientProvider broker.ClientProvider, kubeconfigProvider broker.KubeconfigProvider, gardenerClient client.Client) *BindEndpoint {
return &BindEndpoint{config: cfg,
instancesStorage: instanceStorage,
bindingsStorage: bindingsStorage,
log: log.WithField("service", "BindEndpoint"),
serviceAccountBindingManager: broker.NewServiceAccountBindingsManager(clientProvider, kubeconfigProvider),
gardenerBindingsManager: broker.NewGardenerBindingManager(gardenerClient),
}
Expand Down Expand Up @@ -112,19 +118,39 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d
}

var kubeconfig string
binding := &internal.Binding{
ID: bindingID,
InstanceID: instanceID,

CreatedAt: time.Now(),
UpdatedAt: time.Now(),

ExpirationSeconds: int64(expirationSeconds),
}
if parameters.ServiceAccount {
// get kubeconfig for the instance
kubeconfig, err = b.serviceAccountBindingManager.Create(ctx, instance, bindingID, expirationSeconds)
if err != nil {
message := fmt.Sprintf("failed to create kyma binding for service account using token request: %s", err)
message := fmt.Sprintf("failed to create a Kyma binding using service account's kubeconfig: %s", err)
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message)
}
binding.BindingType = internal.BINDING_TYPE_SERVICE_ACCOUNT
} else {
kubeconfig, err = b.gardenerBindingsManager.Create(ctx, instance, bindingID, expirationSeconds)
if err != nil {
message := fmt.Sprintf("failed to create kyma binding using adminkubeconfig gardener subresource: %s", err)
message := fmt.Sprintf("failed to create a Kyma binding using adminkubeconfig gardener subresource: %s", err)
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message)
}
binding.BindingType = internal.BINDING_TYPE_ADMIN_KUBECONFIG
}

binding.Kubeconfig = kubeconfig

err = b.bindingsStorage.Insert(binding)
if err != nil {
message := fmt.Sprintf("failed to insert Kyma binding into storage: %s", err)
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusInternalServerError, message)

}

return domain.Binding{
Expand Down
Loading

0 comments on commit b75f66b

Please sign in to comment.