Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pkg/app/launcher/cmd/launcher/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go_library(
"@com_github_spf13_cobra//:go_default_library",
"@com_google_cloud_go//secretmanager/apiv1:go_default_library",
"@go_googleapis//google/cloud/secretmanager/v1:secretmanager_go_proto",
"@io_k8s_sigs_yaml//:go_default_library",
"@org_golang_google_grpc//credentials:go_default_library",
"@org_golang_x_sync//errgroup:go_default_library",
"@org_uber_go_zap//:go_default_library",
Expand Down
70 changes: 65 additions & 5 deletions pkg/app/launcher/cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -33,6 +35,7 @@ import (
"golang.org/x/sync/errgroup"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
"google.golang.org/grpc/credentials"
"sigs.k8s.io/yaml"

"github.com/pipe-cd/pipecd/pkg/admin"
"github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice"
Expand Down Expand Up @@ -511,15 +514,72 @@ func makePipedArgs(launcherArgs []string, configFile string) []string {
return pipedArgs
}

func parseConfig(data []byte) (*config.PipedSpec, error) {
cfg, err := config.DecodeYAML(data)
func parseConfig(data []byte) (*LauncherSpec, error) {
js, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
if cfg.Kind != config.KindPiped {
return nil, fmt.Errorf("wrong configuration kind for piped: %v", cfg.Kind)

c := &Config{}
if err := json.Unmarshal(js, c); err != nil {
return nil, err
}

if err := c.Validate(); err != nil {
return nil, err
}
return &c.Spec, nil
}

type Config struct {
Kind config.Kind `json:"kind"`
APIVersion string `json:"apiVersion,omitempty"`
Spec LauncherSpec `json:"spec"`
}

func (c *Config) Validate() error {
if c.Kind != config.KindPiped {
return fmt.Errorf("wrong configuration kind for piped: %v", c.Kind)
}
if c.Spec.ProjectID == "" {
return errors.New("projectID must be set")
}
if c.Spec.PipedID == "" {
return errors.New("pipedID must be set")
}
if c.Spec.PipedKeyData == "" && c.Spec.PipedKeyFile == "" {
return errors.New("either pipedKeyFile or pipedKeyData must be set")
}
if c.Spec.PipedKeyData != "" && c.Spec.PipedKeyFile != "" {
return errors.New("only pipedKeyFile or pipedKeyData can be set")
}
if c.Spec.APIAddress == "" {
return errors.New("apiAddress must be set")
}
return nil
}

type LauncherSpec struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

type name will be used as launcher.LauncherSpec by other packages, and that stutters; consider calling this Spec

https://golang.org/wiki/CodeReviewComments#package-names

Copy link
Member

Choose a reason for hiding this comment

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

nits, is it enough to make this struct just a private struct?

Copy link
Member

@knanao knanao Mar 8, 2022

Choose a reason for hiding this comment

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

I thought it would be okay to define it in /config. but it's up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. Fixed them. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought it would be okay to define it in /config. but it's up to you.

I feel you. That is nice too. Let me apply your suggestion. 👍

// The identifier of the PipeCD project where this piped belongs to.
ProjectID string
// The unique identifier generated for this piped.
PipedID string
// The path to the file containing the generated Key string for this piped.
PipedKeyFile string
// Base64 encoded string of Piped key.
PipedKeyData string
// The address used to connect to the control-plane's API.
APIAddress string `json:"apiAddress"`
}

func (s *LauncherSpec) LoadPipedKey() ([]byte, error) {
if s.PipedKeyData != "" {
return base64.StdEncoding.DecodeString(s.PipedKeyData)
}
if s.PipedKeyFile != "" {
return os.ReadFile(s.PipedKeyFile)
}
return cfg.PipedSpec, nil
return nil, errors.New("either pipedKeyFile or pipedKeyData must be set")
}

func makeDownloadURL(version string) string {
Expand Down