-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterservice.go
113 lines (88 loc) · 2.83 KB
/
clusterservice.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
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 clusterd
import (
etcd "github.com/coreos/etcd/client"
"github.com/rook/rook/pkg/clusterd/inventory"
"github.com/rook/rook/pkg/util"
"github.com/rook/rook/pkg/util/exec"
"github.com/rook/rook/pkg/util/proc"
)
const (
LeaderElectionKey = "orchestrator-leader"
)
type ClusterService struct {
Name string
Leader ServiceLeader
Agents []ServiceAgent
}
// Interface implemented by a service that has been elected leader
type ServiceLeader interface {
// Get the list of etcd keys that when changed should trigger an orchestration
RefreshKeys() []*RefreshKey
// Refresh the service
HandleRefresh(e *RefreshEvent)
}
type ServiceAgent interface {
// Get the name of the service agent
Name() string
// Initialize the agents from the context, allowing them to store desired state in etcd before orchestration starts.
Initialize(context *Context) error
// Configure the service according to the changes requested by the leader
ConfigureLocalService(context *Context) error
// Remove a service that is no longer needed
DestroyLocalService(context *Context) error
}
// The context for loading or applying the configuration state of a service.
type Context struct {
// The registered services for cluster configuration
Services []*ClusterService
// The latest inventory information
Inventory *inventory.Config
// The local node ID
NodeID string
// The etcd client for get/set config values
EtcdClient etcd.KeysAPI
// The implementation of executing a console command
Executor exec.Executor
// The process manager for launching a process
ProcMan *proc.ProcManager
// The root configuration directory used by services
ConfigDir string
// A value indicating if debug logging/tracing should be enabled
Debug bool
}
func copyContext(c *Context) *Context {
return &Context{
Services: c.Services,
NodeID: c.NodeID,
EtcdClient: c.EtcdClient,
Executor: c.Executor,
ProcMan: c.ProcMan,
Inventory: c.Inventory,
ConfigDir: c.ConfigDir,
Debug: c.Debug,
}
}
func (c *Context) GetExecutor() exec.Executor {
if c.Executor == nil {
return &exec.CommandExecutor{}
}
return c.Executor
}
func (c *Context) GetEtcdClient() (etcd.KeysAPI, error) {
if c.EtcdClient == nil {
return util.GetEtcdClientFromEnv()
}
return c.EtcdClient, nil
}