sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/options.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 golang
    18  
    19  import (
    20  	"path"
    21  
    22  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    23  	cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
    24  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
    26  )
    27  
    28  var (
    29  	coreGroups = map[string]string{
    30  		"admission":             "k8s.io",
    31  		"admissionregistration": "k8s.io",
    32  		"apps":                  "",
    33  		"auditregistration":     "k8s.io",
    34  		"apiextensions":         "k8s.io",
    35  		"authentication":        "k8s.io",
    36  		"authorization":         "k8s.io",
    37  		"autoscaling":           "",
    38  		"batch":                 "",
    39  		"certificates":          "k8s.io",
    40  		"coordination":          "k8s.io",
    41  		"core":                  "",
    42  		"events":                "k8s.io",
    43  		"extensions":            "",
    44  		"imagepolicy":           "k8s.io",
    45  		"networking":            "k8s.io",
    46  		"node":                  "k8s.io",
    47  		"metrics":               "k8s.io",
    48  		"policy":                "",
    49  		"rbac.authorization":    "k8s.io",
    50  		"scheduling":            "k8s.io",
    51  		"setting":               "k8s.io",
    52  		"storage":               "k8s.io",
    53  	}
    54  )
    55  
    56  // Options contains the information required to build a new resource.Resource.
    57  type Options struct {
    58  	// Plural is the resource's kind plural form.
    59  	Plural string
    60  
    61  	// CRDVersion is the CustomResourceDefinition API version that will be used for the resource.
    62  	CRDVersion string
    63  	// WebhookVersion is the {Validating,Mutating}WebhookConfiguration API version that will be used for the resource.
    64  	WebhookVersion string
    65  
    66  	// Namespaced is true if the resource should be namespaced.
    67  	Namespaced bool
    68  
    69  	// Flags that define which parts should be scaffolded
    70  	DoAPI        bool
    71  	DoController bool
    72  	DoDefaulting bool
    73  	DoValidation bool
    74  	DoConversion bool
    75  }
    76  
    77  // UpdateResource updates the provided resource with the options
    78  func (opts Options) UpdateResource(res *resource.Resource, c config.Config) {
    79  	if opts.Plural != "" {
    80  		res.Plural = opts.Plural
    81  	}
    82  
    83  	if opts.DoAPI {
    84  		//nolint:staticcheck
    85  		if plugin.IsLegacyLayout(c) {
    86  			res.Path = resource.APIPackagePathLegacy(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
    87  		} else {
    88  			res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
    89  		}
    90  
    91  		res.API = &resource.API{
    92  			CRDVersion: opts.CRDVersion,
    93  			Namespaced: opts.Namespaced,
    94  		}
    95  
    96  	}
    97  
    98  	if opts.DoController {
    99  		res.Controller = true
   100  	}
   101  
   102  	if opts.DoDefaulting || opts.DoValidation || opts.DoConversion {
   103  		// IsLegacyLayout is added to ensure backwards compatibility and should
   104  		// be removed when we remove the go/v3 plugin
   105  		//nolint:staticcheck
   106  		if plugin.IsLegacyLayout(c) {
   107  			res.Path = resource.APIPackagePathLegacy(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
   108  		} else {
   109  			res.Path = resource.APIPackagePath(c.GetRepository(), res.Group, res.Version, c.IsMultiGroup())
   110  		}
   111  		res.Webhooks.WebhookVersion = opts.WebhookVersion
   112  		if opts.DoDefaulting {
   113  			res.Webhooks.Defaulting = true
   114  		}
   115  		if opts.DoValidation {
   116  			res.Webhooks.Validation = true
   117  		}
   118  		if opts.DoConversion {
   119  			res.Webhooks.Conversion = true
   120  		}
   121  	}
   122  
   123  	// domain and path may need to be changed in case we are referring to a builtin core resource:
   124  	//  - Check if we are scaffolding the resource now           => project resource
   125  	//  - Check if we already scaffolded the resource            => project resource
   126  	//  - Check if the resource group is a well-known core group => builtin core resource
   127  	//  - In any other case, default to                          => project resource
   128  	// TODO: need to support '--resource-pkg-path' flag for specifying resourcePath
   129  	if !opts.DoAPI {
   130  		var alreadyHasAPI bool
   131  		if c.GetVersion().Compare(cfgv2.Version) == 0 {
   132  			alreadyHasAPI = c.HasResource(res.GVK)
   133  		} else {
   134  			loadedRes, err := c.GetResource(res.GVK)
   135  			alreadyHasAPI = err == nil && loadedRes.HasAPI()
   136  		}
   137  		if !alreadyHasAPI {
   138  			if domain, found := coreGroups[res.Group]; found {
   139  				res.Domain = domain
   140  				res.Path = path.Join("k8s.io", "api", res.Group, res.Version)
   141  			}
   142  		}
   143  	}
   144  }