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

ztp: OCPBUGS-25820: Include SiteConfig error in SiteConfig Content #1818

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 29 additions & 11 deletions ztp/siteconfig-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
localExtraManifestPath := flag.String("manifestPath", "", "Directory with pre-defined extra manifest")
extraManifestOnly := flag.Bool("extraManifestOnly", false, "Generate extra manifests only")
outPath := flag.String("outPath", siteConfigs.UnsetStringValue, "Directory to write the generated installation resources")
stopOnError := flag.Bool("stopOnError", false, "Stop if there is an error in any of the SiteConfigs")
// Parse command input
flag.Parse()

Expand All @@ -26,17 +27,27 @@ func main() {

for _, siteConfigFile := range siteConfigFiles {
fileData, err := siteConfigs.ReadFile(siteConfigFile)

if err != nil {
log.Printf("Error: could not read file %s: %s\n", siteConfigFile, err)
errorMsg := fmt.Sprintf("Error: could not read file %s: %s\n", siteConfigFile, err)
if *stopOnError {
log.Fatalf(errorMsg)
} else {
siteConfigs.PrintSiteConfigError(fileData, errorMsg)
continue
}
}

siteConfig := siteConfigs.SiteConfig{}
err = yaml.Unmarshal(fileData, &siteConfig)
if err != nil {
log.Printf("Error: could not parse %s as yaml: %s\n", siteConfigFile, err)
fmt.Print(string(siteConfigs.Separator))
fmt.Println(string(fileData))
continue
errorMsg := fmt.Sprintf("Error: could not parse %s as yaml: %s\n", siteConfigFile, err)
if *stopOnError {
log.Fatalf(errorMsg)
} else {
siteConfigs.PrintSiteConfigError(fileData, errorMsg)
continue
}
}

// overwrite the default extraManifestOnly with optional command line argument
Expand All @@ -45,20 +56,27 @@ func main() {
siteConfig.Spec.Clusters[id].ExtraManifestOnly = *extraManifestOnly
}
}

clusters, err := scBuilder.Build(siteConfig)
if err != nil {
log.Printf("Error: could not build the entire SiteConfig defined by %s: %s", siteConfigFile, err)
fmt.Print(string(siteConfigs.Separator))
fmt.Println(string(fileData))
continue
errorMsg := fmt.Sprintf("Error: could not build the entire SiteConfig defined by %s: %s", siteConfigFile, err)
if *stopOnError {
log.Fatalf(errorMsg)
} else {
siteConfigs.PrintSiteConfigError(fileData, errorMsg)
continue
}
}

for cluster, crs := range clusters {
for _, crIntf := range crs {
cr, err := yaml.Marshal(crIntf)
if err != nil {
log.Printf("Error: could not marshal generated cr by %s: %s %s", siteConfigFile, crIntf, err)
errorMsg := fmt.Sprintf("Error: could not marshal generated CR by %s: %s %s", siteConfigFile, crIntf, err)
if *stopOnError {
log.Fatalf(errorMsg)
} else {
siteConfigs.PrintSiteConfigError(fileData, errorMsg)
}
} else {
// write to file when out dir is provided, otherwise write to standard output
if *outPath != siteConfigs.UnsetStringValue {
Expand Down
15 changes: 15 additions & 0 deletions ztp/siteconfig-generator/siteConfig/siteConfigHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,18 @@ func suppressCrGeneration(kind string, crSuppression []string) bool {
}
return false
}

// PrintSiteConfigError function prints the SiteConfig with associated error to std output.
func PrintSiteConfigError(fileData []byte, errorMsg string) {
log.Print(errorMsg)

// Build the error status.
errorStatus := fmt.Sprintf("\nsiteConfigError: \"%s\"", errorMsg)

// Concatenate the SiteConfig file with the error status.
fileDataWithError := fmt.Sprintf(strings.TrimRight(string(fileData), "\n ") + errorStatus)

// Print the final SiteConfig.
fmt.Println(string(Separator))
fmt.Println(string(fileDataWithError))
}