Skip to content

Commit 385f72a

Browse files
Add support for image params for disconnected (#375)
1 parent 4ee039e commit 385f72a

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed

components/component.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ type ComponentInterface interface {
1616
ReconcileComponent(owner metav1.Object, client client.Client, scheme *runtime.Scheme,
1717
enabled bool, namespace string) error
1818
GetComponentName() string
19+
SetImageParamsMap(imageMap map[string]string) map[string]string
1920
}

components/dashboard/dashboard.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ const (
1919
PathISVAddOn = deploy.DefaultManifestPath + "/" + ComponentName + "/overlays/apps-addon"
2020
)
2121

22+
var imageParamMap = map[string]string{
23+
"odh-dashboard-image": "RELATED_IMAGE_ODH_DASHBOARD_IMAGE",
24+
}
25+
2226
type Dashboard struct {
2327
components.Component `json:""`
2428
}
2529

30+
func (d *Dashboard) SetImageParamsMap(imageMap map[string]string) map[string]string {
31+
imageParamMap = imageMap
32+
return imageParamMap
33+
}
34+
2635
func (d *Dashboard) GetComponentName() string {
2736
return ComponentName
2837
}
@@ -50,6 +59,11 @@ func (d *Dashboard) ReconcileComponent(owner metav1.Object, cli client.Client, s
5059
}
5160
}
5261

62+
// Update image parameters
63+
if err := deploy.ApplyImageParams(Path, imageParamMap); err != nil {
64+
return err
65+
}
66+
5367
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
5468
Path,
5569
namespace,

components/datasciencepipelines/datasciencepipelines.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@ const (
1313
Path = deploy.DefaultManifestPath + "/" + ComponentName + "/base"
1414
)
1515

16+
var imageParamMap = map[string]string{
17+
"IMAGES_APISERVER": "RELATED_IMAGE_ODH_ML_PIPELINES_API_SERVER_IMAGE",
18+
"IMAGES_ARTIFACT": "RELATED_IMAGE_ODH_ML_PIPELINES_ARTIFACT_MANAGER_IMAGE",
19+
"IMAGES_PERSISTENTAGENT": "RELATED_IMAGE_ODH_ML_PIPELINES_PERSISTENCEAGENT_IMAGE",
20+
"IMAGES_SCHEDULEDWORKFLOW": "RELATED_IMAGE_ODH_ML_PIPELINES_SCHEDULEDWORKFLOW_IMAGE",
21+
"IMAGES_CACHE": "RELATED_IMAGE_ODH_ML_PIPELINES_CACHE_IMAGE",
22+
"IMAGES_DSPO": "RELATED_IMAGE_ODH_DATA_SCIENCE_PIPELINES_OPERATOR_CONTROLLER_IMAGE",
23+
}
24+
1625
type DataSciencePipelines struct {
1726
components.Component `json:""`
1827
}
1928

