Skip to content

Commit 1fd00fb

Browse files
authored
Merge pull request #1483 from achouhan09/region-fix
Bug-fix for fetching region from AWS cluster obj in GetAWSRegion()
2 parents 88aef43 + 00c3d88 commit 1fd00fb

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

deploy/cluster_role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,11 @@ rules:
183183
- delete
184184
- update
185185
- create
186+
- apiGroups:
187+
- config.openshift.io
188+
resources:
189+
- infrastructures
190+
verbs:
191+
- get
192+
- list
193+
- watch

pkg/bundle/deploy.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package bundle
22

33
const Version = "5.18.0"
44

5-
const Sha256_deploy_cluster_role_yaml = "3f8118853db73926c4f9d14be84ac8f81833c3a7a94a52ecf1e9ebcf712eee93"
5+
const Sha256_deploy_cluster_role_yaml = "31fc622ff7fa66617be3895bddcb6cfdb97883b75b20bdb2bf04052bd14221a8"
66

77
const File_deploy_cluster_role_yaml = `apiVersion: rbac.authorization.k8s.io/v1
88
kind: ClusterRole
@@ -189,6 +189,14 @@ rules:
189189
- delete
190190
- update
191191
- create
192+
- apiGroups:
193+
- config.openshift.io
194+
resources:
195+
- infrastructures
196+
verbs:
197+
- get
198+
- list
199+
- watch
192200
`
193201

194202
const Sha256_deploy_cluster_role_binding_yaml = "15c78355aefdceaf577bd96b4ae949ae424a3febdc8853be0917cf89a63941fc"

pkg/util/util.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
nbapis "github.com/noobaa/noobaa-operator/v5/pkg/apis"
3333
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
3434
"github.com/noobaa/noobaa-operator/v5/pkg/bundle"
35+
configv1 "github.com/openshift/api/config/v1"
3536
routev1 "github.com/openshift/api/route/v1"
3637
secv1 "github.com/openshift/api/security/v1"
3738
cloudcredsv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
@@ -185,6 +186,7 @@ func init() {
185186
Panic(autoscalingv1.AddToScheme(scheme.Scheme))
186187
Panic(kedav1alpha1.AddToScheme(scheme.Scheme))
187188
Panic(apiregistration.AddToScheme(scheme.Scheme))
189+
Panic(configv1.AddToScheme(scheme.Scheme))
188190
}
189191

190192
// KubeConfig loads kubernetes client config from default locations (flags, user dir, etc)
@@ -236,6 +238,7 @@ func MapperProvider(config *rest.Config, httpClient *http.Client) (meta.RESTMapp
236238
g.Name == "autoscaling" ||
237239
g.Name == "batch" ||
238240
g.Name == "keda.sh" ||
241+
g.Name == "config.openshift.io" ||
239242
strings.HasSuffix(g.Name, ".k8s.io") {
240243
return true
241244
}
@@ -1122,9 +1125,11 @@ func GetIBMRegion() (string, error) {
11221125
return region, nil
11231126
}
11241127

1125-
// GetAWSRegion parses the region from a node's name
1128+
// GetAWSRegion determines the AWS region from cluster infrastructure or node name
11261129
func GetAWSRegion() (string, error) {
1127-
// parse the node name to get AWS region according to this:
1130+
// Determine the AWS region based on cluster infrastructure or node name
1131+
// If infrastructure details are unavailable, the node's name is parsed to extract the region
1132+
// Refer to the following for more details:
11281133
// https://docs.aws.amazon.com/en_pv/vpc/latest/userguide/vpc-dns.html#vpc-dns-hostnames
11291134
// The list of regions can be found here:
11301135
// https://docs.aws.amazon.com/general/latest/gr/rande.html
@@ -1164,17 +1169,36 @@ func GetAWSRegion() (string, error) {
11641169
"af-south-1": "af-south-1",
11651170
"il-central-1": "il-central-1",
11661171
}
1167-
nodesList := &corev1.NodeList{}
1168-
if ok := KubeList(nodesList); !ok || len(nodesList.Items) == 0 {
1169-
return "", fmt.Errorf("Failed to list kubernetes nodes")
1172+
var awsRegion string
1173+
infrastructure := &configv1.Infrastructure{
1174+
ObjectMeta: metav1.ObjectMeta{
1175+
Name: "cluster",
1176+
},
1177+
}
1178+
if _, _, err := KubeGet(infrastructure); err != nil {
1179+
log.Infof("Failed to fetch cluster infrastructure details: %v", err)
1180+
}
1181+
if infrastructure.Status.PlatformStatus != nil && infrastructure.Status.PlatformStatus.AWS != nil {
1182+
awsRegion = mapValidAWSRegions[infrastructure.Status.PlatformStatus.AWS.Region]
11701183
}
1171-
nameSplit := strings.Split(nodesList.Items[0].Name, ".")
1172-
if len(nameSplit) < 2 {
1173-
return "", fmt.Errorf("Unexpected node name format: %q", nodesList.Items[0].Name)
1184+
1185+
// Parsing the aws region from node name if not fetched from cluster
1186+
log.Warn("Falling back to parsing the node name as infrastructure details are unavailable or incomplete")
1187+
if awsRegion == "" {
1188+
nodesList := &corev1.NodeList{}
1189+
if ok := KubeList(nodesList); !ok || len(nodesList.Items) == 0 {
1190+
log.Infof("Failed to list kubernetes nodes")
1191+
}
1192+
nameSplit := strings.Split(nodesList.Items[0].Name, ".")
1193+
if len(nameSplit) < 2 {
1194+
log.Infof("Unexpected node name format: %q", nodesList.Items[0].Name)
1195+
}
1196+
awsRegion = mapValidAWSRegions[nameSplit[1]]
11741197
}
1175-
awsRegion := mapValidAWSRegions[nameSplit[1]]
1198+
1199+
// returning error if not fetched from either cluster or node name
11761200
if awsRegion == "" {
1177-
return "", fmt.Errorf("The parsed AWS region is invalid: %q", awsRegion)
1201+
return "", fmt.Errorf("Failed to determine the AWS Region.")
11781202
}
11791203
return awsRegion, nil
11801204
}

0 commit comments

Comments
 (0)