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

feat: add post-check on the params.env file for workbench images #1177

Open
wants to merge 1 commit into
base: incubation
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions components/workbenches/workbenches.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package workbenches
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -16,6 +17,7 @@ import (
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
)
Expand Down Expand Up @@ -140,11 +142,15 @@ func (w *Workbenches) ReconcileComponent(ctx context.Context, cli client.Client,
if enabled {
if (dscispec.DevFlags == nil || dscispec.DevFlags.ManifestsUri == "") && (w.DevFlags == nil || len(w.DevFlags.Manifests) == 0) {
if platform == cluster.ManagedRhods || platform == cluster.SelfManagedRhods {
// for kf-notebook-controller image
if err := deploy.ApplyParams(notebookControllerPath, imageParamMap, false); err != nil {
// for odh-notebook-controller image
if err := common.CheckErrors(
deploy.ApplyParams(notebookControllerPath, imageParamMap, false),
deploy.CheckParams(notebookControllerPath, []string{os.Getenv("RELATED_IMAGE_ODH_NOTEBOOK_CONTROLLER_IMAGE")}),
); err != nil {
return fmt.Errorf("failed to update image %s: %w", notebookControllerPath, err)
}
// for odh-notebook-controller image

// for kf-notebook-controller image
if err := deploy.ApplyParams(kfnotebookControllerPath, imageParamMap, false); err != nil {
return fmt.Errorf("failed to update image %s: %w", kfnotebookControllerPath, err)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ func GetMonitoringData(data string) (string, error) {

return encodedData, nil
}

// checkErrors to check errors from multiple functions.
// If any of the functions return error then return that error immedately.
func CheckErrors(errors ...error) error {
for _, err := range errors {
if err != nil {
return err
}
}
return nil
}
77 changes: 77 additions & 0 deletions pkg/deploy/deploy_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package deploy_test

import (
"os"
"path/filepath"
"testing"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"

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

func TestStringSearch(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Check Params Suite")
}

var _ = Describe("Throw error if image still use old image registry", func() {

var (
tmpFile *os.File
filePath = "/tmp/manifests/components/one"
err error
paramsenv string
)

// create a temp params.env file
BeforeEach(func() {
err = os.MkdirAll(filePath, 0755)
tmpFile, err = os.Create(filepath.Join(filePath, "params.env"))
Expect(err).NotTo(HaveOccurred())

// fake some lines
paramsenv = `url=https://www.odh.rbac.com
myimagee=quay.io/opendatahub/repo:abc
namespace=opendatahub`
_, err = tmpFile.WriteString(paramsenv)
Expect(err).NotTo(HaveOccurred())

err = tmpFile.Close()
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
err = os.Remove(tmpFile.Name())
Expect(err).NotTo(HaveOccurred())
})

Context("when searching in params.env", func() {
It("Should not throw error when string found", func() {
err := deploy.CheckParams(filePath, []string{"abc"})
Expect(err).NotTo(HaveOccurred())
})

It("Should not throw error when all string are matched", func() {
err := deploy.CheckParams(filePath, []string{"rbac", "abc"})
Expect(err).NotTo(HaveOccurred())
})

It("Should not throw error when emptry string provided(as env not found)", func() {
err := deploy.CheckParams(filePath, []string{""})
Expect(err).NotTo(HaveOccurred())
})

It("Should throw error when string not found", func() {
err := deploy.CheckParams(filePath, []string{"vw"})
Expect(err).To(HaveOccurred())
})

It("Should throw error when multiple strings are not found", func() {
err := deploy.CheckParams(filePath, []string{"vw", "123"})
Expect(err).To(HaveOccurred())
})
})

})
45 changes: 45 additions & 0 deletions pkg/deploy/envParams.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,48 @@ func ApplyParams(componentPath string, imageParamsMap map[string]string, isUpdat

return nil
}

// check if matchStrings exists in componentPath's params.env file
// if found string in matchStrings all exist, return nil
// if any step fail, return err.
// if some of the strings are not found, return err.
func CheckParams(componentPath string, matchStrings []string) error {
paramsFile := filepath.Join(componentPath, "params.env")
paramsEnv, err := os.Open(paramsFile)
if err != nil {
if os.IsNotExist(err) {
return err
}
return err
}
defer paramsEnv.Close()

// init a all false map
found := make(map[string]bool)
for _, str := range matchStrings {
found[str] = false
}
scanner := bufio.NewScanner(paramsEnv)
for scanner.Scan() {
line := scanner.Text()
for _, str := range matchStrings {
if strings.Contains(line, str) {
found[str] = true
}
}
}
if err := scanner.Err(); err != nil {
return err
}

var allMissings []string
for _, str := range matchStrings {
if !found[str] {
allMissings = append(allMissings, str)
}
}
if len(allMissings) > 0 {
return fmt.Errorf("such are not found in params.env: %v", allMissings)
}
return nil
}
Loading