Skip to content

Commit

Permalink
confmerger: Stop writing debug configs to disk
Browse files Browse the repository at this point in the history
Signed-off-by: Ridwan Sharif <[email protected]>
  • Loading branch information
ridwanmsharif committed Apr 1, 2022
1 parent a16d9f0 commit 02ca99a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 53 deletions.
4 changes: 1 addition & 3 deletions cmd/google_cloud_ops_agent_engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"flag"
"log"
"os"
"path/filepath"

"github.com/GoogleCloudPlatform/ops-agent/apps"
"github.com/GoogleCloudPlatform/ops-agent/confgenerator"
Expand All @@ -41,8 +40,7 @@ func main() {
}
func run() error {
// TODO(lingshi) Move this to a shared place across Linux and Windows.
confDebugFolder := filepath.Join(os.Getenv("RUNTIME_DIRECTORY"), "conf", "debug")
builtInConfig, mergedConfig, err := confgenerator.MergeConfFiles(*input, confDebugFolder, "linux", apps.BuiltInConfStructs)
builtInConfig, mergedConfig, err := confgenerator.MergeConfFiles(*input, "linux", apps.BuiltInConfStructs)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/ops_agent_windows/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func (s *service) checkForStandaloneAgents(unified *confgenerator.UnifiedConfig)

func (s *service) generateConfigs() error {
// TODO(lingshi) Move this to a shared place across Linux and Windows.
confDebugFolder := filepath.Join(os.Getenv("PROGRAMDATA"), dataDirectory, "run", "conf", "debug")

builtInConfig, mergedConfig, err := confgenerator.MergeConfFiles(s.userConf, confDebugFolder, "windows", apps.BuiltInConfStructs)
builtInConfig, mergedConfig, err := confgenerator.MergeConfFiles(s.userConf, "windows", apps.BuiltInConfStructs)
if err != nil {
return err
}
Expand Down
33 changes: 5 additions & 28 deletions confgenerator/confgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,11 @@ func testGenerateConfsWithValidInput(t *testing.T, platform platformConfig) {
t.Parallel()
confDebugFolder := filepath.Join(dirPath, testName)
userSpecifiedConfPath := filepath.Join(confDebugFolder, "/input.yaml")
builtInConfPath := filepath.Join(confDebugFolder, "/built-in-config.yaml")
mergedConfPath := filepath.Join(confDebugFolder, "/merged-config.yaml")
if _, _, err = confgenerator.MergeConfFiles(userSpecifiedConfPath, confDebugFolder, platform.OS, apps.BuiltInConfStructs); err != nil {
t.Fatalf("MergeConfFiles(%q, %q) got: %v", userSpecifiedConfPath, confDebugFolder, err)
var generatedBuiltInConfig, data []byte
if generatedBuiltInConfig, data, err = confgenerator.MergeConfFiles(userSpecifiedConfPath, platform.OS, apps.BuiltInConfStructs); err != nil {
t.Fatalf("MergeConfFiles(%q) got: %v", userSpecifiedConfPath, err)
}

data, err := ioutil.ReadFile(mergedConfPath)
if err != nil {
t.Fatalf("ReadFile(%q) got: %v", userSpecifiedConfPath, err)
}
t.Logf("merged config:\n%s", data)
uc, err := confgenerator.ParseUnifiedConfigAndValidate(data, platform.OS)
if err != nil {
Expand Down Expand Up @@ -147,18 +142,8 @@ func testGenerateConfsWithValidInput(t *testing.T, platform platformConfig) {
// Compare the expected and generated built-in config and error out in case of diff.
if testName == builtInConfTestName {
expectedBuiltInConfig := readFileContent(t, testName, platform.OS, goldenBuiltInPath, true)
generatedBuiltInConfig, err := ioutil.ReadFile(builtInConfPath)
if err != nil {
t.Fatalf("test %q: error reading %s: %v", testName, builtInConfPath, err)
}
updateOrCompareGolden(t, testName, platform.OS, expectedBuiltInConfig, string(generatedBuiltInConfig), goldenBuiltInPath)
}
if err = os.Remove(builtInConfPath); err != nil {
t.Fatalf("DeleteFile(%q) got: %v", builtInConfPath, err)
}
if err = os.Remove(mergedConfPath); err != nil {
t.Fatalf("DeleteFile(%q) got: %v", mergedConfPath, err)
}
})
}
}
Expand Down Expand Up @@ -221,28 +206,20 @@ func testGenerateConfigsWithInvalidInput(t *testing.T, platform platformConfig)
t.Parallel()
confDebugFolder := filepath.Join(dirPath, testName)
userSpecifiedConfPath := filepath.Join(confDebugFolder, "/input.yaml")
builtInConfPath := filepath.Join(confDebugFolder, "/built-in-config.yaml")
mergedConfPath := filepath.Join(confDebugFolder, "/merged-config.yaml")

invalidInput := readFileContent(t, testName, platform.OS, invalidInputPath, false)
expectedError := readFileContent(t, testName, platform.OS, goldenErrorPath, true)

_, _, actualError := confgenerator.MergeConfFiles(userSpecifiedConfPath, confDebugFolder, platform.OS, apps.BuiltInConfStructs)
var mergedInput []byte
_, mergedInput, actualError := confgenerator.MergeConfFiles(userSpecifiedConfPath, platform.OS, apps.BuiltInConfStructs)
if actualError == nil {
mergedInput := readFileContent(t, testName, platform.OS, mergedInputPath, false)
actualError = generateConfigs(mergedInput, platform)
}
if actualError == nil {
t.Errorf("test %q: generateConfigs succeeded, want error:\n%s\ninvalid input:\n%s", testName, expectedError, invalidInput)
} else {
updateOrCompareGolden(t, testName, platform.OS, expectedError, actualError.Error(), goldenErrorPath)
}

// Clean up built-in and merged config now that the test passes.
if err = os.Remove(builtInConfPath); err != nil {
t.Fatalf("DeleteFile(%q) got: %v", builtInConfPath, err)
}
os.Remove(mergedConfPath)
})
}
}
Expand Down
26 changes: 7 additions & 19 deletions confgenerator/confmerger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,33 @@ package confgenerator
import (
"fmt"
"os"
"path/filepath"

yaml "github.com/goccy/go-yaml"
)

