| 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 { |
| 17 | Metadata struct { |
| 18 | Name string `json:"name"` |
| 19 | Namespace string `json:"namespace"` |
| 20 | Annotations map[string]string `json:"annotations"` |
| 21 | } `json:"metadata"` |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 22 | Status struct { |
| 23 | Conditions []struct { |
| 24 | Type string `json:"type"` |
| 25 | Status string `json:"status"` |
| 26 | } `json:"conditions"` |
| 27 | } `json:"status,omitempty"` |
| 28 | } |
| 29 | |
| 30 | type helmReleaseMonitor struct { |
| 31 | d dynamic.Interface |
| 32 | } |
| 33 | |
| 34 | func (m *helmReleaseMonitor) Get(ref ResourceRef) (Status, error) { |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame^] | 35 | fmt.Printf("--- %+v\n", ref) |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 36 | ctx := context.Background() |
| 37 | res, err := m.d.Resource( |
| 38 | schema.GroupVersionResource{ |
| 39 | Group: "helm.toolkit.fluxcd.io", |
| 40 | Version: "v2beta1", |
| 41 | Resource: "helmreleases", |
| 42 | }, |
| 43 | ).Namespace(ref.Namespace).Get(ctx, ref.Name, metav1.GetOptions{}) |
| 44 | if err != nil { |
| 45 | if errors.IsNotFound(err) { |
| 46 | return StatusNotFound, nil |
| 47 | } |
| 48 | return StatusNoStatus, err |
| 49 | } |
| 50 | b, err := res.MarshalJSON() |
| 51 | if err != nil { |
| 52 | return StatusNoStatus, err |
| 53 | } |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame^] | 54 | var hr HelmRelease |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 55 | if err := json.Unmarshal(b, &hr); err != nil { |
| 56 | return StatusNoStatus, err |
| 57 | } |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame^] | 58 | id, ok := hr.Metadata.Annotations["dodo.cloud/id"] |
| 59 | if !ok { |
| 60 | return StatusNoStatus, fmt.Errorf("missing annotation: dodo.cloud/id") |
| 61 | } |
| 62 | if id != ref.Id { |
| 63 | return StatusNotFound, nil |
| 64 | } |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 65 | // TODO(gio): check more thoroughly |
| 66 | for _, c := range hr.Status.Conditions { |
| 67 | if c.Type == "Ready" && c.Status == "True" { |
| 68 | return StatusSuccess, nil |
| 69 | } |
| 70 | } |
| 71 | return StatusProcessing, nil |
| 72 | } |
| 73 | |
| 74 | func NewHelmReleaseMonitor(kubeconfig string) (ResourceMonitor, error) { |
| 75 | c, err := kube.NewKubeClient(kube.KubeConfigOpts{KubeConfigPath: kubeconfig}) |
| 76 | if err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | d := dynamic.New(c.RESTClient()) |
| 80 | return &helmReleaseMonitor{d}, nil |
| 81 | } |