Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: configurable giterminism config path
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Oct 13, 2023
1 parent 0edca3f commit b852a34
Show file tree
Hide file tree
Showing 30 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/werf/build/main.go
Expand Up @@ -69,6 +69,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -90,6 +90,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGiterminismOptions(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -75,6 +75,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGiterminismOptions(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/ci_env/ci_env.go
Expand Up @@ -64,6 +64,7 @@ Currently supported only GitLab (gitlab) and GitHub (github) CI systems`,
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/cleanup/cleanup.go
Expand Up @@ -60,6 +60,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
17 changes: 9 additions & 8 deletions cmd/werf/common/cmd_data.go
Expand Up @@ -10,14 +10,15 @@ import (
)

type CmdData struct {
GitWorkTree *string
ProjectName *string
Dir *string
ConfigPath *string
ConfigTemplatesDir *string
TmpDir *string
HomeDir *string
SSHKeys *[]string
GitWorkTree *string
ProjectName *string
Dir *string
ConfigPath *string
GiterminismConfigRelPath *string
ConfigTemplatesDir *string
TmpDir *string
HomeDir *string
SSHKeys *[]string

SkipDependenciesRepoRefresh *bool

Expand Down
18 changes: 17 additions & 1 deletion cmd/werf/common/common.go
Expand Up @@ -80,6 +80,11 @@ func SetupConfigPath(cmdData *CmdData, cmd *cobra.Command) {
cmd.Flags().StringVarP(cmdData.ConfigPath, "config", "", os.Getenv("WERF_CONFIG"), `Use custom configuration file (default $WERF_CONFIG or werf.yaml in working directory)`)
}

func SetupGiterminismConfigPath(cmdData *CmdData, cmd *cobra.Command) {
cmdData.GiterminismConfigRelPath = new(string)
cmd.Flags().StringVarP(cmdData.GiterminismConfigRelPath, "giterminism-config", "", os.Getenv("WERF_GITERMINISM_CONFIG"), "Custom path to the giterminism configuration file relative to working directory (default $WERF_GITERMINISM_CONFIG or werf-giterminism.yaml in working directory)")
}

func SetupConfigTemplatesDir(cmdData *CmdData, cmd *cobra.Command) {
cmdData.ConfigTemplatesDir = new(string)
cmd.Flags().StringVarP(cmdData.ConfigTemplatesDir, "config-templates-dir", "", os.Getenv("WERF_CONFIG_TEMPLATES_DIR"), `Custom configuration templates directory (default $WERF_CONFIG_TEMPLATES_DIR or .werf in working directory)`)
Expand Down Expand Up @@ -1039,6 +1044,15 @@ func GetCustomWerfConfigRelPath(giterminismManager giterminism_manager.Interface
return util.GetRelativeToBaseFilepath(giterminismManager.ProjectDir(), customConfigPath), nil
}

func GetWerfGiterminismConfigRelPath(cmdData *CmdData) string {
path := cmdData.GiterminismConfigRelPath
if path == nil || *path == "" {
return "werf-giterminism.yaml"
}

return filepath.ToSlash(*path)
}

func GetCustomWerfConfigTemplatesDirRelPath(giterminismManager giterminism_manager.Interface, cmdData *CmdData) (string, error) {
customConfigTemplatesDirPath := *cmdData.ConfigTemplatesDir
if customConfigTemplatesDirPath == "" {
Expand Down Expand Up @@ -1095,7 +1109,9 @@ func GetGiterminismManager(ctx context.Context, cmdData *CmdData) (giterminism_m
return nil, err
}

return giterminism_manager.NewManager(ctx, workingDir, localGitRepo, headCommit, giterminism_manager.NewManagerOptions{
configRelPath := GetWerfGiterminismConfigRelPath(cmdData)

return giterminism_manager.NewManager(ctx, configRelPath, workingDir, localGitRepo, headCommit, giterminism_manager.NewManagerOptions{
LooseGiterminism: *cmdData.LooseGiterminism,
Dev: *cmdData.Dev,
})
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/compose/main.go
Expand Up @@ -179,6 +179,7 @@ func newCmd(ctx context.Context, composeCmdName string, options *newCmdOptions)
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/config/graph/graph.go
Expand Up @@ -118,6 +118,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/config/list/list.go
Expand Up @@ -47,6 +47,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/config/render/render.go
Expand Up @@ -75,6 +75,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/converge/converge.go
Expand Up @@ -126,6 +126,7 @@ werf converge --repo registry.mydomain.com/web --env production`,
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/dismiss/dismiss.go
Expand Up @@ -84,6 +84,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/export/export.go
Expand Up @@ -90,6 +90,7 @@ func NewExportCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/helm/get_autogenerated_values.go
Expand Up @@ -55,6 +55,7 @@ func NewGetAutogeneratedValuesCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&getAutogeneratedValuedCmdData, cmd)
common.SetupConfigTemplatesDir(&getAutogeneratedValuedCmdData, cmd)
common.SetupConfigPath(&getAutogeneratedValuedCmdData, cmd)
common.SetupGiterminismConfigPath(&getAutogeneratedValuedCmdData, cmd)
common.SetupEnvironment(&getAutogeneratedValuedCmdData, cmd)

common.SetupGiterminismOptions(&getAutogeneratedValuedCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/helm/get_namespace.go
Expand Up @@ -38,6 +38,7 @@ func NewGetNamespaceCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&getNamespaceCmdData, cmd)
common.SetupConfigTemplatesDir(&getNamespaceCmdData, cmd)
common.SetupConfigPath(&getNamespaceCmdData, cmd)
common.SetupGiterminismConfigPath(&getNamespaceCmdData, cmd)
common.SetupEnvironment(&getNamespaceCmdData, cmd)

common.SetupGiterminismOptions(&getNamespaceCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/helm/get_release.go
Expand Up @@ -38,6 +38,7 @@ func NewGetReleaseCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&getReleaseCmdData, cmd)
common.SetupConfigTemplatesDir(&getReleaseCmdData, cmd)
common.SetupConfigPath(&getReleaseCmdData, cmd)
common.SetupGiterminismConfigPath(&getReleaseCmdData, cmd)
common.SetupNamespace(&getReleaseCmdData, cmd)
common.SetupEnvironment(&getReleaseCmdData, cmd)

Expand Down
Expand Up @@ -54,6 +54,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/kube_run/kube_run.go
Expand Up @@ -154,6 +154,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)
common.SetupNamespace(&commonCmdData, cmd)
common.SetupAddAnnotations(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/managed_images/add/add.go
Expand Up @@ -46,6 +46,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/managed_images/ls/ls.go
Expand Up @@ -42,6 +42,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/managed_images/rm/rm.go
Expand Up @@ -46,6 +46,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/plan/plan.go
Expand Up @@ -117,6 +117,7 @@ werf plan --repo registry.mydomain.com/web --env production`,
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/purge/purge.go
Expand Up @@ -50,6 +50,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/render/render.go
Expand Up @@ -84,6 +84,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/run/run.go
Expand Up @@ -118,6 +118,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
1 change: 1 addition & 0 deletions cmd/werf/stage/image/main.go
Expand Up @@ -55,6 +55,7 @@ func NewCmd(ctx context.Context) *cobra.Command {
common.SetupGitWorkTree(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupGiterminismConfigPath(&commonCmdData, cmd)
common.SetupEnvironment(&commonCmdData, cmd)

common.SetupGiterminismOptions(&commonCmdData, cmd)
Expand Down
10 changes: 5 additions & 5 deletions pkg/giterminism_manager/config/config.go
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/werf/werf/pkg/path_matcher"
)

func NewConfig(ctx context.Context, fileReader fileReader) (c Config, err error) {
exist, err := fileReader.IsGiterminismConfigExistAnywhere(ctx)
func NewConfig(ctx context.Context, fileReader fileReader, configRelPath string) (c Config, err error) {
exist, err := fileReader.IsGiterminismConfigExistAnywhere(ctx, configRelPath)
if err != nil {
return c, err
}
Expand All @@ -20,7 +20,7 @@ func NewConfig(ctx context.Context, fileReader fileReader) (c Config, err error)
return Config{}, nil
}

data, err := fileReader.ReadGiterminismConfig(ctx)
data, err := fileReader.ReadGiterminismConfig(ctx, configRelPath)
if err != nil {
return c, err
}
Expand All @@ -38,8 +38,8 @@ func NewConfig(ctx context.Context, fileReader fileReader) (c Config, err error)
}

type fileReader interface {
IsGiterminismConfigExistAnywhere(ctx context.Context) (bool, error)
ReadGiterminismConfig(ctx context.Context) ([]byte, error)
IsGiterminismConfigExistAnywhere(ctx context.Context, relPath string) (bool, error)
ReadGiterminismConfig(ctx context.Context, relPath string) ([]byte, error)
}

type Config struct {
Expand Down
14 changes: 6 additions & 8 deletions pkg/giterminism_manager/file_reader/giterminism_config.go
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/werf/logboek/pkg/types"
)

const GiterminismConfigName = "werf-giterminism.yaml"

func (r FileReader) IsGiterminismConfigExistAnywhere(ctx context.Context) (exist bool, err error) {
func (r FileReader) IsGiterminismConfigExistAnywhere(ctx context.Context, relPath string) (exist bool, err error) {
logboek.Context(ctx).Debug().
LogBlock("IsGiterminismConfigExistAnywhere").
Options(func(options types.LogBlockOptionsInterface) {
Expand All @@ -19,7 +17,7 @@ func (r FileReader) IsGiterminismConfigExistAnywhere(ctx context.Context) (exist
}
}).
Do(func() {
exist, err = r.IsConfigurationFileExistAnywhere(ctx, GiterminismConfigName)
exist, err = r.IsConfigurationFileExistAnywhere(ctx, relPath)

if debug() {
logboek.Context(ctx).Debug().LogF("exist: %v\nerr: %q\n", exist, err)
Expand All @@ -29,7 +27,7 @@ func (r FileReader) IsGiterminismConfigExistAnywhere(ctx context.Context) (exist
return
}

func (r FileReader) ReadGiterminismConfig(ctx context.Context) (data []byte, err error) {
func (r FileReader) ReadGiterminismConfig(ctx context.Context, relPath string) (data []byte, err error) {
logboek.Context(ctx).Debug().
LogBlock("ReadGiterminismConfig").
Options(func(options types.LogBlockOptionsInterface) {
Expand All @@ -38,7 +36,7 @@ func (r FileReader) ReadGiterminismConfig(ctx context.Context) (data []byte, err
}
}).
Do(func() {
data, err = r.readGiterminismConfig(ctx)
data, err = r.readGiterminismConfig(ctx, relPath)

if debug() {
logboek.Context(ctx).Debug().LogF("dataLength: %v\nerr: %q\n", len(data), err)
Expand All @@ -52,8 +50,8 @@ func (r FileReader) ReadGiterminismConfig(ctx context.Context) (data []byte, err
return
}

func (r FileReader) readGiterminismConfig(ctx context.Context) ([]byte, error) {
return r.ReadAndCheckConfigurationFile(ctx, GiterminismConfigName, func(relPath string) bool {
func (r FileReader) readGiterminismConfig(ctx context.Context, relPath string) ([]byte, error) {
return r.ReadAndCheckConfigurationFile(ctx, relPath, func(path string) bool {
return false
})
}
4 changes: 2 additions & 2 deletions pkg/giterminism_manager/manager.go
Expand Up @@ -17,7 +17,7 @@ type NewManagerOptions struct {
Dev bool