sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/api/v1alpha3/provider_type.go (about)

     1  /*
     2  Copyright 2019 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 v1alpha3
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/types"
    22  )
    23  
    24  // +kubebuilder:resource:path=providers,scope=Namespaced,categories=cluster-api
    25  // +kubebuilder:storageversion
    26  // +kubebuilder:object:root=true
    27  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Provider"
    28  // +kubebuilder:printcolumn:name="Type",type="string",JSONPath=".type"
    29  // +kubebuilder:printcolumn:name="Provider",type="string",JSONPath=".providerName"
    30  // +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".version"
    31  
    32  // Provider defines an entry in the provider inventory.
    33  type Provider struct {
    34  	metav1.TypeMeta   `json:",inline"`
    35  	metav1.ObjectMeta `json:"metadata,omitempty"`
    36  
    37  	// ProviderName indicates the name of the provider.
    38  	// +optional
    39  	ProviderName string `json:"providerName,omitempty"`
    40  
    41  	// Type indicates the type of the provider.
    42  	// See ProviderType for a list of supported values
    43  	// +optional
    44  	Type string `json:"type,omitempty"`
    45  
    46  	// Version indicates the component version.
    47  	// +optional
    48  	Version string `json:"version,omitempty"`
    49  
    50  	// WatchedNamespace indicates the namespace where the provider controller is watching.
    51  	// If empty the provider controller is watching for objects in all namespaces.
    52  	//
    53  	// Deprecated: providers complying with the Cluster API v1alpha4 contract or above must watch all namespaces; this field will be removed in a future version of this API
    54  	// +optional
    55  	WatchedNamespace string `json:"watchedNamespace,omitempty"`
    56  }
    57  
    58  // ManifestLabel returns the cluster.x-k8s.io/provider label value for an entry in the provider inventory.
    59  // Please note that this label uniquely identifies the provider, e.g. bootstrap-kubeadm, but not the instances of
    60  // the provider, e.g. namespace-1/bootstrap-kubeadm and namespace-2/bootstrap-kubeadm.
    61  func (p *Provider) ManifestLabel() string {
    62  	return ManifestLabel(p.ProviderName, p.GetProviderType())
    63  }
    64  
    65  // InstanceName return the a name that uniquely identifies an entry in the provider inventory.
    66  // The instanceName is composed by the ManifestLabel and by the namespace where the provider is installed;
    67  // the resulting value uniquely identify a provider instance because clusterctl does not support multiple
    68  // instances of the same provider to be installed in the same namespace.
    69  func (p *Provider) InstanceName() string {
    70  	return types.NamespacedName{Namespace: p.Namespace, Name: p.ManifestLabel()}.String()
    71  }
    72  
    73  // SameAs returns true if two providers have the same ProviderName and Type.
    74  // Please note that there could be many instances of the same provider.
    75  func (p *Provider) SameAs(other Provider) bool {
    76  	return p.ProviderName == other.ProviderName && p.Type == other.Type
    77  }
    78  
    79  // Equals returns true if two providers are identical (same name, provider name, type, version etc.).
    80  func (p *Provider) Equals(other Provider) bool {
    81  	return p.Name == other.Name &&
    82  		p.Namespace == other.Namespace &&
    83  		p.ProviderName == other.ProviderName &&
    84  		p.Type == other.Type &&
    85  		p.WatchedNamespace == other.WatchedNamespace &&
    86  		p.Version == other.Version
    87  }
    88  
    89  // GetProviderType parse the Provider.Type string field and return the typed representation.
    90  func (p *Provider) GetProviderType() ProviderType {
    91  	switch t := ProviderType(p.Type); t {
    92  	case
    93  		CoreProviderType,
    94  		BootstrapProviderType,
    95  		InfrastructureProviderType,
    96  		ControlPlaneProviderType,
    97  		IPAMProviderType,
    98  		RuntimeExtensionProviderType,
    99  		AddonProviderType:
   100  		return t
   101  	default:
   102  		return ProviderTypeUnknown
   103  	}
   104  }
   105  
   106  // ProviderType is a string representation of a Provider type.
   107  type ProviderType string
   108  
   109  const (
   110  	// CoreProviderType is a type reserved for Cluster API core repository.
   111  	CoreProviderType = ProviderType("CoreProvider")
   112  
   113  	// BootstrapProviderType is the type associated with codebases that provide
   114  	// bootstrapping capabilities.
   115  	BootstrapProviderType = ProviderType("BootstrapProvider")
   116  
   117  	// InfrastructureProviderType is the type associated with codebases that provide
   118  	// infrastructure capabilities.
   119  	InfrastructureProviderType = ProviderType("InfrastructureProvider")
   120  
   121  	// ControlPlaneProviderType is the type associated with codebases that provide
   122  	// control-plane capabilities.
   123  	ControlPlaneProviderType = ProviderType("ControlPlaneProvider")
   124  
   125  	// IPAMProviderType is the type associated with codebases that provide
   126  	// IPAM capabilities.
   127  	IPAMProviderType = ProviderType("IPAMProvider")
   128  
   129  	// RuntimeExtensionProviderType is the type associated with codebases that provide
   130  	// runtime extensions.
   131  	RuntimeExtensionProviderType = ProviderType("RuntimeExtensionProvider")
   132  
   133  	// AddonProviderType is the type associated with codebases that provide
   134  	// add-on capabilities.
   135  	AddonProviderType = ProviderType("AddonProvider")
   136  
   137  	// ProviderTypeUnknown is used when the type is unknown.
   138  	ProviderTypeUnknown = ProviderType("")
   139  )
   140  
   141  // Order return an integer that can be used to sort ProviderType values.
   142  func (p ProviderType) Order() int {
   143  	switch p {
   144  	case CoreProviderType:
   145  		return 0
   146  	case BootstrapProviderType:
   147  		return 1
   148  	case ControlPlaneProviderType:
   149  		return 2
   150  	case InfrastructureProviderType:
   151  		return 3
   152  	case IPAMProviderType:
   153  		return 4
   154  	case RuntimeExtensionProviderType:
   155  		return 5
   156  	case AddonProviderType:
   157  		return 6
   158  	default:
   159  		return 99
   160  	}
   161  }
   162  
   163  // +kubebuilder:object:root=true
   164  
   165  // ProviderList contains a list of Provider.
   166  type ProviderList struct {
   167  	metav1.TypeMeta `json:",inline"`
   168  	metav1.ListMeta `json:"metadata,omitempty"`
   169  	Items           []Provider `json:"items"`
   170  }
   171  
   172  // FilterByNamespace returns a new list of providers that reside in the namespace provided.
   173  func (l *ProviderList) FilterByNamespace(namespace string) []Provider {
   174  	return l.filterBy(func(p Provider) bool {
   175  		return p.Namespace == namespace
   176  	})
   177  }
   178  
   179  // FilterByProviderNameAndType returns a new list of provider that match the name and type.
   180  func (l *ProviderList) FilterByProviderNameAndType(provider string, providerType ProviderType) []Provider {
   181  	return l.filterBy(func(p Provider) bool {
   182  		return p.ProviderName == provider && p.Type == string(providerType)
   183  	})
   184  }
   185  
   186  // FilterByProviderNameNamespaceTypeVersion returns a new list of provider that match the name, namespace, type and version.
   187  func (l *ProviderList) FilterByProviderNameNamespaceTypeVersion(provider, namespace string, providerType ProviderType, version string) []Provider {
   188  	return l.filterBy(func(p Provider) bool {
   189  		return p.ProviderName == provider && p.Namespace == namespace && p.Type == string(providerType) && p.Version == version
   190  	})
   191  }
   192  
   193  // FilterByType returns a new list of providers that match the given type.
   194  func (l *ProviderList) FilterByType(providerType ProviderType) []Provider {
   195  	return l.filterBy(func(p Provider) bool {
   196  		return p.GetProviderType() == providerType
   197  	})
   198  }
   199  
   200  // FilterCore returns a new list of providers that are in the core.
   201  func (l *ProviderList) FilterCore() []Provider {
   202  	return l.filterBy(func(p Provider) bool {
   203  		return p.GetProviderType() == CoreProviderType
   204  	})
   205  }
   206  
   207  // FilterNonCore returns a new list of providers that are not in the core.
   208  func (l *ProviderList) FilterNonCore() []Provider {
   209  	return l.filterBy(func(p Provider) bool {
   210  		return p.GetProviderType() != CoreProviderType
   211  	})
   212  }
   213  
   214  func (l *ProviderList) filterBy(predicate func(p Provider) bool) []Provider {
   215  	ret := []Provider{}
   216  	for _, i := range l.Items {
   217  		if predicate(i) {
   218  			ret = append(ret, i)
   219  		}
   220  	}
   221  	return ret
   222  }
   223  
   224  func init() {
   225  	objectTypes = append(objectTypes, &Provider{}, &ProviderList{})
   226  }