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.5KB

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