GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flux.go 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package flux
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "sort"
  7. "strings"
  8. "github.com/pkg/errors"
  9. )
  10. var (
  11. ErrInvalidServiceID = errors.New("invalid service ID")
  12. LegacyServiceIDRegexp = regexp.MustCompile("^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$")
  13. ResourceIDRegexp = regexp.MustCompile("^([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$")
  14. UnqualifiedResourceIDRegexp = regexp.MustCompile("^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$")
  15. )
  16. // ResourceID is an opaque type which uniquely identifies a resource in an
  17. // orchestrator.
  18. type ResourceID struct {
  19. resourceIDImpl
  20. }
  21. type resourceIDImpl interface {
  22. String() string
  23. }
  24. // Old-style <namespace>/<servicename> format
  25. type legacyServiceID struct {
  26. namespace, service string
  27. }
  28. func (id legacyServiceID) String() string {
  29. return fmt.Sprintf("%s/%s", id.namespace, id.service)
  30. }
  31. // New <namespace>:<kind>/<name> format
  32. type resourceID struct {
  33. namespace, kind, name string
  34. }
  35. func (id resourceID) String() string {
  36. return fmt.Sprintf("%s:%s/%s", id.namespace, id.kind, id.name)
  37. }
  38. // ParseResourceID constructs a ResourceID from a string representation
  39. // if possible, returning an error value otherwise.
  40. func ParseResourceID(s string) (ResourceID, error) {
  41. if m := ResourceIDRegexp.FindStringSubmatch(s); m != nil {
  42. return ResourceID{resourceID{m[1], strings.ToLower(m[2]), m[3]}}, nil
  43. }
  44. if m := LegacyServiceIDRegexp.FindStringSubmatch(s); m != nil {
  45. return ResourceID{legacyServiceID{m[1], m[2]}}, nil
  46. }
  47. return ResourceID{}, errors.Wrap(ErrInvalidServiceID, "parsing "+s)
  48. }
  49. // MustParseResourceID constructs a ResourceID from a string representation,
  50. // panicing if the format is invalid.
  51. func MustParseResourceID(s string) ResourceID {
  52. id, err := ParseResourceID(s)
  53. if err != nil {
  54. panic(err)
  55. }
  56. return id
  57. }
  58. // ParseResourceIDOptionalNamespace constructs a ResourceID from either a fully
  59. // qualified string representation, or an unqualified kind/name representation
  60. // and the supplied namespace.
  61. func ParseResourceIDOptionalNamespace(namespace, s string) (ResourceID, error) {
  62. if m := ResourceIDRegexp.FindStringSubmatch(s); m != nil {
  63. return ResourceID{resourceID{m[1], strings.ToLower(m[2]), m[3]}}, nil
  64. }
  65. if m := UnqualifiedResourceIDRegexp.FindStringSubmatch(s); m != nil {
  66. return ResourceID{resourceID{namespace, strings.ToLower(m[1]), m[2]}}, nil
  67. }
  68. return ResourceID{}, errors.Wrap(ErrInvalidServiceID, "parsing "+s)
  69. }
  70. // MakeResourceID constructs a ResourceID from constituent components.
  71. func MakeResourceID(namespace, kind, name string) ResourceID {
  72. return ResourceID{resourceID{namespace, strings.ToLower(kind), name}}
  73. }
  74. // Components returns the constituent components of a ResourceID
  75. func (id ResourceID) Components() (namespace, kind, name string) {
  76. switch impl := id.resourceIDImpl.(type) {
  77. case resourceID:
  78. return impl.namespace, impl.kind, impl.name
  79. case legacyServiceID:
  80. return impl.namespace, "service", impl.service
  81. default:
  82. panic("wrong underlying type")
  83. }
  84. }
  85. // MarshalJSON encodes a ResourceID as a JSON string. This is
  86. // done to maintain backwards compatibility with previous flux
  87. // versions where the ResourceID is a plain string.
  88. func (id ResourceID) MarshalJSON() ([]byte, error) {
  89. if id.resourceIDImpl == nil {
  90. // Sadly needed as it's possible to construct an empty ResourceID literal
  91. return json.Marshal("")
  92. }
  93. return json.Marshal(id.String())
  94. }
  95. // UnmarshalJSON decodes a ResourceID from a JSON string. This is
  96. // done to maintain backwards compatibility with previous flux
  97. // versions where the ResourceID is a plain string.
  98. func (id *ResourceID) UnmarshalJSON(data []byte) (err error) {
  99. var str string
  100. if err := json.Unmarshal(data, &str); err != nil {
  101. return err
  102. }
  103. if string(str) == "" {
  104. // Sadly needed as it's possible to construct an empty ResourceID literal
  105. *id = ResourceID{}
  106. return nil
  107. }
  108. *id, err = ParseResourceID(string(str))
  109. return err
  110. }
  111. // MarshalText encodes a ResourceID as a flat string; this is
  112. // required because ResourceIDs are sometimes used as map keys.
  113. func (id ResourceID) MarshalText() (text []byte, err error) {
  114. return []byte(id.String()), nil
  115. }
  116. // MarshalText decodes a ResourceID from a flat string; this is
  117. // required because ResourceIDs are sometimes used as map keys.
  118. func (id *ResourceID) UnmarshalText(text []byte) error {
  119. result, err := ParseResourceID(string(text))
  120. if err != nil {
  121. return err
  122. }
  123. *id = result
  124. return nil
  125. }
  126. type ResourceIDSet map[ResourceID]struct{}
  127. func (s ResourceIDSet) String() string {
  128. var ids []string
  129. for id := range s {
  130. ids = append(ids, id.String())
  131. }
  132. return "{" + strings.Join(ids, ", ") + "}"
  133. }
  134. func (s ResourceIDSet) Add(ids []ResourceID) {
  135. for _, id := range ids {
  136. s[id] = struct{}{}
  137. }
  138. }
  139. func (s ResourceIDSet) Without(others ResourceIDSet) ResourceIDSet {
  140. if s == nil || len(s) == 0 || others == nil || len(others) == 0 {
  141. return s
  142. }
  143. res := ResourceIDSet{}
  144. for id := range s {
  145. if !others.Contains(id) {
  146. res[id] = struct{}{}
  147. }
  148. }
  149. return res
  150. }
  151. func (s ResourceIDSet) Contains(id ResourceID) bool {
  152. if s == nil {
  153. return false
  154. }
  155. _, ok := s[id]
  156. return ok
  157. }
  158. func (s ResourceIDSet) Intersection(others ResourceIDSet) ResourceIDSet {
  159. if s == nil {
  160. return others
  161. }
  162. if others == nil {
  163. return s
  164. }
  165. result := ResourceIDSet{}
  166. for id := range s {
  167. if _, ok := others[id]; ok {
  168. result[id] = struct{}{}
  169. }
  170. }
  171. return result
  172. }
  173. func (s ResourceIDSet) ToSlice() ResourceIDs {
  174. i := 0
  175. keys := make(ResourceIDs, len(s))
  176. for k := range s {
  177. keys[i] = k
  178. i++
  179. }
  180. return keys
  181. }
  182. type ResourceIDs []ResourceID
  183. func (p ResourceIDs) Len() int { return len(p) }
  184. func (p ResourceIDs) Less(i, j int) bool { return p[i].String() < p[j].String() }
  185. func (p ResourceIDs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  186. func (p ResourceIDs) Sort() { sort.Sort(p) }
  187. func (ids ResourceIDs) Without(others ResourceIDSet) (res ResourceIDs) {
  188. for _, id := range ids {
  189. if !others.Contains(id) {
  190. res = append(res, id)
  191. }
  192. }
  193. return res
  194. }
  195. func (ids ResourceIDs) Contains(id ResourceID) bool {
  196. set := ResourceIDSet{}
  197. set.Add(ids)
  198. return set.Contains(id)
  199. }
  200. func (ids ResourceIDs) Intersection(others ResourceIDSet) ResourceIDSet {
  201. set := ResourceIDSet{}
  202. set.Add(ids)
  203. return set.Intersection(others)
  204. }