Skip to content

Commit

Permalink
Remove logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
KsaweryZietara committed Dec 12, 2024
1 parent c0fd6d2 commit 9014671
Show file tree
Hide file tree
Showing 28 changed files with 137 additions and 278 deletions.
9 changes: 1 addition & 8 deletions cmd/broker/bindings_envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/kyma-project/kyma-environment-broker/internal/storage"
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/pivotal-cf/brokerapi/v8/handlers"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -81,14 +80,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
sch := runtime.NewScheme()
Expand Down
8 changes: 2 additions & 6 deletions cmd/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import (
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/pivotal-cf/brokerapi/v8/domain/apiresponses"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -200,9 +199,6 @@ func NewBrokerSuiteTestWithConfig(t *testing.T, cfg *Config, version ...string)

require.NoError(t, err)

logs := logrus.New()
logs.SetLevel(logrus.DebugLevel)

gardenerClient := gardener.NewDynamicFakeClient()

provisionerClient := provisioner.NewFakeClientWithGardener(gardenerClient, "kcp-system")
Expand Down Expand Up @@ -249,7 +245,7 @@ func NewBrokerSuiteTestWithConfig(t *testing.T, cfg *Config, version ...string)
}
ts.poller = &broker.TimerPoller{PollInterval: 3 * time.Millisecond, PollTimeout: 3 * time.Second, Log: ts.t.Log}

ts.CreateAPI(inputFactory, cfg, db, provisioningQueue, deprovisioningQueue, updateQueue, logs, log, k8sClientProvider, gardener.NewFakeClient(), eventBroker)
ts.CreateAPI(inputFactory, cfg, db, provisioningQueue, deprovisioningQueue, updateQueue, log, k8sClientProvider, gardener.NewFakeClient(), eventBroker)

notificationFakeClient := notification.NewFakeClient()
notificationBundleBuilder := notification.NewBundleBuilder(notificationFakeClient, cfg.Notification)
Expand Down Expand Up @@ -366,7 +362,7 @@ func (s *BrokerSuiteTest) CallAPI(method string, path string, body string) *http
return resp
}

