package controller import ( "maps" "slices" v1 "gitea.renner.uno/konairius/homelab/kuma-sync/api/v1alpha1" "gitea.renner.uno/konairius/homelab/kuma-sync/internal/kumaclient" ) // defaultMaxRedirects is Kuma's default for a monitor with no explicit value. const defaultMaxRedirects = 10 // OwnerKey is the value of the kuma-sync tag for a resource. func OwnerKey(km *v1.KumaMonitor) string { return km.Namespace + "/" + km.Name } // Desired maps a spec to the monitor Kuma should hold. Parent is resolved by // the reconciler. func Desired(km *v1.KumaMonitor) kumaclient.Monitor { s := km.Spec m := kumaclient.Monitor{ Type: s.Type, Name: s.DisplayName, Description: s.Description, Active: !s.Paused, Interval: s.Interval, RetryInterval: s.RetryInterval, MaxRetries: s.MaxRetries, OwnerKey: OwnerKey(km), } if s.Timeout != nil { m.Timeout = *s.Timeout } if h := s.HTTP; h != nil { m.URL = h.URL m.Method = h.Method m.AcceptedStatusCodes = slices.Clone(h.AcceptedStatusCodes) m.Headers = maps.Clone(h.Headers) m.Body = h.Body m.MaxRedirects = defaultMaxRedirects if h.MaxRedirects != nil { m.MaxRedirects = *h.MaxRedirects } m.IgnoreTLS = h.IgnoreTLS m.ExpiryNotification = h.ExpiryNotification } if k := s.Keyword; k != nil { m.Keyword = k.Keyword m.InvertKeyword = k.Invert } if q := s.JSONQuery; q != nil { m.JSONPath = q.Expression m.JSONPathOperator = q.Operator m.ExpectedValue = q.Expected } if d := s.DNS; d != nil { m.Hostname = d.Hostname m.DNSResolveType = d.ResolveType m.DNSResolveServer = d.ResolveServer m.Port = d.Port } if p := s.Port; p != nil { m.Hostname = p.Hostname m.Port = p.Port } return m }