Ejemplo n.º 1
0
// Get retrieves an image by ID that has previously been tagged into an image stream.
// `id` is of the form <repo name>@<image id>.
func (r *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
	name, imageID, err := parseNameAndID(id)
	if err != nil {
		return nil, err
	}

	repo, err := r.imageStreamRegistry.GetImageStream(ctx, name)
	if err != nil {
		return nil, err
	}

	if repo.Status.Tags == nil {
		return nil, errors.NewNotFound(api.Resource("imagestreamimage"), imageID)
	}

	event, err := api.ResolveImageID(repo, imageID)
	if err != nil {
		return nil, err
	}

	imageName := event.Image
	image, err := r.imageRegistry.GetImage(ctx, imageName)
	if err != nil {
		return nil, err
	}
	if err := api.ImageWithMetadata(image); err != nil {
		return nil, err
	}
	image.DockerImageManifest = ""

	if d, err := digest.ParseDigest(imageName); err == nil {
		imageName = d.Hex()
	}
	if len(imageName) > 7 {
		imageName = imageName[:7]
	}

	isi := api.ImageStreamImage{
		ObjectMeta: kapi.ObjectMeta{
			Namespace: kapi.NamespaceValue(ctx),
			Name:      api.MakeImageStreamImageName(name, imageName),
		},
		Image: *image,
	}

	return &isi, nil
}
Ejemplo n.º 2
0
func TestGetImageReferenceForObjectReference(t *testing.T) {
	for _, tc := range []struct {
		name           string
		namespace      string
		objRef         kapi.ObjectReference
		expectedString string
		expectedError  bool
	}{
		{
			name: "isimage without namespace",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamImage",
				Name: imageapi.MakeImageStreamImageName("is", imagetest.BaseImageWith1LayerDigest),
			},
			expectedString: "is@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name:      "isimage with a fallback namespace",
			namespace: "fallback",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamImage",
				Name: imageapi.MakeImageStreamImageName("is", imagetest.BaseImageWith1LayerDigest),
			},
			expectedString: "fallback/is@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name:      "isimage with namespace set",
			namespace: "fallback",
			objRef: kapi.ObjectReference{
				Kind:      "ImageStreamImage",
				Namespace: "ns",
				Name:      imageapi.MakeImageStreamImageName("is", imagetest.BaseImageWith1LayerDigest),
			},
			expectedString: "ns/is@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name: "isimage missing id",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamImage",
				Name: imagetest.InternalRegistryURL + "/is",
			},
			expectedError: true,
		},

		{
			name: "isimage with a tag",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamImage",
				Name: imagetest.InternalRegistryURL + "/is:latest",
			},
			expectedError: true,
		},

		{
			name: "istag without namespace",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamTag",
				Name: "is:latest",
			},
			expectedString: "is:latest",
		},

		{
			name:      "istag with fallback namespace",
			namespace: "fallback",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamTag",
				Name: "is:latest",
			},
			expectedString: "fallback/is:latest",
		},

		{
			name:      "istag with namespace set",
			namespace: "fallback",
			objRef: kapi.ObjectReference{
				Kind:      "ImageStreamTag",
				Namespace: "ns",
				Name:      "is:latest",
			},
			expectedString: "ns/is:latest",
		},

		{
			name: "istag with missing tag",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamTag",
				Name: "is",
			},
			expectedError: true,
		},

		{
			name: "istag with image ID",
			objRef: kapi.ObjectReference{
				Kind: "ImageStreamTag",
				Name: "is@" + imagetest.BaseImageWith1LayerDigest,
			},
			expectedError: true,
		},

		{
			name: "dockerimage without registry url",
			objRef: kapi.ObjectReference{
				Kind:      "DockerImage",
				Namespace: "ns",
				Name:      "repo@" + imagetest.BaseImageWith1LayerDigest,
			},
			expectedString: "docker.io/repo@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name: "dockerimage with a default tag",
			objRef: kapi.ObjectReference{
				Kind:      "DockerImage",
				Namespace: "ns",
				Name:      "library/repo:latest",
			},
			expectedString: "docker.io/repo",
		},

		{
			name: "dockerimage with a non-default tag",
			objRef: kapi.ObjectReference{
				Kind:      "DockerImage",
				Namespace: "ns",
				Name:      "repo:tag",
			},
			expectedString: "docker.io/repo:tag",
		},

		{
			name: "dockerimage referencing docker image",
			objRef: kapi.ObjectReference{
				Kind: "DockerImage",
				Name: "index.docker.io/repo@" + imagetest.BaseImageWith1LayerDigest,
			},
			expectedString: "docker.io/repo@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name: "dockerimage without tag or id",
			objRef: kapi.ObjectReference{
				Kind: "DockerImage",
				Name: "index.docker.io/user/repo",
			},
			expectedString: "docker.io/user/repo",
		},

		{
			name: "dockerimage with internal registry",
			objRef: kapi.ObjectReference{
				Kind: "DockerImage",
				Name: imagetest.MakeDockerImageReference("test", "is", imagetest.BaseImageWith1LayerDigest),
			},
			expectedString: imagetest.InternalRegistryURL + "/test/is@" + imagetest.BaseImageWith1LayerDigest,
		},

		{
			name: "bad king",
			objRef: kapi.ObjectReference{
				Kind: "dockerImage",
				Name: imagetest.MakeDockerImageReference("test", "is", imagetest.BaseImageWith1LayerDigest),
			},
			expectedError: true,
		},
	} {

		res, err := GetImageReferenceForObjectReference(tc.namespace, &tc.objRef)
		if tc.expectedError && err == nil {
			t.Errorf("[%s] got unexpected non-error", tc.name)
		}
		if !tc.expectedError {
			if err != nil {
				t.Errorf("[%s] got unexpected error: %v", tc.name, err)
			}
			if res != tc.expectedString {
				t.Errorf("[%s] got unexpected results (%q != %q)", tc.name, res, tc.expectedString)
			}
		}
	}
}
Ejemplo n.º 3
0
// Get finds the specified image by name of an image repository and id.
func (c *imageStreamImages) Get(name, id string) (result *api.ImageStreamImage, err error) {
	result = &api.ImageStreamImage{}
	err = c.r.Get().Namespace(c.ns).Resource("imageStreamImages").Name(api.MakeImageStreamImageName(name, id)).Do().Into(result)
	return
}
func TestImageStreamImportEvaluatorUsage(t *testing.T) {
	for _, tc := range []struct {
		name            string
		iss             []imageapi.ImageStream
		isiSpec         imageapi.ImageStreamImportSpec
		expectedISCount int64
	}{
		{
			name: "nothing to import",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
			},
		},

		{
			name: "dry run",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: false,
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind: "DockerImage",
						Name: "docker.io/library/fedora",
					},
				},
			},
		},

		{
			name: "wrong from kind",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind:      "ImageStreamImage",
						Namespace: "test",
						Name:      imageapi.MakeImageStreamImageName("someis", imagetest.BaseImageWith1LayerDigest),
					},
				},
			},
			expectedISCount: 1,
		},

		{
			name: "import from repository to empty project",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind: "DockerImage",
						Name: "docker.io/fedora",
					},
				},
			},
			expectedISCount: 1,
		},

		{
			name: "import from repository to existing image stream",
			iss: []imageapi.ImageStream{
				{
					ObjectMeta: kapi.ObjectMeta{
						Namespace: "test",
						Name:      "havingtag",
					},
				},
				{
					ObjectMeta: kapi.ObjectMeta{
						Namespace: "test",
						Name:      "is",
					},
				},
			},
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind: "DockerImage",
						Name: "docker.io/fedora",
					},
				},
			},
			// target image stream already exists
			expectedISCount: 0,
		},

		{
			name: "import from repository to non-empty project",
			iss: []imageapi.ImageStream{
				{
					ObjectMeta: kapi.ObjectMeta{
						Namespace: "test",
						Name:      "spec",
					},
				},
			},
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind: "DockerImage",
						Name: "docker.io/library/fedora",
					},
				},
			},
			expectedISCount: 1,
		},

		{
			name: "import images",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Images: []imageapi.ImageImportSpec{
					{
						From: kapi.ObjectReference{
							Kind: "DockerImage",
							Name: "docker.io/library/fedora:f23",
						},
					},
				},
			},
			expectedISCount: 1,
		},

		{
			name: "import image and repository",
			isiSpec: imageapi.ImageStreamImportSpec{
				Import: true,
				Images: []imageapi.ImageImportSpec{
					{
						From: kapi.ObjectReference{
							Kind: "DockerImage",
							Name: "docker.io/centos:latest",
						},
					},
				},
				Repository: &imageapi.RepositoryImportSpec{
					From: kapi.ObjectReference{
						Kind: "DockerImage",
						Name: "docker.io/library/fedora",
					},
				},
			},
			expectedISCount: 1,
		},
	} {

		fakeClient := &testclient.Fake{}
		fakeClient.AddReactor("get", "imagestreams", imagetest.GetFakeImageStreamGetHandler(t, tc.iss...))

		evaluator := NewImageStreamImportEvaluator(fakeClient)

		isi := &imageapi.ImageStreamImport{
			ObjectMeta: kapi.ObjectMeta{
				Namespace: "test",
				Name:      "is",
			},
			Spec: tc.isiSpec,
		}

		usage := evaluator.Usage(isi)
		expectedUsage := imagetest.ExpectedResourceListFor(tc.expectedISCount)
		expectedResources := kquota.ResourceNames(expectedUsage)
		if len(usage) != len(expectedResources) {
			t.Errorf("[%s]: got unexpected number of computed resources: %d != %d", tc.name, len(usage), len(expectedResources))
		}

		masked := kquota.Mask(usage, expectedResources)
		if len(masked) != len(expectedUsage) {
			for k := range usage {
				if _, exists := masked[k]; !exists {
					t.Errorf("[%s]: got unexpected resource %q from Usage() method", tc.name, k)
				}
			}

			for k := range expectedUsage {
				if _, exists := masked[k]; !exists {
					t.Errorf("[%s]: expected resource %q not computed", tc.name, k)
				}
			}
		}

		for rname, expectedValue := range expectedUsage {
			if v, exists := masked[rname]; exists {
				if v.Cmp(expectedValue) != 0 {
					t.Errorf("[%s]: got unexpected usage for %q: %s != %s", tc.name, rname, v.String(), expectedValue.String())
				}
			}
		}
	}
}