mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-18 17:00:56 +02:00
feat: tag EBS volumes from PostgreSQL CR annotations
Adds a configurable mapping between PostgreSQL CR annotations and EBS
volume tags. The operator reads the configured annotation keys from the
CR metadata and applies them as tags on the associated EBS volumes during
each sync cycle.
Tags are compared against existing EBS tags (extracted from DescribeVolumes,
which is already called for volume management) and CreateTags is only called
when a tag is missing or has a different value, avoiding unnecessary AWS API
calls on steady state.
Configuration example in the operator ConfigMap/OperatorConfiguration:
aws_or_gcp:
ebs_volume_tags_from_annotations:
application: zalando.org/owning-application
team: zalando.org/team
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: tcondeixa <tscondeixa@gmail.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
df3224730f
commit
eba55e124f
+40
-2
@@ -89,11 +89,18 @@ func (r *EBSVolumeResizer) DescribeVolumes(volumeIds []string) ([]VolumeProperti
|
||||
}
|
||||
|
||||
for _, v := range volumeOutput.Volumes {
|
||||
tags := make(map[string]string)
|
||||
for _, tag := range v.Tags {
|
||||
if tag.Key != nil && tag.Value != nil {
|
||||
tags[*tag.Key] = *tag.Value
|
||||
}
|
||||
}
|
||||
|
||||
switch v.VolumeType {
|
||||
case "gp3":
|
||||
p = append(p, VolumeProperties{VolumeID: *v.VolumeId, Size: int64(*v.Size), VolumeType: string(v.VolumeType), Iops: int64(*v.Iops), Throughput: int64(*v.Throughput)})
|
||||
p = append(p, VolumeProperties{VolumeID: *v.VolumeId, Size: int64(*v.Size), VolumeType: string(v.VolumeType), Iops: int64(*v.Iops), Throughput: int64(*v.Throughput), Tags: tags})
|
||||
case "gp2":
|
||||
p = append(p, VolumeProperties{VolumeID: *v.VolumeId, Size: int64(*v.Size), VolumeType: string(v.VolumeType)})
|
||||
p = append(p, VolumeProperties{VolumeID: *v.VolumeId, Size: int64(*v.Size), VolumeType: string(v.VolumeType), Tags: tags})
|
||||
default:
|
||||
return nil, fmt.Errorf("discovered unexpected volume type %s %s", *v.VolumeId, v.VolumeType)
|
||||
}
|
||||
@@ -206,6 +213,37 @@ func (r *EBSVolumeResizer) ModifyVolume(volumeID string, newType *string, newSiz
|
||||
})
|
||||
}
|
||||
|
||||
// TagVolumes tags the given EBS volumes with the provided tags.
|
||||
// Callers are responsible for filtering out volumes that already have the desired tags.
|
||||
func (r *EBSVolumeResizer) TagVolumes(volumeIds []string, tags map[string]string) error {
|
||||
if !r.IsConnectedToProvider() {
|
||||
err := r.ConnectToProvider()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(volumeIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ec2Tags := make([]types.Tag, 0, len(tags))
|
||||
for key, value := range tags {
|
||||
ec2Tags = append(ec2Tags, types.Tag{Key: &key, Value: &value})
|
||||
}
|
||||
|
||||
input := &ec2.CreateTagsInput{
|
||||
Resources: volumeIds,
|
||||
Tags: ec2Tags,
|
||||
}
|
||||
|
||||
_, err := r.connection.CreateTags(context.TODO(), input)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not tag EBS volumes: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DisconnectFromProvider closes connection to the EC2 instance
|
||||
func (r *EBSVolumeResizer) DisconnectFromProvider() error {
|
||||
r.connection = nil
|
||||
|
||||
Reference in New Issue
Block a user