Skip to content
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

Use tag to specify default value of config #2003

Merged
merged 1 commit into from
May 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ecs v1.1.1
github.com/aws/aws-sdk-go-v2/service/lambda v1.1.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.2.0
github.com/creasty/defaults v1.5.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/envoyproxy/protoc-gen-validate v0.1.0
github.com/fsouza/fake-gcs-server v1.21.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creasty/defaults v1.5.1 h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM=
github.com/creasty/defaults v1.5.1/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
github.com/cucumber/gherkin-go/v13 v13.0.0 h1:d09AwPZldyOFlCADAIZQMt7T+VHCrGdp5106+BCIfZU=
github.com/cucumber/gherkin-go/v13 v13.0.0/go.mod h1:pzMEPEIPcPOBDkE8HaWC+Ck6eDBtBmIWVksUkSin/2E=
github.com/cucumber/messages-go/v12 v12.0.0 h1:ZlZYZrYDDc35zCe0HK2y95GUcxHHqr8yB9kdrXCjnzU=
Expand Down
5 changes: 0 additions & 5 deletions pkg/app/piped/cloudprovider/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"github.com/pipe-cd/pipe/pkg/config"
)

const defaultFunctionManifestFilename = "function.yaml"

// Client is wrapper of AWS client.
type Client interface {
IsFunctionExist(ctx context.Context, name string) (bool, error)
Expand All @@ -45,9 +43,6 @@ type Registry interface {

// LoadFunctionManifest returns FunctionManifest object from a given Function config manifest file.
func LoadFunctionManifest(appDir, functionManifestFilename string) (FunctionManifest, error) {
if functionManifestFilename == "" {
functionManifestFilename = defaultFunctionManifestFilename
}
path := filepath.Join(appDir, functionManifestFilename)
return loadFunctionManifest(path)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/model:go_default_library",
"@com_github_creasty_defaults//:go_default_library",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@io_k8s_sigs_yaml//:go_default_library",
],
Expand Down
29 changes: 8 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io/ioutil"

"github.com/creasty/defaults"
"sigs.k8s.io/yaml"

"github.com/pipe-cd/pipe/pkg/model"
Expand Down Expand Up @@ -79,7 +80,6 @@ type Config struct {
APIVersion string
spec interface{}

// Deployment specs.
KubernetesDeploymentSpec *KubernetesDeploymentSpec
TerraformDeploymentSpec *TerraformDeploymentSpec
CloudRunDeploymentSpec *CloudRunDeploymentSpec
Expand All @@ -106,39 +106,23 @@ func (c *Config) init(kind Kind, apiVersion string) error {

switch kind {
case KindKubernetesApp:
c.KubernetesDeploymentSpec = &KubernetesDeploymentSpec{
Input: KubernetesDeploymentInput{
AutoRollback: true,
},
}
c.KubernetesDeploymentSpec = &KubernetesDeploymentSpec{}
c.spec = c.KubernetesDeploymentSpec

case KindTerraformApp:
c.TerraformDeploymentSpec = &TerraformDeploymentSpec{}
c.spec = c.TerraformDeploymentSpec

case KindCloudRunApp:
c.CloudRunDeploymentSpec = &CloudRunDeploymentSpec{
Input: CloudRunDeploymentInput{
AutoRollback: true,
},
}
c.CloudRunDeploymentSpec = &CloudRunDeploymentSpec{}
c.spec = c.CloudRunDeploymentSpec

case KindLambdaApp:
c.LambdaDeploymentSpec = &LambdaDeploymentSpec{
Input: LambdaDeploymentInput{
AutoRollback: true,
},
}
c.LambdaDeploymentSpec = &LambdaDeploymentSpec{}
c.spec = c.LambdaDeploymentSpec

case KindECSApp:
c.ECSDeploymentSpec = &ECSDeploymentSpec{
Input: ECSDeploymentInput{
AutoRollback: true,
},
}
c.ECSDeploymentSpec = &ECSDeploymentSpec{}
c.spec = c.ECSDeploymentSpec

case KindPiped:
Expand Down Expand Up @@ -238,6 +222,9 @@ func DecodeYAML(data []byte) (*Config, error) {
if err := json.Unmarshal(js, c); err != nil {
return nil, err
}
if err := defaults.Set(c); err != nil {
return nil, err
}
if err := c.Validate(); err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestUnmarshalConfig(t *testing.T) {
}`,
wantSpec: &KubernetesDeploymentSpec{
Input: KubernetesDeploymentInput{
AutoRollback: true,
Copy link
Member

Choose a reason for hiding this comment

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

Should it be remained? 👀

Copy link
Member Author

@nghialv nghialv May 26, 2021

Choose a reason for hiding this comment

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

No. This test is for checking the UnmarshalJSON function of Config struct.
Our defaults assigning is done after that and not included in this test.

Copy link
Member

Choose a reason for hiding this comment

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

got it 👍 It addressed here pkg/config/deployment_kubernetes_test.go:76 already. Thanks for your clarification 🙏

Namespace: "default",
Namespace: "default",
},
},
wantErr: false,
Expand All @@ -61,8 +60,7 @@ func TestUnmarshalConfig(t *testing.T) {
}`,
wantSpec: &KubernetesDeploymentSpec{
Input: KubernetesDeploymentInput{
AutoRollback: true,
Namespace: "default",
Namespace: "default",
},
},
wantErr: true,
Expand Down
29 changes: 4 additions & 25 deletions pkg/config/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,14 @@ type ControlPlaneInsightCollector struct {

type InsightCollectorApplication struct {
Enabled bool `json:"enabled"`
Schedule string `json:"schedule"`
Schedule string `json:"schedule" default:"0 * * * *"`
}

type InsightCollectorDeployment struct {
Enabled bool `json:"enabled"`
Schedule string `json:"schedule"`
Retries int `json:"retries"`
RetryInterval Duration `json:"retryInterval"`
}

func (d *ControlPlaneInsightCollector) UnmarshalJSON(data []byte) error {
type alias ControlPlaneInsightCollector
a := &alias{
Application: InsightCollectorApplication{
Schedule: "0 * * * *", // Every hour.
},
Deployment: InsightCollectorDeployment{
Schedule: "30 0,12 * * *", // Every day at 0:30 or 12:30.
Retries: 3,
RetryInterval: Duration(time.Hour),
},
}

if err := json.Unmarshal(data, a); err != nil {
return err
}

*d = ControlPlaneInsightCollector(*a)
return nil
Schedule string `json:"schedule" default:"30 0,12 * * *"`
Retries int `json:"retries" default:"3"`
RetryInterval Duration `json:"retryInterval" default:"1h"`
}

func (c ControlPlaneCache) TTLDuration() time.Duration {
Expand Down
5 changes: 1 addition & 4 deletions pkg/config/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ type GenericDeploymentSpec struct {
TriggerPaths []string `json:"triggerPaths,omitempty"`
// The maximum length of time to execute deployment before giving up.
// Default is 6h.
Timeout Duration `json:"timeout,omitempty"`
Timeout Duration `json:"timeout,omitempty" default:"6h"`
}

func (s *GenericDeploymentSpec) Validate() error {
if s.Timeout == 0 {
s.Timeout = Duration(6 * time.Hour)
}
if s.Pipeline != nil {
for _, stage := range s.Pipeline.Stages {
if stage.AnalysisStageOptions != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/deployment_cloudrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type CloudRunDeploymentInput struct {
ServiceManifestFile string `json:"serviceManifestFile"`
// Automatically reverts to the previous state when the deployment is failed.
// Default is true.
AutoRollback bool `json:"autoRollback"`
AutoRollback bool `json:"autoRollback" default:"true"`
}

// CloudRunSyncStageOptions contains all configurable values for a CLOUDRUN_SYNC stage.
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/deployment_ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ECSDeploymentInput struct {
TaskDefinitionFile string `json:"taskDefinitionFile"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is true.
AutoRollback bool `json:"autoRollback"`
AutoRollback bool `json:"autoRollback" default:"true"`
}

// ECSSyncStageOptions contains all configurable values for a ECS_SYNC stage.
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/deployment_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type KubernetesDeploymentInput struct {

// Automatically reverts all deployment changes on failure.
// Default is true.
AutoRollback bool `json:"autoRollback"`
AutoRollback bool `json:"autoRollback" default:"true"`
}

type InputHelmChart struct {
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/deployment_kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func TestKubernetesDeploymentConfig(t *testing.T) {
},
Timeout: Duration(6 * time.Hour),
},
Input: KubernetesDeploymentInput{AutoRollback: true},
Input: KubernetesDeploymentInput{
AutoRollback: true,
},
TrafficRouting: &KubernetesTrafficRouting{
Method: KubernetesTrafficRoutingMethodPodSelector,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/deployment_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func (s *LambdaDeploymentSpec) Validate() error {
type LambdaDeploymentInput struct {
// The name of service manifest file placing in application directory.
// Default is function.yaml
FunctionManifestFile string `json:"functionManifestFile"`
FunctionManifestFile string `json:"functionManifestFile" default:"function.yaml"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is true.
AutoRollback bool `json:"autoRollback"`
AutoRollback bool `json:"autoRollback" default:"true"`
}

// LambdaSyncStageOptions contains all configurable values for a LAMBDA_SYNC stage.
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/deployment_lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestLambdaDeploymentConfig(t *testing.T) {
Timeout: Duration(6 * time.Hour),
},
Input: LambdaDeploymentInput{
AutoRollback: true,
FunctionManifestFile: "function.yaml",
AutoRollback: true,
},
},
expectedError: nil,
Expand Down
16 changes: 8 additions & 8 deletions pkg/config/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/pipe-cd/pipe/pkg/model"
)
Expand All @@ -42,7 +42,7 @@ type PipedSpec struct {
WebAddress string `json:"webAddress"`
// How often to check whether an application should be synced.
// Default is 1m.
SyncInterval Duration `json:"syncInterval"`
SyncInterval Duration `json:"syncInterval" default:"1m"`
// Git configuration needed for git commands.
Git PipedGit `json:"git"`
// List of git repositories this piped will handle.
Expand All @@ -64,22 +64,22 @@ type PipedSpec struct {
// Validate validates configured data of all fields.
func (s *PipedSpec) Validate() error {
if s.ProjectID == "" {
return fmt.Errorf("projectID must be set")
return errors.New("projectID must be set")
}
if s.PipedID == "" {
return fmt.Errorf("pipedID must be set")
return errors.New("pipedID must be set")
}
if s.PipedKeyFile == "" {
return fmt.Errorf("pipedKeyFile must be set")
return errors.New("pipedKeyFile must be set")
}
if s.APIAddress == "" {
return fmt.Errorf("apiAddress must be set")
return errors.New("apiAddress must be set")
}
if s.WebAddress == "" {
return fmt.Errorf("webAddress must be set")
return errors.New("webAddress must be set")
}
if s.SyncInterval < 0 {
s.SyncInterval = Duration(time.Minute)
return errors.New("syncInterval must be greater than or equal to 0")
}
if s.SealedSecretManagement != nil {
if err := s.SealedSecretManagement.Validate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ def go_repositories():
sum = "h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=",
version = "v1.1.9",
)
go_repository(
name = "com_github_creasty_defaults",
importpath = "github.com/creasty/defaults",
sum = "h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM=",
version = "v1.5.1",
)

go_repository(
name = "com_github_cucumber_gherkin_go_v13",
importpath = "github.com/cucumber/gherkin-go/v13",
Expand Down