@@ -32,6 +32,7 @@ import (
32
32
nbapis "github.com/noobaa/noobaa-operator/v5/pkg/apis"
33
33
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
34
34
"github.com/noobaa/noobaa-operator/v5/pkg/bundle"
35
+ configv1 "github.com/openshift/api/config/v1"
35
36
routev1 "github.com/openshift/api/route/v1"
36
37
secv1 "github.com/openshift/api/security/v1"
37
38
cloudcredsv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
@@ -185,6 +186,7 @@ func init() {
185
186
Panic (autoscalingv1 .AddToScheme (scheme .Scheme ))
186
187
Panic (kedav1alpha1 .AddToScheme (scheme .Scheme ))
187
188
Panic (apiregistration .AddToScheme (scheme .Scheme ))
189
+ Panic (configv1 .AddToScheme (scheme .Scheme ))
188
190
}
189
191
190
192
// 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
236
238
g .Name == "autoscaling" ||
237
239
g .Name == "batch" ||
238
240
g .Name == "keda.sh" ||
241
+ g .Name == "config.openshift.io" ||
239
242
strings .HasSuffix (g .Name , ".k8s.io" ) {
240
243
return true
241
244
}
@@ -1122,9 +1125,11 @@ func GetIBMRegion() (string, error) {
1122
1125
return region , nil
1123
1126
}
1124
1127
1125
- // GetAWSRegion parses the region from a node's name
1128
+ // GetAWSRegion determines the AWS region from cluster infrastructure or node name
1126
1129
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:
1128
1133
// https://docs.aws.amazon.com/en_pv/vpc/latest/userguide/vpc-dns.html#vpc-dns-hostnames
1129
1134
// The list of regions can be found here:
1130
1135
// https://docs.aws.amazon.com/general/latest/gr/rande.html
@@ -1164,17 +1169,36 @@ func GetAWSRegion() (string, error) {
1164
1169
"af-south-1" : "af-south-1" ,
1165
1170
"il-central-1" : "il-central-1" ,
1166
1171
}
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 ]
1170
1183
}
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 ]]
1174
1197
}
1175
- awsRegion := mapValidAWSRegions [nameSplit [1 ]]
1198
+
1199
+ // returning error if not fetched from either cluster or node name
1176
1200
if awsRegion == "" {
1177
- return "" , fmt .Errorf ("The parsed AWS region is invalid: %q" , awsRegion )
1201
+ return "" , fmt .Errorf ("Failed to determine the AWS Region." )
1178
1202
}
1179
1203
return awsRegion , nil
1180
1204
}
0 commit comments