Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions pkg/app/api/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import (

// API implements the behaviors for the gRPC definitions of API.
type API struct {
applicationStore datastore.ApplicationStore
environmentStore datastore.EnvironmentStore
deploymentStore datastore.DeploymentStore
pipedStore datastore.PipedStore
commandStore commandstore.Store
applicationStore datastore.ApplicationStore
environmentStore datastore.EnvironmentStore
deploymentStore datastore.DeploymentStore
pipedStore datastore.PipedStore
imageReferenceStore datastore.ImageReferenceStore
commandStore commandstore.Store

logger *zap.Logger
}
Expand All @@ -50,12 +51,13 @@ func NewAPI(
logger *zap.Logger,
) *API {
a := &API{
applicationStore: datastore.NewApplicationStore(ds),
environmentStore: datastore.NewEnvironmentStore(ds),
deploymentStore: datastore.NewDeploymentStore(ds),
pipedStore: datastore.NewPipedStore(ds),
commandStore: cmds,
logger: logger.Named("api"),
applicationStore: datastore.NewApplicationStore(ds),
environmentStore: datastore.NewEnvironmentStore(ds),
deploymentStore: datastore.NewDeploymentStore(ds),
pipedStore: datastore.NewPipedStore(ds),
imageReferenceStore: datastore.NewImageReferenceStore(ds),
commandStore: cmds,
logger: logger.Named("api"),
}
return a
}
Expand Down Expand Up @@ -312,6 +314,31 @@ func (a *API) GetCommand(ctx context.Context, req *apiservice.GetCommandRequest)
}, nil
}

func (a *API) PushImageReference(ctx context.Context, req *apiservice.PushImageReferenceRequest) (*apiservice.PushImageReferenceResponse, error) {
key, err := requireAPIKey(ctx, model.APIKey_READ_WRITE, a.logger)
if err != nil {
return nil, err
}

im := model.ImageReference{
Id: uuid.New().String(),
RepoName: req.RepoName,
Digest: req.Digest,
Tags: req.Tags,
ProjectId: key.ProjectId,
}
err = a.imageReferenceStore.AddImageReference(ctx, im)
if errors.Is(err, datastore.ErrAlreadyExists) {
return nil, status.Error(codes.AlreadyExists, "The image reference already exists")
}
if err != nil {
a.logger.Error("failed to add image reference", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to add image reference")
}

return &apiservice.PushImageReferenceResponse{}, nil
}

// requireAPIKey checks the existence of an API key inside the given context
// and ensures that it has enough permissions for the give role.
func requireAPIKey(ctx context.Context, role model.APIKey_Role, logger *zap.Logger) (*model.APIKey, error) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/app/api/service/apiservice/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ service APIService {
rpc GetDeployment(GetDeploymentRequest) returns (GetDeploymentResponse) {}

rpc GetCommand(GetCommandRequest) returns (GetCommandResponse) {}

rpc PushImageReference(PushImageReferenceRequest) returns (PushImageReferenceResponse) {}
}

message AddApplicationRequest {
Expand Down Expand Up @@ -92,3 +94,12 @@ message GetCommandRequest {
message GetCommandResponse {
pipe.model.Command command = 1;
}

message PushImageReferenceRequest {
string repo_name = 1 [(validate.rules).string.min_len = 1];
repeated string tags = 2 [(validate.rules).repeated.min_items = 1];
string digest = 3;
}

message PushImageReferenceResponse {
}
2 changes: 2 additions & 0 deletions pkg/datastore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"datastore.go",
"deploymentstore.go",
"environmentstore.go",
"imagereference.go",
"mock.go",
"pipedstatsstore.go",
"pipedstore.go",
Expand All @@ -31,6 +32,7 @@ go_test(
"commandstore_test.go",
"deploymentstore_test.go",
"environmentstore_test.go",
"imagereference_test.go",
"pipedstatsstore_test.go",
"pipedstore_test.go",
"projectstore_test.go",
Expand Down
62 changes: 62 additions & 0 deletions pkg/datastore/imagereference.go
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)
}
75 changes: 75 additions & 0 deletions pkg/datastore/imagereference_test.go
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)
})
}
}
1 change: 1 addition & 0 deletions pkg/model/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ proto_library(
"deployment.proto",
"environment.proto",
"event.proto",
"imagereference.proto",
"insight.proto",
"logblock.proto",
"piped.proto",
Expand Down
38 changes: 38 additions & 0 deletions pkg/model/imagereference.proto
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;
Copy link
Member

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.

Copy link
Member

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.

$ docker images
REPOSITORY                                                         TAG                 IMAGE ID            CREATED             SIZE
nakabonne-test                                                     foo                 87c41dfd667d        13 days ago         194MB
000000000000.dkr.ecr.ap-northeast-1.amazonaws.com/nakabonne-test   foo                 87c41dfd667d        13 days ago         194MB

Really never mind!

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the digest is optional?

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.

Why not use the digest as the ID?

An image digest is a hash of an image. So It depends on the image's manifest & layer.

docker images --digests
REPOSITORY                            TAG                   DIGEST                                                                    IMAGE ID       CREATED         SIZE
alpine                                latest                sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436   389fef711851   3 weeks ago     5.58MB

The same image (same digest) can be stored in multiple registries.

// 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];
}