github.com/metaprov/modela-operator@v0.0.0-20240118193048-f378be8b74d2/controllers/components/object_storage.go (about) 1 package components 2 3 import ( 4 "context" 5 managementv1 "github.com/metaprov/modela-operator/api/v1alpha1" 6 "github.com/metaprov/modela-operator/pkg/helm" 7 "github.com/metaprov/modela-operator/pkg/kube" 8 k8serr "k8s.io/apimachinery/pkg/api/errors" 9 "sigs.k8s.io/controller-runtime/pkg/log" 10 ) 11 12 // Modela system represent the model core system 13 type ObjectStorage struct { 14 Namespace string 15 Version string 16 ReleaseName string 17 RepoUrl string 18 RepoName string 19 Name string 20 PodNamePrefix string 21 Dryrun bool 22 } 23 24 func NewObjectStorage() *ObjectStorage { 25 return &ObjectStorage{ 26 Namespace: "modela-system", 27 ReleaseName: "modela-storage", 28 RepoName: "bitnami", 29 Name: "minio", 30 RepoUrl: "https://charts.bitnami.com/bitnami", 31 Dryrun: false, 32 } 33 } 34 35 func (os ObjectStorage) GetInstallPhase() managementv1.ModelaPhase { 36 return managementv1.ModelaPhaseInstallingObjectStorage 37 } 38 39 func (os ObjectStorage) IsEnabled(modela managementv1.Modela) bool { 40 return modela.Spec.ObjectStore.Install 41 } 42 43 // Check if the database installed 44 func (os ObjectStorage) Installed(ctx context.Context) (bool, error) { 45 if belonging, err := kube.IsDeploymentCreatedByModela(os.Namespace, "modela-storage-minio"); err == nil && !belonging { 46 return true, managementv1.ComponentNotInstalledByModelaError 47 } 48 if installed, err := helm.IsChartInstalled(ctx, os.Name, os.Namespace, os.ReleaseName); !installed { 49 return false, err 50 } 51 52 return true, nil 53 } 54 55 func (os ObjectStorage) Install(ctx context.Context, modela *managementv1.Modela) error { 56 logger := log.FromContext(ctx) 57 58 if err := helm.AddRepo(os.RepoName, os.RepoUrl, false); err != nil { 59 logger.Error(err, "Failed to download Helm Repo", "repo", os.RepoUrl) 60 return err 61 } 62 logger.Info("Added Helm Repo", "repo", os.RepoName) 63 if err := kube.CreateNamespace(os.Namespace, modela.Name); err != nil && !k8serr.IsAlreadyExists(err) { 64 logger.Error(err, "failed to create namespace") 65 return err 66 } 67 68 logger.Info("Applying Helm Chart", "version", os.Version) 69 return helm.InstallChart(ctx, os.Name, os.Namespace, os.ReleaseName, modela.Spec.ObjectStore.Values.Object) 70 } 71 72 // Check if we are still installing the database 73 func (os ObjectStorage) Installing(ctx context.Context) (bool, error) { 74 installed, err := os.Installed(ctx) 75 if !installed { 76 return installed, err 77 } 78 running, err := kube.IsPodRunning(os.Namespace, os.PodNamePrefix) 79 if err != nil { 80 return false, err 81 } 82 return !running, nil 83 } 84 85 func (os ObjectStorage) Ready(ctx context.Context) (bool, error) { 86 installing, err := os.Installing(ctx) 87 if err != nil && err != managementv1.ComponentNotInstalledByModelaError { 88 return false, err 89 } 90 return !installing, nil 91 } 92 93 func (os ObjectStorage) Uninstall(ctx context.Context, modela *managementv1.Modela) error { 94 return helm.UninstallChart(ctx, os.Name, os.Namespace, os.ReleaseName, map[string]interface{}{}) 95 }