sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/config/v2/config.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  //go:deprecated This package has been deprecated
    18  package v2
    19  
    20  import (
    21  	"strings"
    22  
    23  	"sigs.k8s.io/yaml"
    24  
    25  	"sigs.k8s.io/kubebuilder/v3/pkg/config"
    26  	"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
    27  )
    28  
    29  // Version is the config.Version for project configuration 2
    30  var Version = config.Version{Number: 2}
    31  
    32  type cfg struct {
    33  	// Version
    34  	Version config.Version `json:"version"`
    35  
    36  	// String fields
    37  	Domain     string `json:"domain,omitempty"`
    38  	Repository string `json:"repo,omitempty"`
    39  
    40  	// Boolean fields
    41  	MultiGroup bool `json:"multigroup,omitempty"`
    42  
    43  	// Resources
    44  	Gvks []resource.GVK `json:"resources,omitempty"`
    45  }
    46  
    47  // New returns a new config.Config
    48  func New() config.Config {
    49  	return &cfg{Version: Version}
    50  }
    51  
    52  func init() {
    53  	config.Register(Version, New)
    54  }
    55  
    56  // GetVersion implements config.Config
    57  func (c cfg) GetVersion() config.Version {
    58  	return c.Version
    59  }
    60  
    61  // GetDomain implements config.Config
    62  func (c cfg) GetDomain() string {
    63  	return c.Domain
    64  }
    65  
    66  // SetDomain implements config.Config
    67  func (c *cfg) SetDomain(domain string) error {
    68  	c.Domain = domain
    69  	return nil
    70  }
    71  
    72  // GetRepository implements config.Config
    73  func (c cfg) GetRepository() string {
    74  	return c.Repository
    75  }
    76  
    77  // SetRepository implements config.Config
    78  func (c *cfg) SetRepository(repository string) error {
    79  	c.Repository = repository
    80  	return nil
    81  }
    82  
    83  // GetProjectName implements config.Config
    84  func (c cfg) GetProjectName() string {
    85  	return ""
    86  }
    87  
    88  // SetProjectName implements config.Config
    89  func (c *cfg) SetProjectName(string) error {
    90  	return config.UnsupportedFieldError{
    91  		Version: Version,
    92  		Field:   "project name",
    93  	}
    94  }
    95  
    96  // GetPluginChain implements config.Config
    97  func (c cfg) GetPluginChain() []string {
    98  	return []string{"go.kubebuilder.io/v2"}
    99  }
   100  
   101  // SetPluginChain implements config.Config
   102  func (c *cfg) SetPluginChain([]string) error {
   103  	return config.UnsupportedFieldError{
   104  		Version: Version,
   105  		Field:   "plugin chain",
   106  	}
   107  }
   108  
   109  // IsMultiGroup implements config.Config
   110  func (c cfg) IsMultiGroup() bool {
   111  	return c.MultiGroup
   112  }
   113  
   114  // SetMultiGroup implements config.Config
   115  func (c *cfg) SetMultiGroup() error {
   116  	c.MultiGroup = true
   117  	return nil
   118  }
   119  
   120  // ClearMultiGroup implements config.Config
   121  func (c *cfg) ClearMultiGroup() error {
   122  	c.MultiGroup = false
   123  	return nil
   124  }
   125  
   126  // IsComponentConfig implements config.Config
   127  func (c cfg) IsComponentConfig() bool {
   128  	return false
   129  }
   130  
   131  // SetComponentConfig implements config.Config
   132  func (c *cfg) SetComponentConfig() error {
   133  	return config.UnsupportedFieldError{
   134  		Version: Version,
   135  		Field:   "component config",
   136  	}
   137  }
   138  
   139  // ClearComponentConfig implements config.Config
   140  func (c *cfg) ClearComponentConfig() error {
   141  	return config.UnsupportedFieldError{
   142  		Version: Version,
   143  		Field:   "component config",
   144  	}
   145  }
   146  
   147  // ResourcesLength implements config.Config
   148  func (c cfg) ResourcesLength() int {
   149  	return len(c.Gvks)
   150  }
   151  
   152  // HasResource implements config.Config
   153  func (c cfg) HasResource(gvk resource.GVK) bool {
   154  	gvk.Domain = "" // Version 2 does not include domain per resource
   155  
   156  	for _, trackedGVK := range c.Gvks {
   157  		if gvk.IsEqualTo(trackedGVK) {
   158  			return true
   159  		}
   160  	}
   161  
   162  	return false
   163  }
   164  
   165  // GetResource implements config.Config
   166  func (c cfg) GetResource(gvk resource.GVK) (resource.Resource, error) {
   167  	gvk.Domain = "" // Version 2 does not include domain per resource
   168  
   169  	for _, trackedGVK := range c.Gvks {
   170  		if gvk.IsEqualTo(trackedGVK) {
   171  			return resource.Resource{
   172  				GVK: trackedGVK,
   173  			}, nil
   174  		}
   175  	}
   176  
   177  	return resource.Resource{}, config.ResourceNotFoundError{GVK: gvk}
   178  }
   179  
   180  // GetResources implements config.Config
   181  func (c cfg) GetResources() ([]resource.Resource, error) {
   182  	resources := make([]resource.Resource, 0, len(c.Gvks))
   183  	for _, gvk := range c.Gvks {
   184  		resources = append(resources, resource.Resource{
   185  			GVK: gvk,
   186  		})
   187  	}
   188  
   189  	return resources, nil
   190  }
   191  
   192  // AddResource implements config.Config
   193  func (c *cfg) AddResource(res resource.Resource) error {
   194  	// As res is passed by value it is already a shallow copy, and we are only using
   195  	// fields that do not require a deep copy, so no need to make a deep copy
   196  
   197  	res.Domain = "" // Version 2 does not include domain per resource
   198  
   199  	if !c.HasResource(res.GVK) {
   200  		c.Gvks = append(c.Gvks, res.GVK)
   201  	}
   202  
   203  	return nil
   204  }
   205  
   206  // UpdateResource implements config.Config
   207  func (c *cfg) UpdateResource(res resource.Resource) error {
   208  	return c.AddResource(res)
   209  }
   210  
   211  // HasGroup implements config.Config
   212  func (c cfg) HasGroup(group string) bool {
   213  	// Return true if the target group is found in the tracked resources
   214  	for _, r := range c.Gvks {
   215  		if strings.EqualFold(group, r.Group) {
   216  			return true
   217  		}
   218  	}
   219  
   220  	// Return false otherwise
   221  	return false
   222  }
   223  
   224  // ListCRDVersions implements config.Config
   225  func (c cfg) ListCRDVersions() []string {
   226  	return make([]string, 0)
   227  }
   228  
   229  // ListWebhookVersions implements config.Config
   230  func (c cfg) ListWebhookVersions() []string {
   231  	return make([]string, 0)
   232  }
   233  
   234  // DecodePluginConfig implements config.Config
   235  func (c cfg) DecodePluginConfig(string, interface{}) error {
   236  	return config.UnsupportedFieldError{
   237  		Version: Version,
   238  		Field:   "plugins",
   239  	}
   240  }
   241  
   242  // EncodePluginConfig implements config.Config
   243  func (c cfg) EncodePluginConfig(string, interface{}) error {
   244  	return config.UnsupportedFieldError{
   245  		Version: Version,
   246  		Field:   "plugins",
   247  	}
   248  }
   249  
   250  // Marshal implements config.Config
   251  func (c cfg) MarshalYAML() ([]byte, error) {
   252  	content, err := yaml.Marshal(c)
   253  	if err != nil {
   254  		return nil, config.MarshalError{Err: err}
   255  	}
   256  
   257  	return content, nil
   258  }
   259  
   260  // Unmarshal implements config.Config
   261  func (c *cfg) UnmarshalYAML(b []byte) error {
   262  	if err := yaml.UnmarshalStrict(b, c); err != nil {
   263  		return config.UnmarshalError{Err: err}
   264  	}
   265  
   266  	return nil
   267  }