mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 18:13:42 +08:00 
			
		
		
		
	buildx: forbid mismatched drivers
This patch reorders+refactors the runCreate function to ensure that we can detect and notify the user in the scenario that the user attempts to combine multiple drivers in a single builder, which is an unsupported scenario. Previously, we would just overwrite the previous builder with the new driver, potentially invalidating the already existing nodes. Signed-off-by: Justin Chadwell <me@jedevc.com>
This commit is contained in:
		@@ -61,32 +61,6 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	buildkitHost := os.Getenv("BUILDKIT_HOST")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	driverName := in.driver
 | 
					 | 
				
			||||||
	if driverName == "" {
 | 
					 | 
				
			||||||
		if len(args) == 0 && buildkitHost != "" {
 | 
					 | 
				
			||||||
			driverName = "remote"
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			var arg string
 | 
					 | 
				
			||||||
			if len(args) > 0 {
 | 
					 | 
				
			||||||
				arg = args[0]
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			f, err := driver.GetDefaultFactory(ctx, arg, dockerCli.Client(), true)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				return err
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if f == nil {
 | 
					 | 
				
			||||||
				return errors.Errorf("no valid drivers found")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			driverName = f.Name()
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if driver.GetFactory(driverName, true) == nil {
 | 
					 | 
				
			||||||
		return errors.Errorf("failed to find driver %q", in.driver)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	txn, release, err := storeutil.GetStore(dockerCli)
 | 
						txn, release, err := storeutil.GetStore(dockerCli)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@@ -121,17 +95,48 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
 | 
				
			|||||||
				logrus.Warnf("failed to find %q for append, creating a new instance instead", in.name)
 | 
									logrus.Warnf("failed to find %q for append, creating a new instance instead", in.name)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if in.actionLeave {
 | 
								if in.actionLeave {
 | 
				
			||||||
				return errors.Errorf("failed to find instance %q for leave", name)
 | 
									return errors.Errorf("failed to find instance %q for leave", in.name)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						buildkitHost := os.Getenv("BUILDKIT_HOST")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						driverName := in.driver
 | 
				
			||||||
 | 
						if driverName == "" {
 | 
				
			||||||
 | 
							if ng != nil {
 | 
				
			||||||
 | 
								driverName = ng.Driver
 | 
				
			||||||
 | 
							} else if len(args) == 0 && buildkitHost != "" {
 | 
				
			||||||
 | 
								driverName = "remote"
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								var arg string
 | 
				
			||||||
 | 
								if len(args) > 0 {
 | 
				
			||||||
 | 
									arg = args[0]
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								f, err := driver.GetDefaultFactory(ctx, arg, dockerCli.Client(), true)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if f == nil {
 | 
				
			||||||
 | 
									return errors.Errorf("no valid drivers found")
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								driverName = f.Name()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ng != nil {
 | 
						if ng != nil {
 | 
				
			||||||
		if in.nodeName == "" && !in.actionAppend {
 | 
							if in.nodeName == "" && !in.actionAppend {
 | 
				
			||||||
			return errors.Errorf("existing instance for %s but no append mode, specify --node to make changes for existing instances", name)
 | 
								return errors.Errorf("existing instance for %q but no append mode, specify --node to make changes for existing instances", name)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							if driverName != ng.Driver {
 | 
				
			||||||
 | 
								return errors.Errorf("existing instance for %q but has mismatched driver %q", name, ng.Driver)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if driver.GetFactory(driverName, true) == nil {
 | 
				
			||||||
 | 
							return errors.Errorf("failed to find driver %q", driverName)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ngOriginal := ng
 | 
						ngOriginal := ng
 | 
				
			||||||
@@ -141,14 +146,11 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if ng == nil {
 | 
						if ng == nil {
 | 
				
			||||||
		ng = &store.NodeGroup{
 | 
							ng = &store.NodeGroup{
 | 
				
			||||||
			Name: name,
 | 
								Name:   name,
 | 
				
			||||||
 | 
								Driver: driverName,
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ng.Driver == "" || in.driver != "" {
 | 
					 | 
				
			||||||
		ng.Driver = driverName
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var flags []string
 | 
						var flags []string
 | 
				
			||||||
	if in.flags != "" {
 | 
						if in.flags != "" {
 | 
				
			||||||
		flags, err = shlex.Split(in.flags)
 | 
							flags, err = shlex.Split(in.flags)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user