29+
func (d *DataSciencePipelines) SetImageParamsMap(imageMap map[string]string) map[string]string {
30+
imageParamMap = imageMap
31+
return imageParamMap
32+
}
33+
2034
func (d *DataSciencePipelines) GetComponentName() string {
2135
return ComponentName
2236
}
@@ -34,6 +48,11 @@ func (d *DataSciencePipelines) SetEnabled(enabled bool) {
3448

3549
func (d *DataSciencePipelines) ReconcileComponent(owner metav1.Object, client client.Client, scheme *runtime.Scheme, enabled bool, namespace string) error {
3650

51+
// Update image parameters
52+
if err := deploy.ApplyImageParams(Path, imageParamMap); err != nil {
53+
return err
54+
}
55+
3756
err := deploy.DeployManifestsFromPath(owner, client, ComponentName,
3857
Path,
3958
namespace,

components/distributedworkloads/distributedworkloads.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ const (
1414
RayPath = deploy.DefaultManifestPath + "/" + "ray/operator" + "/base"
1515
)
1616

17+
var imageParamMap = map[string]string{}
18+
1719
type DistributedWorkloads struct {
1820
components.Component `json:""`
1921
}
2022

23+
func (d *DistributedWorkloads) SetImageParamsMap(imageMap map[string]string) map[string]string {
24+
imageParamMap = imageMap
25+
return imageParamMap
26+
}
27+
2128
func (d *DistributedWorkloads) GetComponentName() string {
2229
return ComponentName
2330
}
@@ -27,6 +34,11 @@ var _ components.ComponentInterface = (*DistributedWorkloads)(nil)
2734

2835
func (d *DistributedWorkloads) ReconcileComponent(owner metav1.Object, client client.Client, scheme *runtime.Scheme, enabled bool, namespace string) error {
2936

37+
// Update image parameters
38+
if err := deploy.ApplyImageParams(CodeflarePath, imageParamMap); err != nil {
39+
return err
40+
}
41+
3042
// Deploy Codeflare
3143
err := deploy.DeployManifestsFromPath(owner, client, ComponentName,
3244
CodeflarePath,
@@ -37,6 +49,10 @@ func (d *DistributedWorkloads) ReconcileComponent(owner metav1.Object, client cl
3749
return err
3850
}
3951

52+
// Update image parameters
53+
if err := deploy.ApplyImageParams(RayPath, imageParamMap); err != nil {
54+
return err
55+
}
4056
// Deploy Ray Operator
4157
err = deploy.DeployManifestsFromPath(owner, client, ComponentName,
4258
RayPath,

components/kserve/kserve.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ const (
1515
Path = deploy.DefaultManifestPath + "/" + ComponentName + "/base"
1616
)
1717

18+
var imageParamMap = map[string]string{}
19+
1820
type Kserve struct {
1921
components.Component `json:""`
2022
}
2123

24+
func (d *Kserve) SetImageParamsMap(imageMap map[string]string) map[string]string {
25+
imageParamMap = imageMap
26+
return imageParamMap
27+
}
28+
2229
func (d *Kserve) GetComponentName() string {
2330
return ComponentName
2431
}
@@ -41,6 +48,12 @@ func (d *Kserve) ReconcileComponent(owner metav1.Object, cli client.Client, sche
4148
if err != nil {
4249
return err
4350
}
51+
52+
// Update image parameters
53+
if err := deploy.ApplyImageParams(Path, imageParamMap); err != nil {
54+
return err
55+
}
56+
4457
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
4558
Path,
4659
namespace,

components/modelmeshserving/modelmeshserving.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const (
1515
monitoringPath = deploy.DefaultManifestPath + "/" + "modelmesh-monitoring/base"
1616
)
1717

18+
var imageParamMap = map[string]string{
19+
"odh-mm-rest-proxy": "RELATED_IMAGE_ODH_MM_REST_PROXY_IMAGE",
20+
"odh-modelmesh-runtime-adapter": "RELATED_IMAGE_ODH_MODELMESH_RUNTIME_ADAPTER_IMAGE",
21+
"odh-modelmesh": "RELATED_IMAGE_ODH_MODELMESH_IMAGE",
22+
"odh-modelmesh-controller": "RELATED_IMAGE_ODH_MODELMESH_CONTROLLER_IMAGE",
23+
"odh-model-controller": "RELATED_IMAGE_ODH_MODEL_CONTROLLER_IMAGE",
24+
}
25+
1826
type ModelMeshServing struct {
1927
components.Component `json:""`
2028
}
@@ -23,6 +31,11 @@ func (m *ModelMeshServing) GetComponentName() string {
2331
return ComponentName
2432
}
2533

34+
func (m *ModelMeshServing) SetImageParamsMap(imageMap map[string]string) map[string]string {
35+
imageParamMap = imageMap
36+
return imageParamMap
37+
}
38+
2639
// Verifies that Dashboard implements ComponentInterface
2740
var _ components.ComponentInterface = (*ModelMeshServing)(nil)
2841

@@ -41,6 +54,12 @@ func (m *ModelMeshServing) ReconcileComponent(owner metav1.Object, cli client.Cl
4154
if err != nil {
4255
return err
4356
}
57+
58+
// Update image parameters
59+
if err := deploy.ApplyImageParams(Path, imageParamMap); err != nil {
60+
return err
61+
}
62+
4463
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
4564
Path,
4665
namespace,

components/workbenches/workbenches.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const (
1515
notebookImagesPath = deploy.DefaultManifestPath + "/notebook-images/overlays/additional"
1616
)
1717

18+
var imageParamMap = map[string]string{
19+
"odh-notebook-controller-image": "RELATED_IMAGE_ODH_NOTEBOOK_CONTROLLER_IMAGE",
20+
"odh-kf-notebook-controller-image": "RELATED_IMAGE_ODH_KF_NOTEBOOK_CONTROLLER_IMAGE",
21+
}
22+
1823
type Workbenches struct {
1924
components.Component `json:""`
2025
}
@@ -23,6 +28,11 @@ func (w *Workbenches) GetComponentName() string {
2328
return ComponentName
2429
}
2530

31+
func (w *Workbenches) SetImageParamsMap(imageMap map[string]string) map[string]string {
32+
imageParamMap = imageMap
33+
return imageParamMap
34+
}
35+
2636
// Verifies that Dashboard implements ComponentInterface
2737
var _ components.ComponentInterface = (*Workbenches)(nil)
2838

@@ -41,13 +51,23 @@ func (w *Workbenches) ReconcileComponent(owner metav1.Object, cli client.Client,
4151
return err
4252
}
4353

54+
// Update image parameters
55+
if err := deploy.ApplyImageParams(notebookControllerPath, imageParamMap); err != nil {
56+
return err
57+
}
58+
4459
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
4560
notebookControllerPath,
4661
namespace,
4762
scheme, enabled)
4863
if err != nil {
4964
return err
5065
}
66+
67+
// Update image parameters
68+
if err := deploy.ApplyImageParams(notebookImagesPath, imageParamMap); err != nil {
69+
return err
70+
}
5171
err = deploy.DeployManifestsFromPath(owner, cli, ComponentName,
5272
notebookImagesPath,
5373
namespace,

pkg/deploy/deploy.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package deploy
22

33
import (
44
"archive/tar"
5+
"bufio"
56
"compress/gzip"
67
"context"
78
"encoding/json"
@@ -224,4 +225,74 @@ func manageResource(owner metav1.Object, ctx context.Context, cli client.Client,
224225
return cli.Patch(ctx, found, client.RawPatch(types.ApplyPatchType, data), client.ForceOwnership, client.FieldOwner(owner.GetName()))
225226
}
226227

228+
func ApplyImageParams(componentPath string, imageParamsMap map[string]string) error {
229+
envFilePath := componentPath + "/params.env"
230+
// Require params.env at the root folder
231+
file, err := os.Open(envFilePath)
232+
if err != nil {
233+
if os.IsNotExist(err) {
234+
// params.env doesn't exist, do not apply any changes
235+
return nil
236+
}
237+
return err
238+
}
239+
backupPath := envFilePath + ".bak"
240+
defer file.Close()
241+
242+
envMap := make(map[string]string)
243+
scanner := bufio.NewScanner(file)
244+
for scanner.Scan() {
245+
line := scanner.Text()
246+
parts := strings.SplitN(line, "=", 2)
247+
if len(parts) == 2 {
248+
envMap[parts[0]] = parts[1]
249+
}
250+
}
251+
252+
if err := scanner.Err(); err != nil {
253+
return err
254+
}
255+
256+
// Update images with env variables
257+
for key, _ := range envMap {
258+
relatedImageValue := os.Getenv(imageParamsMap[key])
259+
if relatedImageValue != "" {
260+
envMap[key] = relatedImageValue
261+
}
262+
}
263+
264+
// Move the existing file to a backup file
265+
os.Rename(envFilePath, backupPath)
266+
267+
// Now, write the map back to the file
268+
file, err = os.Create(envFilePath)
269+
if err != nil {
270+
// If create fails, restore the backup file
271+
os.Rename(backupPath, envFilePath)
272+
return err
273+
}
274+
defer file.Close()
275+
276+
writer := bufio.NewWriter(file)
277+
for key, value := range envMap {
278+
fmt.Fprintf(writer, "%s=%s\n", key, value)
279+
}
280+
if err := writer.Flush(); err != nil {
281+
if removeErr := os.Remove(envFilePath); removeErr != nil {
282+
fmt.Printf("Failed to remove file: %v", removeErr)
283+
}
284+
if renameErr := os.Rename(backupPath, envFilePath); renameErr != nil {
285+
fmt.Printf("Failed to restore file from backup: %v", renameErr)
286+
}
287+
fmt.Printf("Failed to write to file: %v", err)
288+
return err
289+
}
290+
291+
if err := os.Remove(backupPath); err != nil {
292+
fmt.Printf("Failed to remove backup file: %v", err)
293+
return err
294+
}
295+
return nil
296+
}
297+
227298
// TODO : Add function to cleanup code created as part of pre install and post intall task of a component

0 commit comments

Comments
 (0)