本文整理汇总了Golang中github.com/docker/engine-api/types/swarm.ServiceSpec类的典型用法代码示例。如果您正苦于以下问题:Golang ServiceSpec类的具体用法?Golang ServiceSpec怎么用?Golang ServiceSpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ServiceSpec类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: serviceCreate
func (s *DockerServer) serviceCreate(w http.ResponseWriter, r *http.Request) {
var config swarm.ServiceSpec
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&config)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.cMut.Lock()
defer s.cMut.Unlock()
s.swarmMut.Lock()
defer s.swarmMut.Unlock()
if len(s.nodes) == 0 || s.swarm == nil {
http.Error(w, "no swarm nodes available", http.StatusNotAcceptable)
return
}
if config.Name == "" {
config.Name = s.generateID()
}
for _, s := range s.services {
if s.Spec.Name == config.Name {
http.Error(w, "there's already a service with this name", http.StatusConflict)
return
}
}
service := swarm.Service{
ID: s.generateID(),
Spec: config,
}
containerCount := 1
if service.Spec.Mode.Global != nil {
containerCount = len(s.nodes)
} else if repl := service.Spec.Mode.Replicated; repl != nil {
if repl.Replicas != nil {
containerCount = int(*repl.Replicas)
}
}
for i := 0; i < containerCount; i++ {
container := s.containerForService(&service, fmt.Sprintf("%s-%d", config.Name, i))
chosenNode := s.nodes[s.nodeRR]
s.nodeRR = (s.nodeRR + 1) % len(s.nodes)
task := swarm.Task{
ID: s.generateID(),
ServiceID: service.ID,
NodeID: chosenNode.ID,
Status: swarm.TaskStatus{
State: swarm.TaskStateReady,
ContainerStatus: swarm.ContainerStatus{
ContainerID: container.ID,
},
},
DesiredState: swarm.TaskStateReady,
Spec: config.TaskTemplate,
}
s.tasks = append(s.tasks, &task)
s.containers = append(s.containers, container)
s.notify(container)
}
s.services = append(s.services, &service)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(service)
}
开发者ID:docker,项目名称:dockercraft,代码行数:62,代码来源:swarm.go
示例2: mergeService
func mergeService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
mergeString := func(flag string, field *string) {
if flags.Changed(flag) {
*field, _ = flags.GetString(flag)
}
}
mergeListOpts := func(flag string, field *[]string) {
if flags.Changed(flag) {
value := flags.Lookup(flag).Value.(*opts.ListOpts)
*field = value.GetAll()
}
}
mergeSlice := func(flag string, field *[]string) {
if flags.Changed(flag) {
*field, _ = flags.GetStringSlice(flag)
}
}
mergeInt64Value := func(flag string, field *int64) {
if flags.Changed(flag) {
*field = flags.Lookup(flag).Value.(int64Value).Value()
}
}
mergeDuration := func(flag string, field *time.Duration) {
if flags.Changed(flag) {
*field, _ = flags.GetDuration(flag)
}
}
mergeDurationOpt := func(flag string, field *time.Duration) {
if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*DurationOpt).Value()
}
}
mergeUint64 := func(flag string, field *uint64) {
if flags.Changed(flag) {
*field, _ = flags.GetUint64(flag)
}
}
mergeUint64Opt := func(flag string, field *uint64) {
if flags.Changed(flag) {
*field = *flags.Lookup(flag).Value.(*Uint64Opt).Value()
}
}
cspec := &spec.TaskTemplate.ContainerSpec
task := &spec.TaskTemplate
mergeString(flagName, &spec.Name)
mergeLabels(flags, &spec.Labels)
mergeString("image", &cspec.Image)
mergeSlice("command", &cspec.Command)
mergeSlice("arg", &cspec.Command)
mergeListOpts("env", &cspec.Env)
mergeString("workdir", &cspec.Dir)
mergeString("user", &cspec.User)
mergeMounts(flags, &cspec.Mounts)
if flags.Changed(flagLimitCPU) || flags.Changed(flagLimitMemory) {
if task.Resources == nil {
task.Resources = &swarm.ResourceRequirements{}
}
task.Resources.Limits = &swarm.Resources{}
mergeInt64Value(flagLimitCPU, &task.Resources.Limits.NanoCPUs)
mergeInt64Value(flagLimitMemory, &task.Resources.Limits.MemoryBytes)
}
if flags.Changed(flagReserveCPU) || flags.Changed(flagReserveMemory) {
if task.Resources == nil {
task.Resources = &swarm.ResourceRequirements{}
}
task.Resources.Reservations = &swarm.Resources{}
mergeInt64Value(flagReserveCPU, &task.Resources.Reservations.NanoCPUs)
mergeInt64Value(flagReserveMemory, &task.Resources.Reservations.MemoryBytes)
}
mergeDurationOpt("stop-grace-period", cspec.StopGracePeriod)
if flags.Changed(flagRestartCondition) || flags.Changed(flagRestartDelay) || flags.Changed(flagRestartMaxAttempts) || flags.Changed(flagRestartWindow) {
if task.RestartPolicy == nil {
task.RestartPolicy = &swarm.RestartPolicy{}
}
if flags.Changed(flagRestartCondition) {
value, _ := flags.GetString(flagRestartCondition)
task.RestartPolicy.Condition = swarm.RestartPolicyCondition(value)
}
mergeDurationOpt(flagRestartDelay, task.RestartPolicy.Delay)
mergeUint64Opt(flagRestartMaxAttempts, task.RestartPolicy.MaxAttempts)
mergeDurationOpt((flagRestartWindow), task.RestartPolicy.Window)
}
if flags.Changed(flagConstraint) {
task.Placement = &swarm.Placement{}
mergeSlice(flagConstraint, &task.Placement.Constraints)
//.........这里部分代码省略.........
开发者ID:karunchennuri,项目名称:docker,代码行数:101,代码来源:update.go
注:本文中的github.com/docker/engine-api/types/swarm.ServiceSpec类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论