-
Notifications
You must be signed in to change notification settings - Fork 22
feat(backup): add EtcdBackup CRD for declarative etcd snapshots #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IvanHunters
wants to merge
35
commits into
aenix-io:main
Choose a base branch
from
IvanHunters:feat/etcdbackup-crd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2c34075
feat(api): add EtcdBackup CRD types
969fb81
feat(api): add EtcdBackup validation webhook
7342b87
feat(backup): add backup-agent binary
9f1373c
feat(factory): add backup Job factory
ad42895
feat(controller): add EtcdBackup reconciler
1854d3a
feat(manager): register EtcdBackup controller and webhook
f4adfd6
chore(config): add EtcdBackup CRD manifests, RBAC, and webhook config
56ab880
build: add backup-agent to Dockerfile and Makefile
d66b93d
feat(helm): add EtcdBackup support to Helm chart
bf727ec
fix(backup): address PR review feedback
a899205
feat(backup): add EtcdBackupSchedule CRD for recurring backups
f28c683
fix(backup): address review findings in backup subsystem
329f2d5
feat(bootstrap): add cluster restoration from backup snapshot
7d5b58c
fix: address review findings
3b08271
fix: address second review findings
d198a3d
fix: address third review findings
c3aac43
fix: address fourth review findings
12498f8
fix: address fifth review findings
2c4afc3
fix: skip name length check on update, add missing tests
9c9f26f
fix: add backup name length validation, restrict file permissions, us…
7ae4fd1
fix: add resource limits, restrict dir permissions, validate S3 endpoint
f1132e4
fix: use typed condition constant, requeue on ClusterNotFound
3b20c52
fix: add pod SecurityContext and set -e in restore script
cf69df2
fix: address maintainer review feedback
0406d8c
chore: replace reflect.DeepEqual with semantic.DeepEqual
BROngineer 516d5d6
tests: verify cronjob updated on schedule update
BROngineer 5bf0723
chore: add finished job ttl and active job deadline fields
BROngineer 54658c5
chore: add validation for cron schedule
BROngineer a838863
chore: fix linting
BROngineer bc283b0
chore: revert adding fields to crds
BROngineer f671c51
chore: add context timeout to restore op
BROngineer 4ee52ff
chore: consistent backups naming
BROngineer 58a936e
chore: add indexer and watch for etcd cluster to schedule controller
BROngineer 2657153
chore: fix nilaway false-positive fail in tests
BROngineer 3714779
chore: generate all
BROngineer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| Copyright 2024 The etcd-operator Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| EtcdBackupConditionStarted = "Started" | ||
| EtcdBackupConditionComplete = "Complete" | ||
| EtcdBackupConditionFailed = "Failed" | ||
| ) | ||
|
|
||
| // EtcdBackupSpec defines the desired state of EtcdBackup | ||
| type EtcdBackupSpec struct { | ||
| // ClusterRef references the EtcdCluster to back up. | ||
| ClusterRef corev1.LocalObjectReference `json:"clusterRef"` | ||
| // Destination defines where the backup will be stored. | ||
| Destination BackupDestination `json:"destination"` | ||
| } | ||
|
|
||
| // BackupDestination defines the target location for the backup. Exactly one must be specified. | ||
| type BackupDestination struct { | ||
| // S3 defines S3-compatible storage as the backup destination. | ||
| // +optional | ||
| S3 *S3BackupDestination `json:"s3,omitempty"` | ||
| // PVC defines a PersistentVolumeClaim as the backup destination. | ||
| // +optional | ||
| PVC *PVCBackupDestination `json:"pvc,omitempty"` | ||
| } | ||
|
|
||
| // S3BackupDestination defines S3-compatible storage parameters. | ||
| type S3BackupDestination struct { | ||
| // Endpoint is the S3-compatible endpoint URL (e.g., "https://s3.amazonaws.com"). | ||
| Endpoint string `json:"endpoint"` | ||
| // Bucket is the name of the S3 bucket. | ||
| Bucket string `json:"bucket"` | ||
| // Key is the key prefix (directory path) within the bucket. | ||
| // The operator appends the backup filename automatically. | ||
| // +optional | ||
| Key string `json:"key,omitempty"` | ||
| // CredentialsSecretRef references a Secret containing AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY keys. | ||
| CredentialsSecretRef corev1.LocalObjectReference `json:"credentialsSecretRef"` | ||
| // Region is the AWS region for the S3 bucket. | ||
| // +optional | ||
| Region string `json:"region,omitempty"` | ||
| // ForcePathStyle forces path-style S3 URLs (e.g., endpoint/bucket/key) | ||
| // instead of virtual-hosted-style (e.g., bucket.endpoint/key). | ||
| // Most S3-compatible providers (MinIO, Ceph) require path style. | ||
| // +optional | ||
| ForcePathStyle bool `json:"forcePathStyle,omitempty"` | ||
| } | ||
|
|
||
| // PVCBackupDestination defines a PersistentVolumeClaim as the backup target. | ||
| type PVCBackupDestination struct { | ||
| // ClaimName is the name of the PersistentVolumeClaim to use. | ||
| ClaimName string `json:"claimName"` | ||
| // SubPath is an optional sub-directory within the PVC volume. | ||
| // The operator appends the backup filename automatically. | ||
| // +optional | ||
| SubPath string `json:"subPath,omitempty"` | ||
| } | ||
|
|
||
| // EtcdBackupStatus defines the observed state of EtcdBackup | ||
| type EtcdBackupStatus struct { | ||
| Conditions []metav1.Condition `json:"conditions,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:root=true | ||
| // +kubebuilder:subresource:status | ||
| // +kubebuilder:printcolumn:name="Cluster",type=string,JSONPath=`.spec.clusterRef.name` | ||
| // +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.conditions[?(@.type=="Complete")].status` | ||
| // +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` | ||
|
|
||
| // EtcdBackup is the Schema for the etcdbackups API | ||
| type EtcdBackup struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
|
||
| Spec EtcdBackupSpec `json:"spec,omitempty"` | ||
| Status EtcdBackupStatus `json:"status,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:root=true | ||
|
|
||
| // EtcdBackupList contains a list of EtcdBackup | ||
| type EtcdBackupList struct { | ||
| metav1.TypeMeta `json:",inline"` | ||
| metav1.ListMeta `json:"metadata,omitempty"` | ||
| Items []EtcdBackup `json:"items"` | ||
| } | ||
|
|
||
| func init() { | ||
| SchemeBuilder.Register(&EtcdBackup{}, &EtcdBackupList{}) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| Copyright 2024 The etcd-operator Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/equality" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/util/validation/field" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook" | ||
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
| ) | ||
|
|
||
| var etcdbackuplog = logf.Log.WithName("etcdbackup-resource") | ||
|
|
||
| // SetupWebhookWithManager will setup the manager to manage the webhooks | ||
| func (r *EtcdBackup) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
| return ctrl.NewWebhookManagedBy(mgr). | ||
| For(r). | ||
| Complete() | ||
| } | ||
|
|
||
| // +kubebuilder:webhook:path=/validate-etcd-aenix-io-v1alpha1-etcdbackup,mutating=false,failurePolicy=fail,sideEffects=None,groups=etcd.aenix.io,resources=etcdbackups,verbs=create;update,versions=v1alpha1,name=vetcdbackup.kb.io,admissionReviewVersions=v1 | ||
|
|
||
| var _ webhook.Validator = &EtcdBackup{} | ||
|
|
||
| // ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
| func (r *EtcdBackup) ValidateCreate() (admission.Warnings, error) { | ||
| etcdbackuplog.Info("validate create", "name", r.Name) | ||
|
|
||
| var allErrors field.ErrorList | ||
|
|
||
| // Job name = "{name}-backup" (7 char suffix). | ||
| // Job names must be <= 63 chars (DNS label limit). | ||
| const jobSuffix = "-backup" | ||
| const maxJobNameLen = 63 | ||
| maxNameLen := maxJobNameLen - len(jobSuffix) | ||
| if len(r.Name) > maxNameLen { | ||
| allErrors = append(allErrors, field.Invalid( | ||
| field.NewPath("metadata", "name"), | ||
| r.Name, | ||
| fmt.Sprintf("name must be at most %d characters (Job name limit is %d, suffix %q is %d characters)", | ||
| maxNameLen, maxJobNameLen, jobSuffix, len(jobSuffix)), | ||
| )) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if r.Spec.ClusterRef.Name == "" { | ||
| allErrors = append(allErrors, field.Required( | ||
| field.NewPath("spec", "clusterRef", "name"), | ||
| "clusterRef.name is required", | ||
| )) | ||
| } | ||
|
|
||
| destErrors := r.validateDestination() | ||
| allErrors = append(allErrors, destErrors...) | ||
|
|
||
| if len(allErrors) > 0 { | ||
| return nil, errors.NewInvalid( | ||
| schema.GroupKind{Group: GroupVersion.Group, Kind: "EtcdBackup"}, | ||
| r.Name, allErrors) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
| func (r *EtcdBackup) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { | ||
| etcdbackuplog.Info("validate update", "name", r.Name) | ||
|
|
||
| oldBackup, ok := old.(*EtcdBackup) | ||
| if !ok { | ||
| return nil, fmt.Errorf("expected EtcdBackup but got %T", old) | ||
| } | ||
|
|
||
| if !equality.Semantic.DeepEqual(r.Spec, oldBackup.Spec) { | ||
| var allErrors field.ErrorList | ||
| allErrors = append(allErrors, field.Forbidden( | ||
| field.NewPath("spec"), | ||
| "EtcdBackup spec is immutable", | ||
| )) | ||
| return nil, errors.NewInvalid( | ||
| schema.GroupKind{Group: GroupVersion.Group, Kind: "EtcdBackup"}, | ||
| r.Name, allErrors) | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| // ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
| func (r *EtcdBackup) ValidateDelete() (admission.Warnings, error) { | ||
| etcdbackuplog.Info("validate delete", "name", r.Name) | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (r *EtcdBackup) validateDestination() field.ErrorList { | ||
| return validateBackupDestination(r.Spec.Destination, field.NewPath("spec", "destination")) | ||
| } | ||
|
|
||
| // validateBackupDestination validates a BackupDestination at the given field path. | ||
| // This is shared between EtcdBackup and EtcdCluster (bootstrap restore source) webhooks. | ||
| func validateBackupDestination(dest BackupDestination, destPath *field.Path) field.ErrorList { | ||
| var allErrors field.ErrorList | ||
|
|
||
| if dest.S3 == nil && dest.PVC == nil { | ||
| allErrors = append(allErrors, field.Required( | ||
| destPath, | ||
| "exactly one of s3 or pvc must be specified", | ||
| )) | ||
| return allErrors | ||
| } | ||
|
|
||
| if dest.S3 != nil && dest.PVC != nil { | ||
| allErrors = append(allErrors, field.Invalid( | ||
| destPath, | ||
| "both s3 and pvc", | ||
| "exactly one of s3 or pvc must be specified, not both", | ||
| )) | ||
| return allErrors | ||
| } | ||
|
|
||
| if s3 := dest.S3; s3 != nil { | ||
| s3Path := destPath.Child("s3") | ||
| if s3.Endpoint == "" { | ||
| allErrors = append(allErrors, field.Required(s3Path.Child("endpoint"), "endpoint is required")) | ||
| } else if !strings.HasPrefix(s3.Endpoint, "http://") && !strings.HasPrefix(s3.Endpoint, "https://") { | ||
| allErrors = append(allErrors, field.Invalid(s3Path.Child("endpoint"), s3.Endpoint, | ||
| "endpoint must start with http:// or https://")) | ||
| } | ||
| if s3.Bucket == "" { | ||
| allErrors = append(allErrors, field.Required(s3Path.Child("bucket"), "bucket is required")) | ||
| } | ||
| if s3.CredentialsSecretRef.Name == "" { | ||
| allErrors = append(allErrors, field.Required(s3Path.Child("credentialsSecretRef", "name"), "credentialsSecretRef.name is required")) | ||
| } | ||
| } | ||
|
|
||
| if pvc := dest.PVC; pvc != nil { | ||
| pvcPath := destPath.Child("pvc") | ||
| if pvc.ClaimName == "" { | ||
| allErrors = append(allErrors, field.Required(pvcPath.Child("claimName"), "claimName is required")) | ||
| } | ||
| if pvc.SubPath != "" { | ||
| cleaned := filepath.Clean(pvc.SubPath) | ||
| if strings.HasPrefix(cleaned, "..") || filepath.IsAbs(cleaned) { | ||
| allErrors = append(allErrors, field.Invalid( | ||
| pvcPath.Child("subPath"), pvc.SubPath, | ||
| "subPath must be a relative path and must not contain '..' components", | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return allErrors | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.