github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/swarmkit/api/defaults/service.go (about)

     1  package defaults
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/docker/swarmkit/api"
     7  	"github.com/docker/swarmkit/api/deepcopy"
     8  	gogotypes "github.com/gogo/protobuf/types"
     9  )
    10  
    11  // Service is a ServiceSpec object with all fields filled in using default
    12  // values.
    13  var Service = api.ServiceSpec{
    14  	Task: api.TaskSpec{
    15  		Runtime: &api.TaskSpec_Container{
    16  			Container: &api.ContainerSpec{
    17  				StopGracePeriod: gogotypes.DurationProto(10 * time.Second),
    18  				PullOptions:     &api.ContainerSpec_PullOptions{},
    19  				DNSConfig:       &api.ContainerSpec_DNSConfig{},
    20  			},
    21  		},
    22  		Resources: &api.ResourceRequirements{},
    23  		Restart: &api.RestartPolicy{
    24  			Condition: api.RestartOnAny,
    25  			Delay:     gogotypes.DurationProto(5 * time.Second),
    26  		},
    27  		Placement: &api.Placement{},
    28  	},
    29  	Update: &api.UpdateConfig{
    30  		FailureAction: api.UpdateConfig_PAUSE,
    31  		Monitor:       gogotypes.DurationProto(5 * time.Second),
    32  		Parallelism:   1,
    33  		Order:         api.UpdateConfig_STOP_FIRST,
    34  	},
    35  	Rollback: &api.UpdateConfig{
    36  		FailureAction: api.UpdateConfig_PAUSE,
    37  		Monitor:       gogotypes.DurationProto(5 * time.Second),
    38  		Parallelism:   1,
    39  		Order:         api.UpdateConfig_STOP_FIRST,
    40  	},
    41  }
    42  
    43  // InterpolateService returns a ServiceSpec based on the provided spec, which
    44  // has all unspecified values filled in with default values.
    45  func InterpolateService(origSpec *api.ServiceSpec) *api.ServiceSpec {
    46  	spec := origSpec.Copy()
    47  
    48  	container := spec.Task.GetContainer()
    49  	defaultContainer := Service.Task.GetContainer()
    50  	if container != nil {
    51  		if container.StopGracePeriod == nil {
    52  			container.StopGracePeriod = &gogotypes.Duration{}
    53  			deepcopy.Copy(container.StopGracePeriod, defaultContainer.StopGracePeriod)
    54  		}
    55  		if container.PullOptions == nil {
    56  			container.PullOptions = defaultContainer.PullOptions.Copy()
    57  		}
    58  		if container.DNSConfig == nil {
    59  			container.DNSConfig = defaultContainer.DNSConfig.Copy()
    60  		}
    61  	}
    62  
    63  	if spec.Task.Resources == nil {
    64  		spec.Task.Resources = Service.Task.Resources.Copy()
    65  	}
    66  
    67  	if spec.Task.Restart == nil {
    68  		spec.Task.Restart = Service.Task.Restart.Copy()
    69  	} else {
    70  		if spec.Task.Restart.Delay == nil {
    71  			spec.Task.Restart.Delay = &gogotypes.Duration{}
    72  			deepcopy.Copy(spec.Task.Restart.Delay, Service.Task.Restart.Delay)
    73  		}
    74  	}
    75  
    76  	if spec.Task.Placement == nil {
    77  		spec.Task.Placement = Service.Task.Placement.Copy()
    78  	}
    79  
    80  	if spec.Update == nil {
    81  		spec.Update = Service.Update.Copy()
    82  	} else {
    83  		if spec.Update.Monitor == nil {
    84  			spec.Update.Monitor = &gogotypes.Duration{}
    85  			deepcopy.Copy(spec.Update.Monitor, Service.Update.Monitor)
    86  		}
    87  	}
    88  
    89  	if spec.Rollback == nil {
    90  		spec.Rollback = Service.Rollback.Copy()
    91  	} else {
    92  		if spec.Rollback.Monitor == nil {
    93  			spec.Rollback.Monitor = &gogotypes.Duration{}
    94  			deepcopy.Copy(spec.Rollback.Monitor, Service.Rollback.Monitor)
    95  		}
    96  	}
    97  
    98  	return spec
    99  }