This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
/
machine.go
193 lines (165 loc) · 5.17 KB
/
machine.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
// Copyright 2014 The fleet 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 registry
import (
"strings"
"time"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
"github.com/coreos/fleet/machine"
"path"
)
const (
machinePrefix = "machines"
)
func (r *EtcdRegistry) Machines() (machines []machine.MachineState, err error) {
key := r.prefixed(machinePrefix)
opts := &etcd.GetOptions{
Sort: true,
Recursive: true,
}
resp, err := r.kAPI.Get(context.Background(), key, opts)
if err != nil {
if isEtcdError(err, etcd.ErrorCodeKeyNotFound) {
err = nil
}
return
}
for _, node := range resp.Node.Nodes {
var mach machine.MachineState
mach, err = readMachineState(node)
if err != nil {
return
}
if mach.ID != "" {
machines = append(machines, mach)
}
}
return
}
func (r *EtcdRegistry) CreateMachineState(ms machine.MachineState, ttl time.Duration) (uint64, error) {
val, err := marshal(ms)
if err != nil {
return uint64(0), err
}
key := r.prefixed(machinePrefix, ms.ID, "object")
opts := &etcd.SetOptions{
PrevExist: etcd.PrevNoExist,
TTL: ttl,
}
resp, err := r.kAPI.Set(context.Background(), key, val, opts)
if err != nil {
return uint64(0), err
}
return resp.Node.ModifiedIndex, nil
}
func (r *EtcdRegistry) SetMachineState(ms machine.MachineState, ttl time.Duration) (uint64, error) {
val, err := marshal(ms)
if err != nil {
return uint64(0), err
}
key := r.prefixed(machinePrefix, ms.ID, "object")
opts := &etcd.SetOptions{
PrevExist: etcd.PrevExist,
TTL: ttl,
}
resp, err := r.kAPI.Set(context.Background(), key, val, opts)
if err == nil {
return resp.Node.ModifiedIndex, nil
}
// If state was not present, explicitly create it so the other members
// in the cluster know this is a new member
opts.PrevExist = etcd.PrevNoExist
resp, err = r.kAPI.Set(context.Background(), key, val, opts)
if err != nil {
return uint64(0), err
}
return resp.Node.ModifiedIndex, nil
}
func (r *EtcdRegistry) MachineState(machID string) (machine.MachineState, error) {
key := path.Join(r.keyPrefix, machinePrefix, machID)
opts := &etcd.GetOptions{
Recursive: true,
Sort: true,
}
resp, err := r.kAPI.Get(context.Background(), key, opts)
if err != nil {
return machine.MachineState{}, err
}
return readMachineState(resp.Node)
}
func (r *EtcdRegistry) SetMachineMetadata(machID string, key string, value string) error {
key = path.Join(r.keyPrefix, machinePrefix, machID, "metadata", key)
opts := &etcd.SetOptions{}
_, err := r.kAPI.Set(context.Background(), key, value, opts)
return err
}
func (r *EtcdRegistry) DeleteMachineMetadata(machID string, key string) error {
// Deleting a key sets its value to "" to allow for intelligent merging
// between the machine-defined metadata and the dynamic metadata.
// See mergeMetadata for more detail.
return r.SetMachineMetadata(machID, key, "")
}
func (r *EtcdRegistry) RemoveMachineState(machID string) error {
key := r.prefixed(machinePrefix, machID, "object")
_, err := r.kAPI.Delete(context.Background(), key, nil)
if isEtcdError(err, etcd.ErrorCodeKeyNotFound) {
err = nil
}
return err
}
// mergeMetadata merges the machine-set metadata with the dynamic metadata to better facilitate
// machines leaving and rejoining a cluster.
// Merging metadata uses the following rules:
// - Any keys that are only in one collection are added as-is
// - Any keys that exist in both, the dynamic value takes precence
// - Any keys that have a zero-value string in the dynamic metadata are considered deleted
// and are not included in the final collection
func mergeMetadata(machineMetadata, dynamicMetadata map[string]string) map[string]string {
if dynamicMetadata == nil {
return machineMetadata
}
finalMetadata := make(map[string]string, len(dynamicMetadata))
for k, v := range machineMetadata {
finalMetadata[k] = v
}
for k, v := range dynamicMetadata {
if v == "" {
delete(finalMetadata, k)
} else {
finalMetadata[k] = v
}
}
return finalMetadata
}
// readMachineState reads machine state from an etcd node
func readMachineState(node *etcd.Node) (mach machine.MachineState, err error) {
var metadata map[string]string
for _, obj := range node.Nodes {
if strings.HasSuffix(obj.Key, "/object") {
err = unmarshal(obj.Value, &mach)
if err != nil {
return
}
} else if strings.HasSuffix(obj.Key, "/metadata") {
// Load metadata into a separate map to avoid stepping on it when deserializing the object key
metadata = make(map[string]string, len(obj.Nodes))
for _, mdnode := range obj.Nodes {
metadata[path.Base(mdnode.Key)] = mdnode.Value
}
}
}
mach.Metadata = mergeMetadata(mach.Metadata, metadata)
return
}