...

Source file src/k8s.io/kubectl/pkg/util/util.go

Documentation: k8s.io/kubectl/pkg/util

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

View as plain text