mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-10-31 16:13:45 +08:00 
			
		
		
		
	invoke: load defaults from image config
If user does not specify image certain container parameters, we can load them from the exporter metadata. Additionally, we introduce a new "default" value for the --invoke flag, that keeps all of the default parameters (since cobra does not have an easy way of accepting an optional flag argument). Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
		| @@ -640,14 +640,17 @@ func toSolveOpt(ctx context.Context, di DriverInfo, multiDriver bool, opt Option | |||||||
| // ContainerConfig is configuration for a container to run. | // ContainerConfig is configuration for a container to run. | ||||||
| type ContainerConfig struct { | type ContainerConfig struct { | ||||||
| 	ResultCtx *ResultContext | 	ResultCtx *ResultContext | ||||||
| 	Args      []string |  | ||||||
| 	Env       []string |  | ||||||
| 	User      string |  | ||||||
| 	Cwd       string |  | ||||||
| 	Tty       bool |  | ||||||
| 	Stdin  io.ReadCloser | 	Stdin  io.ReadCloser | ||||||
| 	Stdout io.WriteCloser | 	Stdout io.WriteCloser | ||||||
| 	Stderr io.WriteCloser | 	Stderr io.WriteCloser | ||||||
|  | 	Tty    bool | ||||||
|  |  | ||||||
|  | 	Entrypoint []string | ||||||
|  | 	Cmd        []string | ||||||
|  | 	Env        []string | ||||||
|  | 	User       *string | ||||||
|  | 	Cwd        *string | ||||||
| } | } | ||||||
|  |  | ||||||
| // ResultContext is a build result with the client that built it. | // ResultContext is a build result with the client that built it. | ||||||
| @@ -703,11 +706,53 @@ func Invoke(ctx context.Context, cfg ContainerConfig) error { | |||||||
| 		} | 		} | ||||||
| 		defer ctr.Release(context.TODO()) | 		defer ctr.Release(context.TODO()) | ||||||
|  |  | ||||||
|  | 		imgData := res.Metadata[exptypes.ExporterImageConfigKey] | ||||||
|  | 		var img *specs.Image | ||||||
|  | 		if len(imgData) > 0 { | ||||||
|  | 			img = &specs.Image{} | ||||||
|  | 			if err := json.Unmarshal(imgData, img); err != nil { | ||||||
|  | 				fmt.Println(err) | ||||||
|  | 				return nil, err | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		user := "" | ||||||
|  | 		if cfg.User != nil { | ||||||
|  | 			user = *cfg.User | ||||||
|  | 		} else if img != nil { | ||||||
|  | 			user = img.Config.User | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		cwd := "" | ||||||
|  | 		if cfg.Cwd != nil { | ||||||
|  | 			cwd = *cfg.Cwd | ||||||
|  | 		} else if img != nil { | ||||||
|  | 			cwd = img.Config.WorkingDir | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		env := []string{} | ||||||
|  | 		if img != nil { | ||||||
|  | 			env = append(env, img.Config.Env...) | ||||||
|  | 		} | ||||||
|  | 		env = append(env, cfg.Env...) | ||||||
|  |  | ||||||
|  | 		args := []string{} | ||||||
|  | 		if cfg.Entrypoint != nil { | ||||||
|  | 			args = append(args, cfg.Entrypoint...) | ||||||
|  | 		} else if img != nil { | ||||||
|  | 			args = append(args, img.Config.Entrypoint...) | ||||||
|  | 		} | ||||||
|  | 		if cfg.Cmd != nil { | ||||||
|  | 			args = append(args, cfg.Cmd...) | ||||||
|  | 		} else if img != nil { | ||||||
|  | 			args = append(args, img.Config.Cmd...) | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		proc, err := ctr.Start(ctx, gateway.StartRequest{ | 		proc, err := ctr.Start(ctx, gateway.StartRequest{ | ||||||
| 			Args:   cfg.Args, | 			Args:   args, | ||||||
| 			Env:    cfg.Env, | 			Env:    env, | ||||||
| 			User:   cfg.User, | 			User:   user, | ||||||
| 			Cwd:    cfg.Cwd, | 			Cwd:    cwd, | ||||||
| 			Tty:    cfg.Tty, | 			Tty:    cfg.Tty, | ||||||
| 			Stdin:  cfg.Stdin, | 			Stdin:  cfg.Stdin, | ||||||
| 			Stdout: cfg.Stdout, | 			Stdout: cfg.Stdout, | ||||||
|   | |||||||
| @@ -326,18 +326,20 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu | |||||||
| } | } | ||||||
|  |  | ||||||
| func parseInvokeConfig(invoke string) (cfg build.ContainerConfig, err error) { | func parseInvokeConfig(invoke string) (cfg build.ContainerConfig, err error) { | ||||||
|  | 	cfg.Tty = true | ||||||
|  | 	if invoke == "default" { | ||||||
|  | 		return cfg, nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	csvReader := csv.NewReader(strings.NewReader(invoke)) | 	csvReader := csv.NewReader(strings.NewReader(invoke)) | ||||||
| 	fields, err := csvReader.Read() | 	fields, err := csvReader.Read() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return cfg, err | 		return cfg, err | ||||||
| 	} | 	} | ||||||
| 	cfg.Tty = true |  | ||||||
| 	if len(fields) == 1 && !strings.Contains(fields[0], "=") { | 	if len(fields) == 1 && !strings.Contains(fields[0], "=") { | ||||||
| 		cfg.Args = []string{fields[0]} | 		cfg.Cmd = []string{fields[0]} | ||||||
| 		return cfg, nil | 		return cfg, nil | ||||||
| 	} | 	} | ||||||
| 	var entrypoint string |  | ||||||
| 	var args []string |  | ||||||
| 	for _, field := range fields { | 	for _, field := range fields { | ||||||
| 		parts := strings.SplitN(field, "=", 2) | 		parts := strings.SplitN(field, "=", 2) | ||||||
| 		if len(parts) != 2 { | 		if len(parts) != 2 { | ||||||
| @@ -347,15 +349,15 @@ func parseInvokeConfig(invoke string) (cfg build.ContainerConfig, err error) { | |||||||
| 		value := parts[1] | 		value := parts[1] | ||||||
| 		switch key { | 		switch key { | ||||||
| 		case "args": | 		case "args": | ||||||
| 			args = append(args, value) // TODO: support JSON | 			cfg.Cmd = append(cfg.Cmd, value) // TODO: support JSON | ||||||
| 		case "entrypoint": | 		case "entrypoint": | ||||||
| 			entrypoint = value // TODO: support JSON | 			cfg.Entrypoint = append(cfg.Entrypoint, value) // TODO: support JSON | ||||||
| 		case "env": | 		case "env": | ||||||
| 			cfg.Env = append(cfg.Env, value) | 			cfg.Env = append(cfg.Env, value) | ||||||
| 		case "user": | 		case "user": | ||||||
| 			cfg.User = value | 			cfg.User = &value | ||||||
| 		case "cwd": | 		case "cwd": | ||||||
| 			cfg.Cwd = value | 			cfg.Cwd = &value | ||||||
| 		case "tty": | 		case "tty": | ||||||
| 			cfg.Tty, err = strconv.ParseBool(value) | 			cfg.Tty, err = strconv.ParseBool(value) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| @@ -365,13 +367,6 @@ func parseInvokeConfig(invoke string) (cfg build.ContainerConfig, err error) { | |||||||
| 			return cfg, errors.Errorf("unknown key %q", key) | 			return cfg, errors.Errorf("unknown key %q", key) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	cfg.Args = args |  | ||||||
| 	if entrypoint != "" { |  | ||||||
| 		cfg.Args = append([]string{entrypoint}, cfg.Args...) |  | ||||||
| 	} |  | ||||||
| 	if len(cfg.Args) == 0 { |  | ||||||
| 		cfg.Args = []string{"sh"} |  | ||||||
| 	} |  | ||||||
| 	return cfg, nil | 	return cfg, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Justin Chadwell
					Justin Chadwell