diff --git a/components/workbenches/workbenches.go b/components/workbenches/workbenches.go index 0f3e55efe17..dd9519be532 100644 --- a/components/workbenches/workbenches.go +++ b/components/workbenches/workbenches.go @@ -5,6 +5,7 @@ package workbenches import ( "context" "fmt" + "os" "path/filepath" "strings" @@ -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" ) @@ -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) } diff --git a/pkg/common/common.go b/pkg/common/common.go index 4fb00641649..5a540f1c296 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -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 +} diff --git a/pkg/deploy/deploy_suite_test.go b/pkg/deploy/deploy_suite_test.go new file mode 100644 index 00000000000..3ec0adcce4a --- /dev/null +++ b/pkg/deploy/deploy_suite_test.go @@ -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()) + }) + }) + +}) diff --git a/pkg/deploy/envParams.go b/pkg/deploy/envParams.go index b3d748ee689..349b49846d1 100644 --- a/pkg/deploy/envParams.go +++ b/pkg/deploy/envParams.go @@ -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 +}