@@ -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
11261129func 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