Skip to content

Commit

Permalink
Merge branch 'main' into maintenance/fix-sb-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslaw-pieszka authored Dec 12, 2024
2 parents 3dd69cb + c0fd6d2 commit 515920d
Show file tree
Hide file tree
Showing 27 changed files with 339 additions and 296 deletions.
6 changes: 3 additions & 3 deletions cmd/broker/bindings_envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ func TestCreateBindingEndpoint(t *testing.T) {
publisher := event.NewPubSub(log)

//// api handler
bindEndpoint := broker.NewBind(*bindingCfg, db, logs, skrK8sClientProvider, skrK8sClientProvider, publisher)
getBindingEndpoint := broker.NewGetBinding(logs, db)
unbindEndpoint := broker.NewUnbind(logs, db, brokerBindings.NewServiceAccountBindingsManager(skrK8sClientProvider, skrK8sClientProvider), publisher)
bindEndpoint := broker.NewBind(*bindingCfg, db, log, skrK8sClientProvider, skrK8sClientProvider, publisher)
getBindingEndpoint := broker.NewGetBinding(log, db)
unbindEndpoint := broker.NewUnbind(log, db, brokerBindings.NewServiceAccountBindingsManager(skrK8sClientProvider, skrK8sClientProvider), publisher)
apiHandler := handlers.NewApiHandler(broker.KymaEnvironmentBroker{
ServicesEndpoint: nil,
ProvisionEndpoint: nil,
Expand Down
2 changes: 1 addition & 1 deletion cmd/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (s *BrokerSuiteTest) CreateAPI(inputFactory broker.PlanValidator, cfg *Conf
kcBuilder := &kcMock.KcBuilder{}
kcBuilder.On("Build", nil).Return("--kubeconfig file", nil)
createAPI(s.router, servicesConfig, inputFactory, cfg, db, provisioningQueue, deprovisionQueue, updateQueue,
lager.NewLogger("api"), logs, log, planDefaults, kcBuilder, skrK8sClientProvider, skrK8sClientProvider, gardenerClient, fakeKcpK8sClient, eventBroker)
lager.NewLogger("api"), log, planDefaults, kcBuilder, skrK8sClientProvider, skrK8sClientProvider, gardenerClient, fakeKcpK8sClient, eventBroker)

s.httpServer = httptest.NewServer(s.router)
}
Expand Down
30 changes: 9 additions & 21 deletions cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import (
"github.com/kyma-project/kyma-environment-broker/internal/swagger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -218,14 +217,6 @@ func main() {
defer cancel()

// set default formatted
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
})
logs := logrus.New()
logs.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
})

