| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package mesosutil |
| 20 |
|
| 21 |
import ( |
| 22 |
"github.com/gogo/protobuf/proto" |
| 23 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 24 |
) |
| 25 |
|
| 26 |
func NewValueRange(begin, end uint64) *mesos.Value_Range { |
| 27 |
return &mesos.Value_Range{Begin: proto.Uint64(begin), End: proto.Uint64(end)} |
| 28 |
} |
| 29 |
|
| 30 |
func FilterResources(resources []*mesos.Resource, filter func(*mesos.Resource) bool) (result []*mesos.Resource) { |
| 31 |
for _, resource := range resources { |
| 32 |
if filter(resource) { |
| 33 |
result = append(result, resource) |
| 34 |
} |
| 35 |
} |
| 36 |
return result |
| 37 |
} |
| 38 |
|
| 39 |
func AddResourceRevocable(resource *mesos.Resource) *mesos.Resource { |
| 40 |
resource.Revocable = &mesos.Resource_RevocableInfo{} |
| 41 |
return resource |
| 42 |
} |
| 43 |
|
| 44 |
func NewScalarResourceWithRevocable(name string, value float64) *mesos.Resource { |
| 45 |
return AddResourceRevocable(NewScalarResource(name, value)) |
| 46 |
} |
| 47 |
|
| 48 |
func AddResourceReservation(resource *mesos.Resource, principal string, role string) *mesos.Resource { |
| 49 |
resource.Reservation = &mesos.Resource_ReservationInfo{Principal: proto.String(principal)} |
| 50 |
resource.Role = proto.String(role) |
| 51 |
return resource |
| 52 |
} |
| 53 |
|
| 54 |
func NewScalarResourceWithReservation(name string, value float64, principal string, role string) *mesos.Resource { |
| 55 |
return AddResourceReservation(NewScalarResource(name, value), principal, role) |
| 56 |
} |
| 57 |
|
| 58 |
func NewRangesResourceWithReservation(name string, ranges []*mesos.Value_Range, principal string, role string) *mesos.Resource { |
| 59 |
return AddResourceReservation(NewRangesResource(name, ranges), principal, role) |
| 60 |
} |
| 61 |
|
| 62 |
func NewSetResourceWithReservation(name string, items []string, principal string, role string) *mesos.Resource { |
| 63 |
return AddResourceReservation(NewSetResource(name, items), principal, role) |
| 64 |
} |
| 65 |
|
| 66 |
func NewVolumeResourceWithReservation(val float64, containerPath string, persistenceId string, mode *mesos.Volume_Mode, principal string, role string) *mesos.Resource { |
| 67 |
return AddResourceReservation(NewVolumeResource(val, containerPath, persistenceId, mode), principal, role) |
| 68 |
} |
| 69 |
|
| 70 |
func NewScalarResource(name string, val float64) *mesos.Resource { |
| 71 |
return &mesos.Resource{ |
| 72 |
Name: proto.String(name), |
| 73 |
Type: mesos.Value_SCALAR.Enum(), |
| 74 |
Scalar: &mesos.Value_Scalar{Value: proto.Float64(val)}, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func NewRangesResource(name string, ranges []*mesos.Value_Range) *mesos.Resource { |
| 79 |
return &mesos.Resource{ |
| 80 |
Name: proto.String(name), |
| 81 |
Type: mesos.Value_RANGES.Enum(), |
| 82 |
Ranges: &mesos.Value_Ranges{Range: ranges}, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func NewSetResource(name string, items []string) *mesos.Resource { |
| 87 |
return &mesos.Resource{ |
| 88 |
Name: proto.String(name), |
| 89 |
Type: mesos.Value_SET.Enum(), |
| 90 |
Set: &mesos.Value_Set{Item: items}, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func NewVolumeResource(val float64, containerPath string, persistenceId string, mode *mesos.Volume_Mode) *mesos.Resource { |
| 95 |
resource := NewScalarResource("disk", val) |
| 96 |
resource.Disk = &mesos.Resource_DiskInfo{ |
| 97 |
Persistence: &mesos.Resource_DiskInfo_Persistence{Id: proto.String(persistenceId)}, |
| 98 |
Volume: &mesos.Volume{ContainerPath: proto.String(containerPath), Mode: mode}, |
| 99 |
} |
| 100 |
return resource |
| 101 |
} |
| 102 |
|
| 103 |
func NewFrameworkID(id string) *mesos.FrameworkID { |
| 104 |
return &mesos.FrameworkID{Value: proto.String(id)} |
| 105 |
} |
| 106 |
|
| 107 |
func NewFrameworkInfo(user, name string, frameworkId *mesos.FrameworkID) *mesos.FrameworkInfo { |
| 108 |
return &mesos.FrameworkInfo{ |
| 109 |
User: proto.String(user), |
| 110 |
Name: proto.String(name), |
| 111 |
Id: frameworkId, |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func NewMasterInfo(id string, ip, port uint32) *mesos.MasterInfo { |
| 116 |
return &mesos.MasterInfo{ |
| 117 |
Id: proto.String(id), |
| 118 |
Ip: proto.Uint32(ip), |
| 119 |
Port: proto.Uint32(port), |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func NewOfferID(id string) *mesos.OfferID { |
| 124 |
return &mesos.OfferID{Value: proto.String(id)} |
| 125 |
} |
| 126 |
|
| 127 |
func NewOffer(offerId *mesos.OfferID, frameworkId *mesos.FrameworkID, slaveId *mesos.SlaveID, hostname string) *mesos.Offer { |
| 128 |
return &mesos.Offer{ |
| 129 |
Id: offerId, |
| 130 |
FrameworkId: frameworkId, |
| 131 |
SlaveId: slaveId, |
| 132 |
Hostname: proto.String(hostname), |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func FilterOffersResources(offers []*mesos.Offer, filter func(*mesos.Resource) bool) (result []*mesos.Resource) { |
| 137 |
for _, offer := range offers { |
| 138 |
result = FilterResources(offer.Resources, filter) |
| 139 |
} |
| 140 |
return result |
| 141 |
} |
| 142 |
|
| 143 |
func NewSlaveID(id string) *mesos.SlaveID { |
| 144 |
return &mesos.SlaveID{Value: proto.String(id)} |
| 145 |
} |
| 146 |
|
| 147 |
func NewTaskID(id string) *mesos.TaskID { |
| 148 |
return &mesos.TaskID{Value: proto.String(id)} |
| 149 |
} |
| 150 |
|
| 151 |
func NewTaskInfo( |
| 152 |
name string, |
| 153 |
taskId *mesos.TaskID, |
| 154 |
slaveId *mesos.SlaveID, |
| 155 |
resources []*mesos.Resource, |
| 156 |
) *mesos.TaskInfo { |
| 157 |
return &mesos.TaskInfo{ |
| 158 |
Name: proto.String(name), |
| 159 |
TaskId: taskId, |
| 160 |
SlaveId: slaveId, |
| 161 |
Resources: resources, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func NewTaskStatus(taskId *mesos.TaskID, state mesos.TaskState) *mesos.TaskStatus { |
| 166 |
return &mesos.TaskStatus{ |
| 167 |
TaskId: taskId, |
| 168 |
State: mesos.TaskState(state).Enum(), |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func NewStatusUpdate(frameworkId *mesos.FrameworkID, taskStatus *mesos.TaskStatus, timestamp float64, uuid []byte) *mesos.StatusUpdate { |
| 173 |
return &mesos.StatusUpdate{ |
| 174 |
FrameworkId: frameworkId, |
| 175 |
Status: taskStatus, |
| 176 |
Timestamp: proto.Float64(timestamp), |
| 177 |
Uuid: uuid, |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func NewCommandInfo(command string) *mesos.CommandInfo { |
| 182 |
return &mesos.CommandInfo{Value: proto.String(command)} |
| 183 |
} |
| 184 |
|
| 185 |
func NewExecutorID(id string) *mesos.ExecutorID { |
| 186 |
return &mesos.ExecutorID{Value: proto.String(id)} |
| 187 |
} |
| 188 |
|
| 189 |
func NewExecutorInfo(execId *mesos.ExecutorID, command *mesos.CommandInfo) *mesos.ExecutorInfo { |
| 190 |
return &mesos.ExecutorInfo{ |
| 191 |
ExecutorId: execId, |
| 192 |
Command: command, |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func NewCreateOperation(volumes []*mesos.Resource) *mesos.Offer_Operation { |
| 197 |
return &mesos.Offer_Operation{ |
| 198 |
Type: mesos.Offer_Operation_CREATE.Enum(), |
| 199 |
Create: &mesos.Offer_Operation_Create{Volumes: volumes}, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func NewDestroyOperation(volumes []*mesos.Resource) *mesos.Offer_Operation { |
| 204 |
return &mesos.Offer_Operation{ |
| 205 |
Type: mesos.Offer_Operation_DESTROY.Enum(), |
| 206 |
Destroy: &mesos.Offer_Operation_Destroy{Volumes: volumes}, |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func NewReserveOperation(resources []*mesos.Resource) *mesos.Offer_Operation { |
| 211 |
return &mesos.Offer_Operation{ |
| 212 |
Type: mesos.Offer_Operation_RESERVE.Enum(), |
| 213 |
Reserve: &mesos.Offer_Operation_Reserve{Resources: resources}, |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func NewUnreserveOperation(resources []*mesos.Resource) *mesos.Offer_Operation { |
| 218 |
return &mesos.Offer_Operation{ |
| 219 |
Type: mesos.Offer_Operation_UNRESERVE.Enum(), |
| 220 |
Unreserve: &mesos.Offer_Operation_Unreserve{Resources: resources}, |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func NewLaunchOperation(tasks []*mesos.TaskInfo) *mesos.Offer_Operation { |
| 225 |
return &mesos.Offer_Operation{ |
| 226 |
Type: mesos.Offer_Operation_LAUNCH.Enum(), |
| 227 |
Launch: &mesos.Offer_Operation_Launch{TaskInfos: tasks}, |
| 228 |
} |
| 229 |
} |