|
| 1 | +// Copyright 2022 IBM Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package pvcprovider |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "github.com/go-logr/logr" |
| 23 | + |
| 24 | + "github.com/kserve/modelmesh-runtime-adapter/internal/util" |
| 25 | + "github.com/kserve/modelmesh-runtime-adapter/pullman" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + // config fields |
| 30 | + configPVCName = "name" |
| 31 | + |
| 32 | + // defaults |
| 33 | + defaultPVCMountBase = "/pvc_mounts" |
| 34 | +) |
| 35 | + |
| 36 | +type pvcProvider struct { |
| 37 | + pvcMountBase string |
| 38 | +} |
| 39 | + |
| 40 | +// pvcProvider implements StorageProvider |
| 41 | +var _ pullman.StorageProvider = (*pvcProvider)(nil) |
| 42 | + |
| 43 | +func (p pvcProvider) GetKey(config pullman.Config) string { |
| 44 | + // Since the same instance of the repository can be used for all PVCs, there is no need to distinguish them here. |
| 45 | + return "" |
| 46 | +} |
| 47 | + |
| 48 | +func (p pvcProvider) NewRepository(config pullman.Config, log logr.Logger) (pullman.RepositoryClient, error) { |
| 49 | + if p.pvcMountBase == "" { |
| 50 | + p.pvcMountBase = defaultPVCMountBase |
| 51 | + } |
| 52 | + |
| 53 | + fileInfo, err := os.Stat(p.pvcMountBase) |
| 54 | + if err != nil { |
| 55 | + if os.IsNotExist(err) { |
| 56 | + return nil, fmt.Errorf("the PVC mount base '%s' doesn't exist: %w", p.pvcMountBase, err) |
| 57 | + } |
| 58 | + return nil, fmt.Errorf("failed to access the PVC mount base '%s': %w", p.pvcMountBase, err) |
| 59 | + } |
| 60 | + |
| 61 | + if !fileInfo.IsDir() { |
| 62 | + return nil, fmt.Errorf("the PVC mount base '%s' is not a directory", p.pvcMountBase) |
| 63 | + } |
| 64 | + |
| 65 | + return &pvcRepositoryClient{ |
| 66 | + pvcProvider: p, |
| 67 | + log: log, |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +type pvcRepositoryClient struct { |
| 72 | + pvcProvider pvcProvider |
| 73 | + log logr.Logger |
| 74 | +} |
| 75 | + |
| 76 | +// pvcRepositoryClient implements RepositoryClient |
| 77 | +var _ pullman.RepositoryClient = (*pvcRepositoryClient)(nil) |
| 78 | + |
| 79 | +func (r *pvcRepositoryClient) Pull(ctx context.Context, pc pullman.PullCommand) error { |
| 80 | + targets := pc.Targets |
| 81 | + destDir := pc.Directory |
| 82 | + |
| 83 | + // Process per-command configuration |
| 84 | + pvcName, ok := pullman.GetString(pc.RepositoryConfig, configPVCName) |
| 85 | + if !ok { |
| 86 | + return fmt.Errorf("required configuration '%s' missing from command", configPVCName) |
| 87 | + } |
| 88 | + |
| 89 | + // create destination directories |
| 90 | + if mkdirErr := os.MkdirAll(destDir, 0755); mkdirErr != nil { |
| 91 | + return fmt.Errorf("unable to create directories '%s': %w", destDir, mkdirErr) |
| 92 | + } |
| 93 | + |
| 94 | + pvcDir, joinErr := util.SecureJoin(r.pvcProvider.pvcMountBase, pvcName) |
| 95 | + if joinErr != nil { |
| 96 | + return fmt.Errorf("unable to join paths '%s' and '%s': %v", r.pvcProvider.pvcMountBase, pvcName, joinErr) |
| 97 | + } |
| 98 | + r.log.V(1).Info("The PVC directory is set", "pvcDir", pvcDir) |
| 99 | + |
| 100 | + for _, pt := range targets { |
| 101 | + fullModelPath, joinErr := util.SecureJoin(pvcDir, pt.RemotePath) |
| 102 | + if joinErr != nil { |
| 103 | + return fmt.Errorf("unable to join paths '%s' and '%s': %v", pvcDir, pt.RemotePath, joinErr) |
| 104 | + } |
| 105 | + r.log.V(1).Info("The model path is set", "fullModelPath", fullModelPath) |
| 106 | + |
| 107 | + // check the local model path /pvcMountBase/pvcName/modelPath exists |
| 108 | + if _, err := os.Stat(fullModelPath); err != nil { |
| 109 | + return fmt.Errorf("unable to access model local path '%s': %w", fullModelPath, err) |
| 110 | + } |
| 111 | + |
| 112 | + // create symlink |
| 113 | + linkPath, err := util.SecureJoin(destDir, filepath.Base(fullModelPath)) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("unable to join paths '%s' and '%s': %v", destDir, filepath.Base(fullModelPath), err) |
| 116 | + } |
| 117 | + if err := os.Symlink(fullModelPath, linkPath); err != nil { |
| 118 | + return fmt.Errorf("unable to create symlink '%s': %w", linkPath, err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func init() { |
| 126 | + p := pvcProvider{} |
| 127 | + pullman.RegisterProvider("pvc", p) |
| 128 | +} |
0 commit comments