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

do not perform autodetection to reduce memory consuption in server #1784

Merged
merged 6 commits into from
Oct 29, 2024
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
77 changes: 4 additions & 73 deletions libs/digger_config/digger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,16 @@ func HandleYamlProjectGeneration(config *DiggerConfigYaml, terraformDir string,
DefaultWorkflow: workflow,
WorkflowFile: b.WorkflowFile,
FilterPath: path.Join(terraformDir, *b.RootDir),
};
}

// allow blocks to pass in roles that can be assummed by aws
// allow blocks to pass in roles that can be assummed by aws
tgParsingConfig.AwsRoleToAssume = b.AwsRoleToAssume


err := hydrateDiggerConfigYamlWithTerragrunt(config, tgParsingConfig, terraformDir)
if err != nil {
return err
}

}
} else {
includePatterns = []string{b.Include}
Expand Down Expand Up @@ -325,16 +324,7 @@ func LoadDiggerConfigYaml(workingDir string, generateProjects bool, changedFiles
}

if fileName == "" {
configYaml, err = AutoDetectDiggerConfig(workingDir)
if err != nil {
return nil, fmt.Errorf("failed to auto detect digger digger_config: %v", err)
}
marshalledConfig, err := yaml.Marshal(configYaml)
if err != nil {
log.Printf("failed to marshal auto detected digger digger_config: %v", err)
} else {
log.Printf("Auto detected digger digger_config: \n%v", string(marshalledConfig))
}
return nil, fmt.Errorf("could not fimd digger.yml or digger.yaml in root of repository")
} else {
data, err := os.ReadFile(fileName)
if err != nil {
Expand All @@ -358,7 +348,6 @@ func LoadDiggerConfigYaml(workingDir string, generateProjects bool, changedFiles
}
}


return configYaml, nil
}

Expand Down Expand Up @@ -514,64 +503,6 @@ func hydrateDiggerConfigYamlWithTerragrunt(configYaml *DiggerConfigYaml, parsing
return nil
}

func AutoDetectDiggerConfig(workingDir string) (*DiggerConfigYaml, error) {
configYaml := &DiggerConfigYaml{}
telemetry := true
configYaml.Telemetry = &telemetry

TraverseToNestedProjects := false
configYaml.TraverseToNestedProjects = &TraverseToNestedProjects

AllowDraftPRs := false
configYaml.AllowDraftPRs = &AllowDraftPRs

terragruntDirWalker := &FileSystemTerragruntDirWalker{}
terraformDirWalker := &FileSystemTopLevelTerraformDirWalker{}
moduleDirWalker := &FileSystemModuleDirWalker{}

terragruntDirs, err := terragruntDirWalker.GetDirs(workingDir, configYaml)

if err != nil {
return nil, err
}

terraformDirs, err := terraformDirWalker.GetDirs(workingDir, configYaml)
if err != nil {
return nil, err
}

moduleDirs, err := moduleDirWalker.GetDirs(workingDir, configYaml)

var modulePatterns []string
for _, dir := range moduleDirs {
modulePatterns = append(modulePatterns, dir+"/**")
}

if err != nil {
return nil, err
}
if len(terragruntDirs) > 0 {
configYaml.GenerateProjectsConfig = &GenerateProjectsConfigYaml{
Terragrunt: true,
}
return configYaml, nil
} else if len(terraformDirs) > 0 {
for _, dir := range terraformDirs {
var projectName string
if dir == "./" {
projectName = "default"
} else {
projectName = strings.ReplaceAll(dir, "/", "_")
}
project := ProjectYaml{Name: projectName, Dir: dir, Workflow: defaultWorkflowName, Workspace: "default", Terragrunt: false, IncludePatterns: modulePatterns}
configYaml.Projects = append(configYaml.Projects, &project)
}
return configYaml, nil
} else {
return nil, fmt.Errorf("no terragrunt or terraform project detected in the repository")
}
}

func (c *DiggerConfig) GetProject(projectName string) *Project {
for _, project := range c.Projects {
if projectName == project.Name {
Expand Down
76 changes: 2 additions & 74 deletions libs/digger_config/digger_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,8 @@ func TestNoDiggerYaml(t *testing.T) {
defer deleteFile()

os.Chdir(tempDir)
dg, _, _, err := LoadDiggerConfig("./", true, nil)

assert.NoError(t, err, "expected error to be nil")
assert.NotNil(t, dg, "expected digger digger_config to be not nil")
assert.Equal(t, 1, len(dg.Projects))
assert.Equal(t, false, dg.AutoMerge)
assert.Equal(t, true, dg.Telemetry)
assert.Equal(t, false, dg.TraverseToNestedProjects)
assert.Equal(t, 1, len(dg.Workflows))
assert.Equal(t, "default", dg.Projects[0].Name)
assert.Equal(t, "./", dg.Projects[0].Dir)

workflow := dg.Workflows["default"]
assert.NotNil(t, workflow, "expected workflow to be not nil")
assert.NotNil(t, workflow.Plan)
assert.NotNil(t, workflow.Plan.Steps)

assert.NotNil(t, workflow.Apply)
assert.NotNil(t, workflow.Apply.Steps)
assert.NotNil(t, workflow.EnvVars)
assert.NotNil(t, workflow.Configuration)
_, _, _, err := LoadDiggerConfig("./", true, nil)
assert.Error(t, err, "expected error since digger.yml and digger.yaml is missing")
}

func TestDefaultDiggerConfig(t *testing.T) {
Expand Down Expand Up @@ -367,37 +348,6 @@ generate_projects:
assert.Equal(t, 2, len(dg.Projects))
}

func TestGenerateProjectsWithoutDiggerConfig(t *testing.T) {
tempDir, teardown := setUp()
defer teardown()

dirsWithTfToCreate := []string{"dev/test1", "dev/test1/db", "dev/test1/vpc", "dev/test2", "dev/test2/db", "dev/test2/vpc", "dev/project", "prod/test1", "prod/test2", "prod/project", "test", "modules/test1", "modules/test2"}

for _, dir := range dirsWithTfToCreate {
err := os.MkdirAll(path.Join(tempDir, dir), os.ModePerm)
defer createFile(path.Join(tempDir, dir, "main.tf"), "")()
assert.NoError(t, err, "expected error to be nil")
}

dirtsWithoutTfToCreate := []string{"docs", "random", "docs/random"}
for _, dir := range dirtsWithoutTfToCreate {
err := os.MkdirAll(path.Join(tempDir, dir), os.ModePerm)
assert.NoError(t, err, "expected error to be nil")
}

dg, _, _, err := LoadDiggerConfig(tempDir, true, nil)
assert.NoError(t, err, "expected error to be nil")
assert.NotNil(t, dg, "expected digger digger_config to be not nil")
assert.Equal(t, "dev_project", dg.Projects[0].Name)
assert.Equal(t, "dev_test1", dg.Projects[1].Name)
assert.Equal(t, "dev_test2", dg.Projects[2].Name)
assert.Equal(t, "prod_project", dg.Projects[3].Name)
assert.Equal(t, "prod_test1", dg.Projects[4].Name)
assert.Equal(t, "prod_test2", dg.Projects[5].Name)
assert.Equal(t, "test", dg.Projects[6].Name)
assert.Equal(t, 7, len(dg.Projects))
}

func TestDiggerGenerateProjectsWithSubDirs(t *testing.T) {
tempDir, teardown := setUp()
defer teardown()
Expand Down Expand Up @@ -431,28 +381,6 @@ generate_projects:
assert.Equal(t, 2, len(dg.Projects))
}

// A .tfvars file should not be recognised as .tf and break parsing for projects nested deeper
// Issue: https://github.com/diggerhq/digger/issues/887
func TestDiggerGenerateProjectsWithTfvars(t *testing.T) {
tempDir, teardown := setUp()
defer teardown()

dirsWithTfToCreate := []string{"dev/us-east-1"}

for _, dir := range dirsWithTfToCreate {
err := os.MkdirAll(path.Join(tempDir, dir), os.ModePerm)
defer createFile(path.Join(tempDir, dir, "main.tf"), "")()
assert.NoError(t, err, "expected error to be nil")
}

defer createFile(path.Join(tempDir, "dev", "blank.tfvars"), "")()

dg, _, _, err := LoadDiggerConfig(tempDir, true, nil)
assert.NoError(t, err, "expected error to be nil")
assert.NotNil(t, dg, "expected digger digger_config to be not nil")
assert.Equal(t, 1, len(dg.Projects))
}

func TestDiggerGenerateProjectsIgnoreSubdirs(t *testing.T) {
tempDir, teardown := setUp()
defer teardown()
Expand Down
Loading