func (s *BrokerSuiteTest) CreateAPI(inputFactory broker.PlanValidator, cfg *Config, db storage.BrokerStorage, provisioningQueue *process.Queue, deprovisionQueue *process.Queue, updateQueue *process.Queue, logs logrus.FieldLogger, log *slog.Logger, skrK8sClientProvider *kubeconfig.FakeProvider, gardenerClient client.Client, eventBroker *event.PubSub) {
func (s *BrokerSuiteTest) CreateAPI(inputFactory broker.PlanValidator, cfg *Config, db storage.BrokerStorage, provisioningQueue *process.Queue, deprovisionQueue *process.Queue, updateQueue *process.Queue, log *slog.Logger, skrK8sClientProvider *kubeconfig.FakeProvider, gardenerClient client.Client, eventBroker *event.PubSub) {
servicesConfig := map[string]broker.Service{
broker.KymaServiceName: {
Description: "",
Expand Down
10 changes: 5 additions & 5 deletions common/deprovision/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package deprovision

import (
"fmt"
"log/slog"
"net/http"
"time"

"github.com/sirupsen/logrus"
"golang.org/x/oauth2/clientcredentials"
)

Expand All @@ -17,7 +17,7 @@ type Client interface {
}

type DeprovisionClient struct {
log logrus.FieldLogger
log *slog.Logger
URL string
client *http.Client
}
Expand All @@ -34,7 +34,7 @@ func NewDeprovisionClient(parameters DeprovisionParameters) *DeprovisionClient {
httpClientOAuth.Timeout = 30 * time.Second

return &DeprovisionClient{
log: logrus.WithField("client", "deprovision"),
log: slog.With("client", "deprovision"),
URL: parameters.EndpointURL,
client: httpClientOAuth,
}
Expand All @@ -45,7 +45,7 @@ func (c DeprovisionClient) DeprovisionRuntime(instanceID string) error {
url := c.URL + "/oauth/v2/service_instances/" +
instanceID + "?accepts_incomplete=true&service_id=faebbe18-0a84-11e5-ab14-d663bd873d97&plan_id=0c712d43-b1e6-470s-9fe5-8e1d552aa6a5"

c.log.Infof("url: %s", url)
c.log.Info(fmt.Sprintf("url: %s", url))
request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("while creating the HTTP Delete request for deprovisioning: %w", err)
Expand All @@ -65,7 +65,7 @@ func (c DeprovisionClient) DeprovisionRuntime(instanceID string) error {
if response.StatusCode != http.StatusOK {
return fmt.Errorf("calling %s returned %d (%s) status", request.URL.String(), response.StatusCode, response.Status)
}
c.log.Infof("Deprovisioning request returned code: " + response.Status)
c.log.Info(fmt.Sprintf("Deprovisioning request returned code: %s", response.Status))

return err
}
8 changes: 4 additions & 4 deletions common/hyperscaler/azure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package azure
import (
"context"
"fmt"
"log/slog"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -30,7 +30,7 @@ type Client struct {
// the actual azure client
eventHubNamespaceClient eventhub.NamespacesClient
resourceGroupClient resources.GroupsClient
logger logrus.FieldLogger
logger *slog.Logger
}

type ResourceGroupDoesNotExistError struct {
Expand All @@ -45,7 +45,7 @@ func (e ResourceGroupDoesNotExistError) Error() string {
return e.message
}

func NewAzureClient(namespaceClient eventhub.NamespacesClient, resourceGroupClient resources.GroupsClient, logger logrus.FieldLogger) *Client {
func NewAzureClient(namespaceClient eventhub.NamespacesClient, resourceGroupClient resources.GroupsClient, logger *slog.Logger) *Client {
return &Client{
eventHubNamespaceClient: namespaceClient,
resourceGroupClient: resourceGroupClient,
Expand Down Expand Up @@ -82,7 +82,7 @@ func (nc *Client) DeleteResourceGroup(ctx context.Context, tags Tags) (resources
if resourceGroup.Name == nil {
return resources.GroupsDeleteFuture{}, fmt.Errorf("resource group name is nil")
}
nc.logger.Infof("deleting resource group: %s", *resourceGroup.Name)
nc.logger.Info(fmt.Sprintf("deleting resource group: %s", *resourceGroup.Name))
future, err := nc.resourceGroupClient.Delete(ctx, *resourceGroup.Name)
return future, err
}
Expand Down
6 changes: 3 additions & 3 deletions common/hyperscaler/azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package azure

import (
"fmt"
"log/slog"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/sirupsen/logrus"
)

type HyperscalerProvider interface {
GetClient(config *Config, logger logrus.FieldLogger) (Interface, error)
GetClient(config *Config, logger *slog.Logger) (Interface, error)
}

var _ HyperscalerProvider = (*azureProvider)(nil)
Expand All @@ -24,7 +24,7 @@ func NewAzureProvider() HyperscalerProvider {
}

// GetClient gets a client for interacting with Azure
func (ac *azureProvider) GetClient(config *Config, logger logrus.FieldLogger) (Interface, error) {
func (ac *azureProvider) GetClient(config *Config, logger *slog.Logger) (Interface, error) {

environment, err := config.Environment()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions common/hyperscaler/azure/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package testing
import (
"context"
"fmt"
"log/slog"

"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/sirupsen/logrus"

"github.com/kyma-project/kyma-environment-broker/common/hyperscaler/azure"
"github.com/kyma-project/kyma-environment-broker/internal/ptr"
)
Expand Down Expand Up @@ -153,7 +152,7 @@ type FakeHyperscalerProvider struct {
Err error
}

func (ac *FakeHyperscalerProvider) GetClient(config *azure.Config, logger logrus.FieldLogger) (azure.Interface, error) {
func (ac *FakeHyperscalerProvider) GetClient(config *azure.Config, logger *slog.Logger) (azure.Interface, error) {
return ac.Client, ac.Err
}

Expand Down
12 changes: 6 additions & 6 deletions docs/user/03-20-runtime-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ You can configure SAP BTP, Kyma runtime operations by providing additional steps
```go
type Step interface {
Name() string
Run(operation internal.Operation, logger logrus.FieldLogger) (internal.Operation, time.Duration, error)
Run(operation internal.Operation, logger *slog.Logger) (internal.Operation, time.Duration, error)
}
```

Expand Down Expand Up @@ -101,14 +101,14 @@ You can configure SAP BTP, Kyma runtime operations by providing additional steps
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/kyma-incubator/compass/components/provisioner/pkg/gqlschema"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal"
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal/storage"
"github.com/sirupsen/logrus"
)
type HelloWorldStep struct {
Expand All @@ -133,7 +133,7 @@ You can configure SAP BTP, Kyma runtime operations by providing additional steps
}
// Your step can be repeated in case any other step fails, even if your step has already done its job
func (s *HelloWorldStep) Run(operation internal.Operation, log *logrus.Entry) (internal.Operation, time.Duration, error) {
func (s *HelloWorldStep) Run(operation internal.Operation, log *slog.Logger) (internal.Operation, time.Duration, error) {
log.Info("Start step")
// Check whether your step should be run or if its job has been done in the previous iteration
Expand All @@ -153,7 +153,7 @@ You can configure SAP BTP, Kyma runtime operations by providing additional steps
body := ExternalBodyResponse{}
err = json.NewDecoder(response.Body).Decode(&body)
if err != nil {
log.Errorf("error: %s", err)
log.Error(fmt.Sprintf("error: %s", err))
// Handle a process failure by returning an error or time.Duration
}
Expand All @@ -163,7 +163,7 @@ You can configure SAP BTP, Kyma runtime operations by providing additional steps
operation.HelloWorlds = body.data
updatedOperation, err := s.operationStorage.UpdateOperation(operation)
if err != nil {
log.Errorf("error: %s", err)
log.Error(fmt.Sprintf("error: %s", err))
// Handle a process failure by returning an error or time.Duration
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.5
github.com/sebdah/goldie/v2 v2.5.5
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.10.0
github.com/vrischmann/envconfig v1.3.0
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
Expand Down Expand Up @@ -104,6 +103,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand Down
25 changes: 12 additions & 13 deletions internal/broker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"time"

"github.com/kyma-project/kyma-environment-broker/internal"
"github.com/pivotal-cf/brokerapi/v8/domain/apiresponses"
"github.com/pkg/errors"
"golang.org/x/oauth2/clientcredentials"

log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -143,11 +142,11 @@ func (c *Client) Deprovision(instance internal.Instance) (string, error) {
}

response := serviceInstancesResponseDTO{}
log.Infof("Requesting deprovisioning of the environment with instance id: %q", instance.InstanceID)
slog.Info(fmt.Sprintf("Requesting deprovisioning of the environment with instance id: %q", instance.InstanceID))
err = c.poller.Invoke(func() (bool, error) {
err := c.executeRequest(http.MethodDelete, deprovisionURL, http.StatusAccepted, nil, &response)
if err != nil {
log.Warn(fmt.Sprintf("while executing request: %s", err.Error()))
slog.Warn(fmt.Sprintf("while executing request: %s", err.Error()))
return false, nil
}
return true, nil
Expand Down Expand Up @@ -200,13 +199,13 @@ func (c *Client) Unbind(binding internal.Binding) error {
return err
}

log.Infof("sending unbind request for service binding with ID %q and instance ID: %q", binding.ID, binding.InstanceID)
slog.Info(fmt.Sprintf("sending unbind request for service binding with ID %q and instance ID: %q", binding.ID, binding.InstanceID))

emptyResponse := &apiresponses.EmptyResponse{}
for requestAttemptNum := 1; requestAttemptNum <= c.RequestRetries; requestAttemptNum++ {
if err = c.executeRequest(http.MethodDelete, unbindURL, http.StatusOK, nil, emptyResponse); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Warnf("request failed - timeout (attempt %d/%d)", requestAttemptNum, c.RequestRetries)
slog.Warn(fmt.Sprintf("request failed - timeout (attempt %d/%d)", requestAttemptNum, c.RequestRetries))
continue
}
break
Expand All @@ -217,7 +216,7 @@ func (c *Client) Unbind(binding internal.Binding) error {
return err
}

log.Infof("successfully unbound service binding with ID %q", binding.ID)
slog.Info(fmt.Sprintf("successfully unbound service binding with ID %q", binding.ID))

return nil
}
Expand All @@ -226,22 +225,22 @@ func processResponse(instanceID string, statusCode int, resp *http.Response) (su
switch statusCode {
case http.StatusAccepted, http.StatusOK:
{
log.Infof("Request for instanceID: %s accepted with status: %+v", instanceID, statusCode)
slog.Info(fmt.Sprintf("Request for instanceID: %s accepted with status: %+v", instanceID, statusCode))
operation, err := decodeOperation(resp)
if err != nil {
return false, err
}
log.Infof("For instanceID: %s we received operation: %s", instanceID, operation)
slog.Info(fmt.Sprintf("For instanceID: %s we received operation: %s", instanceID, operation))
return true, nil
}
case http.StatusUnprocessableEntity:
{
log.Warnf("For instanceID: %s we received entity unprocessable - status: %+v", instanceID, statusCode)
slog.Warn(fmt.Sprintf("For instanceID: %s we received entity unprocessable - status: %+v", instanceID, statusCode))
description, errorString, err := decodeErrorResponse(resp)
if err != nil {
return false, fmt.Errorf("for instanceID: %s: %w", instanceID, err)
}
log.Warnf("error: %+v description: %+v instanceID: %s", errorString, description, instanceID)
slog.Warn(fmt.Sprintf("error: %+v description: %+v instanceID: %s", errorString, description, instanceID))
return false, nil
}
default:
Expand Down Expand Up @@ -279,7 +278,7 @@ func decodeErrorResponse(resp *http.Response) (string, string, error) {
func prepareExpirationRequest(instance internal.Instance, brokerConfigURL string) (*http.Request, error) {
expireInstanceUrl := fmt.Sprintf(updateInstanceTmpl, brokerConfigURL, expireInstanceURL, instance.InstanceID)

log.Infof("Requesting expiration of the environment with instanceID: %q", instance.InstanceID)
slog.Info(fmt.Sprintf("Requesting expiration of the environment with instanceID: %q", instance.InstanceID))

request, err := http.NewRequest(http.MethodPut, expireInstanceUrl, nil)
if err != nil {
Expand Down Expand Up @@ -345,7 +344,7 @@ func (c *Client) executeRequest(method, url string, expectedStatus int, requestB

func (c *Client) warnOnError(do func() error) {
if err := do(); err != nil {
log.Warn(err.Error())
slog.Warn(err.Error())
}
}

Expand Down
Loading

0 comments on commit 9014671

Please sign in to comment.