Exemplo n.º 1
0
func (privateRegistry *PrivateRegistry) DeleteImageInRepository(repositoryName string, tag string) error {
	request, err := http.NewRequest("GET", privateRegistry.getPrivateRegistryEndpoint()+"/v2/"+repositoryName+"/manifests/"+tag, nil)
	if err != nil {
		log.Error(err)
		return err
	}

	// For registry version 2.3 and later
	request.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
	response, err := restclient.GetInsecureHTTPSClient().Do(request)
	if err != nil {
		log.Error(err)
		return err
	}
	digest := response.Header.Get("Docker-Content-Digest")

	if len(digest) == 0 {
		// The tag has no image
		return nil
	}

	_, err = restclient.RequestDelete(privateRegistry.getPrivateRegistryEndpoint()+"/v2/"+repositoryName+"/manifests/"+digest, nil, nil, false)
	requestError, _ := err.(restclient.RequestError)
	if requestError.StatusCode == 404 {
		// Not found so the target doesn't exist
		return nil
	}
	if err != nil {
		log.Error(err)
		return err
	}

	return nil
}
Exemplo n.º 2
0
func (privateRegistry *PrivateRegistry) GetAllImageTag(repositoryName string) ([]string, error) {
	result, err := restclient.RequestGet(privateRegistry.getPrivateRegistryEndpoint()+"/v2/"+repositoryName+"/tags/list", nil, false)
	if err != nil {
		log.Error("Fail to get all image tags with repository %s and private registry: %v, error: %s", repositoryName, privateRegistry, err)
		return nil, err
	}

	jsonMap, _ := result.(map[string]interface{})
	tagJsonSlice, _ := jsonMap["tags"].([]interface{})
	tagSlice := make([]string, 0)
	for _, tagJsonInterface := range tagJsonSlice {
		if tag, ok := tagJsonInterface.(string); ok {
			request, err := http.NewRequest("GET", privateRegistry.getPrivateRegistryEndpoint()+"/v2/"+repositoryName+"/manifests/"+tag, nil)
			if err != nil {
				log.Error(err)
				return nil, err
			}

			// For registry version 2.3 and later
			request.Header.Add("Accept", "application/vnd.docker.distribution.manifest.v2+json")
			response, err := restclient.GetInsecureHTTPSClient().Do(request)
			if err != nil {
				log.Error(err)
				return nil, err
			}
			digest := response.Header.Get("Docker-Content-Digest")

			if len(digest) > 0 {
				tagSlice = append(tagSlice, tag)
			}
		}
	}

	return tagSlice, nil
}