sigs.k8s.io/cluster-api@v1.7.1/exp/runtime/api/v1alpha1/extensionconfig_types.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 v1alpha1
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  
    22  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    23  )
    24  
    25  // ANCHOR: ExtensionConfigSpec
    26  
    27  // ExtensionConfigSpec defines the desired state of ExtensionConfig.
    28  type ExtensionConfigSpec struct {
    29  	// ClientConfig defines how to communicate with the Extension server.
    30  	ClientConfig ClientConfig `json:"clientConfig"`
    31  
    32  	// NamespaceSelector decides whether to call the hook for an object based
    33  	// on whether the namespace for that object matches the selector.
    34  	// Defaults to the empty LabelSelector, which matches all objects.
    35  	// +optional
    36  	NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"`
    37  
    38  	// Settings defines key value pairs to be passed to all calls
    39  	// to all supported RuntimeExtensions.
    40  	// Note: Settings can be overridden on the ClusterClass.
    41  	// +optional
    42  	Settings map[string]string `json:"settings,omitempty"`
    43  }
    44  
    45  // ClientConfig contains the information to make a client
    46  // connection with an Extension server.
    47  type ClientConfig struct {
    48  	// URL gives the location of the Extension server, in standard URL form
    49  	// (`scheme://host:port/path`).
    50  	// Note: Exactly one of `url` or `service` must be specified.
    51  	//
    52  	// The scheme must be "https".
    53  	//
    54  	// The `host` should not refer to a service running in the cluster; use
    55  	// the `service` field instead.
    56  	//
    57  	// A path is optional, and if present may be any string permissible in
    58  	// a URL. If a path is set it will be used as prefix to the hook-specific path.
    59  	//
    60  	// Attempting to use a user or basic auth e.g. "user:password@" is not
    61  	// allowed. Fragments ("#...") and query parameters ("?...") are not
    62  	// allowed either.
    63  	//
    64  	// +optional
    65  	URL *string `json:"url,omitempty"`
    66  
    67  	// Service is a reference to the Kubernetes service for the Extension server.
    68  	// Note: Exactly one of `url` or `service` must be specified.
    69  	//
    70  	// If the Extension server is running within a cluster, then you should use `service`.
    71  	//
    72  	// +optional
    73  	Service *ServiceReference `json:"service,omitempty"`
    74  
    75  	// CABundle is a PEM encoded CA bundle which will be used to validate the Extension server's server certificate.
    76  	// +optional
    77  	CABundle []byte `json:"caBundle,omitempty"`
    78  }
    79  
    80  // ServiceReference holds a reference to a Kubernetes Service of an Extension server.
    81  type ServiceReference struct {
    82  	// Namespace is the namespace of the service.
    83  	Namespace string `json:"namespace"`
    84  
    85  	// Name is the name of the service.
    86  	Name string `json:"name"`
    87  
    88  	// Path is an optional URL path and if present may be any string permissible in
    89  	// a URL. If a path is set it will be used as prefix to the hook-specific path.
    90  	// +optional
    91  	Path *string `json:"path,omitempty"`
    92  
    93  	// Port is the port on the service that's hosting the Extension server.
    94  	// Defaults to 443.
    95  	// Port should be a valid port number (1-65535, inclusive).
    96  	// +optional
    97  	Port *int32 `json:"port,omitempty"`
    98  }
    99  
   100  // ANCHOR_END: ExtensionConfigSpec
   101  
   102  // ANCHOR: ExtensionConfigStatus
   103  
   104  // ExtensionConfigStatus defines the observed state of ExtensionConfig.
   105  type ExtensionConfigStatus struct {
   106  	// Handlers defines the current ExtensionHandlers supported by an Extension.
   107  	// +optional
   108  	// +listType=map
   109  	// +listMapKey=name
   110  	Handlers []ExtensionHandler `json:"handlers,omitempty"`
   111  
   112  	// Conditions define the current service state of the ExtensionConfig.
   113  	// +optional
   114  	Conditions clusterv1.Conditions `json:"conditions,omitempty"`
   115  }
   116  
   117  // ExtensionHandler specifies the details of a handler for a particular runtime hook registered by an Extension server.
   118  type ExtensionHandler struct {
   119  	// Name is the unique name of the ExtensionHandler.
   120  	Name string `json:"name"`
   121  
   122  	// RequestHook defines the versioned runtime hook which this ExtensionHandler serves.
   123  	RequestHook GroupVersionHook `json:"requestHook"`
   124  
   125  	// TimeoutSeconds defines the timeout duration for client calls to the ExtensionHandler.
   126  	// Defaults to 10 is not set.
   127  	// +optional
   128  	TimeoutSeconds *int32 `json:"timeoutSeconds,omitempty"`
   129  
   130  	// FailurePolicy defines how failures in calls to the ExtensionHandler should be handled by a client.
   131  	// Defaults to Fail if not set.
   132  	// +optional
   133  	FailurePolicy *FailurePolicy `json:"failurePolicy,omitempty"`
   134  }
   135  
   136  // GroupVersionHook defines the runtime hook when the ExtensionHandler is called.
   137  type GroupVersionHook struct {
   138  	// APIVersion is the group and version of the Hook.
   139  	APIVersion string `json:"apiVersion"`
   140  
   141  	// Hook is the name of the hook.
   142  	Hook string `json:"hook"`
   143  }
   144  
   145  // FailurePolicy specifies how unrecognized errors when calling the ExtensionHandler are handled.
   146  // FailurePolicy helps with extensions not working consistently, e.g. due to an intermittent network issue.
   147  // The following type of errors are never ignored by FailurePolicy Ignore:
   148  // - Misconfigurations (e.g. incompatible types)
   149  // - Extension explicitly returns a Status Failure.
   150  type FailurePolicy string
   151  
   152  const (
   153  	// FailurePolicyIgnore means that an error when calling the extension is ignored.
   154  	FailurePolicyIgnore FailurePolicy = "Ignore"
   155  
   156  	// FailurePolicyFail means that an error when calling the extension is propagated as an error.
   157  	FailurePolicyFail FailurePolicy = "Fail"
   158  )
   159  
   160  // ANCHOR_END: ExtensionConfigStatus
   161  
   162  // +kubebuilder:object:root=true
   163  // +kubebuilder:resource:path=extensionconfigs,shortName=ext,scope=Cluster,categories=cluster-api
   164  // +kubebuilder:subresource:status
   165  // +kubebuilder:storageversion
   166  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of ExtensionConfig"
   167  
   168  // ExtensionConfig is the Schema for the ExtensionConfig API.
   169  type ExtensionConfig struct {
   170  	metav1.TypeMeta   `json:",inline"`
   171  	metav1.ObjectMeta `json:"metadata,omitempty"`
   172  
   173  	// ExtensionConfigSpec is the desired state of the ExtensionConfig
   174  	Spec ExtensionConfigSpec `json:"spec,omitempty"`
   175  
   176  	// ExtensionConfigStatus is the current state of the ExtensionConfig
   177  	Status ExtensionConfigStatus `json:"status,omitempty"`
   178  }
   179  
   180  // GetConditions returns the set of conditions for this object.
   181  func (e *ExtensionConfig) GetConditions() clusterv1.Conditions {
   182  	return e.Status.Conditions
   183  }
   184  
   185  // SetConditions sets the conditions on this object.
   186  func (e *ExtensionConfig) SetConditions(conditions clusterv1.Conditions) {
   187  	e.Status.Conditions = conditions
   188  }
   189  
   190  // +kubebuilder:object:root=true
   191  
   192  // ExtensionConfigList contains a list of ExtensionConfig.
   193  type ExtensionConfigList struct {
   194  	metav1.TypeMeta `json:",inline"`
   195  	metav1.ListMeta `json:"metadata,omitempty"`
   196  	Items           []ExtensionConfig `json:"items"`
   197  }
   198  
   199  func init() {
   200  	objectTypes = append(objectTypes, &ExtensionConfig{}, &ExtensionConfigList{})
   201  }
   202  
   203  const (
   204  	// RuntimeExtensionDiscoveredCondition is a condition set on an ExtensionConfig object once it has been discovered by the Runtime SDK client.
   205  	RuntimeExtensionDiscoveredCondition clusterv1.ConditionType = "Discovered"
   206  
   207  	// DiscoveryFailedReason documents failure of a Discovery call.
   208  	DiscoveryFailedReason string = "DiscoveryFailed"
   209  
   210  	// InjectCAFromSecretAnnotation is the annotation that specifies that an ExtensionConfig
   211  	// object wants injection of CAs. The value is a reference to a Secret
   212  	// as <namespace>/<name>.
   213  	InjectCAFromSecretAnnotation string = "runtime.cluster.x-k8s.io/inject-ca-from-secret"
   214  
   215  	// PendingHooksAnnotation is the annotation used to keep track of pending runtime hooks.
   216  	// The annotation will be used to track the intent to call a hook as soon as an operation completes;
   217  	// the intent will be removed as soon as the hook call completes successfully.
   218  	PendingHooksAnnotation string = "runtime.cluster.x-k8s.io/pending-hooks"
   219  
   220  	// OkToDeleteAnnotation is the annotation used to indicate if a cluster is ready to be fully deleted.
   221  	// This annotation is added to the cluster after the BeforeClusterDelete hook has passed.
   222  	OkToDeleteAnnotation string = "runtime.cluster.x-k8s.io/ok-to-delete"
   223  )