-
Notifications
You must be signed in to change notification settings - Fork 208
Add implementation for pipectl tool #1241
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
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d6d1fd
Add implementation for pipectl tool
nghialv c9e8cd5
Switch to use the default logger
nghialv 149ee87
Add wait-status option to sync-app command
nghialv d22518e
Fix lint
nghialv c41064a
Fix wait-status flag
nghialv f633ca0
Simplify git-path flags
nghialv 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["client.go"], | ||
| importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/client", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/app/api/service/apiservice:go_default_library", | ||
| "//pkg/rpc/rpcauth:go_default_library", | ||
| "//pkg/rpc/rpcclient:go_default_library", | ||
| "@com_github_spf13_cobra//:go_default_library", | ||
| "@org_golang_google_grpc//credentials:go_default_library", | ||
| ], | ||
| ) |
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,99 @@ | ||
| // Copyright 2020 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 client | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/grpc/credentials" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/app/api/service/apiservice" | ||
| "github.com/pipe-cd/pipe/pkg/rpc/rpcauth" | ||
| "github.com/pipe-cd/pipe/pkg/rpc/rpcclient" | ||
| ) | ||
|
|
||
| type Options struct { | ||
| Address string | ||
| APIKey string | ||
| APIKeyFile string | ||
| Insecure bool | ||
| CertFile string | ||
| } | ||
|
|
||
| func (o *Options) RegisterPersistentFlags(cmd *cobra.Command) { | ||
| cmd.PersistentFlags().StringVar(&o.Address, "address", o.Address, "The address to control-plane api.") | ||
| cmd.PersistentFlags().StringVar(&o.APIKey, "api-key", o.APIKey, "The API key used while authenticating with control-plane.") | ||
| cmd.PersistentFlags().StringVar(&o.APIKeyFile, "api-key-file", o.APIKeyFile, "Path to the file containing API key used while authenticating with control-plane.") | ||
| cmd.PersistentFlags().BoolVar(&o.Insecure, "insecure", o.Insecure, "Whether disabling transport security while connecting to control-plane.") | ||
| cmd.PersistentFlags().StringVar(&o.CertFile, "cert-file", o.CertFile, "The path to the TLS certificate file.") | ||
| } | ||
|
|
||
| func (o *Options) Validate() error { | ||
| if o.Address == "" { | ||
| return errors.New("address must be set") | ||
| } | ||
| if o.APIKey == "" && o.APIKeyFile == "" { | ||
| return errors.New("either api-key or api-key-file must be set") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (o *Options) NewClient(ctx context.Context) (apiservice.Client, error) { | ||
| if err := o.Validate(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| var creds credentials.PerRPCCredentials | ||
| var err error | ||
|
|
||
| if o.APIKey != "" { | ||
| creds = rpcclient.NewPerRPCCredentials(o.APIKey, rpcauth.APIKeyCredentials, !o.Insecure) | ||
| } else { | ||
| creds, err = rpcclient.NewPerRPCCredentialsFromFile(o.APIKeyFile, rpcauth.APIKeyCredentials, !o.Insecure) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| options := []rpcclient.DialOption{ | ||
| rpcclient.WithBlock(), | ||
| rpcclient.WithPerRPCCredentials(creds), | ||
| } | ||
|
|
||
| if !o.Insecure { | ||
| if o.CertFile != "" { | ||
| options = append(options, rpcclient.WithTLS(o.CertFile)) | ||
| } else { | ||
| config := &tls.Config{} | ||
| options = append(options, rpcclient.WithTransportCredentials(credentials.NewTLS(config))) | ||
| } | ||
| } else { | ||
| options = append(options, rpcclient.WithInsecure()) | ||
| } | ||
|
|
||
| client, err := apiservice.NewClient(ctx, o.Address, options...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return client, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = [ | ||
| "add.go", | ||
| "application.go", | ||
| "sync.go", | ||
| ], | ||
| importpath = "github.com/pipe-cd/pipe/pkg/app/pipectl/cmd/application", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/app/api/service/apiservice:go_default_library", | ||
| "//pkg/app/pipectl/client:go_default_library", | ||
| "//pkg/app/pipectl/log:go_default_library", | ||
| "//pkg/cli:go_default_library", | ||
| "//pkg/model:go_default_library", | ||
| "@com_github_spf13_cobra//:go_default_library", | ||
| ], | ||
| ) |
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,108 @@ | ||
| // Copyright 2020 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 application | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/app/api/service/apiservice" | ||
| "github.com/pipe-cd/pipe/pkg/cli" | ||
| "github.com/pipe-cd/pipe/pkg/model" | ||
| ) | ||
|
|
||
| type add struct { | ||
| root *command | ||
|
|
||
| appName string | ||
| appKind string | ||
| envID string | ||
| pipedID string | ||
| cloudProvider string | ||
|
|
||
| gitPathRepoID string | ||
| gitPathAppDir string | ||
| gitPathConfigFileName string | ||
| } | ||
|
|
||
| func newAddCommand(root *command) *cobra.Command { | ||
| c := &add{ | ||
| root: root, | ||
| gitPathConfigFileName: ".pipe.yaml", | ||
| } | ||
| cmd := &cobra.Command{ | ||
| Use: "add", | ||
| Short: "Add a new application.", | ||
| RunE: cli.WithContext(c.run), | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&c.appName, "app-name", c.appName, "The application name.") | ||
| cmd.Flags().StringVar(&c.appKind, "app-kind", c.appKind, "The kind of application. (KUBERNETES|TERRAFORM|LAMBDA|CLOUDRUN)") | ||
| cmd.Flags().StringVar(&c.envID, "env-id", c.envID, "The ID of environment where this application should belong to.") | ||
| cmd.Flags().StringVar(&c.pipedID, "piped-id", c.pipedID, "The ID of piped that should handle this applicaiton.") | ||
| cmd.Flags().StringVar(&c.cloudProvider, "cloud-provider", c.cloudProvider, "The cloud provider name. One of the registered providers in the piped configuration.") | ||
|
|
||
| cmd.Flags().StringVar(&c.gitPathRepoID, "gitpath-repo-id", c.gitPathRepoID, "The repository ID. One the registered repositories in the piped configuration.") | ||
| cmd.Flags().StringVar(&c.gitPathAppDir, "gitpath-app-dir", c.gitPathAppDir, "The relative path from the root of repository to the application directory.") | ||
| cmd.Flags().StringVar(&c.gitPathConfigFileName, "gitpath-config-file-name", c.gitPathConfigFileName, "The configuration file name. Default is .pipe.yaml") | ||
|
|
||
| cmd.MarkFlagRequired("app-name") | ||
| cmd.MarkFlagRequired("app-kind") | ||
| cmd.MarkFlagRequired("env-id") | ||
| cmd.MarkFlagRequired("piped-id") | ||
| cmd.MarkFlagRequired("cloud-provider") | ||
| cmd.MarkFlagRequired("gitpath-repo-id") | ||
| cmd.MarkFlagRequired("gitpath-app-dir") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (c *add) run(ctx context.Context, _ cli.Telemetry) error { | ||
| logger := c.root.logOptions.NewLogger() | ||
| cli, err := c.root.clientOptions.NewClient(ctx) | ||
| if err != nil { | ||
| logger.Fatal("Failed to initialize client: %v", err) | ||
| } | ||
| defer cli.Close() | ||
|
|
||
| appKind, ok := model.ApplicationKind_value[c.appKind] | ||
| if !ok { | ||
| logger.Fatal("Unsupported application kind %s", c.appKind) | ||
nghialv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| req := &apiservice.AddApplicationRequest{ | ||
| Name: c.appName, | ||
| EnvId: c.envID, | ||
| PipedId: c.pipedID, | ||
| GitPath: &model.ApplicationGitPath{ | ||
| Repo: &model.ApplicationGitRepository{ | ||
| Id: c.gitPathRepoID, | ||
| }, | ||
| Path: c.gitPathAppDir, | ||
| ConfigFilename: c.gitPathConfigFileName, | ||
| }, | ||
| Kind: model.ApplicationKind(appKind), | ||
| CloudProvider: c.cloudProvider, | ||
| } | ||
|
|
||
| resp, err := cli.AddApplication(ctx, req) | ||
| if err != nil { | ||
| logger.Fatal("Failed to add application: %v", err) | ||
nghialv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| logger.Info("Successfully added application id = %s", resp.ApplicationId) | ||
| return 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2020 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 application | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/app/pipectl/client" | ||
| "github.com/pipe-cd/pipe/pkg/app/pipectl/log" | ||
| ) | ||
|
|
||
| type command struct { | ||
| clientOptions *client.Options | ||
| logOptions *log.Options | ||
| } | ||
|
|
||
| func NewCommand() *cobra.Command { | ||
| c := &command{ | ||
| clientOptions: &client.Options{}, | ||
| logOptions: &log.Options{}, | ||
| } | ||
| cmd := &cobra.Command{ | ||
| Use: "application", | ||
| Short: "Manage application resources.", | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| cmd.AddCommand(newAddCommand(c)) | ||
| cmd.AddCommand(newSyncCommand(c)) | ||
|
|
||
| c.clientOptions.RegisterPersistentFlags(cmd) | ||
| c.logOptions.RegisterPersistentFlags(cmd) | ||
|
|
||
| return cmd | ||
| } |
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.
I feel like it should be changeable via
--config-file-nameor something like that. And the default value should refer topkg.model.DefaultDeploymentConfigFileName.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.
Sorry, I just now found it has been already changeable.
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 changed to use the value defined in the model package and simplified the flags.