| 1 |
package calls |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"math/rand" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 9 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
func Filters(fo ...mesos.FilterOpt) scheduler.CallOpt { |
| 14 |
return func(c *scheduler.Call) { |
| 15 |
switch c.Type { |
| 16 |
case scheduler.Call_ACCEPT: |
| 17 |
c.Accept.Filters = mesos.OptionalFilters(fo...) |
| 18 |
case scheduler.Call_ACCEPT_INVERSE_OFFERS: |
| 19 |
c.AcceptInverseOffers.Filters = mesos.OptionalFilters(fo...) |
| 20 |
case scheduler.Call_DECLINE: |
| 21 |
c.Decline.Filters = mesos.OptionalFilters(fo...) |
| 22 |
case scheduler.Call_DECLINE_INVERSE_OFFERS: |
| 23 |
c.DeclineInverseOffers.Filters = mesos.OptionalFilters(fo...) |
| 24 |
default: |
| 25 |
panic("filters not supported for type " + c.Type.String()) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func RefuseSecondsWithJitter(r *rand.Rand, d time.Duration) scheduler.CallOpt { |
| 33 |
return Filters(func(f *mesos.Filters) { |
| 34 |
s := time.Duration(r.Int63n(int64(d))).Seconds() |
| 35 |
f.RefuseSeconds = &s |
| 36 |
}) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func RefuseSeconds(d time.Duration) scheduler.CallOpt { |
| 41 |
asFloat := d.Seconds() |
| 42 |
return Filters(func(f *mesos.Filters) { |
| 43 |
f.RefuseSeconds = &asFloat |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func Framework(id string) scheduler.CallOpt { |
| 49 |
return func(c *scheduler.Call) { |
| 50 |
c.FrameworkID = &mesos.FrameworkID{Value: id} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func Subscribe(info *mesos.FrameworkInfo) *scheduler.Call { |
| 57 |
return &scheduler.Call{ |
| 58 |
Type: scheduler.Call_SUBSCRIBE, |
| 59 |
FrameworkID: info.GetID(), |
| 60 |
Subscribe: &scheduler.Call_Subscribe{FrameworkInfo: info}, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func SubscribeTo(frameworkID string) scheduler.CallOpt { |
| 68 |
return func(call *scheduler.Call) { |
| 69 |
if call.Subscribe == nil { |
| 70 |
panic("illegal call option: Call.Subscribe was unexpectedly nil") |
| 71 |
} |
| 72 |
var frameworkProto *mesos.FrameworkID |
| 73 |
if frameworkID != "" { |
| 74 |
frameworkProto = &mesos.FrameworkID{Value: frameworkID} |
| 75 |
} |
| 76 |
call.Subscribe.FrameworkInfo.ID = frameworkProto |
| 77 |
call.FrameworkID = frameworkProto |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
type acceptBuilder struct { |
| 82 |
offerIDs map[mesos.OfferID]struct{} |
| 83 |
operations []mesos.Offer_Operation |
| 84 |
} |
| 85 |
|
| 86 |
type AcceptOpt func(*acceptBuilder) |
| 87 |
|
| 88 |
type OfferOperations []mesos.Offer_Operation |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
func (ob OfferOperations) WithOffers(ids ...mesos.OfferID) AcceptOpt { |
| 93 |
return func(ab *acceptBuilder) { |
| 94 |
for i := range ids { |
| 95 |
ab.offerIDs[ids[i]] = struct{}{} |
| 96 |
} |
| 97 |
ab.operations = append(ab.operations, ob...) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
func Accept(ops ...AcceptOpt) *scheduler.Call { |
| 104 |
ab := &acceptBuilder{ |
| 105 |
offerIDs: make(map[mesos.OfferID]struct{}, len(ops)), |
| 106 |
} |
| 107 |
for _, op := range ops { |
| 108 |
op(ab) |
| 109 |
} |
| 110 |
offerIDs := make([]mesos.OfferID, 0, len(ab.offerIDs)) |
| 111 |
for id := range ab.offerIDs { |
| 112 |
offerIDs = append(offerIDs, id) |
| 113 |
} |
| 114 |
return &scheduler.Call{ |
| 115 |
Type: scheduler.Call_ACCEPT, |
| 116 |
Accept: &scheduler.Call_Accept{ |
| 117 |
OfferIDs: offerIDs, |
| 118 |
Operations: ab.operations, |
| 119 |
}, |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func AcceptInverseOffers(offerIDs ...mesos.OfferID) *scheduler.Call { |
| 126 |
return &scheduler.Call{ |
| 127 |
Type: scheduler.Call_ACCEPT_INVERSE_OFFERS, |
| 128 |
AcceptInverseOffers: &scheduler.Call_AcceptInverseOffers{ |
| 129 |
InverseOfferIDs: offerIDs, |
| 130 |
}, |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
func DeclineInverseOffers(offerIDs ...mesos.OfferID) *scheduler.Call { |
| 137 |
return &scheduler.Call{ |
| 138 |
Type: scheduler.Call_DECLINE_INVERSE_OFFERS, |
| 139 |
DeclineInverseOffers: &scheduler.Call_DeclineInverseOffers{ |
| 140 |
InverseOfferIDs: offerIDs, |
| 141 |
}, |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func OpLaunch(ti ...mesos.TaskInfo) mesos.Offer_Operation { |
| 147 |
return mesos.Offer_Operation{ |
| 148 |
Type: mesos.Offer_Operation_LAUNCH, |
| 149 |
Launch: &mesos.Offer_Operation_Launch{ |
| 150 |
TaskInfos: ti, |
| 151 |
}, |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func OpLaunchGroup(ei mesos.ExecutorInfo, ti ...mesos.TaskInfo) mesos.Offer_Operation { |
| 156 |
return mesos.Offer_Operation{ |
| 157 |
Type: mesos.Offer_Operation_LAUNCH_GROUP, |
| 158 |
LaunchGroup: &mesos.Offer_Operation_LaunchGroup{ |
| 159 |
Executor: ei, |
| 160 |
TaskGroup: mesos.TaskGroupInfo{ |
| 161 |
Tasks: ti, |
| 162 |
}, |
| 163 |
}, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func OpReserve(rs ...mesos.Resource) mesos.Offer_Operation { |
| 168 |
return mesos.Offer_Operation{ |
| 169 |
Type: mesos.Offer_Operation_RESERVE, |
| 170 |
Reserve: &mesos.Offer_Operation_Reserve{ |
| 171 |
Resources: rs, |
| 172 |
}, |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func OpUnreserve(rs ...mesos.Resource) mesos.Offer_Operation { |
| 177 |
return mesos.Offer_Operation{ |
| 178 |
Type: mesos.Offer_Operation_UNRESERVE, |
| 179 |
Unreserve: &mesos.Offer_Operation_Unreserve{ |
| 180 |
Resources: rs, |
| 181 |
}, |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func OpCreate(rs ...mesos.Resource) mesos.Offer_Operation { |
| 186 |
return mesos.Offer_Operation{ |
| 187 |
Type: mesos.Offer_Operation_CREATE, |
| 188 |
Create: &mesos.Offer_Operation_Create{ |
| 189 |
Volumes: rs, |
| 190 |
}, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func OpDestroy(rs ...mesos.Resource) mesos.Offer_Operation { |
| 195 |
return mesos.Offer_Operation{ |
| 196 |
Type: mesos.Offer_Operation_DESTROY, |
| 197 |
Destroy: &mesos.Offer_Operation_Destroy{ |
| 198 |
Volumes: rs, |
| 199 |
}, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
func Revive() *scheduler.Call { |
| 206 |
return &scheduler.Call{Type: scheduler.Call_REVIVE} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func ReviveWith(roles []string) *scheduler.Call { |
| 212 |
return &scheduler.Call{ |
| 213 |
Type: scheduler.Call_REVIVE, |
| 214 |
Revive: &scheduler.Call_Revive{Roles: roles}, |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
func Suppress() *scheduler.Call { |
| 221 |
return &scheduler.Call{Type: scheduler.Call_SUPPRESS} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func SuppressWith(roles []string) *scheduler.Call { |
| 227 |
return &scheduler.Call{ |
| 228 |
Type: scheduler.Call_SUPPRESS, |
| 229 |
Suppress: &scheduler.Call_Suppress{Roles: roles}, |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
func Decline(offerIDs ...mesos.OfferID) *scheduler.Call { |
| 236 |
return &scheduler.Call{ |
| 237 |
Type: scheduler.Call_DECLINE, |
| 238 |
Decline: &scheduler.Call_Decline{ |
| 239 |
OfferIDs: offerIDs, |
| 240 |
}, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
func Kill(taskID, agentID string) *scheduler.Call { |
| 247 |
return &scheduler.Call{ |
| 248 |
Type: scheduler.Call_KILL, |
| 249 |
Kill: &scheduler.Call_Kill{ |
| 250 |
TaskID: mesos.TaskID{Value: taskID}, |
| 251 |
AgentID: optionalAgentID(agentID), |
| 252 |
}, |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
func Shutdown(executorID, agentID string) *scheduler.Call { |
| 259 |
return &scheduler.Call{ |
| 260 |
Type: scheduler.Call_SHUTDOWN, |
| 261 |
Shutdown: &scheduler.Call_Shutdown{ |
| 262 |
ExecutorID: mesos.ExecutorID{Value: executorID}, |
| 263 |
AgentID: mesos.AgentID{Value: agentID}, |
| 264 |
}, |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
func Acknowledge(agentID, taskID string, uuid []byte) *scheduler.Call { |
| 271 |
return &scheduler.Call{ |
| 272 |
Type: scheduler.Call_ACKNOWLEDGE, |
| 273 |
Acknowledge: &scheduler.Call_Acknowledge{ |
| 274 |
AgentID: mesos.AgentID{Value: agentID}, |
| 275 |
TaskID: mesos.TaskID{Value: taskID}, |
| 276 |
UUID: uuid, |
| 277 |
}, |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
func ReconcileTasks(tasks map[string]string) scheduler.ReconcileOpt { |
| 285 |
return func(cr *scheduler.Call_Reconcile) { |
| 286 |
if len(tasks) == 0 { |
| 287 |
cr.Tasks = nil |
| 288 |
return |
| 289 |
} |
| 290 |
result := make([]scheduler.Call_Reconcile_Task, len(tasks)) |
| 291 |
i := 0 |
| 292 |
for k, v := range tasks { |
| 293 |
result[i].TaskID = mesos.TaskID{Value: k} |
| 294 |
result[i].AgentID = optionalAgentID(v) |
| 295 |
i++ |
| 296 |
} |
| 297 |
cr.Tasks = result |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
func Reconcile(opts ...scheduler.ReconcileOpt) *scheduler.Call { |
| 305 |
return &scheduler.Call{ |
| 306 |
Type: scheduler.Call_RECONCILE, |
| 307 |
Reconcile: (&scheduler.Call_Reconcile{}).With(opts...), |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
func Message(agentID, executorID string, data []byte) *scheduler.Call { |
| 314 |
return &scheduler.Call{ |
| 315 |
Type: scheduler.Call_MESSAGE, |
| 316 |
Message: &scheduler.Call_Message{ |
| 317 |
AgentID: mesos.AgentID{Value: agentID}, |
| 318 |
ExecutorID: mesos.ExecutorID{Value: executorID}, |
| 319 |
Data: data, |
| 320 |
}, |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
func Request(requests ...mesos.Request) *scheduler.Call { |
| 327 |
return &scheduler.Call{ |
| 328 |
Type: scheduler.Call_REQUEST, |
| 329 |
Request: &scheduler.Call_Request{ |
| 330 |
Requests: requests, |
| 331 |
}, |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func optionalAgentID(agentID string) *mesos.AgentID { |
| 336 |
if agentID == "" { |
| 337 |
return nil |
| 338 |
} |
| 339 |
return &mesos.AgentID{Value: agentID} |
| 340 |
} |
| 341 |
|
| 342 |
func errInvalidCall(reason string) error { |
| 343 |
return errors.New("invalid call: " + reason) |
| 344 |
} |