Skip to content

Commit

Permalink
consolidate regex and string replace funcs into one w helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-garrison committed Oct 24, 2023
1 parent 4822cbc commit 5c07176
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 35 deletions.
4 changes: 2 additions & 2 deletions components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (d *Dashboard) applyRhodsSpecificConfigs(cli client.Client, owner metav1.Ob
deploy.ManagedRhods: "dedicated-admins",
}[platform]

if err := common.ReplaceStringsInFile(dashboardConfig, map[string]string{"<admin_groups>": adminGroups}); err != nil {
if err := common.ReplaceInFile(dashboardConfig, map[string]string{"<admin_groups>": adminGroups}); err != nil {
return err
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (d *Dashboard) deployConsoleLink(cli client.Client, owner metav1.Object, na

domainIndex := strings.Index(consoleRoute.Spec.Host, ".")
consoleLinkDomain := consoleRoute.Spec.Host[domainIndex+1:]
err := common.ReplaceStringsInFile(pathConsoleLink, map[string]string{
err := common.ReplaceInFile(pathConsoleLink, map[string]string{
"<rhods-dashboard-url>": "https://rhods-dashboard-" + namespace + "." + consoleLinkDomain,
"<section-title>": sectionTitle,
})
Expand Down
4 changes: 2 additions & 2 deletions components/workbenches/workbenches.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ func (w *Workbenches) SetGatewayName(dscispec *dsci.DSCInitializationSpec) error
ossmEnv := filepath.Join(kfnotebookControllerServiceMeshPath, "ossm.env")
namespace := dscispec.ApplicationsNamespace

pattern := `ISTIO_GATEWAY=(.*)/odh-gateway`
const pattern = `ISTIO_GATEWAY=(.*)/odh-gateway`
replacement := fmt.Sprintf("ISTIO_GATEWAY=%s/odh-gateway", namespace)

return common.ReplacePatternsInFile(ossmEnv, map[string]string{pattern: replacement})
return common.ReplaceInFile(ossmEnv, map[string]string{pattern: replacement})
}
6 changes: 3 additions & 3 deletions controllers/dscinitialization/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func configurePrometheus(ctx context.Context, dsciInit *dsci.DSCInitialization,
r.Log.Info("Success: read prometheus data from prometheus.yaml from CM")

// Update prometheus manifests
err = common.ReplaceStringsInFile(filepath.Join(prometheusManifestsPath, "prometheus.yaml"),
err = common.ReplaceInFile(filepath.Join(prometheusManifestsPath, "prometheus.yaml"),
map[string]string{
"<set_alertmanager_host>": alertmanagerRoute.Spec.Host,
"<alertmanager_config_hash>": alertmanagerData,
Expand All @@ -100,7 +100,7 @@ func configurePrometheus(ctx context.Context, dsciInit *dsci.DSCInitialization,
return err
}

err = common.ReplaceStringsInFile(filepath.Join(prometheusManifestsPath, "prometheus-viewer-rolebinding.yaml"),
err = common.ReplaceInFile(filepath.Join(prometheusManifestsPath, "prometheus-viewer-rolebinding.yaml"),
map[string]string{
"<odh_monitoring_project>": dsciInit.Spec.Monitoring.Namespace,
})
Expand Down Expand Up @@ -163,7 +163,7 @@ func configureAlertManager(ctx context.Context, dsciInit *dsci.DSCInitialization

// Replace variables in alertmanager configmap
// TODO: Following variables can later be exposed by the API
err = common.ReplaceStringsInFile(filepath.Join(deploy.DefaultManifestPath, "monitoring", "alertmanager", "alertmanager-configs.yaml"),
err = common.ReplaceInFile(filepath.Join(deploy.DefaultManifestPath, "monitoring", "alertmanager", "alertmanager-configs.yaml"),
map[string]string{
"<snitch_url>": b64.StdEncoding.EncodeToString(deadmansnitchSecret.Data["SNITCH_URL"]),
"<pagerduty_token>": b64.StdEncoding.EncodeToString(pagerDutySecret.Data["PAGERDUTY_KEY"]),
Expand Down
38 changes: 10 additions & 28 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,18 @@ import (
"fmt"
"os"
"regexp"
"strings"
)

// ReplaceStringsInFile replaces variable with value in manifests during runtime.
func ReplaceStringsInFile(fileName string, replacements map[string]string) error {
// ReplaceInFile replaces content in the given file either by plain strings or regex patterns based on the content.
func ReplaceInFile(fileName string, replacements map[string]string) error {
// Read the contents of the file
fileContent, err := os.ReadFile(fileName)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}

// Replace all occurrences of the strings in the map
newContent := string(fileContent)
for string1, string2 := range replacements {
newContent = strings.ReplaceAll(newContent, string1, string2)
}
// Replace content using the helper function
newContent := ReplaceInContent(string(fileContent), replacements)

// Write the modified content back to the file
err = os.WriteFile(fileName, []byte(newContent), 0)
Expand All @@ -47,28 +43,14 @@ func ReplaceStringsInFile(fileName string, replacements map[string]string) error
return nil
}

// ReplacePatternsInFile uses regexes to replace patterns with replacements in manifests during runtime.
func ReplacePatternsInFile(fileName string, replacements map[string]string) error {
// Read the contents of the file
fileContent, err := os.ReadFile(fileName)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}

// Convert file content to a string and perform regex-based replacements.
newContent := string(fileContent)
// ReplaceInContent replaces content in the given string either by plain strings or regex patterns based on the content
func ReplaceInContent(content string, replacements map[string]string) string {
// Replace all occurrences of the strings or patterns in the map
newContent := content
for pattern, replacement := range replacements {
regexPattern := regexp.MustCompile(pattern)
newContent = regexPattern.ReplaceAllStringFunc(newContent, func(_ string) string {
return replacement
})
newContent = regexPattern.ReplaceAllString(newContent, replacement)
}

// Write the modified content back to the file
err = os.WriteFile(fileName, []byte(newContent), 0)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
return newContent
}

0 comments on commit 5c07176

Please sign in to comment.