-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Changes made in configuration.py to accept environmental variables #2390
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
base: master
Are you sure you want to change the base?
Changes from all commits
afd1d44
8f356ef
6b85a93
638abe2
b778952
b295c2d
9c35894
1c69346
e33362f
f258b31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import multiprocessing | ||
import sys | ||
import urllib3 | ||
import os | ||
|
||
import six | ||
from six.moves import http_client as httplib | ||
|
@@ -158,9 +159,15 @@ def __init__(self, host="http://localhost", | |
""" | ||
|
||
self.proxy = None | ||
if(os.getenv("HTTPS_PROXY")):self.proxy=os.getenv("HTTPS_PROXY") | ||
if(os.getenv("https_proxy")):self.proxy=os.getenv("https_proxy") | ||
if(os.getenv("HTTP_PROXY")):self.proxy=os.getenv("HTTP_PROXY") | ||
if(os.getenv("http_proxy")):self.proxy=os.getenv("http_proxy") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If both HTTPS_PROXY and HTTP_PROXY are set, does this mean HTTP_PROXY will override HTTPS_PROXY? Is this behavior matching the websocket client behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If both HTTPS_PROXY and HTTP_PROXY are set, HTTP_PROXY will override HTTPS_PROXY. But Web client has only one self.proxy variable. So, I think it will work fine unless user provide both HTTPS_PROXY and HTTP_PROXY. If user needed both variables we need to check that condition. Even then we can just give HTTPS_PROXY priority... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If user need to declare both HTTPS_PROXY and HTTP_PROXY. He need to mention which proxy he is using. If user declared both HTTPS_PROXY and HTTP_PROXY and he didn't mention then it will choose latest declared. If we need to give HTTPS_PROXY priority over HTTP_PROXY. Then I need to change the code accordingly.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @roycaihw What is the exact requirement here...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm okay with having override behavior, just want to make sure we are aligned with what websocket client is doing According to the original issue, websocket client makes use of these env variables, so I wanted to understand if the same override behavior exists there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the original issue (#2321), he gave only one variable HTTPS_PROXY. So, the changes will not override the self.proxy variable. |
||
"""Proxy URL | ||
""" | ||
self.no_proxy = None | ||
if(os.getenv("NO_PROXY")):self.no_proxy=os.getenv("NO_PROXY") | ||
if(os.getenv("no_proxy")):self.no_proxy=os.getenv("no_proxy") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar question here. Does no_proxy override NO_PROXY if both of them are set? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes no_proxy override NO_PROXY. But this is for case sensitive environments like macOS . Only if user declared both no_proxy and NO_PROXY. In windows it will treat both as same. So, I added both no_proxy and NO_PROXY. But they are same... |
||
"""bypass proxy for host in the no_proxy list. | ||
""" | ||
self.proxy_headers = None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env bash | ||
# insert_proxy_config.sh - run this after openapi-generator release.sh | ||
CONFIG_PATH="../python_kubernetes/kubernetes/client" | ||
|
||
# Compute the full file path | ||
CONFIG_FILE="$CONFIG_PATH/configuration.py" | ||
|
||
# --- Normalize Windows-style backslashes to Unix forward slashes --- | ||
CONFIG_FILE="$(echo "$CONFIG_FILE" | sed 's|\\|/|g')" | ||
|
||
# --- Ensure the target file exists and is writable --- | ||
if [ ! -f "$CONFIG_FILE" ] || [ ! -w "$CONFIG_FILE" ]; then | ||
echo "Error: $CONFIG_FILE does not exist or is not writable." >&2 | ||
exit 1 | ||
fi | ||
|
||
# --- Step 1: Ensure 'import os' follows existing imports (idempotent) --- | ||
if ! grep -qE '^import os$' "$CONFIG_FILE"; then | ||
LAST_IMPORT=$(grep -nE '^(import |from )' "$CONFIG_FILE" | tail -n1 | cut -d: -f1) | ||
if [ -n "$LAST_IMPORT" ]; then | ||
sed -i "$((LAST_IMPORT+1))i import os" "$CONFIG_FILE" | ||
else | ||
if head -n1 "$CONFIG_FILE" | grep -q '^#!'; then | ||
sed -i '2i import os' "$CONFIG_FILE" | ||
else | ||
sed -i '1i import os' "$CONFIG_FILE" | ||
fi | ||
fi | ||
echo "Inserted 'import os' after existing imports in $CONFIG_FILE." | ||
else | ||
echo "'import os' already present; no changes made." | ||
fi | ||
|
||
# --- Step 2: Insert proxy & no_proxy environment code --- | ||
if ! grep -q 'os.getenv("HTTPS_PROXY"' "$CONFIG_FILE"; then | ||
PROXY_LINE=$(grep -n "self.proxy = None" "$CONFIG_FILE" | cut -d: -f1) | ||
NO_PROXY_LINE=$(grep -n "^[[:space:]]*self\.no_proxy[[:space:]]*=[[:space:]]*None" "$CONFIG_FILE" | cut -d: -f1) | ||
|
||
if [ -n "$PROXY_LINE" ]; then | ||
INDENT=$(sed -n "${PROXY_LINE}s/^\( *\).*/\1/p" "$CONFIG_FILE") | ||
|
||
BLOCK="" | ||
|
||
if [ -z "$NO_PROXY_LINE" ]; then | ||
# self.no_proxy = None is not present → insert full block after self.proxy = None | ||
BLOCK+="${INDENT}# Load proxy from environment variables (if set)\n" | ||
BLOCK+="${INDENT}if os.getenv(\"HTTPS_PROXY\"): self.proxy = os.getenv(\"HTTPS_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"https_proxy\"): self.proxy = os.getenv(\"https_proxy\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"HTTP_PROXY\"): self.proxy = os.getenv(\"HTTP_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"http_proxy\"): self.proxy = os.getenv(\"http_proxy\")\n" | ||
BLOCK+="${INDENT}self.no_proxy = None\n" | ||
BLOCK+="${INDENT}# Load no_proxy from environment variables (if set)\n" | ||
BLOCK+="${INDENT}if os.getenv(\"NO_PROXY\"): self.no_proxy = os.getenv(\"NO_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"no_proxy\"): self.no_proxy = os.getenv(\"no_proxy\")" | ||
|
||
sed -i "${PROXY_LINE}a $BLOCK" "$CONFIG_FILE" | ||
echo "Inserted full proxy + no_proxy block after 'self.proxy = None'." | ||
else | ||
# self.no_proxy = None exists → insert only env logic after that | ||
BLOCK+="${INDENT}# Load proxy from environment variables (if set)\n" | ||
BLOCK+="${INDENT}if os.getenv(\"HTTPS_PROXY\"): self.proxy = os.getenv(\"HTTPS_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"https_proxy\"): self.proxy = os.getenv(\"https_proxy\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"HTTP_PROXY\"): self.proxy = os.getenv(\"HTTP_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"http_proxy\"): self.proxy = os.getenv(\"http_proxy\")\n" | ||
BLOCK+="${INDENT}# Load no_proxy from environment variables (if set)\n" | ||
BLOCK+="${INDENT}if os.getenv(\"NO_PROXY\"): self.no_proxy = os.getenv(\"NO_PROXY\")\n" | ||
BLOCK+="${INDENT}if os.getenv(\"no_proxy\"): self.no_proxy = os.getenv(\"no_proxy\")" | ||
|
||
sed -i "${NO_PROXY_LINE}a $BLOCK" "$CONFIG_FILE" | ||
echo "Inserted environment block after 'self.no_proxy = None'." | ||
fi | ||
else | ||
echo "Warning: 'self.proxy = None' not found in $CONFIG_FILE. No proxy code inserted." | ||
fi | ||
else | ||
echo "Proxy environment code already present; no changes made." | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is generated by the upstream openapi generator. Could you make the change upstream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have checked https://github.com/OpenAPITools/openapi-generator. We need to change in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/configuration.mustache (as it is python code change.). But while I am at it. I observed that there is no no_proxy variable there.

I am not sure whether I can add no_proxy variable to it or not. It was removed for some reasons according to this issue OpenAPITools/openapi-generator#20226.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is only for self.proxy=None. I can add it to https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/configuration.mustache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This client is based on an old version of the upstream generator. The upstream generator probably had some change to the no_proxy variable since then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of upstreaming the changes can we downstream the changes to files that using configuration.py variables. i.e. python_kubernetes\kubernetes\base\stream\ws_client.py and python_kubernetes\kubernetes\client\rest.py. We can add this change in above files. But I have a doubt will it impact this changes if we use Open-api generator...

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even I cann't delete it. It is not my commit.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@roycaihw Can you check and approve my PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@roycaihw Can you check and approve my PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@roycaihw Can you check and approve my PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though this client is using a old version of the generator, can we first get this change accepted by the upstream generator, and then we can patch this client?