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

Define the LoadApplication function in the config package #3730

Merged
merged 4 commits into from
Jun 7, 2022
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
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)
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)
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
}
27 changes: 27 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,28 @@ func (c *DeploymentChainTriggerCondition) Validate() error {
}
return nil
}

func LoadApplication(repoPath string, app *model.Application) (*GenericApplicationSpec, error) {
knanao marked this conversation as resolved.
Show resolved Hide resolved
var (
relPath = app.GitPath.GetApplicationConfigFilePath()
absPath = filepath.Join(repoPath, relPath)
)

cfg, err := 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 := 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
}