-
Notifications
You must be signed in to change notification settings - Fork 566
/
main.go
257 lines (238 loc) · 7.76 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"strings"
units "github.com/docker/go-units"
"github.com/pachyderm/pachyderm/src/client"
healthclient "github.com/pachyderm/pachyderm/src/client/health"
pfsclient "github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pkg/discovery"
"github.com/pachyderm/pachyderm/src/client/pkg/grpcutil"
"github.com/pachyderm/pachyderm/src/client/pkg/shard"
"github.com/pachyderm/pachyderm/src/client/pkg/uuid"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
"github.com/pachyderm/pachyderm/src/client/version"
"github.com/pachyderm/pachyderm/src/server/health"
pfs_server "github.com/pachyderm/pachyderm/src/server/pfs/server"
cache_pb "github.com/pachyderm/pachyderm/src/server/pkg/cache/groupcachepb"
cache_server "github.com/pachyderm/pachyderm/src/server/pkg/cache/server"
"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
"github.com/pachyderm/pachyderm/src/server/pkg/metrics"
"github.com/pachyderm/pachyderm/src/server/pkg/netutil"
ppsserver "github.com/pachyderm/pachyderm/src/server/pps"
pps_server "github.com/pachyderm/pachyderm/src/server/pps/server"
flag "github.com/spf13/pflag"
"go.pedge.io/lion"
"go.pedge.io/lion/proto"
"google.golang.org/grpc"
"k8s.io/kubernetes/pkg/api"
kube_client "k8s.io/kubernetes/pkg/client/restclient"
kube "k8s.io/kubernetes/pkg/client/unversioned"
)
var readinessCheck bool
var migrate string
func init() {
flag.BoolVar(&readinessCheck, "readiness-check", false, "Set to true when checking if local pod is ready")
flag.StringVar(&migrate, "migrate", "", "Use the format FROM_VERSION-TO_VERSION; e.g. 1.2.4-1.3.0")
flag.Parse()
}
type appEnv struct {
Port uint16 `env:"PORT,default=650"`
NumShards uint64 `env:"NUM_SHARDS,default=32"`
StorageRoot string `env:"PACH_ROOT,default=/pach"`
StorageBackend string `env:"STORAGE_BACKEND,default="`
PPSEtcdPrefix string `env:"PPS_ETCD_PREFIX,default=pachyderm_pps"`
PFSEtcdPrefix string `env:"PFS_ETCD_PREFIX,default=pachyderm_pfs"`
KubeAddress string `env:"KUBERNETES_PORT_443_TCP_ADDR,required"`
EtcdAddress string `env:"ETCD_PORT_2379_TCP_ADDR,required"`
Namespace string `env:"NAMESPACE,default=default"`
Metrics bool `env:"METRICS,default=true"`
Init bool `env:"INIT,default=false"`
BlockCacheBytes string `env:"BLOCK_CACHE_BYTES,default=5G"`
PFSCacheBytes string `env:"PFS_CACHE_BYTES,default=1G"`
WorkerImage string `env:"WORKER_IMAGE,default="`
WorkerImagePullPolicy string `env:"WORKER_IMAGE_PULL_POLICY,default="`
LogLevel string `env:"LOG_LEVEL,default=info"`
}
func main() {
cmdutil.Main(do, &appEnv{})
}
func do(appEnvObj interface{}) error {
go func() {
lion.Println(http.ListenAndServe(":651", nil))
}()
appEnv := appEnvObj.(*appEnv)
switch appEnv.LogLevel {
case "debug":
lion.SetLevel(lion.LevelDebug)
case "info":
lion.SetLevel(lion.LevelInfo)
case "error":
lion.SetLevel(lion.LevelError)
default:
lion.Errorf("Unrecognized log level %s, falling back to default of \"info\"", appEnv.LogLevel)
lion.SetLevel(lion.LevelInfo)
}
etcdAddress := fmt.Sprintf("http://%s:2379", appEnv.EtcdAddress)
etcdClient := getEtcdClient(etcdAddress)
if readinessCheck {
c, err := client.NewFromAddress("127.0.0.1:650")
if err != nil {
return err
}
// We want to use a PPS API instead of a PFS API because PFS APIs
// typically talk to every node, but the point of the readiness probe
// is that it checks to see if this particular node is functioning,
// and removing it from the service if it's not. So if we use a PFS
// API such as ListRepo for readiness probe, then the failure of any
// node will result in the failures of all readiness probes, causing
// all nodes to be removed from the pachd service.
_, err = c.ListPipeline()
if err != nil {
return err
}
os.Exit(0)
return nil
}
clusterID, err := getClusterID(etcdClient)
if err != nil {
return err
}
kubeClient, err := getKubeClient(appEnv)
if err != nil {
return err
}
var reporter *metrics.Reporter
if appEnv.Metrics {
reporter = metrics.NewReporter(clusterID, kubeClient)
}
address, err := netutil.ExternalIP()
if err != nil {
return err
}
address = fmt.Sprintf("%s:%d", address, appEnv.Port)
sharder := shard.NewSharder(
etcdClient,
appEnv.NumShards,
appEnv.Namespace,
)
go func() {
if err := sharder.AssignRoles(address, nil); err != nil {
protolion.Printf("error from sharder.AssignRoles: %s", sanitizeErr(err))
}
}()
pfsCacheBytes, err := units.RAMInBytes(appEnv.PFSCacheBytes)
if err != nil {
return err
}
router := shard.NewRouter(
sharder,
grpcutil.NewDialer(
grpc.WithInsecure(),
),
address,
)
cacheServer := cache_server.NewCacheServer(router, appEnv.NumShards)
pfsAPIServer, err := pfs_server.NewAPIServer(address, []string{etcdAddress}, appEnv.PFSEtcdPrefix, pfsCacheBytes, reporter)
if err != nil {
return err
}
ppsAPIServer, err := pps_server.NewAPIServer(
etcdAddress,
appEnv.PPSEtcdPrefix,
ppsserver.NewHasher(appEnv.NumShards, appEnv.NumShards),
address,
kubeClient,
getNamespace(),
appEnv.WorkerImage,
appEnv.WorkerImagePullPolicy,
reporter,
)
if err != nil {
return err
}
go func() {
if err := sharder.RegisterFrontends(nil, address, []shard.Frontend{cacheServer}); err != nil {
protolion.Printf("error from sharder.RegisterFrontend %s", sanitizeErr(err))
}
}()
go func() {
if err := sharder.Register(nil, address, []shard.Server{ppsAPIServer, cacheServer}); err != nil {
protolion.Printf("error from sharder.Register %s", sanitizeErr(err))
}
}()
blockCacheBytes, err := units.RAMInBytes(appEnv.BlockCacheBytes)
if err != nil {
return err
}
blockAPIServer, err := pfs_server.NewBlockAPIServer(appEnv.StorageRoot, blockCacheBytes, appEnv.StorageBackend)
if err != nil {
return err
}
healthServer := health.NewHealthServer()
return grpcutil.Serve(
func(s *grpc.Server) {
pfsclient.RegisterAPIServer(s, pfsAPIServer)
pfsclient.RegisterObjectAPIServer(s, blockAPIServer)
ppsclient.RegisterAPIServer(s, ppsAPIServer)
cache_pb.RegisterGroupCacheServer(s, cacheServer)
healthclient.RegisterHealthServer(s, healthServer)
},
grpcutil.ServeOptions{
Version: version.Version,
MaxMsgSize: grpcutil.MaxMsgSize,
},
grpcutil.ServeEnv{
GRPCPort: appEnv.Port,
},
)
}
func getEtcdClient(etcdAddress string) discovery.Client {
return discovery.NewEtcdClient(etcdAddress)
}
const clusterIDKey = "cluster-id"
func getClusterID(client discovery.Client) (string, error) {
id, err := client.Get(clusterIDKey)
// if it's a key not found error then we create the key
if err != nil && strings.Contains(err.Error(), "not found") {
// This might error if it races with another pachd trying to set the
// cluster id so we ignore the error.
client.Set(clusterIDKey, uuid.NewWithoutDashes(), 0)
} else if err != nil {
return "", err
} else {
return id, nil
}
return client.Get(clusterIDKey)
}
func getKubeClient(env *appEnv) (*kube.Client, error) {
kubeClient, err := kube.NewInCluster()
if err != nil {
protolion.Errorf("falling back to insecure kube client due to error from NewInCluster: %s", sanitizeErr(err))
} else {
return kubeClient, err
}
config := &kube_client.Config{
Host: fmt.Sprintf("%s:443", env.KubeAddress),
Insecure: true,
}
return kube.New(config)
}
// getNamespace returns the kubernetes namespace that this pachd pod runs in
func getNamespace() string {
namespace := os.Getenv("PACHD_POD_NAMESPACE")
if namespace != "" {
return namespace
}
return api.NamespaceDefault
}
func sanitizeErr(err error) error {
if err == nil {
return nil
}
return errors.New(grpc.ErrorDesc(err))
}