-
Notifications
You must be signed in to change notification settings - Fork 305
Implement live state reporter for Cloud Run #3284
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["report.go"], | ||
| importpath = "github.com/pipe-cd/pipecd/pkg/app/piped/livestatereporter/cloudrun", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/app/piped/livestatestore/cloudrun:go_default_library", | ||
| "//pkg/app/server/service/pipedservice:go_default_library", | ||
| "//pkg/config:go_default_library", | ||
| "//pkg/model:go_default_library", | ||
| "@org_golang_google_grpc//:go_default_library", | ||
| "@org_uber_go_zap//:go_default_library", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright 2022 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 cloudrun | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| "google.golang.org/grpc" | ||
|
|
||
| "github.com/pipe-cd/pipecd/pkg/app/piped/livestatestore/cloudrun" | ||
| "github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice" | ||
| "github.com/pipe-cd/pipecd/pkg/config" | ||
| "github.com/pipe-cd/pipecd/pkg/model" | ||
| ) | ||
|
|
||
| type applicationLister interface { | ||
| ListByCloudProvider(name string) []*model.Application | ||
| } | ||
|
|
||
| type apiClient interface { | ||
| ReportApplicationLiveState(ctx context.Context, req *pipedservice.ReportApplicationLiveStateRequest, opts ...grpc.CallOption) (*pipedservice.ReportApplicationLiveStateResponse, error) | ||
| ReportApplicationLiveStateEvents(ctx context.Context, req *pipedservice.ReportApplicationLiveStateEventsRequest, opts ...grpc.CallOption) (*pipedservice.ReportApplicationLiveStateEventsResponse, error) | ||
| } | ||
|
|
||
| type Reporter interface { | ||
| Run(ctx context.Context) error | ||
| ProviderName() string | ||
| } | ||
|
|
||
| type reporter struct { | ||
| provider config.PipedCloudProvider | ||
| appLister applicationLister | ||
| stateGetter cloudrun.Getter | ||
| apiClient apiClient | ||
| snapshotFlushInterval time.Duration | ||
| logger *zap.Logger | ||
|
|
||
| snapshotVersions map[string]model.ApplicationLiveStateVersion | ||
| } | ||
|
|
||
| func NewReporter(cp config.PipedCloudProvider, appLister applicationLister, stateGetter cloudrun.Getter, apiClient apiClient, logger *zap.Logger) Reporter { | ||
| logger = logger.Named("cloudrun-reporter").With( | ||
| zap.String("cloud-provider", cp.Name), | ||
| ) | ||
| return &reporter{ | ||
| provider: cp, | ||
| appLister: appLister, | ||
| stateGetter: stateGetter, | ||
| apiClient: apiClient, | ||
| snapshotFlushInterval: 10 * time.Minute, | ||
| logger: logger, | ||
| snapshotVersions: make(map[string]model.ApplicationLiveStateVersion), | ||
| } | ||
| } | ||
|
|
||
| func (r *reporter) Run(ctx context.Context) error { | ||
| r.logger.Info("start running app live state reporter") | ||
|
|
||
| r.logger.Info("waiting for livestatestore to be ready") | ||
| if err := r.stateGetter.WaitForReady(ctx, 10*time.Minute); err != nil { | ||
| r.logger.Error("livestatestore was unable to be ready in time", zap.Error(err)) | ||
| return err | ||
| } | ||
|
|
||
| snapshotTicker := time.NewTicker(r.snapshotFlushInterval) | ||
| defer snapshotTicker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-snapshotTicker.C: | ||
| r.flushSnapshots(ctx) | ||
|
|
||
| case <-ctx.Done(): | ||
| r.logger.Info("app live state reporter has been stopped") | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (r *reporter) ProviderName() string { | ||
| return r.provider.Name | ||
| } | ||
|
|
||
| func (r *reporter) flushSnapshots(ctx context.Context) error { | ||
| apps := r.appLister.ListByCloudProvider(r.provider.Name) | ||
| for _, app := range apps { | ||
| state, ok := r.stateGetter.GetState(app.Id) | ||
| if !ok { | ||
| r.logger.Info(fmt.Sprintf("no app state of cloudrun application %s to report", app.Id)) | ||
| continue | ||
| } | ||
|
|
||
| snapshot := &model.ApplicationLiveStateSnapshot{ | ||
| ApplicationId: app.Id, | ||
| PipedId: app.PipedId, | ||
| ProjectId: app.ProjectId, | ||
| Kind: app.Kind, | ||
| Cloudrun: &model.CloudRunApplicationLiveState{ | ||
| Resources: state.Resources, | ||
| }, | ||
| Version: &state.Version, | ||
| } | ||
| snapshot.DetermineAppHealthStatus() | ||
| req := &pipedservice.ReportApplicationLiveStateRequest{ | ||
| Snapshot: snapshot, | ||
| } | ||
|
|
||
| if _, err := r.apiClient.ReportApplicationLiveState(ctx, req); err != nil { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We may need a new RPC to send a bunch of applications.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I think it's good to add new RPC to improve performance in the future. |
||
| r.logger.Error("failed to report application live state", | ||
| zap.String("application-id", app.Id), | ||
| zap.Error(err), | ||
| ) | ||
| continue | ||
| } | ||
| r.snapshotVersions[app.Id] = state.Version | ||
| r.logger.Info(fmt.Sprintf("successfully reported application live state for application: %s", app.Id)) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that the state will be updated every 10 minutes, right?
Do we have any way to make it refresh faster on UI?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nghialv
Exactly.
Currently, No we don't. Since livestatestore fetch the live state at 15s intervals and driftdetection and livestatereporter refer to it. But as one idea, it may be better that adding
forceRefreshflag intoGetApplicationLiveStateRequestin order to get latest live state synchronously.BTW, It might be a good idea to make this interval one minute like as driftdetection.