server/container_create: Simplify Kube/OCI process merge

Handling both the nil ociConfig check and kubeCommands length check on
the same line lets us drop one level of nesting.  And that initial
zero-length check makes the internal kubeCommands nil check redundant,
so we can drop that too.

Also adjust the buildOCIProcessArgs argument from a *v1.Image to
*v1.ImageConfig, because we don't need anything else from Image here.
This moves the nil Image check from inside buildOCIProcessArgs to the
calling function, but I think that's worth the tighter scoping.

Signed-off-by: W. Trevor King <wking@tremily.us>
This commit is contained in:
W. Trevor King 2018-01-19 09:15:20 -08:00
parent 54e76afc03
commit cff96ecf5c

View file

@ -376,7 +376,7 @@ func addDevices(sb *sandbox.Sandbox, containerConfig *pb.ContainerConfig, specge
}
// buildOCIProcessArgs build an OCI compatible process arguments slice.
func buildOCIProcessArgs(containerKubeConfig *pb.ContainerConfig, imageOCIConfig *v1.Image) ([]string, error) {
func buildOCIProcessArgs(containerKubeConfig *pb.ContainerConfig, ociConfig *v1.ImageConfig) ([]string, error) {
//# Start the nginx container using the default command, but use custom
//arguments (arg1 .. argN) for that command.
//kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
@ -388,15 +388,10 @@ func buildOCIProcessArgs(containerKubeConfig *pb.ContainerConfig, imageOCIConfig
kubeArgs := containerKubeConfig.Args
// merge image config and kube config
// same as docker does today...
if imageOCIConfig != nil {
if len(kubeCommands) == 0 {
if ociConfig != nil && len(kubeCommands) == 0 {
kubeCommands = ociConfig.Entrypoint
if len(kubeArgs) == 0 {
kubeArgs = imageOCIConfig.Config.Cmd
}
if kubeCommands == nil {
kubeCommands = imageOCIConfig.Config.Entrypoint
}
kubeArgs = ociConfig.Cmd
}
}
@ -1179,10 +1174,13 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string,
return nil, err
}
processArgs, err := buildOCIProcessArgs(containerConfig, containerImageConfig)
processArgs := []string{}
if containerImageConfig != nil {
processArgs, err := buildOCIProcessArgs(containerConfig, &containerImageConfig.Config)
if err != nil {
return nil, err
}
}
specgen.SetProcessArgs(processArgs)
envs := mergeEnvs(containerImageConfig, containerConfig.GetEnvs())