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

Sort results of plan-preview #5540

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 17 additions & 0 deletions pkg/app/piped/planpreview/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"os"
"sort"

"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand All @@ -41,6 +42,7 @@ const (
workspacePattern = "plan-preview-builder-*"
defaultWorkerAppNum = 3
maxWorkerNum = 100
labelEnvKey = "env"
)

var (
Expand Down Expand Up @@ -197,10 +199,25 @@ func (b *builder) build(ctx context.Context, id string, cmd model.Command_BuildP
results = append(results, r)
}

sortResults(results)

logger.Info("successfully collected plan-preview results of all applications")
return results, nil
}

func sortResults(results []*model.ApplicationPlanPreviewResult) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code will work well. 👍

We discussed it, and I'd like to propose moving the sort to the pipectl side like this (draft code): 66df2a5

Usage example: pipectl planpreview --sort-label-keys env,team --......

Purposes:

  • Users and PipeCD developers don't depend on "env" labels
  • For extensibility and customizability
  • To separate the responsibilities of piped and pipectl

Note:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf. ApplicationPlanPreviewResult.Labels is passed here and can be read on the pipectl side.

Labels: app.Labels,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your discussion on future customization and for sharing the draft code 😍

I'll take a look at it later and update my PR based on your code.

Please wait a little while for the fix!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you kindly confirm if my understanding is correct? 👀

For this PR:

Sorting is handled only on the pipectl side, meaning there's no need to specify the order when listing piped at the control plane.

For another PR:

Add arguments for actions-plan-preview.

sort.SliceStable(results, func(i, j int) bool {
a, b := results[i], results[j]
if a.ApplicationName != b.ApplicationName {
return a.ApplicationName < b.ApplicationName
}
if a.Labels[labelEnvKey] != b.Labels[labelEnvKey] {
return a.Labels[labelEnvKey] < b.Labels[labelEnvKey]
}
return a.ApplicationKind < b.ApplicationKind
})
}

func (b *builder) buildApp(ctx context.Context, worker int, command string, app *model.Application, repo git.Repo, mergedCommit string) *model.ApplicationPlanPreviewResult {
logger := b.logger.With(
zap.Int("worker", worker),
Expand Down
72 changes: 72 additions & 0 deletions pkg/app/piped/planpreview/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package planpreview

import (
"testing"

"github.com/pipe-cd/pipecd/pkg/model"

"github.com/stretchr/testify/require"
)

func TestSortResults(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input []*model.ApplicationPlanPreviewResult
expected []*model.ApplicationPlanPreviewResult
}{
{
name: "sort by name, kind",
input: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES},
},
expected: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appA", ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appB", ApplicationKind: model.ApplicationKind_CLOUDRUN},
},
},
{
name: "sort by name, env, kind",
input: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
},
expected: []*model.ApplicationPlanPreviewResult{
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appA", Labels: map[string]string{labelEnvKey: "prd"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_KUBERNETES},
{ApplicationName: "appB", Labels: map[string]string{labelEnvKey: "dev"}, ApplicationKind: model.ApplicationKind_CLOUDRUN},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
sortResults(tt.input)
require.Equal(t, tt.expected, tt.input)
})
}
}
6 changes: 6 additions & 0 deletions pkg/app/server/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,12 @@ func (a *API) RequestPlanPreview(ctx context.Context, req *apiservice.RequestPla
Value: false,
},
},
Orders: []datastore.Order{
{
Field: "Id",
Direction: datastore.Asc,
},
},
Comment on lines +851 to +856
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensured the order of the piped list.

However, I’m not entirely sure about the key selection. If there's a better field to use, please let me know.

})
if err != nil {
return nil, gRPCStoreError(err, "list pipeds")
Expand Down