Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug-fix for fetching region from AWS cluster obj in GetAWSRegion() #1483

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 34 additions & 10 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 @@ -1122,9 +1125,11 @@ func GetIBMRegion() (string, error) {
return region, nil
}

// GetAWSRegion parses the region from a node's name
// GetAWSRegion determines the AWS region from cluster infrastructure or node name
func GetAWSRegion() (string, error) {
// parse the node name to get AWS region according to this:
// Determine the AWS region based on cluster infrastructure or node name
// If infrastructure details are unavailable, the node's name is parsed to extract the region
// Refer to the following for more details:
// https://docs.aws.amazon.com/en_pv/vpc/latest/userguide/vpc-dns.html#vpc-dns-hostnames
// The list of regions can be found here:
// https://docs.aws.amazon.com/general/latest/gr/rande.html
Expand Down Expand Up @@ -1164,17 +1169,36 @@ 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 {
achouhan09 marked this conversation as resolved.
Show resolved Hide resolved
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
log.Warn("Falling back to parsing the node name as infrastructure details are unavailable or incomplete")
if awsRegion == "" {
achouhan09 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading