util.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "crypto/md5"
  16. "errors"
  17. "fmt"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. )
  25. // ParseRFC3339 parses an RFC3339 date in either RFC3339Nano or RFC3339 format.
  26. func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) {
  27. if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil {
  28. return metav1.Time{Time: t}, nil
  29. }
  30. t, err := time.Parse(time.RFC3339, s)
  31. if err != nil {
  32. return metav1.Time{}, err
  33. }
  34. return metav1.Time{Time: t}, nil
  35. }
  36. // HashObject returns the hash of a Object hash by a Codec
  37. func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
  38. data, err := runtime.Encode(codec, obj)
  39. if err != nil {
  40. return "", err
  41. }
  42. return fmt.Sprintf("%x", md5.Sum(data)), nil
  43. }
  44. // ParseFileSource parses the source given.
  45. //
  46. // Acceptable formats include:
  47. // 1. source-path: the basename will become the key name
  48. // 2. source-name=source-path: the source-name will become the key name and
  49. // source-path is the path to the key file.
  50. //
  51. // Key names cannot include '='.
  52. func ParseFileSource(source string) (keyName, filePath string, err error) {
  53. numSeparators := strings.Count(source, "=")
  54. switch {
  55. case numSeparators == 0:
  56. return path.Base(filepath.ToSlash(source)), source, nil
  57. case numSeparators == 1 && strings.HasPrefix(source, "="):
  58. return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "="))
  59. case numSeparators == 1 && strings.HasSuffix(source, "="):
  60. return "", "", fmt.Errorf("file path for key name %v missing", strings.TrimSuffix(source, "="))
  61. case numSeparators > 1:
  62. return "", "", errors.New("Key names or file paths cannot contain '='")
  63. default:
  64. components := strings.Split(source, "=")
  65. return components[0], components[1], nil
  66. }
  67. }
  68. // ParseLiteralSource parses the source key=val pair into its component pieces.
  69. // This functionality is distinguished from strings.SplitN(source, "=", 2) since
  70. // it returns an error in the case of empty keys, values, or a missing equals sign.
  71. func ParseLiteralSource(source string) (keyName, value string, err error) {
  72. // leading equal is invalid
  73. if strings.Index(source, "=") == 0 {
  74. return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source)
  75. }
  76. // split after the first equal (so values can have the = character)
  77. items := strings.SplitN(source, "=", 2)
  78. if len(items) != 2 {
  79. return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source)
  80. }
  81. return items[0], items[1], nil
  82. }