// MergeConfFiles merges the user provided config with the built-in config struct for the platform.
// It will write the merged config file to disk and return the config bytes.
func MergeConfFiles(userConfPath, confDebugFolder, platform string, builtInConfStructs map[string]*UnifiedConfig) ([]byte, []byte, error) {
builtInConfPath := filepath.Join(confDebugFolder, "built-in-config.yaml")
mergedConfPath := filepath.Join(confDebugFolder, "merged-config.yaml")

// Write the built-in conf to disk for debugging purpose.
builtInStruct := builtInConfStructs[platform]
builtInYaml, err := yaml.Marshal(builtInStruct)
func MergeConfFiles(userConfPath, platform string, builtInConfStructs map[string]*UnifiedConfig) ([]byte, []byte, error) {
mergedConf, err := mergeConfFiles(userConfPath, platform, builtInConfStructs)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert the built-in config %q to yaml: %w \n", builtInConfPath, err)
}

if err := writeConfigFile(builtInYaml, builtInConfPath); err != nil {
return nil, nil, err
}

mergedConf, err := mergeConfFiles(builtInConfPath, userConfPath, mergedConfPath, platform, builtInConfStructs)
builtInStruct := builtInConfStructs[platform]
builtInYaml, err := yaml.Marshal(builtInStruct)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to convert the built-in config for %s to yaml: %w \n", platform, err)
}

mergedConfigYaml, err := yaml.Marshal(mergedConf)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert the merged config %q to yaml: %w \n", mergedConfPath, err)
}
if err := writeConfigFile(mergedConfigYaml, mergedConfPath); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to convert the merged config %+v to yaml: %w \n", mergedConf, err)
}

return builtInYaml, mergedConfigYaml, nil
}

func mergeConfFiles(builtInConfPath, userConfPath, mergedConfPath, platform string, builtInConfStructs map[string]*UnifiedConfig) (*UnifiedConfig, error) {
func mergeConfFiles(userConfPath, platform string, builtInConfStructs map[string]*UnifiedConfig) (*UnifiedConfig, error) {
builtInStruct := builtInConfStructs[platform]

// Read the built-in config file.
Expand Down

0 comments on commit 02ca99a

Please sign in to comment.