generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 12
/
removeMountTargetsFromOtherVpcs.go
82 lines (73 loc) · 2.33 KB
/
removeMountTargetsFromOtherVpcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package nfsinstance
import (
"context"
"fmt"
efsTypes "github.com/aws/aws-sdk-go-v2/service/efs/types"
"github.com/elliotchance/pie/v2"
"github.com/kyma-project/cloud-manager/pkg/composed"
"k8s.io/utils/pointer"
)
// removeMountTargetsFromOtherVpcs is a migration fix for a bug when wrong VPC was choosen to create EFS in.
// If there are more then 3 mount targets, it will load subnet for all of them and delete those from EFS
// that belong to a VPC other than the one specified in the ipRange.status.vpcId
func removeMountTargetsFromOtherVpcs(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)
logger := composed.LoggerFromCtx(ctx)
if len(state.mountTargets) <= 3 {
return nil, nil
}
logger.
WithValues(
"mountTargets",
fmt.Sprintf("%v", pie.Map(state.mountTargets, func(mt efsTypes.MountTargetDescription) string {
mtID := pointer.StringDeref(mt.MountTargetId, "")
return fmt.Sprintf(
"{id:%s, az:%s, ip: %s, sg: %v}",
mtID,
pointer.StringDeref(mt.AvailabilityZoneId, ""),
pointer.StringDeref(mt.IpAddress, ""),
state.mountTargetSecurityGroups[mtID],
)
})),
).
Info("Exploring mount targets to remove")
var mountTargetsToRemove []string
for _, mt := range state.mountTargets {
subnetId := pointer.StringDeref(mt.SubnetId, "")
subnet, err := state.awsClient.DescribeSubnet(ctx, subnetId)
if err != nil {
logger.
WithValues("subnetId", subnetId).
Error(err, "Error describing subnet")
continue
}
if subnet == nil {
logger.
WithValues("subnetId", subnetId).
Info("EFS mount target subnet does not exist")
continue
}
vpcId := pointer.StringDeref(subnet.VpcId, "")
if vpcId != "" && vpcId != state.IpRange().Status.VpcId {
mountTargetsToRemove = append(mountTargetsToRemove, pointer.StringDeref(mt.MountTargetId, ""))
}
}
logger.
WithValues(
"mountTargetsToRemove",
fmt.Sprintf("%v", mountTargetsToRemove),
).
Info("Mount Targets from other VPC to remove")
for _, mtId := range mountTargetsToRemove {
logger.
WithValues("mountTargetId", mtId).
Info("Removing mount target from other VPC")
err := state.awsClient.DeleteMountTarget(ctx, mtId)
if err != nil {
logger.
WithValues("mountTargetId", mtId).
Error(err, "Error removing mount target from other VPC")
}
}
return nil, nil
}