sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/resource/utils.go (about)

     1  /*
     2  Copyright 2022 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 resource
    18  
    19  import (
    20  	"fmt"
    21  	"path"
    22  	"strings"
    23  
    24  	"github.com/gobuffalo/flect"
    25  )
    26  
    27  const V1beta1 = "v1beta1"
    28  const V1 = "v1"
    29  
    30  // validateAPIVersion validates CRD or Webhook versions
    31  func validateAPIVersion(version string) error {
    32  	switch version {
    33  	case V1beta1, V1:
    34  		return nil
    35  	default:
    36  		return fmt.Errorf("API version must be one of: v1beta1, v1")
    37  	}
    38  }
    39  
    40  // safeImport returns a cleaned version of the provided string that can be used for imports
    41  func safeImport(unsafe string) string {
    42  	safe := unsafe
    43  
    44  	// Remove dashes and dots
    45  	safe = strings.Replace(safe, "-", "", -1)
    46  	safe = strings.Replace(safe, ".", "", -1)
    47  
    48  	return safe
    49  }
    50  
    51  // APIPackagePath returns the default path
    52  func APIPackagePath(repo, group, version string, multiGroup bool) string {
    53  	if multiGroup && group != "" {
    54  		return path.Join(repo, "api", group, version)
    55  	}
    56  	return path.Join(repo, "api", version)
    57  }
    58  
    59  // APIPackagePathLegacy returns the default path
    60  func APIPackagePathLegacy(repo, group, version string, multiGroup bool) string {
    61  	if multiGroup {
    62  		if group != "" {
    63  			return path.Join(repo, "apis", group, version)
    64  		}
    65  		return path.Join(repo, "apis", version)
    66  	}
    67  	return path.Join(repo, "api", version)
    68  }
    69  
    70  // RegularPlural returns a default plural form when none was specified
    71  func RegularPlural(singular string) string {
    72  	return flect.Pluralize(strings.ToLower(singular))
    73  }