delete the tags defined from inherit that are not in the labels anymore

This commit is contained in:
tcondeixa
2026-06-29 17:42:30 +02:00
parent 0fe4eaac6b
commit 3cf121b72d
5 changed files with 63 additions and 14 deletions
+2 -2
View File
@@ -69,8 +69,8 @@ $(GENERATED_CRDS): $(GENERATED)
go tool controller-gen crd:crdVersions=v1,allowDangerousTypes=true paths=./pkg/apis/acid.zalan.do/... output:crd:dir=manifests
@mv manifests/acid.zalan.do_postgresqls.yaml manifests/postgresql.crd.yaml
@# hack to use lowercase kind and listKind
@sed -i -e 's/kind: Postgresql/kind: postgresql/' manifests/postgresql.crd.yaml
@sed -i -e 's/listKind: PostgresqlList/listKind: postgresqlList/' manifests/postgresql.crd.yaml
@sed -i.bak 's/kind: Postgresql/kind: postgresql/' manifests/postgresql.crd.yaml && rm manifests/postgresql.crd.yaml.bak
@sed -i.bak 's/listKind: PostgresqlList/listKind: postgresqlList/' manifests/postgresql.crd.yaml && rm manifests/postgresql.crd.yaml.bak
@hack/adjust_postgresql_crd.sh
@mv manifests/acid.zalan.do_operatorconfigurations.yaml manifests/operatorconfiguration.crd.yaml
@mv manifests/acid.zalan.do_postgresteams.yaml manifests/postgresteam.crd.yaml
+2 -2
View File
@@ -13,12 +13,12 @@
file="${1:-"manifests/postgresql.crd.yaml"}"
sed -i '/^[[:space:]]*standby:$/{
sed -i '' '/^[[:space:]]*standby:$/{
# Capture the indentation
s/^\([[:space:]]*\)standby:$/\1standby:\n\1 anyOf:\n\1 - required:\n\1 - s3_wal_path\n\1 - required:\n\1 - gs_wal_path\n\1 - required:\n\1 - standby_host\n\1 not:\n\1 required:\n\1 - s3_wal_path\n\1 - gs_wal_path/
}' "$file"
sed -i '/^[[:space:]]*maintenanceWindows:$/{
sed -i '' '/^[[:space:]]*maintenanceWindows:$/{
# Capture the indentation
s/^\([[:space:]]*\)maintenanceWindows:$/\1maintenanceWindows:\n\1 items:\n\1 pattern: '\''^\\ *((Mon|Tue|Wed|Thu|Fri|Sat|Sun):(2[0-3]|[01]?\\d):([0-5]?\\d)|(2[0-3]|[01]?\\d):([0-5]?\\d))-((2[0-3]|[01]?\\d):([0-5]?\\d)|(2[0-3]|[01]?\\d):([0-5]?\\d))\\ *$'\''\n\1 type: string/
}' "$file"
+30 -10
View File
@@ -532,19 +532,25 @@ func (c *Cluster) tagEBSVolumes() error {
desiredTags[labelKey] = labelValue
}
if len(desiredTags) == 0 {
c.logger.Debugf("no tags to apply from configured labels")
return nil
}
volumesToTag := make([]string, 0, len(c.EBSVolumes))
volumesToUntag := make([]string, 0, len(c.EBSVolumes))
staleKeys := make(map[string]bool)
for volumeID, volumeProps := range c.EBSVolumes {
if c.tagsNeedUpdate(volumeProps.Tags, desiredTags) {
if len(desiredTags) > 0 && c.tagsNeedUpdate(volumeProps.Tags, desiredTags) {
volumesToTag = append(volumesToTag, volumeID)
}
for _, labelKey := range c.OpConfig.EBSTagsInheritLabels {
if _, wanted := desiredTags[labelKey]; !wanted {
if _, exists := volumeProps.Tags[labelKey]; exists {
volumesToUntag = append(volumesToUntag, volumeID)
staleKeys[labelKey] = true
}
}
}
}
if len(volumesToTag) == 0 {
if len(volumesToTag) == 0 && len(volumesToUntag) == 0 {
c.logger.Debugf("all EBS volumes already have the desired tags")
return nil
}
@@ -560,11 +566,25 @@ func (c *Cluster) tagEBSVolumes() error {
}()
}
if err := c.VolumeResizer.TagVolumes(volumesToTag, desiredTags); err != nil {
return fmt.Errorf("could not tag EBS volumes: %v", err)
if len(volumesToTag) > 0 {
if err := c.VolumeResizer.TagVolumes(volumesToTag, desiredTags); err != nil {
return fmt.Errorf("could not tag EBS volumes: %v", err)
}
c.logger.Infof("successfully tagged %d EBS volumes with labels: %v", len(volumesToTag), desiredTags)
}
if len(volumesToUntag) > 0 {
keysToDelete := make([]string, 0, len(staleKeys))
for k := range staleKeys {
keysToDelete = append(keysToDelete, k)
}
if err := c.VolumeResizer.UntagVolumes(volumesToUntag, keysToDelete); err != nil {
c.logger.Warningf("could not remove stale EBS tags %v: %v", keysToDelete, err)
} else {
c.logger.Infof("removed stale tags %v from %d EBS volumes", keysToDelete, len(volumesToUntag))
}
}
c.logger.Infof("successfully tagged %d EBS volumes with labels: %v", len(volumesToTag), desiredTags)
return nil
}
+28
View File
@@ -241,6 +241,34 @@ func (r *EBSVolumeResizer) TagVolumes(volumeIds []string, tags map[string]string
return nil
}
// UntagVolumes removes the given tag keys from the provided EBS volumes.
func (r *EBSVolumeResizer) UntagVolumes(volumeIds []string, tagKeys []string) error {
if !r.IsConnectedToProvider() {
if err := r.ConnectToProvider(); err != nil {
return err
}
}
if len(volumeIds) == 0 || len(tagKeys) == 0 {
return nil
}
ec2Tags := make([]types.Tag, 0, len(tagKeys))
for _, key := range tagKeys {
k := key
ec2Tags = append(ec2Tags, types.Tag{Key: &k})
}
_, err := r.connection.DeleteTags(context.TODO(), &ec2.DeleteTagsInput{
Resources: volumeIds,
Tags: ec2Tags,
})
if err != nil {
return fmt.Errorf("could not untag EBS volumes: %v", err)
}
return nil
}
// DisconnectFromProvider closes connection to the EC2 instance
func (r *EBSVolumeResizer) DisconnectFromProvider() error {
r.connection = nil
+1
View File
@@ -26,4 +26,5 @@ type VolumeResizer interface {
DisconnectFromProvider() error
DescribeVolumes(providerVolumesID []string) ([]VolumeProperties, error)
TagVolumes(providerVolumesID []string, tags map[string]string) error
UntagVolumes(providerVolumesID []string, tagKeys []string) error
}