Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ genrule(
# gazelle:exclude pkg/model/common.pb.validate.go
# gazelle:exclude pkg/model/deployment.pb.validate.go
# gazelle:exclude pkg/model/environment.pb.validate.go
# gazelle:exclude pkg/model/imagereference.pb.validate.go
# gazelle:exclude pkg/model/event.pb.validate.go
# gazelle:exclude pkg/model/insight.pb.validate.go
# gazelle:exclude pkg/model/logblock.pb.validate.go
# gazelle:exclude pkg/model/notificationevent.pb.validate.go
Expand Down
2 changes: 1 addition & 1 deletion cmd/pipectl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_library(
deps = [
"//pkg/app/pipectl/cmd/application:go_default_library",
"//pkg/app/pipectl/cmd/deployment:go_default_library",
"//pkg/app/pipectl/cmd/image:go_default_library",
"//pkg/app/pipectl/cmd/event:go_default_library",
"//pkg/cli:go_default_library",
],
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pipectl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/application"
"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/deployment"
"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/image"
"github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/event"
"github.com/pipe-cd/pipe/pkg/cli"
)

Expand All @@ -33,7 +33,7 @@ func main() {
app.AddCommands(
application.NewCommand(),
deployment.NewCommand(),
image.NewCommand(),
event.NewCommand(),
)

if err := app.Run(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions docs/content/en/docs/user-guide/command-line-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ pipectl deployment wait-status \
--status=DEPLOYMENT_SUCCESS
```

### Pushing an image reference
### Registering an event for EventWatcher

Push a new container image reference that can be used by ImageWatcher.
Register an event that can be used by EventWatcher.

``` console
pipectl image push-reference \
pipectl event register \
--address=CONTROL_PLANE_API_ADDRESS \
--api-key=API_KEY \
--repo-name=gcr.io/pipecd/example \
--tag=v0.1.0
--name=example-image-pushed \
--data=gcr.io/pipecd/example:v0.1.0
```

### You want more?
Expand Down
46 changes: 22 additions & 24 deletions pkg/app/api/grpcapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +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
imageReferenceStore datastore.ImageReferenceStore
commandStore commandstore.Store
applicationStore datastore.ApplicationStore
environmentStore datastore.EnvironmentStore
deploymentStore datastore.DeploymentStore
pipedStore datastore.PipedStore
eventStore datastore.EventStore
commandStore commandstore.Store

logger *zap.Logger
}
Expand All @@ -51,13 +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),
imageReferenceStore: datastore.NewImageReferenceStore(ds),
commandStore: cmds,
logger: logger.Named("api"),
applicationStore: datastore.NewApplicationStore(ds),
environmentStore: datastore.NewEnvironmentStore(ds),
deploymentStore: datastore.NewDeploymentStore(ds),
pipedStore: datastore.NewPipedStore(ds),
eventStore: datastore.NewEventStore(ds),
commandStore: cmds,
logger: logger.Named("api"),
}
return a
}
Expand Down Expand Up @@ -314,29 +314,27 @@ func (a *API) GetCommand(ctx context.Context, req *apiservice.GetCommandRequest)
}, nil
}

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

im := model.ImageReference{
err = a.eventStore.AddEvent(ctx, model.Event{
Id: uuid.New().String(),
RepoName: req.RepoName,
Digest: req.Digest,
Tags: req.Tags,
Name: req.Name,
Data: req.Data,
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")
return nil, status.Error(codes.AlreadyExists, "The event 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")
a.logger.Error("failed to register event", zap.Error(err))
return nil, status.Error(codes.Internal, "Failed to register event")
}

return &apiservice.PushImageReferenceResponse{}, nil
return &apiservice.RegisterEventResponse{}, nil
}

// requireAPIKey checks the existence of an API key inside the given context
Expand Down
11 changes: 5 additions & 6 deletions pkg/app/api/service/apiservice/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ service APIService {

rpc GetCommand(GetCommandRequest) returns (GetCommandResponse) {}

rpc PushImageReference(PushImageReferenceRequest) returns (PushImageReferenceResponse) {}
rpc RegisterEvent(RegisterEventRequest) returns (RegisterEventResponse) {}
}

message AddApplicationRequest {
Expand Down Expand Up @@ -95,11 +95,10 @@ 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 RegisterEventRequest {
string name = 1 [(validate.rules).string.min_len = 1];
string data = 2 [(validate.rules).string.min_len = 1];
}

message PushImageReferenceResponse {
message RegisterEventResponse {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"image.go",
"pushreference.go",
"event.go",
"register.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/image",
importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/event",
visibility = ["//visibility:public"],
deps = [
"//pkg/app/api/service/apiservice:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package image
package event

import (
"github.com/spf13/cobra"
Expand All @@ -29,12 +29,12 @@ func NewCommand() *cobra.Command {
clientOptions: &client.Options{},
}
cmd := &cobra.Command{
Use: "image",
Short: "Manage image resources.",
Use: "event",
Short: "Manage event resources.",
}

cmd.AddCommand(
newPushReferenceCommand(c),
newRegisterCommand(c),
)

c.clientOptions.RegisterPersistentFlags(cmd)
Expand Down
71 changes: 71 additions & 0 deletions pkg/app/pipectl/cmd/event/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 event

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/pipe-cd/pipe/pkg/app/api/service/apiservice"
"github.com/pipe-cd/pipe/pkg/cli"
)

type register struct {
root *command

name string
data string
}

func newRegisterCommand(root *command) *cobra.Command {
r := &register{
root: root,
}
cmd := &cobra.Command{
Use: "register",
Comment thread
nghialv marked this conversation as resolved.
Short: "Register an event.",
RunE: cli.WithContext(r.run),
}

cmd.Flags().StringVar(&r.name, "name", r.name, "The name of event.")
cmd.Flags().StringVar(&r.data, "data", r.data, "The string value of event data.")

cmd.MarkFlagRequired("name")
cmd.MarkFlagRequired("data")

return cmd
}

func (r *register) run(ctx context.Context, t cli.Telemetry) error {
cli, err := r.root.clientOptions.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to initialize client: %w", err)
}
defer cli.Close()

req := &apiservice.RegisterEventRequest{
Name: r.name,
Data: r.data,
}

if _, err := cli.RegisterEvent(ctx, req); err != nil {
return fmt.Errorf("failed to register event: %w", err)
}

t.Logger.Info("Successfully registered event")
return nil
}
74 changes: 0 additions & 74 deletions pkg/app/pipectl/cmd/image/pushreference.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/datastore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_library(
"datastore.go",
"deploymentstore.go",
"environmentstore.go",
"imagereference.go",
"event.go",
"mock.go",
"pipedstatsstore.go",
"pipedstore.go",
Expand All @@ -32,7 +32,7 @@ go_test(
"commandstore_test.go",
"deploymentstore_test.go",
"environmentstore_test.go",
"imagereference_test.go",
"event_test.go",
"pipedstatsstore_test.go",
"pipedstore_test.go",
"projectstore_test.go",
Expand Down
Loading