-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.go
124 lines (107 loc) · 3.43 KB
/
server.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
119
120
121
122
123
124
package repo
import (
"fmt"
"sync"
"time"
mapset "github.com/deckarep/golang-set"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/flomesh-io/fsm/pkg/catalog"
"github.com/flomesh-io/fsm/pkg/certificate"
"github.com/flomesh-io/fsm/pkg/configurator"
"github.com/flomesh-io/fsm/pkg/k8s"
"github.com/flomesh-io/fsm/pkg/messaging"
"github.com/flomesh-io/fsm/pkg/sidecar/providers/pipy/client"
"github.com/flomesh-io/fsm/pkg/sidecar/providers/pipy/registry"
"github.com/flomesh-io/fsm/pkg/workerpool"
)
const (
// ServerType is the type identifier for the ADS server
ServerType = "pipy-Repo"
// workerPoolSize is the default number of workerpool workers (0 is GOMAXPROCS)
workerPoolSize = 0
fsmCodebaseConfig = "config.json"
)
var (
fsmCodebase = "fsm-base"
fsmSidecarCodebase = "fsm-sidecar"
fsmCodebaseRepo = fmt.Sprintf("/%s", fsmCodebase)
)
// NewRepoServer creates a new Aggregated Discovery Service server
func NewRepoServer(meshCatalog catalog.MeshCataloger, proxyRegistry *registry.ProxyRegistry, _ bool, fsmNamespace string, cfg configurator.Configurator, certManager *certificate.Manager, kubecontroller k8s.Controller, msgBroker *messaging.Broker) *Server {
if len(cfg.GetRepoServerCodebase()) > 0 {
fsmCodebase = fmt.Sprintf("%s/%s", cfg.GetRepoServerCodebase(), fsmCodebase)
fsmSidecarCodebase = fmt.Sprintf("%s/%s", cfg.GetRepoServerCodebase(), fsmSidecarCodebase)
fsmCodebaseRepo = fmt.Sprintf("/%s", fsmCodebase)
}
server := Server{
catalog: meshCatalog,
proxyRegistry: proxyRegistry,
fsmNamespace: fsmNamespace,
cfg: cfg,
certManager: certManager,
workQueues: workerpool.NewWorkerPool(workerPoolSize),
kubeController: kubecontroller,
configVerMutex: sync.Mutex{},
configVersion: make(map[string]uint64),
pluginSet: mapset.NewSet(),
msgBroker: msgBroker,
repoClient: client.NewRepoClient(cfg.GetRepoServerIPAddr(), uint16(cfg.GetProxyServerPort())),
}
prettyConfig = func() bool {
return cfg.GetMeshConfig().Spec.FeatureFlags.EnableSidecarPrettyConfig
}
return &server
}
// Start starts the codebase push server
func (s *Server) Start(_ uint32, _ *certificate.Certificate) error {
// wait until pipy repo is up
err := wait.PollImmediate(10*time.Second, 300*time.Second, func() (bool, error) {
success, err := s.repoClient.IsRepoUp()
if success {
log.Info().Msg("Repo is READY!")
return success, nil
}
log.Error().Msg("Repo is not up, sleeping ...")
return success, err
})
if err != nil {
log.Error().Err(err)
return err
}
s.repoClient.Restore = func() error {
_, restoreErr := s.repoClient.Batch(fmt.Sprintf("%d", 0), []client.Batch{
{
Basepath: fsmCodebase,
Items: fsmCodebaseItems,
},
})
if restoreErr != nil {
log.Error().Err(restoreErr)
return restoreErr
}
// wait until base codebase is ready
restoreErr = wait.PollImmediate(5*time.Second, 90*time.Second, func() (bool, error) {
success, _, codebaseErr := s.repoClient.GetCodebase(fsmCodebase)
if success {
log.Info().Msg("Base codebase is READY!")
return success, nil
}
log.Error().Msg("Base codebase is NOT READY, sleeping ...")
return success, codebaseErr
})
if restoreErr != nil {
log.Error().Err(restoreErr)
return restoreErr
}
return nil
}
err = s.repoClient.Restore()
if err != nil {
log.Error().Err(err)
return err
}
// Start broadcast listener thread
go s.broadcastListener()
s.ready = true
return nil
}