annotations.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. BackendType string
  24. BackendPublicIP string
  25. BackendPublicIPOverwrite string
  26. }
  27. func newAnnotations(prefix string) (annotations, error) {
  28. slashCnt := strings.Count(prefix, "/")
  29. if slashCnt > 1 {
  30. return annotations{}, errors.New("subnet/kube: prefix can contain at most single slash")
  31. }
  32. if slashCnt == 0 {
  33. prefix += "/"
  34. }
  35. if !strings.HasSuffix(prefix, "/") && !strings.HasSuffix(prefix, "-") {
  36. prefix += "-"
  37. }
  38. // matches is a regexp matching the format used by the kubernetes for
  39. // annotations. Following rules apply:
  40. //
  41. // - must start with FQDN - must contain at most one slash "/"
  42. // - must contain only lowercase letters, nubers, underscores,
  43. // hyphens, dots and slash
  44. matches, err := regexp.MatchString(`(?:[a-z0-9_-]+\.)+[a-z0-9_-]+/(?:[a-z0-9_-]+-)?$`, prefix)
  45. if err != nil {
  46. panic(err)
  47. }
  48. if !matches {
  49. return annotations{}, errors.New("subnet/kube: prefix must be in a format: fqdn/[0-9a-z-_]*")
  50. }
  51. a := annotations{
  52. SubnetKubeManaged: prefix + "kube-subnet-manager",
  53. BackendData: prefix + "backend-data",
  54. BackendType: prefix + "backend-type",
  55. BackendPublicIP: prefix + "public-ip",
  56. BackendPublicIPOverwrite: prefix + "public-ip-overwrite",
  57. }
  58. return a, nil
  59. }