-
Notifications
You must be signed in to change notification settings - Fork 208
Add gRPC API to push metadata of container image #1358
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
Merged
+227
−11
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/model" | ||
| ) | ||
|
|
||
| const imageReferenceModelKind = "ImageReference" | ||
|
|
||
| var ( | ||
| imageReferenceFactory = func() interface{} { | ||
| return &model.ImageReference{} | ||
| } | ||
| ) | ||
|
|
||
| type ImageReferenceStore interface { | ||
| AddImageReference(ctx context.Context, im model.ImageReference) error | ||
| } | ||
|
|
||
| type imageReferenceStore struct { | ||
| backend | ||
| nowFunc func() time.Time | ||
| } | ||
|
|
||
| func NewImageReferenceStore(ds DataStore) ImageReferenceStore { | ||
| return &imageReferenceStore{ | ||
| backend: backend{ | ||
| ds: ds, | ||
| }, | ||
| nowFunc: time.Now, | ||
| } | ||
| } | ||
|
|
||
| func (s *imageReferenceStore) AddImageReference(ctx context.Context, im model.ImageReference) error { | ||
| now := s.nowFunc().Unix() | ||
| if im.CreatedAt == 0 { | ||
| im.CreatedAt = now | ||
| } | ||
| if im.UpdatedAt == 0 { | ||
| im.UpdatedAt = now | ||
| } | ||
| if err := im.Validate(); err != nil { | ||
| return err | ||
| } | ||
| return s.ds.Create(ctx, imageReferenceModelKind, im.Id, &im) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package datastore | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/golang/mock/gomock" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/model" | ||
| ) | ||
|
|
||
| func TestAddImageReference(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| im := model.ImageReference{ | ||
| Id: "id", | ||
| RepoName: "repo", | ||
| Tags: []string{"tag"}, | ||
| ProjectId: "projectId", | ||
| CreatedAt: 12345, | ||
| UpdatedAt: 12345, | ||
| } | ||
|
|
||
| testcases := []struct { | ||
| name string | ||
| im model.ImageReference | ||
| ds DataStore | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "Invalid image reference", | ||
| im: model.ImageReference{}, | ||
| ds: func() DataStore { | ||
| return NewMockDataStore(ctrl) | ||
| }(), | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "OK to create", | ||
| im: im, | ||
| ds: func() DataStore { | ||
| ds := NewMockDataStore(ctrl) | ||
| ds.EXPECT(). | ||
| Create(gomock.Any(), "ImageReference", im.Id, &im). | ||
| Return(nil) | ||
| return ds | ||
| }(), | ||
| wantErr: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| s := NewImageReferenceStore(tc.ds) | ||
| err := s.AddImageReference(context.Background(), tc.im) | ||
| assert.Equal(t, tc.wantErr, err != nil) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| syntax = "proto3"; | ||
|
|
||
| package pipe.model; | ||
| option go_package = "github.com/pipe-cd/pipe/pkg/model"; | ||
|
|
||
| import "validate/validate.proto"; | ||
|
|
||
| message ImageReference { | ||
| // The generated unique identifier. | ||
| string id = 1 [(validate.rules).string.min_len = 1]; | ||
| // The repository name. | ||
| string repo_name = 2 [(validate.rules).string.min_len = 1]; | ||
| // The image digest. | ||
| string digest = 3; | ||
| // The image tags. | ||
| repeated string tags = 4 [(validate.rules).repeated.min_items = 1]; | ||
| // The ID of the project this image belongs to. | ||
| string project_id = 5 [(validate.rules).string.min_len = 1]; | ||
|
|
||
| // Unix time when the image metadata was created. | ||
| int64 created_at = 14 [(validate.rules).int64.gt = 0]; | ||
| // Unix time of the last time when the image metadata is updated. | ||
| int64 updated_at = 15 [(validate.rules).int64.gt = 0]; | ||
| } | ||
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.
just a comment: This must be unique, so I initially thought it's better to use this as an id, while it's enough to be an optional field. Never mind.
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 found that the digest must not be unique. Unlike other general container registries, we have to handle multiple repositories (including gcr.io, ecr.amazonaws.com and docker hub). So it often happens that multiple references with the same digest to be registered.
Really never mind!
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.
as far as I know, the combination of repository and tag is used to identify container images, the digest just used to compare 2 images, which have the same digest means they are the same. Image digest is hard to read/use so they do use repository and tag combination instead on refer to an image. In our case, digest should be just saved as external metadata to be used for future purposes.
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.
Because we don't actually need this value, and if it's required then the user will have to get that value to set.
Our user does not only use the docker they may use bazel and other tools to build their container images.
An image digest is a hash of an image. So It depends on the image's manifest & layer.
The same image (same digest) can be stored in multiple registries.