Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 69 additions & 2 deletions pkg/app/api/grpcapi/piped_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,75 @@ func (a *PipedAPI) GetDesiredVersion(ctx context.Context, _ *pipedservice.GetDes
}

func (a *PipedAPI) UpdateApplicationConfigurations(ctx context.Context, req *pipedservice.UpdateApplicationConfigurationsRequest) (*pipedservice.UpdateApplicationConfigurationsResponse, error) {
// TODO: Update the given application configurations
return nil, status.Errorf(codes.Unimplemented, "UpdateApplicationConfigurations is not implemented yet")
projectID, pipedID, _, err := rpcauth.ExtractPipedToken(ctx)
if err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

since we have appInfo.Id, better to validate if the updating application belongs to the requested piped as other RPCs.

Copy link
Member Author

Choose a reason for hiding this comment

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

For sure

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied at d22b018

// TODO: Consider bulk-updating multiple apps
for _, appInfo := range req.Applications {
opts := datastore.ListOptions{
// NOTE: Assume that no more than one application is referring to a single configuration file.
Limit: 1,
Filters: []datastore.ListFilter{
{
Field: "ProjectId",
Operator: datastore.OperatorEqual,
Value: projectID,
},
{
Field: "PipedId",
Operator: datastore.OperatorEqual,
Value: pipedID,
},
{
Field: "GitPath.Repo.Id",
Operator: datastore.OperatorEqual,
Value: appInfo.RepoId,
},
{
Field: "GitPath.Path",
Operator: datastore.OperatorEqual,
Value: appInfo.Path,
},
{
Field: "GitPath.ConfigFilename",
Operator: datastore.OperatorEqual,
Value: appInfo.ConfigFilename,
},
{
Field: "Disabled",
Operator: datastore.OperatorEqual,
Value: false,
},
},
}
Copy link
Member

Choose a reason for hiding this comment

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

Application ID can be included in the request by Piped to avoid this listing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean to uniquely identify the application based on the RepoID, Path, and ConfigFilename, get the id, and cache it on the Piped side?

Copy link
Member

Choose a reason for hiding this comment

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

No. I think since this is for updating the existing applications then Piped already knew their ID before sending this UpdateApplicationConfigurations request.

https://github.com/pipe-cd/pipe/blob/master/pkg/app/piped/appconfigreporter/appconfigreporter.go#L146
https://github.com/pipe-cd/pipe/blob/master/pkg/app/piped/appconfigreporter/appconfigreporter.go#L197

So I meant that we can add an application_id field to this https://github.com/pipe-cd/pipe/blob/master/pkg/model/common.proto#L59-L65 model or the RPC request to avoid re-finding each one on the server to update.

Copy link
Member

Choose a reason for hiding this comment

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

More specifically, we can update this map to be a map from GitPathKey to AppID (make(map[string]string, len(apps)))

https://github.com/pipe-cd/pipe/blob/ce3e2b322cb5caef628d7ae7188396eb961beb7a/pkg/app/piped/appconfigreporter/appconfigreporter.go#L147

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds nice. I originally applied in this way this because I was trying to allow multiple applications to refer to a single config file, but now that the premise has changed, it seems better to identify them by ID.

// TODO: Enable to update multiple apps that match the given condition instead of accessing it twice
// like: UPDATE Application SET Name = 'foo' WHERE GitPath.Repo.Id = '?' AND GitPath.Path = '?'
apps, _, err := a.applicationStore.ListApplications(ctx, opts)
if err != nil {
a.logger.Error("failed to fetch applications", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to fetch applications")
}
if len(apps) == 0 {
a.logger.Error("not found any enabled apps that match the terms",
zap.String("repo-id", appInfo.RepoId),
zap.String("path", appInfo.Path),
zap.String("config-filename", appInfo.ConfigFilename),
)
return nil, status.Error(codes.NotFound, "not found any enabled apps that match the terms")
}
updater := func(app *model.Application) error {
app.Name = appInfo.Name
app.Kind = appInfo.Kind
app.Labels = appInfo.Labels
return nil
}
if err := a.applicationStore.UpdateApplication(ctx, apps[0].Id, updater); err != nil {
a.logger.Error("failed to update application", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to update application")
}
}
return &pipedservice.UpdateApplicationConfigurationsResponse{}, nil
}

func (a *PipedAPI) ReportUnregisteredApplicationConfigurations(ctx context.Context, req *pipedservice.ReportUnregisteredApplicationConfigurationsRequest) (*pipedservice.ReportUnregisteredApplicationConfigurationsResponse, error) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/app/piped/appconfigreporter/appconfigreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (r *Reporter) findRegisteredApps(ctx context.Context, repoID string, repo g
if _, registered := registeredAppPaths[gitPathKey]; !registered {
continue
}
appInfo, err := r.readApplicationInfo(repo.GetPath(), filepath.Dir(path), filepath.Base(path))
appInfo, err := r.readApplicationInfo(repo.GetPath(), repoID, filepath.Dir(path), filepath.Base(path))
if err != nil {
r.logger.Error("failed to read application info",
zap.String("repo-id", repoID),
Expand Down Expand Up @@ -313,7 +313,7 @@ func (r *Reporter) scanAllFiles(repoRoot, repoID string, shouldSkip func(string)
return nil
}

appInfo, err := r.readApplicationInfo(repoRoot, filepath.Dir(cfgRelPath), filepath.Base(cfgRelPath))
appInfo, err := r.readApplicationInfo(repoRoot, repoID, filepath.Dir(cfgRelPath), filepath.Base(cfgRelPath))
if err != nil {
r.logger.Error("failed to read application info",
zap.String("repo-id", repoID),
Expand All @@ -338,7 +338,7 @@ func makeGitPathKey(repoID, cfgFilePath string) string {
return fmt.Sprintf("%s:%s", repoID, cfgFilePath)
}

func (r *Reporter) readApplicationInfo(repoDir, appDirRelPath, cfgFilename string) (*model.ApplicationInfo, error) {
func (r *Reporter) readApplicationInfo(repoDir, repoID, appDirRelPath, cfgFilename string) (*model.ApplicationInfo, error) {
b, err := fs.ReadFile(r.fileSystem, filepath.Join(repoDir, appDirRelPath, cfgFilename))
if err != nil {
return nil, fmt.Errorf("failed to open the configuration file: %w", err)
Expand All @@ -361,6 +361,7 @@ func (r *Reporter) readApplicationInfo(repoDir, appDirRelPath, cfgFilename strin
// TODO: Convert Kind string into dedicated type
//Kind: cfg.Kind,
Labels: spec.Labels,
RepoId: repoID,
Path: appDirRelPath,
ConfigFilename: cfgFilename,
}, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/app/piped/appconfigreporter/appconfigreporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ spec:
{
Name: "app-1",
Labels: map[string]string{"key-1": "value-1"},
RepoId: "repo-1",
Path: "app-1",
ConfigFilename: ".pipe.yaml",
},
Expand Down Expand Up @@ -139,6 +140,7 @@ spec:
{
Name: "app-1",
Labels: map[string]string{"key-1": "value-1"},
RepoId: "repo-1",
Path: "app-1",
ConfigFilename: "dev.pipecd.yaml",
},
Expand Down Expand Up @@ -260,6 +262,7 @@ spec:
{
Name: "app-1",
Labels: map[string]string{"key-1": "value-1"},
RepoId: "repo-1",
Path: "app-1",
ConfigFilename: ".pipe.yaml",
},
Expand Down Expand Up @@ -292,6 +295,7 @@ spec:
{
Name: "app-1",
Labels: map[string]string{"key-1": "value-1"},
RepoId: "repo-1",
Path: "app-1",
ConfigFilename: ".pipe.yaml",
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/model/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ message ApplicationInfo {
string name = 1 [(validate.rules).string.min_len = 1];
ApplicationKind kind = 2 [(validate.rules).enum.defined_only = true];
map<string, string> labels = 3;
string path = 4 [(validate.rules).string.pattern = "^[^/].+$"];
string config_filename = 5 [(validate.rules).string.min_len = 1];
string repo_id = 4 [(validate.rules).string.min_len = 1];
string path = 5 [(validate.rules).string.pattern = "^[^/].+$"];
string config_filename = 6;
}