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