Kaynağa Gözat

Add option to choose registry for porter yaml (#2817)

Mohammed Nafees 3 yıl önce
ebeveyn
işleme
bb5e9df1b7

+ 20 - 0
api/client/registry.go

@@ -100,6 +100,26 @@ func (c *Client) ListRegistries(
 	return resp, err
 }
 
+// GetRegistry returns a registry given a project id and registry id
+func (c *Client) GetRegistry(
+	ctx context.Context,
+	projectID, registryID uint,
+) (*types.Registry, error) {
+	resp := &types.Registry{}
+
+	err := c.getRequest(
+		fmt.Sprintf(
+			"/projects/%d/registries/%d",
+			projectID,
+			registryID,
+		),
+		nil,
+		resp,
+	)
+
+	return resp, err
+}
+
 // DeleteProjectRegistry deletes a registry given a project id and registry id
 func (c *Client) DeleteProjectRegistry(
 	ctx context.Context,

+ 2 - 14
cli/cmd/apply.go

@@ -489,20 +489,7 @@ func (d *DeployDriver) createApplication(resource *switchboardModels.Resource, c
 	// create new release
 	color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
 
-	regList, err := client.ListRegistries(context.Background(), d.target.Project)
-	if err != nil {
-		return nil, fmt.Errorf("for resource %s, error listing registries: %w", resource.Name, err)
-	}
-
-	var registryURL string
-
-	if len(*regList) == 0 {
-		return nil, fmt.Errorf("no registry found")
-	} else {
-		registryURL = (*regList)[0].URL
-	}
-
-	color.New(color.FgBlue).Printf("for resource %s, using registry %s\n", resource.Name, registryURL)
+	color.New(color.FgBlue).Printf("for resource %s, using registry %s\n", resource.Name, d.target.RegistryURL)
 
 	// attempt to get repo suffix from environment variables
 	var repoSuffix string
@@ -534,6 +521,7 @@ func (d *DeployDriver) createApplication(resource *switchboardModels.Resource, c
 	}
 
 	var subdomain string
+	var err error
 
 	if appConf.Build.Method == "registry" {
 		subdomain, err = createAgent.CreateFromRegistry(appConf.Build.Image, appConf.Values)

+ 37 - 0
cli/cmd/preview/utils.go

@@ -168,6 +168,14 @@ func GetTarget(resourceName string, input map[string]interface{}) (*preview.Targ
 		}
 	}
 
+	if registryURL, ok := input["registry_url"]; ok {
+		registryURLVal, ok := registryURL.(string)
+		if !ok {
+			return nil, fmt.Errorf("error parsing target for resource '%s': invalid registry_url provided", resourceName)
+		}
+		output.RegistryURL = registryURLVal
+	}
+
 	if appName, ok := input["app_name"]; ok {
 		appNameVal, ok := appName.(string)
 		if !ok {
@@ -177,16 +185,45 @@ func GetTarget(resourceName string, input map[string]interface{}) (*preview.Targ
 	}
 
 	// lastly, just put in the defaults
+
 	if output.Project == 0 {
 		output.Project = config.GetCLIConfig().Project
 	}
+
 	if output.Cluster == 0 {
 		output.Cluster = config.GetCLIConfig().Cluster
 	}
+
 	if output.Namespace == "" {
 		output.Namespace = "default"
 	}
 
+	if output.RegistryURL == "" {
+		apiClient := config.GetAPIClient()
+
+		if config.GetCLIConfig().Registry == 0 {
+			regList, err := apiClient.ListRegistries(context.Background(), output.Project)
+
+			if err != nil {
+				return nil, fmt.Errorf("for resource '%s', error listing registries in project: %w", resourceName, err)
+			}
+
+			if len(*regList) == 0 {
+				return nil, fmt.Errorf("for resource '%s', no registries found in project", resourceName)
+			}
+
+			output.RegistryURL = (*regList)[0].URL
+		} else {
+			reg, err := apiClient.GetRegistry(context.Background(), output.Project, config.GetCLIConfig().Registry)
+
+			if err != nil {
+				return nil, fmt.Errorf("for resource '%s', error getting registry from CLI config: %w", resourceName, err)
+			}
+
+			output.RegistryURL = reg.URL
+		}
+	}
+
 	return output, nil
 }
 

+ 5 - 4
internal/integrations/preview/utils.go

@@ -11,10 +11,11 @@ type Source struct {
 }
 
 type Target struct {
-	AppName   string `mapstructure:"app_name"`
-	Project   uint
-	Cluster   uint
-	Namespace string
+	AppName     string `mapstructure:"app_name"`
+	Project     uint
+	Cluster     uint
+	Namespace   string
+	RegistryURL string `mapstructure:"registry_url"`
 }
 
 type RandomStringDriverConfig struct {