Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions pkg/app/piped/livestatereporter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/pipe-cd/pipecd/pkg/app/piped/livestatereporter",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/piped/livestatereporter/cloudrun:go_default_library",
"//pkg/app/piped/livestatereporter/kubernetes:go_default_library",
"//pkg/app/piped/livestatestore:go_default_library",
"//pkg/app/server/service/pipedservice:go_default_library",
Expand Down
16 changes: 16 additions & 0 deletions pkg/app/piped/livestatereporter/cloudrun/BUILD.bazel
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",
],
)
134 changes: 134 additions & 0 deletions pkg/app/piped/livestatereporter/cloudrun/report.go
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:

Copy link
Copy Markdown
Member

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?

@knanao knanao Feb 21, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@nghialv

This means that the state will be updated every 10 minutes, right?

Exactly.

Do we have any way to make it refresh faster on UI?

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 forceRefresh flag into GetApplicationLiveStateRequest in order to get latest live state synchronously.
BTW, It might be a good idea to make this interval one minute like as driftdetection.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

@knanao knanao Feb 21, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
}
13 changes: 10 additions & 3 deletions pkg/app/piped/livestatereporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"

"github.com/pipe-cd/pipecd/pkg/app/piped/livestatereporter/cloudrun"
"github.com/pipe-cd/pipecd/pkg/app/piped/livestatereporter/kubernetes"
"github.com/pipe-cd/pipecd/pkg/app/piped/livestatestore"
"github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice"
Expand Down Expand Up @@ -62,16 +63,22 @@ func NewReporter(appLister applicationLister, stateGetter livestatestore.Getter,
}

for _, cp := range cfg.CloudProviders {
errFmt := fmt.Sprintf("unable to find live state getter for cloud provider: %s", cp.Name)
switch cp.Type {
case model.CloudProviderKubernetes:
sg, ok := stateGetter.KubernetesGetter(cp.Name)
if !ok {
r.logger.Error(fmt.Sprintf("unable to find live state getter for cloud provider: %s", cp.Name))
r.logger.Error(errFmt)
continue
}
r.reporters = append(r.reporters, kubernetes.NewReporter(cp, appLister, sg, apiClient, logger))

default:
case model.CloudProviderCloudRun:
sg, ok := stateGetter.CloudRunGetter(cp.Name)
if !ok {
r.logger.Error(errFmt)
continue
}
r.reporters = append(r.reporters, cloudrun.NewReporter(cp, appLister, sg, apiClient, logger))
}
}

Expand Down