...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/utils/controller/key.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/utils/controller

     1  // Copyright 2021 Chaos Mesh 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  //
    15  
    16  package controller
    17  
    18  import (
    19  	"strings"
    20  
    21  	"github.com/pkg/errors"
    22  	"k8s.io/apimachinery/pkg/types"
    23  )
    24  
    25  func ParseNamespacedName(namespacedName string) (types.NamespacedName, error) {
    26  	parts := strings.Split(namespacedName, "/")
    27  	if len(parts) > 1 {
    28  		return types.NamespacedName{
    29  			Namespace: parts[0],
    30  			Name:      parts[1],
    31  		}, nil
    32  	}
    33  
    34  	return types.NamespacedName{
    35  		Namespace: "",
    36  		Name:      "",
    37  	}, errors.New("too few parts of namespacedname")
    38  
    39  }
    40  
    41  func ParseNamespacedNameContainer(namespacedName string) (types.NamespacedName, string, error) {
    42  	parts := strings.Split(namespacedName, "/")
    43  	if len(parts) > 2 {
    44  		//  a lowercase RFC 1123 label must consist of lower case alphanumeric
    45  		//  characters or '-', and must start and end with an alphanumeric
    46  		//  character, so the container name can never have "/"
    47  		return types.NamespacedName{
    48  			Namespace: parts[0],
    49  			Name:      parts[1],
    50  		}, parts[2], nil
    51  	}
    52  
    53  	return types.NamespacedName{
    54  		Namespace: "",
    55  		Name:      "",
    56  	}, "", errors.New("too few parts of namespacedname")
    57  
    58  }
    59  
    60  func ParseNamespacedNameContainerVolumePath(record string) (types.NamespacedName, string, string, error) {
    61  	parts := strings.Split(record, "/")
    62  	if len(parts) > 3 {
    63  		return types.NamespacedName{
    64  			Namespace: parts[0],
    65  			Name:      parts[1],
    66  		}, parts[2], strings.Join(parts[3:], "/"), nil
    67  	}
    68  
    69  	return types.NamespacedName{
    70  		Namespace: "",
    71  		Name:      "",
    72  	}, "", "", errors.New("too few parts of namespacedname")
    73  }
    74