|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// BackupLabelingPolicy controls whether backup labeling is active for a resource type |
| 25 | +// +kubebuilder:validation:Enum=enabled;disabled |
| 26 | +type BackupLabelingPolicy string |
| 27 | + |
| 28 | +const ( |
| 29 | + // BackupLabelingEnabled enables backup labeling for the resource type |
| 30 | + BackupLabelingEnabled BackupLabelingPolicy = "enabled" |
| 31 | + // BackupLabelingDisabled disables backup labeling for the resource type |
| 32 | + BackupLabelingDisabled BackupLabelingPolicy = "disabled" |
| 33 | +) |
| 34 | + |
| 35 | +// OpenStackBackupConfigSpec defines the desired state of OpenStackBackupConfig. |
| 36 | +type OpenStackBackupConfigSpec struct { |
| 37 | + // DefaultRestoreOrder is the restore order assigned to user-provided resources |
| 38 | + // +kubebuilder:validation:Optional |
| 39 | + // +kubebuilder:default="10" |
| 40 | + DefaultRestoreOrder string `json:"defaultRestoreOrder"` |
| 41 | + |
| 42 | + // Secrets configuration for backup labeling |
| 43 | + // +kubebuilder:validation:Optional |
| 44 | + // +kubebuilder:default={labeling:enabled} |
| 45 | + Secrets ResourceBackupConfig `json:"secrets"` |
| 46 | + |
| 47 | + // ConfigMaps configuration for backup labeling |
| 48 | + // Defaults: Excludes kube-root-ca.crt and openshift-service-ca.crt |
| 49 | + // +kubebuilder:validation:Optional |
| 50 | + // +kubebuilder:default={labeling:enabled,excludeNames:{"kube-root-ca.crt","openshift-service-ca.crt"}} |
| 51 | + ConfigMaps ResourceBackupConfig `json:"configMaps"` |
| 52 | + |
| 53 | + // NetworkAttachmentDefinitions configuration for backup labeling |
| 54 | + // +kubebuilder:validation:Optional |
| 55 | + // +kubebuilder:default={labeling:enabled} |
| 56 | + NetworkAttachmentDefinitions ResourceBackupConfig `json:"networkAttachmentDefinitions"` |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +// ResourceBackupConfig defines backup labeling rules for a resource type |
| 61 | +type ResourceBackupConfig struct { |
| 62 | + // Labeling controls whether to label this resource type for backup |
| 63 | + // +kubebuilder:validation:Optional |
| 64 | + Labeling *BackupLabelingPolicy `json:"labeling,omitempty"` |
| 65 | + |
| 66 | + // RestoreOrder overrides the default restore order for this resource type. |
| 67 | + // If empty, the global DefaultRestoreOrder is used. |
| 68 | + // +kubebuilder:validation:Optional |
| 69 | + RestoreOrder string `json:"restoreOrder,omitempty"` |
| 70 | + |
| 71 | + // ExcludeLabelKeys is a list of label keys - resources with any of these labels are excluded |
| 72 | + // Example: ["service-cert", "osdp-service"] excludes service-cert and dataplane service secrets |
| 73 | + // +kubebuilder:validation:Optional |
| 74 | + ExcludeLabelKeys []string `json:"excludeLabelKeys,omitempty"` |
| 75 | + |
| 76 | + // ExcludeNames is a list of resource names to exclude from backup labeling |
| 77 | + // Example: ["kube-root-ca.crt", "openshift-service-ca.crt"] for system ConfigMaps |
| 78 | + // +kubebuilder:validation:Optional |
| 79 | + ExcludeNames []string `json:"excludeNames,omitempty"` |
| 80 | + |
| 81 | + // IncludeLabelSelector allows filtering resources by label selector |
| 82 | + // Only resources matching this selector will be labeled (in addition to ownerRef check) |
| 83 | + // +kubebuilder:validation:Optional |
| 84 | + IncludeLabelSelector map[string]string `json:"includeLabelSelector,omitempty"` |
| 85 | +} |
| 86 | + |
| 87 | +// OpenStackBackupConfigStatus defines the observed state of OpenStackBackupConfig. |
| 88 | +type OpenStackBackupConfigStatus struct { |
| 89 | + // LabeledResources tracks how many resources of each type were labeled |
| 90 | + // +kubebuilder:validation:Optional |
| 91 | + LabeledResources ResourceCounts `json:"labeledResources,omitempty"` |
| 92 | + |
| 93 | + // Conditions represents the latest available observations of the resource's current state |
| 94 | + // +operator-sdk:csv:customresourcedefinitions:type=status |
| 95 | + Conditions condition.Conditions `json:"conditions,omitempty"` |
| 96 | +} |
| 97 | + |
| 98 | +// ResourceCounts tracks labeled resource counts by type |
| 99 | +type ResourceCounts struct { |
| 100 | + // Secrets is the number of secrets labeled for backup |
| 101 | + // +kubebuilder:validation:Optional |
| 102 | + Secrets int `json:"secrets"` |
| 103 | + |
| 104 | + // ConfigMaps is the number of configmaps labeled for backup |
| 105 | + // +kubebuilder:validation:Optional |
| 106 | + ConfigMaps int `json:"configMaps"` |
| 107 | + |
| 108 | + // NetworkAttachmentDefinitions is the number of NADs labeled for backup |
| 109 | + // +kubebuilder:validation:Optional |
| 110 | + NetworkAttachmentDefinitions int `json:"networkAttachmentDefinitions"` |
| 111 | + |
| 112 | + // CRs is the number of CR instances labeled for backup |
| 113 | + // +kubebuilder:validation:Optional |
| 114 | + CRs int `json:"crs"` |
| 115 | +} |
| 116 | + |
| 117 | +// +kubebuilder:object:root=true |
| 118 | +// +kubebuilder:subresource:status |
| 119 | +// +kubebuilder:resource:shortName=osbkpcfg;osbackupcfg;osbackupconfig |
| 120 | +// +kubebuilder:printcolumn:name="Secrets",type="integer",JSONPath=".status.labeledResources.secrets",description="Labeled Secrets" |
| 121 | +// +kubebuilder:printcolumn:name="ConfigMaps",type="integer",JSONPath=".status.labeledResources.configMaps",description="Labeled ConfigMaps" |
| 122 | +// +kubebuilder:printcolumn:name="NADs",type="integer",JSONPath=".status.labeledResources.networkAttachmentDefinitions",description="Labeled NADs" |
| 123 | +// +kubebuilder:printcolumn:name="CRs",type="integer",JSONPath=".status.labeledResources.crs",description="Labeled CR instances" |
| 124 | +// +kubebuilder:metadata:labels=backup.openstack.org/restore=true |
| 125 | +// +kubebuilder:metadata:labels=backup.openstack.org/category=controlplane |
| 126 | +// +kubebuilder:metadata:labels=backup.openstack.org/restore-order=20 |
| 127 | + |
| 128 | +// OpenStackBackupConfig is the Schema for the openstackbackupconfigs API. |
| 129 | +// It configures automatic backup labeling for user-provided resources (without ownerReferences). |
| 130 | +type OpenStackBackupConfig struct { |
| 131 | + metav1.TypeMeta `json:",inline"` |
| 132 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 133 | + |
| 134 | + Spec OpenStackBackupConfigSpec `json:"spec,omitempty"` |
| 135 | + Status OpenStackBackupConfigStatus `json:"status,omitempty"` |
| 136 | +} |
| 137 | + |
| 138 | +// +kubebuilder:object:root=true |
| 139 | + |
| 140 | +// OpenStackBackupConfigList contains a list of OpenStackBackupConfig. |
| 141 | +type OpenStackBackupConfigList struct { |
| 142 | + metav1.TypeMeta `json:",inline"` |
| 143 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 144 | + Items []OpenStackBackupConfig `json:"items"` |
| 145 | +} |
| 146 | + |
| 147 | +func init() { |
| 148 | + SchemeBuilder.Register(&OpenStackBackupConfig{}, &OpenStackBackupConfigList{}) |
| 149 | +} |
0 commit comments