|
| 1 | +/* |
| 2 | +Copyright 2022 Kong Inc. |
| 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 v2alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 22 | + |
| 23 | + commonv1alpha1 "github.com/kong/kubernetes-configuration/api/common/v1alpha1" |
| 24 | + operatorv1beta1 "github.com/kong/kubernetes-configuration/api/gateway-operator/v1beta1" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + SchemeBuilder.Register(&ControlPlane{}, &ControlPlaneList{}) |
| 29 | +} |
| 30 | + |
| 31 | +// ControlPlane is the Schema for the controlplanes API |
| 32 | +// |
| 33 | +// +genclient |
| 34 | +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object |
| 35 | +// +kubebuilder:object:root=true |
| 36 | +// +kubebuilder:subresource:status |
| 37 | +// +kubebuilder:resource:shortName=kocp,categories=kong;all |
| 38 | +// +kubebuilder:printcolumn:name="Ready",description="The Resource is ready",type=string,JSONPath=`.status.conditions[?(@.type=='Ready')].status` |
| 39 | +// +apireference:kgo:include |
| 40 | +// +kong:channels=gateway-operator |
| 41 | +type ControlPlane struct { |
| 42 | + metav1.TypeMeta `json:",inline"` |
| 43 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 44 | + |
| 45 | + Spec ControlPlaneSpec `json:"spec,omitempty"` |
| 46 | + Status ControlPlaneStatus `json:"status,omitempty"` |
| 47 | +} |
| 48 | + |
| 49 | +// ControlPlaneList contains a list of ControlPlane |
| 50 | +// |
| 51 | +// +kubebuilder:object:root=true |
| 52 | +// +apireference:kgo:include |
| 53 | +type ControlPlaneList struct { |
| 54 | + metav1.TypeMeta `json:",inline"` |
| 55 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 56 | + Items []ControlPlane `json:"items"` |
| 57 | +} |
| 58 | + |
| 59 | +// ControlPlaneSpec defines the desired state of ControlPlane |
| 60 | +// |
| 61 | +// +apireference:kgo:include |
| 62 | +type ControlPlaneSpec struct { |
| 63 | + ControlPlaneOptions `json:",inline"` |
| 64 | + |
| 65 | + // GatewayClass indicates the Gateway resources which this ControlPlane |
| 66 | + // should be responsible for configuring routes for (e.g. HTTPRoute, |
| 67 | + // TCPRoute, UDPRoute, TLSRoute, e.t.c.). |
| 68 | + // |
| 69 | + // Required for the ControlPlane to have any effect: at least one Gateway |
| 70 | + // must be present for configuration to be pushed to the data-plane and |
| 71 | + // only Gateway resources can be used to identify data-plane entities. |
| 72 | + // |
| 73 | + // +optional |
| 74 | + GatewayClass *gatewayv1.ObjectName `json:"gatewayClass,omitempty"` |
| 75 | + |
| 76 | + // IngressClass enables support for the older Ingress resource and indicates |
| 77 | + // which Ingress resources this ControlPlane should be responsible for. |
| 78 | + // |
| 79 | + // Routing configured this way will be applied to the Gateway resources |
| 80 | + // indicated by GatewayClass. |
| 81 | + // |
| 82 | + // If omitted, Ingress resources will not be supported by the ControlPlane. |
| 83 | + // |
| 84 | + // +optional |
| 85 | + IngressClass *string `json:"ingressClass,omitempty"` |
| 86 | +} |
| 87 | + |
| 88 | +// ControlPlaneOptions indicates the specific information needed to |
| 89 | +// deploy and connect a ControlPlane to a DataPlane object. |
| 90 | +// |
| 91 | +// +apireference:kgo:include |
| 92 | +// +kubebuilder:validation:XValidation:message="Extension not allowed for ControlPlane",rule="has(self.extensions) ? self.extensions.all(e, (e.group == 'konnect.konghq.com' && e.kind == 'KonnectExtension') || (e.group == 'gateway-operator.konghq.com' && e.kind == 'DataPlaneMetricsExtension')) : true" |
| 93 | +type ControlPlaneOptions struct { |
| 94 | + // DataPlanes designates the target data plane to configure. |
| 95 | + // |
| 96 | + // It can be either a URL to an externally managed DataPlane (e.g. installed |
| 97 | + // independently with Helm) or a name of a DataPlane resource that is |
| 98 | + // managed by the operator. |
| 99 | + // |
| 100 | + // +required |
| 101 | + DataPlane ControlPlaneDataPlaneTarget `json:"dataplane,omitempty"` |
| 102 | + |
| 103 | + // Extensions provide additional or replacement features for the ControlPlane |
| 104 | + // resources to influence or enhance functionality. |
| 105 | + // |
| 106 | + // +optional |
| 107 | + // +kubebuilder:validation:MinItems=0 |
| 108 | + // +kubebuilder:validation:MaxItems=2 |
| 109 | + Extensions []commonv1alpha1.ExtensionRef `json:"extensions,omitempty"` |
| 110 | + |
| 111 | + // WatchNamespaces indicates the namespaces to watch for resources. |
| 112 | + // |
| 113 | + // +optional |
| 114 | + // +kubebuilder:default={type: all} |
| 115 | + WatchNamespaces *operatorv1beta1.WatchNamespaces `json:"watchNamespaces,omitempty"` |
| 116 | + |
| 117 | + // FeatureGates is a list of feature gates that are enabled for this ControlPlane. |
| 118 | + // |
| 119 | + // +optional |
| 120 | + // +listType=map |
| 121 | + // +listMapKey=name |
| 122 | + // +kubebuilder:validation:MaxItems=32 |
| 123 | + FeatureGates []ControlPlaneFeatureGate `json:"featureGates,omitempty"` |
| 124 | + |
| 125 | + // Controllers defines the controllers that are enabled for this ControlPlane. |
| 126 | + // |
| 127 | + // +optional |
| 128 | + // +listType=map |
| 129 | + // +listMapKey=name |
| 130 | + // +kubebuilder:validation:MaxItems=32 |
| 131 | + Controllers []ControlPlaneController `json:"controllers,omitempty"` |
| 132 | + |
| 133 | + // AdminAPI defines the configuration for the Kong Admin API. |
| 134 | + // |
| 135 | + // +optional |
| 136 | + AdminAPI *ControlPlaneAdminAPI `json:"adminAPI,omitempty"` |
| 137 | +} |
| 138 | + |
| 139 | +// ControlPlaneDataPlaneTarget defines the target for the DataPlane that the ControlPlane |
| 140 | +// is responsible for configuring. |
| 141 | +// |
| 142 | +// +kubebuilder:validation:XValidation:message="URL has to be provided when type is set to url",rule="self.type != 'url' || has(self.url)" |
| 143 | +// +kubebuilder:validation:XValidation:message="Name cannot be provided when type is set to url",rule="self.type != 'url' || !has(self.name)" |
| 144 | +// +kubebuilder:validation:XValidation:message="Name has to be provided when type is set to name",rule="self.type != 'name' || has(self.name)" |
| 145 | +// +kubebuilder:validation:XValidation:message="URL cannot be provided when type is set to name",rule="self.type != 'name' || !has(self.url)" |
| 146 | +type ControlPlaneDataPlaneTarget struct { |
| 147 | + // Type indicates the type of the DataPlane target. |
| 148 | + // |
| 149 | + // +kubebuilder:validation:Enum=url;name |
| 150 | + // +kubebuilder:validation:Required |
| 151 | + Type string `json:"type,omitempty"` |
| 152 | + |
| 153 | + // URL is the URL of the DataPlane target. This is used for configuring |
| 154 | + // externally managed DataPlanes like those installed independently with Helm. |
| 155 | + // |
| 156 | + // +optional |
| 157 | + URL string `json:"url,omitempty"` |
| 158 | + |
| 159 | + // Name is the name of the DataPlane to configure. |
| 160 | + // |
| 161 | + // +optional |
| 162 | + Name string `json:"name,omitempty"` |
| 163 | +} |
| 164 | + |
| 165 | +// ControlPlaneAdminAPI defines the configuration for the Kong Admin API that |
| 166 | +// a ControlPlane when configuring the DataPlane. |
| 167 | +type ControlPlaneAdminAPI struct { |
| 168 | + // Workspace indicates the Kong Workspace to use for the ControlPlane. |
| 169 | + // If left empty then no Kong workspace will be used. |
| 170 | + // |
| 171 | + // +optional |
| 172 | + Workspace string `json:"workspace,omitempty"` |
| 173 | +} |
| 174 | + |
| 175 | +// ControlPlaneController defines a controller state for the ControlPlane. |
| 176 | +// It overrides the default behavior as defined in the deployed operator version. |
| 177 | +// |
| 178 | +// +apireference:kgo:include |
| 179 | +type ControlPlaneController struct { |
| 180 | + // Name is the name of the controller. |
| 181 | + // |
| 182 | + // +kubebuilder:validation:Required |
| 183 | + // +kubebuilder:validation:MinLength=1 |
| 184 | + Name string `json:"name,omitempty"` |
| 185 | + |
| 186 | + // Enabled indicates whether the controller is enabled or not. |
| 187 | + // |
| 188 | + // +kubebuilder:validation:Required |
| 189 | + Enabled *bool `json:"enabled,omitempty"` |
| 190 | +} |
| 191 | + |
| 192 | +// ControlPlaneFeatureGate defines a feature gate state for the ControlPlane. |
| 193 | +// It overrides the default behavior as defined in the deployed operator version. |
| 194 | +// |
| 195 | +// +apireference:kgo:include |
| 196 | +type ControlPlaneFeatureGate struct { |
| 197 | + // Name is the name of the feature gate. |
| 198 | + // |
| 199 | + // +kubebuilder:validation:Required |
| 200 | + // +kubebuilder:validation:MinLength=1 |
| 201 | + Name string `json:"name,omitempty"` |
| 202 | + |
| 203 | + // Enabled indicates whether the feature gate is enabled or not. |
| 204 | + // |
| 205 | + // +kubebuilder:validation:Required |
| 206 | + Enabled *bool `json:"enabled,omitempty"` |
| 207 | +} |
| 208 | + |
| 209 | +// ControlPlaneStatus defines the observed state of ControlPlane |
| 210 | +// |
| 211 | +// +apireference:kgo:include |
| 212 | +type ControlPlaneStatus struct { |
| 213 | + // Conditions describe the current conditions of the Gateway. |
| 214 | + // |
| 215 | + // +optional |
| 216 | + // +listType=map |
| 217 | + // +listMapKey=type |
| 218 | + // +kubebuilder:validation:MaxItems=8 |
| 219 | + // +kubebuilder:default={{type: "Scheduled", status: "Unknown", reason:"NotReconciled", message:"Waiting for controller", lastTransitionTime: "1970-01-01T00:00:00Z"}} |
| 220 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 221 | +} |
| 222 | + |
| 223 | +// GetConditions returns the ControlPlane Status Conditions |
| 224 | +func (c *ControlPlane) GetConditions() []metav1.Condition { |
| 225 | + return c.Status.Conditions |
| 226 | +} |
| 227 | + |
| 228 | +// SetConditions sets the ControlPlane Status Conditions |
| 229 | +func (c *ControlPlane) SetConditions(conditions []metav1.Condition) { |
| 230 | + c.Status.Conditions = conditions |
| 231 | +} |
| 232 | + |
| 233 | +// GetExtensions retrieves the ControlPlane Extensions |
| 234 | +func (c *ControlPlane) GetExtensions() []commonv1alpha1.ExtensionRef { |
| 235 | + return c.Spec.Extensions |
| 236 | +} |
0 commit comments