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

chore: already calculated should be info log level #347

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions acr_controller/service/acr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"context"
"encoding/json"
"errors"
"sync"

log "github.com/sirupsen/logrus"
Expand All @@ -25,12 +24,14 @@ type acrService struct {
applicationClientset appclientset.Interface
applicationServiceClient argoclient.ApplicationClient
lock sync.Mutex
logger *log.Logger
}

func NewACRService(applicationClientset appclientset.Interface, applicationServiceClient argoclient.ApplicationClient) ACRService {
return &acrService{
applicationClientset: applicationClientset,
applicationServiceClient: applicationServiceClient,
logger: log.New(),
}
}

Expand Down Expand Up @@ -68,8 +69,8 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat
}

if getChangeRevision(app) != "" {
log.Infof("Change revision already calculated for application %s", app.Name)
return errors.New("change revision already calculated")
c.logger.Infof("Change revision already calculated for application %s", app.Name)
return nil
}

revision, err := c.calculateRevision(ctx, app)
Expand All @@ -78,11 +79,11 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat
}

if revision == nil || *revision == "" {
log.Infof("Revision for application %s is empty", app.Name)
c.logger.Infof("Revision for application %s is empty", app.Name)
return nil
}

log.Infof("Change revision for application %s is %s", app.Name, *revision)
c.logger.Infof("Change revision for application %s is %s", app.Name, *revision)

app, err = c.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(ctx, app.Name, metav1.GetOptions{})
if err != nil {
Expand All @@ -92,17 +93,17 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat
revisions := []string{*revision}

if app.Status.OperationState != nil && app.Status.OperationState.Operation.Sync != nil {
log.Infof("Patch operation sync result for application %s", app.Name)
c.logger.Infof("Patch operation sync result for application %s", app.Name)
return c.patchOperationSyncResultWithChangeRevision(ctx, app, revisions)
}

log.Infof("Patch operation for application %s", app.Name)
c.logger.Infof("Patch operation for application %s", app.Name)
return c.patchOperationWithChangeRevision(ctx, app, revisions)
}

func (c *acrService) calculateRevision(ctx context.Context, a *application.Application) (*string, error) {
currentRevision, previousRevision := c.getRevisions(ctx, a)
log.Infof("Calculate revision for application '%s', current revision '%s', previous revision '%s'", a.Name, currentRevision, previousRevision)
c.logger.Infof("Calculate revision for application '%s', current revision '%s', previous revision '%s'", a.Name, currentRevision, previousRevision)
changeRevisionResult, err := c.applicationServiceClient.GetChangeRevision(ctx, &appclient.ChangeRevisionRequest{
AppName: pointer.String(a.GetName()),
Namespace: pointer.String(a.GetNamespace()),
Expand Down
19 changes: 18 additions & 1 deletion acr_controller/service/acr_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"testing"

"github.com/sirupsen/logrus"
test2 "github.com/sirupsen/logrus/hooks/test"

"github.com/argoproj/argo-cd/v2/acr_controller/application/mocks"
appclient "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand Down Expand Up @@ -207,6 +210,7 @@ func newTestACRService(client *mocks.ApplicationClient) *acrService {
return &acrService{
applicationClientset: fakeAppsClientset,
applicationServiceClient: client,
logger: logrus.New(),
}
}

Expand Down Expand Up @@ -285,7 +289,12 @@ func Test_ChangeRevision(r *testing.T) {
client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{
Revision: pointer.String("new-revision"),
}, nil)

logger, logHook := test2.NewNullLogger()

acrService := newTestACRService(client)
acrService.logger = logger

app := createTestApp(syncedAppWithHistory)

err := acrService.ChangeRevision(context.TODO(), app)
Expand All @@ -297,6 +306,14 @@ func Test_ChangeRevision(r *testing.T) {
assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision)

err = acrService.ChangeRevision(context.TODO(), app)
require.Error(t, err, "change revision already calculated")

require.NoError(t, err)

lastLogEntry := logHook.LastEntry()
if lastLogEntry == nil {
t.Fatal("No log entry")
}

require.Equal(t, "Change revision already calculated for application guestbook", lastLogEntry.Message)
})
}
Loading