Skip to content

Commit e2dafac

Browse files
Merge pull request #8217 from hamzy/PowerVS-fix-sg-rules
no-jira: PowerVS: Set VPC service region
2 parents 4eb0837 + ec34234 commit e2dafac

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

pkg/infrastructure/powervs/clusterapi/powervs.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/IBM/vpc-go-sdk/vpcv1"
1313
"github.com/sirupsen/logrus"
14+
"k8s.io/apimachinery/pkg/util/sets"
1415
"k8s.io/apimachinery/pkg/util/wait"
1516
"k8s.io/utils/ptr"
1617
capibm "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
@@ -53,12 +54,12 @@ func leftInContext(ctx context.Context) time.Duration {
5354
func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput) error {
5455
var (
5556
client *powervsconfig.Client
57+
vpcRegion string
5658
instanceCRN string
5759
rules *vpcv1.SecurityGroupRuleCollection
5860
rule *vpcv1.SecurityGroupRulePrototype
59-
found = false
60-
ports = [...]int64{22, 10258, 22623}
61-
port int64
61+
wantedPorts = sets.New[int64](22, 10258, 22623)
62+
foundPorts = sets.Set[int64]{}
6263
err error
6364
)
6465

@@ -79,6 +80,10 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
7980
}
8081
logrus.Debugf("InfraReady: powerVSCluster = %+v", powerVSCluster)
8182
logrus.Debugf("InfraReady: powerVSCluster.Status = %+v", powerVSCluster.Status)
83+
if powerVSCluster.Status.VPC == nil || powerVSCluster.Status.VPC.ID == nil {
84+
return fmt.Errorf("vpc is empty in InfraReady?")
85+
}
86+
logrus.Debugf("InfraReady: powerVSCluster.Status.VPC.ID = %s", *powerVSCluster.Status.VPC.ID)
8287

8388
// Get the image from the provider
8489
key = crclient.ObjectKey{
@@ -100,6 +105,19 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
100105
}
101106
logrus.Debugf("InfraReady: NewClient returns %+v", client)
102107

108+
// We need to set the region we will eventually query inside
109+
vpcRegion = in.InstallConfig.Config.Platform.PowerVS.VPCRegion
110+
if vpcRegion == "" {
111+
vpcRegion, err = powervstypes.VPCRegionForPowerVSRegion(in.InstallConfig.Config.Platform.PowerVS.Region)
112+
if err != nil {
113+
return fmt.Errorf("failed to get VPC region (%s) in InfraReady: %w", vpcRegion, err)
114+
}
115+
}
116+
logrus.Debugf("InfraReady: vpcRegion = %s", vpcRegion)
117+
if err = client.SetVPCServiceURLForRegion(ctx, vpcRegion); err != nil {
118+
return fmt.Errorf("failed to set the VPC service region (%s) in InfraReady: %w", vpcRegion, err)
119+
}
120+
103121
// Step 1.
104122
// Create DNS records for the two load balancers
105123
// map[string]VPCLoadBalancerStatus
@@ -177,7 +195,7 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
177195
}
178196

179197
// Step 2.
180-
// See if port 6443 is already allowed.
198+
// See which ports are already allowed.
181199
rules, err = client.ListSecurityGroupRules(ctx, *powerVSCluster.Status.VPC.ID)
182200
if err != nil {
183201
return fmt.Errorf("failed to list security group rules: %w", err)
@@ -197,17 +215,19 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
197215
*securityGroupRule.PortMin,
198216
*securityGroupRule.PortMax)
199217
if *securityGroupRule.Direction == "inbound" &&
200-
*securityGroupRule.Protocol == "tcp" &&
201-
*securityGroupRule.PortMin == 6443 {
202-
found = true
218+
*securityGroupRule.Protocol == "tcp" {
219+
foundPorts.Insert(*securityGroupRule.PortMin)
203220
}
204221
case "*vpcv1.SecurityGroupRuleSecurityGroupRuleProtocolIcmp":
205222
}
206223
}
224+
logrus.Debugf("InfraReady: foundPorts = %+v", foundPorts)
225+
logrus.Debugf("InfraReady: wantedPorts = %+v", wantedPorts)
226+
logrus.Debugf("InfraReady: wantedPorts.Difference(foundPorts) = %+v", wantedPorts.Difference(foundPorts))
207227

208228
// Step 3.
209229
// Add to security group rules
210-
for _, port = range ports {
230+
for port := range wantedPorts.Difference(foundPorts) {
211231
rule = &vpcv1.SecurityGroupRulePrototype{
212232
Direction: ptr.To("inbound"),
213233
Protocol: ptr.To("tcp"),
@@ -234,35 +254,8 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
234254
return fmt.Errorf("failed to add security group rule for port %d: %w", port, err)
235255
}
236256
}
237-
if !found {
238-
port = 6443
239-
rule = &vpcv1.SecurityGroupRulePrototype{
240-
Direction: ptr.To("inbound"),
241-
Protocol: ptr.To("tcp"),
242-
PortMin: ptr.To(port),
243-
PortMax: ptr.To(port),
244-
}
245-
246-
backoff := wait.Backoff{
247-
Duration: 15 * time.Second,
248-
Factor: 1.1,
249-
Cap: leftInContext(ctx),
250-
Steps: math.MaxInt32}
251-
err = wait.ExponentialBackoffWithContext(ctx, backoff, func(context.Context) (bool, error) {
252-
logrus.Debugf("InfraReady: Adding port %d to security group rule to %v",
253-
port,
254-
*powerVSCluster.Status.VPC.ID)
255-
err2 := client.AddSecurityGroupRule(ctx, *powerVSCluster.Status.VPC.ID, rule)
256-
if err == nil {
257-
return true, nil
258-
}
259-
return false, err2
260-
})
261-
if err != nil {
262-
return fmt.Errorf("failed to add security group rule for port %d: %w", port, err)
263-
}
264-
}
265257

258+
// Allow ping so we can debug
266259
rule = &vpcv1.SecurityGroupRulePrototype{
267260
Direction: ptr.To("inbound"),
268261
Protocol: ptr.To("icmp"),

0 commit comments

Comments
 (0)