func ManifestToImageConfig(ctx context.Context, srcManifest distribution.Manifest, blobs distribution.BlobService, location ManifestLocation) (*dockerv1client.DockerImageConfig, []distribution.Descriptor, error) {
	switch t := srcManifest.(type) {
	case *schema2.DeserializedManifest:
		if t.Config.MediaType != schema2.MediaTypeImageConfig {
			return nil, nil, fmt.Errorf("%s does not have the expected image configuration media type: %s", location, t.Config.MediaType)
		}
		configJSON, err := blobs.Get(ctx, t.Config.Digest)
		if err != nil {
			return nil, nil, fmt.Errorf("cannot retrieve image configuration for %s: %v", location, err)
		}
		klog.V(4).Infof("Raw image config json:\n%s", string(configJSON))
		config := &dockerv1client.DockerImageConfig{}
		if err := json.Unmarshal(configJSON, &config); err != nil {
			return nil, nil, fmt.Errorf("unable to parse image configuration: %v", err)
		}

		base := config
		layers := t.Layers
		base.Size = 0
		for _, layer := range t.Layers {
			base.Size += layer.Size
		}

		return base, layers, nil

	case *schema1.SignedManifest:
		if klog.V(4).Enabled() {
			_, configJSON, _ := srcManifest.Payload()
			klog.Infof("Raw image config json:\n%s", string(configJSON))
		}
		if len(t.History) == 0 {
			return nil, nil, fmt.Errorf("input image is in an unknown format: no v1Compatibility history")
		}
		config := &dockerv1client.DockerV1CompatibilityImage{}
		if err := json.Unmarshal([]byte(t.History[0].V1Compatibility), &config); err != nil {
			return nil, nil, err
		}

		base := &dockerv1client.DockerImageConfig{}
		if err := dockerv1client.Convert_DockerV1CompatibilityImage_to_DockerImageConfig(config, base); err != nil {
			return nil, nil, err
		}

		// schema1 layers are in reverse order
		layers := make([]distribution.Descriptor, 0, len(t.FSLayers))
		for i := len(t.FSLayers) - 1; i >= 0; i-- {
			layer := distribution.Descriptor{
				MediaType: schema2.MediaTypeLayer,
				Digest:    t.FSLayers[i].BlobSum,
				// size must be reconstructed from the blobs
			}
			// we must reconstruct the tar sum from the blobs
			add.AddLayerToConfig(base, layer, "")
			layers = append(layers, layer)
		}

		return base, layers, nil

	default:
		return nil, nil, fmt.Errorf("unknown image manifest of type %T from %s", srcManifest, location)
	}
}
// ManifestToImageConfig takes an image manifest and converts it into a structured object.
ManifestToImageConfig takes an image manifest and converts it into a structured object.
// ManifestToImageConfig takes an image manifest and converts it into a structured object .
func ManifestToImageConfig(ctx context.Context, srcManifest distribution.Manifest, blobs distribution.BlobService, location ManifestLocation) (*dockerv1client.DockerImageConfig, []distribution.Descriptor, error) {
	switch t := srcManifest.(type) {
	case *schema2.DeserializedManifest:
		if t.Config.MediaType != schema2.MediaTypeImageConfig {
			return nil, nil, fmt.Errorf("%s does not have the expected image configuration media type: %s", location, t.Config.MediaType)
		}
		configJSON, err := blobs.Get(ctx, t.Config.Digest)
		if err != nil {
			return nil, nil, fmt.Errorf("cannot retrieve image configuration for %s: %v", location, err)
		}
		klog.V(4).Infof("Raw image config json:\n%s", string(configJSON))
		config := &dockerv1client.DockerImageConfig{}
		if err := json.Unmarshal(configJSON, &config); err != nil {
			return nil, nil, fmt.Errorf("unable to parse image configuration: %v", err)
		}
		base := config
		layers := t.Layers
		base.Size = 0
		for _, layer := range t.Layers {
			base.Size += layer.Size
		}
		return base, layers, nil
	case *schema1.SignedManifest:
		if klog.V(4).Enabled() {
			_, configJSON, _ := srcManifest.Payload()
			klog.Infof("Raw image config json:\n%s", string(configJSON))
		}
		if len(t.History) == 0 {
			return nil, nil, fmt.Errorf("input image is in an unknown format: no v1Compatibility history")
		}
		config := &dockerv1client.DockerV1CompatibilityImage{}
		if err := json.Unmarshal([]byte(t.History[0].V1Compatibility), &config); err != nil {
			return nil, nil, err
		}
		base := &dockerv1client.DockerImageConfig{}
		if err := dockerv1client.Convert_DockerV1CompatibilityImage_to_DockerImageConfig(config, base); err != nil {
			return nil, nil, err
		}
		layers := make([]distribution.Descriptor, 0, len(t.FSLayers))
		for i := len(t.FSLayers) - 1; i >= 0; i-- {
			layer := distribution.Descriptor{
				MediaType: schema2.MediaTypeLayer,
				Digest:    t.FSLayers[i].BlobSum,
			}
			add.AddLayerToConfig(base, layer, "")
			layers = append(layers, layer)
		}
		return base, layers, nil
	default:
		return nil, nil, fmt.Errorf("unknown image manifest of type %T from %s", srcManifest, location)
	}
}
func ManifestToImageConfig ( ctx context . Context , srcManifest distribution . Manifest , blobs distribution . BlobService , location ManifestLocation ) ( * dockerv1client . DockerImageConfig , [ ] distribution . Descriptor , error ) { switch t := srcManifest . ( type ) { case * schema2 . DeserializedManifest : if t . Config . MediaType != schema2 . MediaTypeImageConfig { return nil , nil , fmt . Errorf ( " " , location , t . Config . MediaType ) } configJSON , err := blobs . Get ( ctx , t . Config . Digest ) if err != nil { return nil , nil , fmt . Errorf ( " " , location , err ) } klog . V ( 4 ) . Infof ( " \n " , string ( configJSON ) ) config := & dockerv1client . DockerImageConfig { } if err := json . Unmarshal ( configJSON , & config ) ; err != nil { return nil , nil , fmt . Errorf ( " " , err ) } base := config layers := t . Layers base . Size = 0 for _ , layer := range t . Layers { base . Size += layer . Size } return base , layers , nil case * schema1 . SignedManifest : if klog . V ( 4 ) . Enabled ( ) { _ , configJSON , _ := srcManifest . Payload ( ) klog . Infof ( " \n " , string ( configJSON ) ) } if len ( t . History ) == 0 { return nil , nil , fmt . Errorf ( " " ) } config := & dockerv1client . DockerV1CompatibilityImage { } if err := json . Unmarshal ( [ ] byte ( t . History [ 0 ] . V1Compatibility ) , & config ) ; err != nil { return nil , nil , err } base := & dockerv1client . DockerImageConfig { } if err := dockerv1client . Convert_DockerV1CompatibilityImage_to_DockerImageConfig ( config , base ) ; err != nil { return nil , nil , err } layers := make ( [ ] distribution . Descriptor , 0 , len ( t . FSLayers ) ) for i := len ( t . FSLayers ) - 1 ; i >= 0 ; i -- { layer := distribution . Descriptor { MediaType : schema2 . MediaTypeLayer , Digest : t . FSLayers [ i ] . BlobSum , } add . AddLayerToConfig ( base , layer , " " ) layers = append ( layers , layer ) } return base , layers , nil default : return nil , nil , fmt . Errorf ( " " , srcManifest , location ) } }
ManifestToImageConfig takes an image manifest and converts it into a structured object.
ManifestToImageConfig takes an image manifest and converts it into a structured object .
// schema1 layers are in reverse order // size must be reconstructed from the blobs // we must reconstruct the tar sum from the blobs