-
Notifications
You must be signed in to change notification settings - Fork 148
/
cloudrun.go
118 lines (96 loc) · 2.95 KB
/
cloudrun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2023 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"
"time"
"go.uber.org/zap"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/cloudrun"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)
type Store struct {
store *store
logger *zap.Logger
interval time.Duration
firstSyncedCh chan error
}
type Getter interface {
GetState(appID string) (State, bool)
GetServiceManifest(appID string) (provider.ServiceManifest, bool)
WaitForReady(ctx context.Context, timeout time.Duration) error
}
type State struct {
Resources []*model.CloudRunResourceState
Version model.ApplicationLiveStateVersion
}
func NewStore(ctx context.Context, cfg *config.PlatformProviderCloudRunConfig, platformProvider string, logger *zap.Logger) (*Store, error) {
logger = logger.Named("cloudrun").
With(zap.String("platform-provider", platformProvider))
client, err := provider.DefaultRegistry().Client(ctx, platformProvider, cfg, logger)
if err != nil {
return nil, err
}
store := &Store{
store: &store{
client: client,
logger: logger.Named("store"),
},
interval: 15 * time.Second,
logger: logger,
firstSyncedCh: make(chan error, 1),
}
return store, nil
}
func (s *Store) Run(ctx context.Context) error {
s.logger.Info("start running cloudrun app state store")
tick := time.NewTicker(s.interval)
defer tick.Stop()
// Run the first sync cloudrun services.
if err := s.store.run(ctx); err != nil {
s.firstSyncedCh <- err
return err
}
s.logger.Info("successfully the first synced all cloudrun services")
close(s.firstSyncedCh)
for {
select {
case <-ctx.Done():
s.logger.Info("cloudrun app state store has been stopped")
return nil
case <-tick.C:
if err := s.store.run(ctx); err != nil {
s.logger.Error("failed to sync cloudrun services", zap.Error(err))
continue
}
s.logger.Info("successfully synced all cloudrun services")
}
}
}
func (s *Store) GetServiceManifest(appID string) (provider.ServiceManifest, bool) {
return s.store.getServiceManifest(appID)
}
func (s *Store) GetState(appID string) (State, bool) {
return s.store.getState(appID)
}
func (s *Store) WaitForReady(ctx context.Context, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
select {
case <-ctx.Done():
return nil
case err := <-s.firstSyncedCh:
return err
}
}