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