logLevel := new(slog.LevelVar)
logLevel.Set(slog.LevelInfo)
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Expand All @@ -239,9 +230,6 @@ func main() {
fatalOnError(err, log)

if cfg.LogLevel != "" {
l, _ := logrus.ParseLevel(cfg.LogLevel)
logs.SetLevel(l)

logLevel.Set(cfg.getLogLevel())
}

Expand Down Expand Up @@ -344,7 +332,7 @@ func main() {

// create server
router := mux.NewRouter()
createAPI(router, servicesConfig, inputFactory, &cfg, db, provisionQueue, deprovisionQueue, updateQueue, logger, logs, log,
createAPI(router, servicesConfig, inputFactory, &cfg, db, provisionQueue, deprovisionQueue, updateQueue, logger, log,
inputFactory.GetPlanDefaults, kcBuilder, skrK8sClientProvider, skrK8sClientProvider, gardenerClient, kcpK8sClient, eventBroker)

// create metrics endpoint
Expand Down Expand Up @@ -423,28 +411,28 @@ func logConfiguration(logs *slog.Logger, cfg Config) {
}

func createAPI(router *mux.Router, servicesConfig broker.ServicesConfig, planValidator broker.PlanValidator, cfg *Config, db storage.BrokerStorage,
provisionQueue, deprovisionQueue, updateQueue *process.Queue, logger lager.Logger, logs logrus.FieldLogger, log *slog.Logger, planDefaults broker.PlanDefaults, kcBuilder kubeconfig.KcBuilder, clientProvider K8sClientProvider, kubeconfigProvider KubeconfigProvider, gardenerClient, kcpK8sClient client.Client, publisher event.Publisher) {
provisionQueue, deprovisionQueue, updateQueue *process.Queue, logger lager.Logger, logs *slog.Logger, planDefaults broker.PlanDefaults, kcBuilder kubeconfig.KcBuilder, clientProvider K8sClientProvider, kubeconfigProvider KubeconfigProvider, gardenerClient, kcpK8sClient client.Client, publisher event.Publisher) {

suspensionCtxHandler := suspension.NewContextUpdateHandler(db.Operations(), provisionQueue, deprovisionQueue, logs)

defaultPlansConfig, err := servicesConfig.DefaultPlansConfig()
fatalOnError(err, log)
fatalOnError(err, logs)

debugSink, err := lager.NewRedactingSink(lager.NewWriterSink(os.Stdout, lager.DEBUG), []string{"instance-details"}, []string{})
fatalOnError(err, log)
fatalOnError(err, logs)
logger.RegisterSink(debugSink)
errorSink, err := lager.NewRedactingSink(lager.NewWriterSink(os.Stderr, lager.ERROR), []string{"instance-details"}, []string{})
fatalOnError(err, log)
fatalOnError(err, logs)
logger.RegisterSink(errorSink)

freemiumGlobalAccountIds, err := whitelist.ReadWhitelistedGlobalAccountIdsFromFile(cfg.FreemiumWhitelistedGlobalAccountsFilePath)
fatalOnError(err, log)
logs.Infof("Number of globalAccountIds for unlimited freeemium: %d\n", len(freemiumGlobalAccountIds))
fatalOnError(err, logs)
logs.Info(fmt.Sprintf("Number of globalAccountIds for unlimited freemium: %d", len(freemiumGlobalAccountIds)))

// backward compatibility for tests
convergedCloudRegionProvider, err := broker.NewDefaultConvergedCloudRegionsProvider(cfg.SapConvergedCloudRegionMappingsFilePath, &broker.YamlRegionReader{})
fatalOnError(err, log)
logs.Infof("%s plan region mappings loaded", broker.SapConvergedCloudPlanName)
fatalOnError(err, logs)
logs.Info(fmt.Sprintf("%s plan region mappings loaded", broker.SapConvergedCloudPlanName))

// create KymaEnvironmentBroker endpoints
kymaEnvBroker := &broker.KymaEnvironmentBroker{
Expand Down
19 changes: 13 additions & 6 deletions internal/appinfo/runtime_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package appinfo_test
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand All @@ -15,7 +17,6 @@ import (
"github.com/kyma-project/kyma-environment-broker/internal/broker"
"github.com/kyma-project/kyma-environment-broker/internal/fixture"
"github.com/kyma-project/kyma-environment-broker/internal/httputil"
"github.com/kyma-project/kyma-environment-broker/internal/logger"
"github.com/kyma-project/kyma-environment-broker/internal/storage"
"github.com/kyma-project/kyma-environment-broker/internal/storage/driver/memory"
"github.com/pivotal-cf/brokerapi/v8/domain"
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestRuntimeInfoHandlerSuccess(t *testing.T) {
var (
fixReq = httptest.NewRequest("GET", "http://example.com/foo", nil)
respSpy = httptest.NewRecorder()
writer = httputil.NewResponseWriter(logger.NewLogDummy(), true)
writer = httputil.NewResponseWriter(fixLogger(), true)
memStorage = newInMemoryStorage(t, tc.instances, tc.provisionOp, tc.deprovisionOp)
)

Expand All @@ -115,7 +116,7 @@ func TestRuntimeInfoHandlerFailures(t *testing.T) {
var (
fixReq = httptest.NewRequest("GET", "http://example.com/foo", nil)
respSpy = httptest.NewRecorder()
writer = httputil.NewResponseWriter(logger.NewLogDummy(), true)
writer = httputil.NewResponseWriter(fixLogger(), true)
expBody = `{
"status": 500,
"requestId": "",
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestRuntimeInfoHandlerOperationRecognition(t *testing.T) {
req, err := http.NewRequest("GET", "/info/runtimes", nil)
require.NoError(t, err)

responseWriter := httputil.NewResponseWriter(logger.NewLogDummy(), true)
responseWriter := httputil.NewResponseWriter(fixLogger(), true)
runtimesInfoHandler := appinfo.NewRuntimeInfoHandler(instances, operations, broker.PlansConfig{}, "", responseWriter)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -332,7 +333,7 @@ func TestRuntimeInfoHandlerOperationRecognition(t *testing.T) {
req, err := http.NewRequest("GET", "/info/runtimes", nil)
require.NoError(t, err)

responseWriter := httputil.NewResponseWriter(logger.NewLogDummy(), true)
responseWriter := httputil.NewResponseWriter(fixLogger(), true)
runtimesInfoHandler := appinfo.NewRuntimeInfoHandler(instances, operations, broker.PlansConfig{}, "", responseWriter)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -471,7 +472,7 @@ func TestRuntimeInfoHandlerOperationRecognition(t *testing.T) {
req, err := http.NewRequest("GET", "/info/runtimes", nil)
require.NoError(t, err)

responseWriter := httputil.NewResponseWriter(logger.NewLogDummy(), true)
responseWriter := httputil.NewResponseWriter(fixLogger(), true)
runtimesInfoHandler := appinfo.NewRuntimeInfoHandler(instances, operations, broker.PlansConfig{}, "", responseWriter)

rr := httptest.NewRecorder()
Expand Down Expand Up @@ -576,3 +577,9 @@ func fixSucceededOperation(operationType internal.OperationType, idx int) intern
Type: operationType,
}
}

func fixLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
}
26 changes: 13 additions & 13 deletions internal/broker/bind_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"time"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/pivotal-cf/brokerapi/v8/domain/apiresponses"

"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -44,7 +44,7 @@ type BindEndpoint struct {
serviceAccountBindingManager broker.BindingsManager
publisher event.Publisher

log logrus.FieldLogger
log *slog.Logger
}

type BindingContext struct {
Expand All @@ -69,14 +69,14 @@ type Credentials struct {
Kubeconfig string `json:"kubeconfig"`
}

func NewBind(cfg BindingConfig, db storage.BrokerStorage, log logrus.FieldLogger, clientProvider broker.ClientProvider, kubeconfigProvider broker.KubeconfigProvider,
func NewBind(cfg BindingConfig, db storage.BrokerStorage, log *slog.Logger, clientProvider broker.ClientProvider, kubeconfigProvider broker.KubeconfigProvider,
publisher event.Publisher) *BindEndpoint {
return &BindEndpoint{config: cfg,
instancesStorage: db.Instances(),
bindingsStorage: db.Bindings(),
publisher: publisher,
operationsStorage: db.Operations(),
log: log.WithField("service", "BindEndpoint"),
log: log.With("service", "BindEndpoint"),
serviceAccountBindingManager: broker.NewServiceAccountBindingsManager(clientProvider, kubeconfigProvider),
}
}
Expand All @@ -95,10 +95,10 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d
}

func (b *BindEndpoint) bind(ctx context.Context, instanceID, bindingID string, details domain.BindDetails, asyncAllowed bool) (domain.Binding, error) {
b.log.Infof("Bind instanceID: %s", instanceID)
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)
b.log.Info(fmt.Sprintf("Bind instanceID: %s", instanceID))
b.log.Info(fmt.Sprintf("Bind parameters: %s", string(details.RawParameters)))
b.log.Info(fmt.Sprintf("Bind context: %s", string(details.RawContext)))
b.log.Info(fmt.Sprintf("Bind asyncAllowed: %v", asyncAllowed))

if !b.config.Enabled {
return domain.Binding{}, fmt.Errorf("not supported")
Expand Down Expand Up @@ -203,7 +203,7 @@ func (b *BindEndpoint) bind(ctx context.Context, instanceID, bindingID string, d
bindingCount := len(bindingList)
message := fmt.Sprintf("reaching the maximum (%d) number of non expired bindings for instance %s", b.config.MaxBindingsCount, instanceID)
if bindingCount == b.config.MaxBindingsCount-1 {
b.log.Infof(message)
b.log.Info(message)
}
if bindingCount >= b.config.MaxBindingsCount {
expiredCount := 0
Expand All @@ -213,11 +213,11 @@ func (b *BindEndpoint) bind(ctx context.Context, instanceID, bindingID string, d
}
}
if (bindingCount - expiredCount) == (b.config.MaxBindingsCount - 1) {
b.log.Infof(message)
b.log.Info(message)
}
if (bindingCount - expiredCount) >= b.config.MaxBindingsCount {
message := fmt.Sprintf("maximum number of non expired bindings reached: %d", b.config.MaxBindingsCount)
b.log.Infof(message+" for instance %s", instanceID)
b.log.Info(fmt.Sprintf(message+" for instance %s", instanceID))
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message)
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func (b *BindEndpoint) bind(ctx context.Context, instanceID, bindingID string, d
kubeconfig, expiresAt, err = b.serviceAccountBindingManager.Create(ctx, instance, bindingID, expirationSeconds)
if err != nil {
message := fmt.Sprintf("failed to create a Kyma binding using service account's kubeconfig: %s", err)
b.log.Errorf("for instance %s %s", instanceID, message)
b.log.Error(fmt.Sprintf("for instance %s %s", instanceID, message))
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message)
}

Expand All @@ -262,7 +262,7 @@ func (b *BindEndpoint) bind(ctx context.Context, instanceID, bindingID string, d
message := fmt.Sprintf("failed to update Kyma binding in storage: %s", err)
return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusInternalServerError, message)
}
b.log.Infof("Successfully created binding %s for instance %s", bindingID, instanceID)
b.log.Info(fmt.Sprintf("Successfully created binding %s for instance %s", bindingID, instanceID))
b.publisher.Publish(context.Background(), BindingCreated{PlanID: instance.ServicePlanID})

return domain.Binding{
Expand Down
21 changes: 7 additions & 14 deletions internal/broker/bind_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"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"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -49,14 +48,8 @@ func TestCreateBindingEndpoint(t *testing.T) {
Level: slog.LevelDebug,
}))

logs := logrus.New()
logs.SetLevel(logrus.DebugLevel)
logs.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
})

brokerLogger := lager.NewLogger("test")
brokerLogger.RegisterSink(lager.NewWriterSink(logs.Writer(), lager.DEBUG))
brokerLogger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))

//// schema

Expand All @@ -83,7 +76,7 @@ func TestCreateBindingEndpoint(t *testing.T) {
publisher := event.NewPubSub(log)

//// api handler
bindEndpoint := NewBind(*bindingCfg, db, logs, &provider{}, &provider{}, publisher)
bindEndpoint := NewBind(*bindingCfg, db, fixLogger(), &provider{}, &provider{}, publisher)

// test relies on checking if got nil on kubeconfig provider but the instance got inserted either way
t.Run("should INSERT binding despite error on k8s api call", func(t *testing.T) {
Expand Down Expand Up @@ -194,7 +187,7 @@ func TestCreateSecondBindingWithTheSameIdButDifferentParams(t *testing.T) {

publisher := event.NewPubSub(log)

svc := NewBind(*bindingCfg, brokerStorage, logrus.New(), nil, nil, publisher)
svc := NewBind(*bindingCfg, brokerStorage, fixLogger(), nil, nil, publisher)
params := BindingParams{
ExpirationSeconds: 601,
}
Expand Down Expand Up @@ -245,7 +238,7 @@ func TestCreateSecondBindingWithTheSameIdAndParams(t *testing.T) {

publisher := event.NewPubSub(log)

svc := NewBind(*bindingCfg, brokerStorage, logrus.New(), nil, nil, publisher)
svc := NewBind(*bindingCfg, brokerStorage, fixLogger(), nil, nil, publisher)
params := BindingParams{
ExpirationSeconds: 600,
}
Expand Down Expand Up @@ -297,7 +290,7 @@ func TestCreateSecondBindingWithTheSameIdAndParamsForExpired(t *testing.T) {
// event publisher
publisher := event.NewPubSub(log)

svc := NewBind(*bindingCfg, brokerStorage, logrus.New(), nil, nil, publisher)
svc := NewBind(*bindingCfg, brokerStorage, fixLogger(), nil, nil, publisher)
params := BindingParams{
ExpirationSeconds: 600,
}
Expand Down Expand Up @@ -351,7 +344,7 @@ func TestCreateSecondBindingWithTheSameIdAndParamsForBindingInProgress(t *testin
// event publisher
publisher := event.NewPubSub(log)

svc := NewBind(*bindingCfg, brokerStorage, logrus.New(), nil, nil, publisher)
svc := NewBind(*bindingCfg, brokerStorage, fixLogger(), nil, nil, publisher)
params := BindingParams{
ExpirationSeconds: 600,
}
Expand Down Expand Up @@ -402,7 +395,7 @@ func TestCreateSecondBindingWithTheSameIdAndParamsNotExplicitlyDefined(t *testin

publisher := event.NewPubSub(log)

svc := NewBind(*bindingCfg, brokerStorage, logrus.New(), nil, nil, publisher)
svc := NewBind(*bindingCfg, brokerStorage, fixLogger(), nil, nil, publisher)

// when
resp, err := svc.Bind(context.Background(), instanceID, bindingID, domain.BindDetails{}, false)
Expand Down
Loading

0 comments on commit 515920d

Please sign in to comment.