| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame^] | 1 | package status |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | |
| 7 | "github.com/giolekva/pcloud/core/installer/kube" |
| 8 | |
| 9 | "k8s.io/apimachinery/pkg/api/errors" |
| 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | "k8s.io/apimachinery/pkg/runtime/schema" |
| 12 | "k8s.io/client-go/dynamic" |
| 13 | ) |
| 14 | |
| 15 | type helmRelease struct { |
| 16 | Status struct { |
| 17 | Conditions []struct { |
| 18 | Type string `json:"type"` |
| 19 | Status string `json:"status"` |
| 20 | } `json:"conditions"` |
| 21 | } `json:"status,omitempty"` |
| 22 | } |
| 23 | |
| 24 | type helmReleaseMonitor struct { |
| 25 | d dynamic.Interface |
| 26 | } |
| 27 | |
| 28 | func (m *helmReleaseMonitor) Get(ref ResourceRef) (Status, error) { |
| 29 | ctx := context.Background() |
| 30 | res, err := m.d.Resource( |
| 31 | schema.GroupVersionResource{ |
| 32 | Group: "helm.toolkit.fluxcd.io", |
| 33 | Version: "v2beta1", |
| 34 | Resource: "helmreleases", |
| 35 | }, |
| 36 | ).Namespace(ref.Namespace).Get(ctx, ref.Name, metav1.GetOptions{}) |
| 37 | if err != nil { |
| 38 | if errors.IsNotFound(err) { |
| 39 | return StatusNotFound, nil |
| 40 | } |
| 41 | return StatusNoStatus, err |
| 42 | } |
| 43 | b, err := res.MarshalJSON() |
| 44 | if err != nil { |
| 45 | return StatusNoStatus, err |
| 46 | } |
| 47 | var hr helmRelease |
| 48 | if err := json.Unmarshal(b, &hr); err != nil { |
| 49 | return StatusNoStatus, err |
| 50 | } |
| 51 | // TODO(gio): check more thoroughly |
| 52 | for _, c := range hr.Status.Conditions { |
| 53 | if c.Type == "Ready" && c.Status == "True" { |
| 54 | return StatusSuccess, nil |
| 55 | } |
| 56 | } |
| 57 | return StatusProcessing, nil |
| 58 | } |
| 59 | |
| 60 | func NewHelmReleaseMonitor(kubeconfig string) (ResourceMonitor, error) { |
| 61 | c, err := kube.NewKubeClient(kube.KubeConfigOpts{KubeConfigPath: kubeconfig}) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | d := dynamic.New(c.RESTClient()) |
| 66 | return &helmReleaseMonitor{d}, nil |
| 67 | } |