-
Notifications
You must be signed in to change notification settings - Fork 71
/
server.go
149 lines (126 loc) · 3.84 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// (C) Copyright Confidential Containers Contributors
// SPDX-License-Identifier: Apache-2.0
package adaptor
import (
"context"
"log"
"net"
"os"
"path/filepath"
"sync"
"time"
"github.com/containerd/ttrpc"
pbHypervisor "github.com/kata-containers/kata-containers/src/runtime/protocols/hypervisor"
"github.com/confidential-containers/cloud-api-adaptor/pkg/adaptor/cloud"
"github.com/confidential-containers/cloud-api-adaptor/pkg/adaptor/proxy"
"github.com/confidential-containers/cloud-api-adaptor/pkg/adaptor/vminfo"
"github.com/confidential-containers/cloud-api-adaptor/pkg/podnetwork"
"github.com/confidential-containers/cloud-api-adaptor/pkg/util/tlsutil"
pbPodVMInfo "github.com/confidential-containers/cloud-api-adaptor/proto/podvminfo"
)
var logger = log.New(log.Writer(), "[adaptor] ", log.LstdFlags|log.Lmsgprefix)
const (
DefaultSocketPath = "/run/peerpod/hypervisor.sock"
DefaultPodsDir = "/run/peerpod/pods"
)
type ServerConfig struct {
TLSConfig *tlsutil.TLSConfig
SocketPath string
CriSocketPath string
PauseImage string
PodsDir string
ForwarderPort string
ProxyTimeout time.Duration
AAKBCParams string
EnableCloudConfigVerify bool
}
type Server interface {
Start(ctx context.Context) error
Shutdown() error
Ready() chan struct{}
}
type server struct {
cloudService cloud.Service
vmInfoService pbPodVMInfo.PodVMInfoService
workerNode podnetwork.WorkerNode
ttRpc *ttrpc.Server
readyCh chan struct{}
stopCh chan struct{}
socketPath string
stopOnce sync.Once
enableCloudConfigVerify bool
}
func NewServer(provider cloud.Provider, cfg *ServerConfig, workerNode podnetwork.WorkerNode) Server {
logger.Printf("server config: %#v", cfg)
agentFactory := proxy.NewFactory(cfg.PauseImage, cfg.CriSocketPath, cfg.TLSConfig, cfg.ProxyTimeout)
cloudService := cloud.NewService(provider, agentFactory, workerNode, cfg.PodsDir, cfg.ForwarderPort, cfg.AAKBCParams)
vmInfoService := vminfo.NewService(cloudService)
return &server{
socketPath: cfg.SocketPath,
cloudService: cloudService,
vmInfoService: vmInfoService,
workerNode: workerNode,
readyCh: make(chan struct{}),
stopCh: make(chan struct{}),
enableCloudConfigVerify: cfg.EnableCloudConfigVerify,
}
}
func (s *server) Start(ctx context.Context) (err error) {
if s.enableCloudConfigVerify {
verifierErr := s.cloudService.ConfigVerifier()
if verifierErr != nil {
return err
}
}
ttRpc, err := ttrpc.NewServer()
if err != nil {
return err
}
s.ttRpc = ttRpc
if err := os.MkdirAll(filepath.Dir(s.socketPath), os.ModePerm); err != nil {
return err
}
if err := os.RemoveAll(s.socketPath); err != nil { // just in case socket wasn't cleaned
return err
}
pbHypervisor.RegisterHypervisorService(s.ttRpc, s.cloudService)
pbPodVMInfo.RegisterPodVMInfoService(s.ttRpc, s.vmInfoService)
listener, err := net.Listen("unix", s.socketPath)
if err != nil {
return err
}
ttRpcErr := make(chan error)
go func() {
defer close(ttRpcErr)
if err := s.ttRpc.Serve(ctx, listener); err != nil {
ttRpcErr <- err
}
}()
defer func() {
ttRpcShutdownErr := s.ttRpc.Shutdown(context.Background())
if ttRpcShutdownErr != nil && err == nil {
err = ttRpcShutdownErr
}
}()
close(s.readyCh)
logger.Printf("server started")
select {
case <-ctx.Done():
shutdownErr := s.Shutdown()
if shutdownErr != nil && err == nil {
err = shutdownErr
}
case <-s.stopCh:
case err = <-ttRpcErr:
}
return err
}
func (s *server) Shutdown() error {
s.stopOnce.Do(func() {
close(s.stopCh)
})
return s.cloudService.Teardown()
}
func (s *server) Ready() chan struct{} {
return s.readyCh
}