-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Replace ImageStreamMapping by ImageStreamImport #16475
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,16 @@ package server | |
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/docker/distribution" | ||
"github.com/docker/distribution/context" | ||
"github.com/docker/distribution/digest" | ||
"github.com/docker/distribution/manifest/schema2" | ||
"github.com/docker/distribution/registry/api/errcode" | ||
regapi "github.com/docker/distribution/registry/api/v2" | ||
|
||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
imageapi "github.com/openshift/origin/pkg/image/apis/image" | ||
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1" | ||
quotautil "github.com/openshift/origin/pkg/quota/util" | ||
) | ||
|
||
var _ distribution.ManifestService = &manifestService{} | ||
|
@@ -47,38 +40,29 @@ func (m *manifestService) Exists(ctx context.Context, dgst digest.Digest) (bool, | |
func (m *manifestService) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) { | ||
context.GetLogger(ctx).Debugf("(*manifestService).Get") | ||
|
||
image, _, _, err := m.repo.getStoredImageOfImageStream(dgst) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ref := imageapi.DockerImageReference{ | ||
cacheName := imageapi.DockerImageReference{ | ||
Namespace: m.repo.namespace, | ||
Name: m.repo.name, | ||
Registry: m.repo.config.registryAddr, | ||
} | ||
if isImageManaged(image) { | ||
// Reference without a registry part refers to repository containing locally managed images. | ||
// Such an entry is retrieved, checked and set by blobDescriptorService operating only on local blobs. | ||
ref.Registry = "" | ||
} else { | ||
// Repository with a registry points to remote repository. This is used by pullthrough middleware. | ||
ref = ref.DockerClientDefaults().AsRepository() | ||
} | ||
}.Exact() | ||
|
||
manifest, err := m.manifests.Get(withRepository(ctx, m.repo), dgst, options...) | ||
switch err.(type) { | ||
case distribution.ErrManifestUnknownRevision: | ||
break | ||
case nil: | ||
m.repo.rememberLayersOfManifest(dgst, manifest, ref.Exact()) | ||
m.migrateManifest(withRepository(ctx, m.repo), image, dgst, manifest, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may still want to remove manifest and config from the image object. |
||
m.repo.rememberLayersOfManifest(dgst, manifest, cacheName) | ||
return manifest, nil | ||
default: | ||
context.GetLogger(m.ctx).Errorf("unable to get manifest from storage: %v", err) | ||
return nil, err | ||
} | ||
|
||
image, _, _, err := m.repo.getStoredImageOfImageStream(dgst) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(image.DockerImageManifest) == 0 { | ||
// We don't have the manifest in the storage and we don't have the manifest | ||
// inside the image so there is no point to continue. | ||
|
@@ -88,7 +72,7 @@ func (m *manifestService) Get(ctx context.Context, dgst digest.Digest, options . | |
} | ||
} | ||
|
||
manifest, err = m.repo.manifestFromImageWithCachedLayers(image, ref.Exact()) | ||
manifest, err = m.repo.manifestFromImageWithCachedLayers(image, cacheName) | ||
if err == nil { | ||
m.migrateManifest(withRepository(ctx, m.repo), image, dgst, manifest, false) | ||
} | ||
|
@@ -104,7 +88,7 @@ func (m *manifestService) Put(ctx context.Context, manifest distribution.Manifes | |
if err != nil { | ||
return "", regapi.ErrorCodeManifestInvalid.WithDetail(err) | ||
} | ||
mediaType, payload, canonical, err := mh.Payload() | ||
mediaType, _, canonical, err := mh.Payload() | ||
if err != nil { | ||
return "", regapi.ErrorCodeManifestInvalid.WithDetail(err) | ||
} | ||
|
@@ -127,80 +111,6 @@ func (m *manifestService) Put(ctx context.Context, manifest distribution.Manifes | |
// Calculate digest | ||
dgst := digest.FromBytes(canonical) | ||
|
||
// Upload to openshift | ||
ism := imageapiv1.ImageStreamMapping{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: m.repo.namespace, | ||
Name: m.repo.name, | ||
}, | ||
Image: imageapiv1.Image{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: dgst.String(), | ||
Annotations: map[string]string{ | ||
imageapi.ManagedByOpenShiftAnnotation: "true", | ||
}, | ||
}, | ||
DockerImageReference: fmt.Sprintf("%s/%s/%s@%s", m.repo.config.registryAddr, m.repo.namespace, m.repo.name, dgst.String()), | ||
DockerImageManifest: string(payload), | ||
DockerImageManifestMediaType: mediaType, | ||
}, | ||
} | ||
|
||
for _, option := range options { | ||
if opt, ok := option.(distribution.WithTagOption); ok { | ||
ism.Tag = opt.Tag | ||
break | ||
} | ||
} | ||
|
||
if err = mh.FillImageMetadata(ctx, &ism.Image); err != nil { | ||
return "", err | ||
} | ||
|
||
// Remove the raw manifest as it's very big and this leads to a large memory consumption in etcd. | ||
ism.Image.DockerImageManifest = "" | ||
ism.Image.DockerImageConfig = "" | ||
|
||
if _, err = m.repo.registryOSClient.ImageStreamMappings(m.repo.namespace).Create(&ism); err != nil { | ||
// if the error was that the image stream wasn't found, try to auto provision it | ||
statusErr, ok := err.(*kerrors.StatusError) | ||
if !ok { | ||
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err) | ||
return "", err | ||
} | ||
|
||
if quotautil.IsErrorQuotaExceeded(statusErr) { | ||
context.GetLogger(ctx).Errorf("denied creating ImageStreamMapping: %v", statusErr) | ||
return "", distribution.ErrAccessDenied | ||
} | ||
|
||
status := statusErr.ErrStatus | ||
kind := strings.ToLower(status.Details.Kind) | ||
isValidKind := kind == "imagestream" /*pre-1.2*/ || kind == "imagestreams" /*1.2 to 1.6*/ || kind == "imagestreammappings" /*1.7+*/ | ||
if !isValidKind || status.Code != http.StatusNotFound || status.Details.Name != m.repo.name { | ||
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err) | ||
return "", err | ||
} | ||
|
||
if _, err := m.repo.createImageStream(ctx); err != nil { | ||
if e, ok := err.(errcode.Error); ok && e.ErrorCode() == errcode.ErrorCodeUnknown { | ||
// TODO: convert statusErr to distribution error | ||
return "", statusErr | ||
} | ||
return "", err | ||
} | ||
|
||
// try to create the ISM again | ||
if _, err := m.repo.registryOSClient.ImageStreamMappings(m.repo.namespace).Create(&ism); err != nil { | ||
if quotautil.IsErrorQuotaExceeded(err) { | ||
context.GetLogger(ctx).Errorf("denied a creation of ImageStreamMapping: %v", err) | ||
return "", distribution.ErrAccessDenied | ||
} | ||
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err) | ||
return "", err | ||
} | ||
} | ||
|
||
return dgst, nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This moves the status of "source of truth" from etcd to registry's storage. Is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it fixes #9550.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO etcd should stay as a source of truth. Deleting an image using
oc delete
should prevent registry from serving it, which applies to deleting imagestreamtag or whole imagestream. Tagging the image into new image stream usingoc tag
should allow registry to serve it from the corresponding repository.Moving the source of truth from etcd to storage implies a need for one-way synchronization of storage state to etcd. We don't have that in place yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, you are right, this code has a bug. Image streams should be the source of truth for tagged manifests. Though, image streams don't contain information about untagged manifests and can't be used as a source of truth for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we don't keep the history of image presence in image streams (something like untagged images). And I don't think it's desirable since image stream is already too fat.
The rejection to serve a manifest can be determined from the absence of image reference in the image stream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to get a rejection, I want to pull it back even if I don't tag it.