| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 1 | package status |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | ) |
| 6 | |
| 7 | type Status int |
| 8 | |
| 9 | const ( |
| 10 | StatusNoStatus Status = iota |
| 11 | StatusNotFound |
| 12 | StatusPending |
| 13 | StatusProcessing |
| 14 | StatusSuccess |
| 15 | StatusFailure |
| 16 | ) |
| 17 | |
| 18 | func IsStatusTerminal(s Status) bool { |
| 19 | return s == StatusSuccess || s == StatusFailure |
| 20 | } |
| 21 | |
| 22 | func StatusString(s Status) string { |
| 23 | switch s { |
| 24 | case StatusNoStatus: |
| 25 | return "no_status" |
| 26 | case StatusNotFound: |
| 27 | return "not_found" |
| 28 | case StatusPending: |
| 29 | return "pending" |
| 30 | case StatusProcessing: |
| 31 | return "processing" |
| 32 | case StatusSuccess: |
| 33 | return "success" |
| 34 | case StatusFailure: |
| 35 | return "failure" |
| 36 | default: |
| 37 | panic("MUST NOT REACH!") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | type ResourceType int |
| 42 | |
| 43 | const ( |
| 44 | ResourceHelmRelease ResourceType = iota |
| gio | ea6d912 | 2025-05-22 17:57:18 +0400 | [diff] [blame^] | 45 | ResourceIngress |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | type ResourceRef struct { |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 49 | Id string |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 50 | Name string |
| 51 | Namespace string |
| 52 | } |
| 53 | |
| 54 | type Resource struct { |
| 55 | ResourceRef |
| 56 | Type ResourceType |
| 57 | } |
| 58 | |
| 59 | type Monitor interface { |
| 60 | Get(r Resource) (Status, error) |
| 61 | } |
| 62 | |
| 63 | type ResourceMonitor interface { |
| 64 | Get(r ResourceRef) (Status, error) |
| 65 | } |
| 66 | |
| 67 | type delegatingMonitor struct { |
| 68 | m map[ResourceType]ResourceMonitor |
| 69 | } |
| 70 | |
| 71 | func NewDelegatingMonitor(kubeconfig string) (Monitor, error) { |
| 72 | helm, err := NewHelmReleaseMonitor(kubeconfig) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| gio | ea6d912 | 2025-05-22 17:57:18 +0400 | [diff] [blame^] | 76 | ingress, err := NewIngressMonitor(kubeconfig) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 80 | return delegatingMonitor{ |
| 81 | map[ResourceType]ResourceMonitor{ |
| 82 | ResourceHelmRelease: helm, |
| gio | ea6d912 | 2025-05-22 17:57:18 +0400 | [diff] [blame^] | 83 | ResourceIngress: ingress, |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 84 | }, |
| 85 | }, nil |
| 86 | } |
| 87 | |
| 88 | func (m delegatingMonitor) Get(r Resource) (Status, error) { |
| 89 | if rm, ok := m.m[r.Type]; !ok { |
| 90 | return StatusNoStatus, fmt.Errorf("unknown resource type: %d", r.Type) |
| 91 | } else { |
| 92 | return rm.Get(r.ResourceRef) |
| 93 | } |
| 94 | } |