Prechádzať zdrojové kódy

hotfix for CheckIfImageExists to not return an error

Mohammed Nafees 4 rokov pred
rodič
commit
f45cd4f2e8
3 zmenil súbory, kde vykonal 20 pridanie a 30 odobranie
  1. 1 5
      cli/cmd/deploy/create.go
  2. 1 7
      cli/cmd/deploy/deploy.go
  3. 18 18
      cli/cmd/docker/agent.go

+ 1 - 5
cli/cmd/deploy/create.go

@@ -273,11 +273,7 @@ func (c *CreateAgent) CreateFromDocker(
 		return "", err
 	}
 
-	imageExists, err := agent.CheckIfImageExists(fmt.Sprintf("%s:%s", imageURL, imageTag))
-
-	if err != nil {
-		return "", err
-	}
+	imageExists := agent.CheckIfImageExists(fmt.Sprintf("%s:%s", imageURL, imageTag))
 
 	if imageExists && imageTag != "latest" && !forceBuild {
 		fmt.Printf("%s:%s already exists in the registry, so skipping build\n", imageURL, imageTag)

+ 1 - 7
cli/cmd/deploy/deploy.go

@@ -140,13 +140,7 @@ func NewDeployAgent(client *client.Client, app string, opts *DeployOpts) (*Deplo
 	err = coalesceEnvGroups(deployAgent.client, deployAgent.opts.ProjectID, deployAgent.opts.ClusterID,
 		deployAgent.opts.Namespace, deployAgent.opts.EnvGroups, deployAgent.release.Config)
 
-	imageExists, err := deployAgent.agent.CheckIfImageExists(fmt.Sprintf("%s:%s", deployAgent.imageRepo, deployAgent.tag))
-
-	if err != nil {
-		return nil, err
-	}
-
-	deployAgent.imageExists = imageExists
+	deployAgent.imageExists = deployAgent.agent.CheckIfImageExists(fmt.Sprintf("%s:%s", deployAgent.imageRepo, deployAgent.tag))
 
 	return deployAgent, err
 }

+ 18 - 18
cli/cmd/docker/agent.go

@@ -168,11 +168,11 @@ func getRegistryRepositoryPair(image, prefix string) []string {
 }
 
 // CheckIfImageExists checks if the image exists in the registry
-func (a *Agent) CheckIfImageExists(image string) (bool, error) {
+func (a *Agent) CheckIfImageExists(image string) bool {
 	registryToken, err := a.getContainerRegistryToken(image)
 
 	if err != nil {
-		return false, err
+		return false
 	}
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
@@ -182,7 +182,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		gcrRegRepo := getRegistryRepositoryPair(image, "gcr.io")
 
 		if len(gcrRegRepo) != 2 {
-			return false, errors.New("invalid GCR image URL")
+			return false
 		}
 
 		req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf(
@@ -190,7 +190,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		), nil)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		req.Header.Add("Content-Type", "application/json")
@@ -199,7 +199,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		resp, err := http.DefaultClient.Do(req)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		defer resp.Body.Close()
@@ -211,23 +211,23 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		err = json.NewDecoder(resp.Body).Decode(&tags)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		reqTag := strings.Split(image, ":")[1]
 
 		for _, tag := range tags.Tags {
 			if tag == reqTag {
-				return true, nil
+				return true
 			}
 		}
 
-		return false, nil
+		return false
 	} else if strings.Contains(image, "registry.digitalocean.com") {
 		doRegRepo := getRegistryRepositoryPair(image, "registry.digitalocean.com")
 
 		if len(doRegRepo) != 2 {
-			return false, errors.New("invalid DOCR image URL")
+			return false
 		}
 
 		req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf(
@@ -235,7 +235,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		), nil)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		req.Header.Add("Content-Type", "application/json")
@@ -244,7 +244,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		resp, err := http.DefaultClient.Do(req)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		defer resp.Body.Close()
@@ -259,7 +259,7 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		err = json.NewDecoder(resp.Body).Decode(&digest)
 
 		if err != nil {
-			return false, err
+			return false
 		}
 
 		reqTag := strings.Split(image, ":")[1]
@@ -267,30 +267,30 @@ func (a *Agent) CheckIfImageExists(image string) (bool, error) {
 		for _, manifest := range digest.Manifests {
 			for _, tag := range manifest.Tags {
 				if tag == reqTag {
-					return true, nil
+					return true
 				}
 			}
 		}
 
-		return false, nil
+		return false
 	}
 
 	encodedRegistryAuth, err := a.getEncodedRegistryAuth(image)
 
 	if err != nil {
-		return false, err
+		return false
 	}
 
 	_, err = a.client.DistributionInspect(context.Background(), image, encodedRegistryAuth)
 
 	if err == nil {
-		return true, nil
+		return true
 	} else if strings.Contains(err.Error(), "image not found") ||
 		strings.Contains(err.Error(), "does not exist in the registry") {
-		return false, nil
+		return false
 	}
 
-	return false, err
+	return false
 }
 
 // PullImage pulls an image specified by the image string