From e4a11ef50a7c9b9fb83bd04a92ae31e75753d6e5 Mon Sep 17 00:00:00 2001 From: Aayush Chouhan Date: Fri, 29 Nov 2024 19:41:15 +0530 Subject: [PATCH] Fix for region fetch from GetAWSRegion() Signed-off-by: Aayush Chouhan --- deploy/cluster_role.yaml | 8 ++++++++ pkg/bundle/deploy.go | 10 +++++++++- pkg/util/util.go | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/deploy/cluster_role.yaml b/deploy/cluster_role.yaml index 01091771d8..a6fd281239 100644 --- a/deploy/cluster_role.yaml +++ b/deploy/cluster_role.yaml @@ -183,3 +183,11 @@ rules: - delete - update - create + - apiGroups: + - config.openshift.io + resources: + - infrastructures + verbs: + - get + - list + - watch diff --git a/pkg/bundle/deploy.go b/pkg/bundle/deploy.go index 5467257d96..1b95f6a199 100644 --- a/pkg/bundle/deploy.go +++ b/pkg/bundle/deploy.go @@ -2,7 +2,7 @@ package bundle const Version = "5.18.0" -const Sha256_deploy_cluster_role_yaml = "3f8118853db73926c4f9d14be84ac8f81833c3a7a94a52ecf1e9ebcf712eee93" +const Sha256_deploy_cluster_role_yaml = "31fc622ff7fa66617be3895bddcb6cfdb97883b75b20bdb2bf04052bd14221a8" const File_deploy_cluster_role_yaml = `apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole @@ -189,6 +189,14 @@ rules: - delete - update - create + - apiGroups: + - config.openshift.io + resources: + - infrastructures + verbs: + - get + - list + - watch ` const Sha256_deploy_cluster_role_binding_yaml = "15c78355aefdceaf577bd96b4ae949ae424a3febdc8853be0917cf89a63941fc" diff --git a/pkg/util/util.go b/pkg/util/util.go index 3011e2faff..08f8ee1ac4 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -32,6 +32,7 @@ import ( nbapis "github.com/noobaa/noobaa-operator/v5/pkg/apis" nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1" "github.com/noobaa/noobaa-operator/v5/pkg/bundle" + configv1 "github.com/openshift/api/config/v1" routev1 "github.com/openshift/api/route/v1" secv1 "github.com/openshift/api/security/v1" cloudcredsv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1" @@ -185,6 +186,7 @@ func init() { Panic(autoscalingv1.AddToScheme(scheme.Scheme)) Panic(kedav1alpha1.AddToScheme(scheme.Scheme)) Panic(apiregistration.AddToScheme(scheme.Scheme)) + Panic(configv1.AddToScheme(scheme.Scheme)) } // 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 g.Name == "autoscaling" || g.Name == "batch" || g.Name == "keda.sh" || + g.Name == "config.openshift.io" || strings.HasSuffix(g.Name, ".k8s.io") { return true } @@ -1164,17 +1167,35 @@ func GetAWSRegion() (string, error) { "af-south-1": "af-south-1", "il-central-1": "il-central-1", } - nodesList := &corev1.NodeList{} - if ok := KubeList(nodesList); !ok || len(nodesList.Items) == 0 { - return "", fmt.Errorf("Failed to list kubernetes nodes") + var awsRegion string + infrastructure := &configv1.Infrastructure{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cluster", + }, + } + if _, _, err := KubeGet(infrastructure); err != nil { + log.Infof("Failed to fetch cluster infrastructure details: %v", err) + } + if infrastructure.Status.PlatformStatus != nil && infrastructure.Status.PlatformStatus.AWS != nil { + awsRegion = mapValidAWSRegions[infrastructure.Status.PlatformStatus.AWS.Region] } - nameSplit := strings.Split(nodesList.Items[0].Name, ".") - if len(nameSplit) < 2 { - return "", fmt.Errorf("Unexpected node name format: %q", nodesList.Items[0].Name) + + // Parsing the aws region from node name if not fetched from cluster + if awsRegion == "" { + nodesList := &corev1.NodeList{} + if ok := KubeList(nodesList); !ok || len(nodesList.Items) == 0 { + log.Infof("Failed to list kubernetes nodes") + } + nameSplit := strings.Split(nodesList.Items[0].Name, ".") + if len(nameSplit) < 2 { + log.Infof("Unexpected node name format: %q", nodesList.Items[0].Name) + } + awsRegion = mapValidAWSRegions[nameSplit[1]] } - awsRegion := mapValidAWSRegions[nameSplit[1]] + + // returning error if not fetched from either cluster or node name if awsRegion == "" { - return "", fmt.Errorf("The parsed AWS region is invalid: %q", awsRegion) + return "", fmt.Errorf("Failed to determine the AWS Region.") } return awsRegion, nil }