annotations.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2016 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, err := regexp.MatchString(`(?:[a-z0-9_-]+\.)+[a-z0-9_-]+/(?:[a-z0-9_-]+-)?$`, prefix)
  39. if err != nil {
  40. panic(err)
  41. }
  42. if !matches {
  43. return annotations{}, errors.New("subnet/kube: prefix must be in a format: fqdn/[0-9a-z-_]*")
  44. }
  45. a := annotations{
  46. SubnetKubeManaged: prefix + "kube-subnet-manager",
  47. BackendData: prefix + "backend-data",
  48. BackendType: prefix + "backend-type",
  49. BackendPublicIP: prefix + "public-ip",
  50. BackendPublicIPOverwrite: prefix + "public-ip-overwrite",
  51. }
  52. return a, nil
  53. }