Skip to content

Commit

Permalink
Fix for region fetch from GetAWSRegion()
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Dec 11, 2024
1 parent 88aef43 commit e4a11ef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
8 changes: 8 additions & 0 deletions deploy/cluster_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,11 @@ rules:
- delete
- update
- create
- apiGroups:
- config.openshift.io
resources:
- infrastructures
verbs:
- get
- list
- watch
10 changes: 9 additions & 1 deletion pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
37 changes: 29 additions & 8 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e4a11ef

Please sign in to comment.