annotations.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package kube
  15. import (
  16. "errors"
  17. "regexp"
  18. "strings"
  19. )
  20. type annotations struct {
  21. SubnetKubeManaged string
  22. BackendData string
  23. BackendV6Data string
  24. BackendType string
  25. BackendPublicIP string
  26. BackendPublicIPv6 string
  27. BackendPublicIPOverwrite string
  28. BackendPublicIPv6Overwrite string
  29. }
  30. func newAnnotations(prefix string) (annotations, error) {
  31. slashCnt := strings.Count(prefix, "/")
  32. if slashCnt > 1 {
  33. return annotations{}, errors.New("subnet/kube: prefix can contain at most single slash")
  34. }
  35. if slashCnt == 0 {
  36. prefix += "/"
  37. }
  38. if !strings.HasSuffix(prefix, "/") && !strings.HasSuffix(prefix, "-") {
  39. prefix += "-"
  40. }
  41. // matches is a regexp matching the format used by the kubernetes for
  42. // annotations. Following rules apply:
  43. //
  44. // - must start with FQDN - must contain at most one slash "/"
  45. // - must contain only lowercase letters, nubers, underscores,
  46. // hyphens, dots and slash
  47. matches, err := regexp.MatchString(`(?:[a-z0-9_-]+\.)+[a-z0-9_-]+/(?:[a-z0-9_-]+-)?$`, prefix)
  48. if err != nil {
  49. panic(err)
  50. }
  51. if !matches {
  52. return annotations{}, errors.New("subnet/kube: prefix must be in a format: fqdn/[0-9a-z-_]*")
  53. }
  54. a := annotations{
  55. SubnetKubeManaged: prefix + "kube-subnet-manager",
  56. BackendData: prefix + "backend-data",
  57. BackendV6Data: prefix + "backend-v6-data",
  58. BackendType: prefix + "backend-type",
  59. BackendPublicIP: prefix + "public-ip",
  60. BackendPublicIPOverwrite: prefix + "public-ip-overwrite",
  61. BackendPublicIPv6: prefix + "public-ipv6",
  62. BackendPublicIPv6Overwrite: prefix + "public-ipv6-overwrite",
  63. }
  64. return a, nil
  65. }