Skip to content

Commit

Permalink
feat: parameterizes gateway namespace for kubeflow
Browse files Browse the repository at this point in the history
Update components/workbenches/workbenches.go

Co-authored-by: Bartosz Majsak <[email protected]>

consolidate regex and string replace funcs into one w helper

move to package, add testing

move out gateway const

rename to ossmcommon

clean up unit tests

rename pkg, move io funcs, rename gw func
  • Loading branch information
cam-garrison authored and bartoszmajsak committed Oct 26, 2023
1 parent 78a62f2 commit 724837d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 14 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: 4 additions & 0 deletions components/workbenches/workbenches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package workbenches

import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/servicemesh"
"path/filepath"
"strings"

Expand Down Expand Up @@ -130,6 +131,9 @@ func (w *Workbenches) ReconcileComponent(cli client.Client, owner metav1.Object,
}
if shouldConfigureServiceMesh {
actualNbCtrlPath = notebookControllerServiceMeshPath
if err := servicemesh.OverwriteIstioGatewayVar(dscispec.ApplicationsNamespace, kfnotebookControllerServiceMeshPath); err != nil {
return err
}
}

if err := deploy.DeployManifestsFromPath(cli, owner, actualNbCtrlPath, dscispec.ApplicationsNamespace, ComponentName, enabled); err != nil {
Expand Down
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
16 changes: 10 additions & 6 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ package common
import (
"fmt"
"os"
"strings"
"regexp"
)

// 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
// Replace content using string or regex
newContent := string(fileContent)
for string1, string2 := range replacements {
newContent = strings.ReplaceAll(newContent, string1, string2)
for pattern, replacement := range replacements {
regexPattern, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("failed to compile pattern: %w", err)
}
newContent = regexPattern.ReplaceAllString(newContent, replacement)
}

// Write the modified content back to the file
Expand Down
8 changes: 5 additions & 3 deletions pkg/feature/raw_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ package feature
import (
"context"
"fmt"
"os"
"regexp"
"strings"

"github.com/ghodss/yaml"
"github.com/pkg/errors"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8stypes "k8s.io/apimachinery/pkg/types"
"os"
"regexp"
"strings"
)

const (
Expand Down
21 changes: 21 additions & 0 deletions pkg/feature/servicemesh/manifests_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package servicemesh

import (
"fmt"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"path/filepath"
)

const (
gatewayPattern = `ISTIO_GATEWAY=(.*)`
)

// OverwriteIstioGatewayVar replaces the ISTIO_GATEWAY with given namespace and "odh-gateway" in the specified ossm.env file.
// This is used in conjunction with kustomize overlays for Kubeflow notebook controllers. By overwritting referenced we can set
// proper values for environment variables populated through Kustomize.
func OverwriteIstioGatewayVar(namespace, path string) error {
envFile := filepath.Join(path, "ossm.env")
replacement := fmt.Sprintf("ISTIO_GATEWAY=%s", namespace+"/odh-gateway")

return common.ReplaceInFile(envFile, map[string]string{gatewayPattern: replacement})
}
59 changes: 59 additions & 0 deletions pkg/feature/servicemesh/manifests_config_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package servicemesh_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/servicemesh"
"os"
"path/filepath"
)

var _ = Describe("Overwriting gateway name in env file", func() {

It("should replace gateway name in the file", func() {
namespace := "test-namespace"
path := createTempEnvFile()
Expect(servicemesh.OverwriteIstioGatewayVar(namespace, path)).To(Succeed())

updatedContents, err := os.ReadFile(filepath.Join(path, "ossm.env"))
Expect(err).NotTo(HaveOccurred())

Expect(string(updatedContents)).To(ContainSubstring("ISTIO_GATEWAY=test-namespace/odh-gateway"))
})

It("should fail if the file does not exist", func() {
Expect(servicemesh.OverwriteIstioGatewayVar("test-namespace", "wrong_directory")).To(Not(Succeed()))
})

It("should not modify other text in the file", func() {
namespace := "test-namespace"
path := createTempEnvFile()

Expect(servicemesh.OverwriteIstioGatewayVar(namespace, path)).To(Succeed())

updatedContents, err := os.ReadFile(filepath.Join(path, "ossm.env"))
Expect(err).NotTo(HaveOccurred())

Expect(string(updatedContents)).To(ContainSubstring("AnotherSetting=value"))
})
})

const testContent = `
ISTIO_GATEWAY=default-namespace/odh-gateway
AnotherSetting=value`

func createTempEnvFile() string {
var err error

tempDir := GinkgoT().TempDir()

tempFilePath := filepath.Join(tempDir, "ossm.env")
tempFile, err := os.Create(tempFilePath)
Expect(err).NotTo(HaveOccurred())

_, err = tempFile.WriteString(testContent)
Expect(err).NotTo(HaveOccurred())
defer tempFile.Close()

return tempDir
}
13 changes: 13 additions & 0 deletions pkg/feature/servicemesh/servicemesh_suite_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package servicemesh_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestServiceMeshSetup(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Service Mesh setup unit tests")
}

0 comments on commit 724837d

Please sign in to comment.