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
2 changes: 1 addition & 1 deletion pkg/app/piped/driftdetector/cloudrun/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Appl
if err != nil {
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
if appKind, ok := cfg.Kind.ToApplicationKind(); !ok || appKind != app.Kind {
return nil, fmt.Errorf("application in application configuration file is not match, got: %s, expected: %s", appKind, app.Kind)
}
return cfg, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/piped/driftdetector/kubernetes/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Appl
if err != nil {
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
if appKind, ok := cfg.Kind.ToApplicationKind(); !ok || appKind != app.Kind {
return nil, fmt.Errorf("application in application configuration file is not match, got: %s, expected: %s", appKind, app.Kind)
}

Expand Down
28 changes: 1 addition & 27 deletions pkg/app/piped/planpreview/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"go.uber.org/zap"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -303,7 +302,7 @@ func (b *builder) cloneHeadCommit(ctx context.Context, headBranch, headCommit st
func (b *builder) findTriggerApps(ctx context.Context, repo git.Repo, apps []*model.Application, headCommit string) (triggerApps []*model.Application, failedResults []*model.ApplicationPlanPreviewResult, err error) {
d := trigger.NewOnCommitDeterminer(repo, headCommit, b.commitGetter, b.logger)
determine := func(app *model.Application) (bool, error) {
appCfg, err := loadApplicationConfiguration(repo.GetPath(), app)
appCfg, err := config.LoadApplication(repo.GetPath(), app.GitPath.GetApplicationConfigFilePath(), app.Kind)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -410,28 +409,3 @@ func (b *builder) getMostRecentlySuccessfulDeployment(ctx context.Context, appli

return deploy.(*model.ApplicationDeploymentReference), nil
}

func loadApplicationConfiguration(repoPath string, app *model.Application) (*config.GenericApplicationSpec, error) {
var (
relPath = app.GitPath.GetApplicationConfigFilePath()
absPath = filepath.Join(repoPath, relPath)
)

cfg, err := config.LoadFromYAML(absPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("application config file %s was not found in Git", relPath)
}
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
return nil, fmt.Errorf("invalid application kind in the application config file, got: %s, expected: %s", appKind, app.Kind)
}

spec, ok := cfg.GetGenericApplication()
if !ok {
return nil, fmt.Errorf("unsupported application kind: %s", app.Kind)
}

return &spec, nil
}
29 changes: 1 addition & 28 deletions pkg/app/piped/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package trigger
import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -219,7 +217,7 @@ func (t *Trigger) checkRepoCandidates(ctx context.Context, repoID string, cs []c
continue
}

appCfg, err := loadApplicationConfiguration(gitRepo.GetPath(), app)
appCfg, err := config.LoadApplication(gitRepo.GetPath(), app.GitPath.GetApplicationConfigFilePath(), app.Kind)
if err != nil {
t.logger.Error("failed to load application config file",
zap.String("app", app.Name),
Expand Down Expand Up @@ -493,28 +491,3 @@ func (t *Trigger) notifyDeploymentTriggerFailed(app *model.Application, appCfg *
},
})
}

func loadApplicationConfiguration(repoPath string, app *model.Application) (*config.GenericApplicationSpec, error) {
var (
relPath = app.GitPath.GetApplicationConfigFilePath()
absPath = filepath.Join(repoPath, relPath)
)

cfg, err := config.LoadFromYAML(absPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("application config file %s was not found in Git", relPath)
}
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
return nil, fmt.Errorf("invalid application kind in the application config file, got: %s, expected: %s", appKind, app.Kind)
}

spec, ok := cfg.GetGenericApplication()
if !ok {
return nil, fmt.Errorf("unsupported application kind: %s", app.Kind)
}

return &spec, nil
}
24 changes: 24 additions & 0 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package config
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"

"github.com/pipe-cd/pipecd/pkg/model"
Expand Down Expand Up @@ -644,3 +646,25 @@ func (c *DeploymentChainTriggerCondition) Validate() error {
}
return nil
}

func LoadApplication(repoPath, configRelPath string, appKind model.ApplicationKind) (*GenericApplicationSpec, error) {
var absPath = filepath.Join(repoPath, configRelPath)

cfg, err := LoadFromYAML(absPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("application config file %s was not found in Git", configRelPath)
}
return nil, err
}
if kind, ok := cfg.Kind.ToApplicationKind(); !ok || kind != appKind {
return nil, fmt.Errorf("invalid application kind in the application config file, got: %s, expected: %s", kind, appKind)
}

spec, ok := cfg.GetGenericApplication()
if !ok {
return nil, fmt.Errorf("unsupported application kind: %s", appKind)
}

return &spec, nil
}
15 changes: 1 addition & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/creasty/defaults"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -68,18 +67,6 @@ const (
KindEventWatcher Kind = "EventWatcher"
)

// ToApplicationKind converts itself into model.ApplicationKind if it's for an application config.
// Return false as a second returned value if it's not an application config.
func (k Kind) ToApplicationKind() (model.ApplicationKind, bool) {
upper := strings.ToUpper(string(k))
kind := strings.TrimSuffix(upper, "APP")
appKind, ok := model.ApplicationKind_value[kind]
if !ok {
return -1, false
}
return model.ApplicationKind(appKind), true
}

var (
ErrNotFound = errors.New("not found")
)
Expand Down Expand Up @@ -243,7 +230,7 @@ func DecodeYAML(data []byte) (*Config, error) {
}

// ToApplicationKind converts configuration kind to application kind.
func ToApplicationKind(k Kind) (model.ApplicationKind, bool) {
func (k Kind) ToApplicationKind() (model.ApplicationKind, bool) {
switch k {
case KindKubernetesApp:
return model.ApplicationKind_KUBERNETES, true
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestKind_ToApplicationKind(t *testing.T) {
{
name: "Not an app config",
k: KindPiped,
want: -1,
want: model.ApplicationKind_KUBERNETES,
wantOk: false,
},
}
Expand Down