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

fix(): Fixed tunnel status reporting in the slicegw CR #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bharath-avesha
Copy link
Contributor

The gw tunnel status is recorded in the slicegateway CR status field. There are a number of fields like the interface name, rx and tx rates, latency and pkt loss in the status field but not all of them are getting filled. This is occurring because of an incorrect type conversion between gwpod.TunnelStatus and gwsidecar.TunnelStatus. The former struct is what is written to the status field of the CR and the latter is the struct that is filled by the grpc messaging between the operator and the gw sidecar.

The code changes in this PR address the above issue. It also adds additional info in the status field of the CR like the remote port number an instance of gw client is connecting to, and the tunnel state in string format for ease of debugging. The code also contains a couple of fixes for issues reported by the Go static analysis tool: removed unnecessary input params from a couple of functions and updated error messages to start with lowercase.

Signed-off-by: Bharath Horatti <bharath@aveshasystems.com>
if isClient(slicegateway) {
// get the remote port number this gw pod is connected to on the remote cluster
_, remotePortInUse := getClientGwRemotePortInUse(ctx, r.Client, slicegateway, GetDepNameFromPodName(slicegateway.Status.Config.SliceGatewayID, gwPod.PodName))
gwPod.RemotePort = int32(remotePortInUse)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an integer with architecture-dependent bit size from
strconv.Atoi
to a lower bit size type int32 without an upper bound check.

Copilot Autofix AI 9 days ago

To fix the problem, we need to ensure that the integer value parsed from the string does not exceed the bounds of the int32 type before performing the conversion. This can be achieved by using strconv.ParseInt with a specified bit size of 32, which directly returns an int64 that can be safely cast to int32 if within bounds.

  1. Replace the use of strconv.Atoi with strconv.ParseInt specifying a bit size of 32.
  2. Ensure that the parsed value is within the bounds of int32 before performing the conversion.
Suggested changeset 2
controllers/slicegateway/slicegateway.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/controllers/slicegateway/slicegateway.go b/controllers/slicegateway/slicegateway.go
--- a/controllers/slicegateway/slicegateway.go
+++ b/controllers/slicegateway/slicegateway.go
@@ -678,3 +678,8 @@
 			_, remotePortInUse := getClientGwRemotePortInUse(ctx, r.Client, slicegateway, GetDepNameFromPodName(slicegateway.Status.Config.SliceGatewayID, gwPod.PodName))
-			gwPod.RemotePort = int32(remotePortInUse)
+			if remotePortInUse >= math.MinInt32 && remotePortInUse <= math.MaxInt32 {
+				gwPod.RemotePort = int32(remotePortInUse)
+			} else {
+				log.Error(fmt.Errorf("remotePortInUse out of int32 bounds"), "Invalid remote port", "remotePortInUse", remotePortInUse)
+				gwPod.RemotePort = 0 // or some default value
+			}
 		}
EOF
@@ -678,3 +678,8 @@
_, remotePortInUse := getClientGwRemotePortInUse(ctx, r.Client, slicegateway, GetDepNameFromPodName(slicegateway.Status.Config.SliceGatewayID, gwPod.PodName))
gwPod.RemotePort = int32(remotePortInUse)
if remotePortInUse >= math.MinInt32 && remotePortInUse <= math.MaxInt32 {
gwPod.RemotePort = int32(remotePortInUse)
} else {
log.Error(fmt.Errorf("remotePortInUse out of int32 bounds"), "Invalid remote port", "remotePortInUse", remotePortInUse)
gwPod.RemotePort = 0 // or some default value
}
}
controllers/slicegateway/utils.go
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/controllers/slicegateway/utils.go b/controllers/slicegateway/utils.go
--- a/controllers/slicegateway/utils.go
+++ b/controllers/slicegateway/utils.go
@@ -336,5 +336,5 @@
 				if envVar.Name == "NODE_PORT" {
-					nodePort, err := strconv.Atoi(envVar.Value)
-					if err == nil {
-						return true, nodePort
+					nodePort64, err := strconv.ParseInt(envVar.Value, 10, 32)
+					if err == nil && nodePort64 >= math.MinInt32 && nodePort64 <= math.MaxInt32 {
+						return true, int(nodePort64)
 					}
EOF
@@ -336,5 +336,5 @@
if envVar.Name == "NODE_PORT" {
nodePort, err := strconv.Atoi(envVar.Value)
if err == nil {
return true, nodePort
nodePort64, err := strconv.ParseInt(envVar.Value, 10, 32)
if err == nil && nodePort64 >= math.MinInt32 && nodePort64 <= math.MaxInt32 {
return true, int(nodePort64)
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant