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

replace utcnow with now, due to utcnow will be deprecated #2215

Merged
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
3 changes: 1 addition & 2 deletions examples/deployment_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def restart_deployment(api, deployment):
# update `spec.template.metadata` section
# to add `kubectl.kubernetes.io/restartedAt` annotation
deployment.spec.template.metadata.annotations = {
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
.replace(tzinfo=pytz.UTC)
"kubectl.kubernetes.io/restartedAt": datetime.datetime.now(tz=pytz.UTC)
.isoformat()
}

Expand Down
3 changes: 1 addition & 2 deletions examples/dynamic-client/deployment_rolling_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def main():

deployment_manifest["spec"]["template"]["metadata"] = {
"annotations": {
"kubectl.kubernetes.io/restartedAt": datetime.datetime.utcnow()
.replace(tzinfo=pytz.UTC)
"kubectl.kubernetes.io/restartedAt": datetime.datetime.now(tz=pytz.UTC)
.isoformat()
}
}
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/base/config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _create_temp_file_with_content(content, temp_file_path=None):

def _is_expired(expiry):
return ((parse_rfc3339(expiry) - EXPIRY_SKEW_PREVENTION_DELAY) <=
datetime.datetime.utcnow().replace(tzinfo=UTC))
datetime.datetime.now(tz=UTC))


class FileOrData(object):
Expand Down
14 changes: 7 additions & 7 deletions kubernetes/base/config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from kubernetes.client import Configuration

from .config_exception import ConfigException
from .dateutil import format_rfc3339, parse_rfc3339
from .dateutil import UTC, format_rfc3339, parse_rfc3339
from .kube_config import (ENV_KUBECONFIG_PATH_SEPARATOR, CommandTokenSource,
ConfigNode, FileOrData, KubeConfigLoader,
KubeConfigMerger, _cleanup_temp_files,
Expand Down Expand Up @@ -89,10 +89,10 @@ def _raise_exception(st):
TEST_PASSWORD = "pass"
# token for me:pass
TEST_BASIC_TOKEN = "Basic bWU6cGFzcw=="
DATETIME_EXPIRY_PAST = datetime.datetime.utcnow(
) - datetime.timedelta(minutes=PAST_EXPIRY_TIMEDELTA)
DATETIME_EXPIRY_FUTURE = datetime.datetime.utcnow(
) + datetime.timedelta(minutes=FUTURE_EXPIRY_TIMEDELTA)
DATETIME_EXPIRY_PAST = datetime.datetime.now(tz=UTC
).replace(tzinfo=None) - datetime.timedelta(minutes=PAST_EXPIRY_TIMEDELTA)
DATETIME_EXPIRY_FUTURE = datetime.datetime.now(tz=UTC
).replace(tzinfo=None) + datetime.timedelta(minutes=FUTURE_EXPIRY_TIMEDELTA)
TEST_TOKEN_EXPIRY_PAST = _format_expiry_datetime(DATETIME_EXPIRY_PAST)

TEST_SSL_HOST = "https://test-host"
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def test_load_gcp_token_no_refresh(self):
def test_load_gcp_token_with_refresh(self):
def cred(): return None
cred.token = TEST_ANOTHER_DATA_BASE64
cred.expiry = datetime.datetime.utcnow()
cred.expiry = datetime.datetime.now(tz=UTC).replace(tzinfo=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate why do we do .replace(tzinfo=None)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!
Because the output of datetime.datetime.utcnow() is timezone-naive, but datetime.datetime.now(tz=UTC)'s output is not, hence I utilize .replace(tzinfo=None) to replace the timezone information from the datetime object. Essentially, it converts the datetime object to a timezone-naive form, without any specific time zone associated with it.
For an instance:
datetime.datetime.utcnow() output is :
2024-04-02 12:25:49.213759
datetime.datetime.now(tz=UTC) output is:
2024-04-02 12:25:49.213766+00:00
datetime.datetime.now(tz=UTC).replace(tzinfo=None) output is:
2024-04-02 12:25:49.213766

Hope above explanation makes sense

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for the explanation!


loader = KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
Expand Down Expand Up @@ -1124,7 +1124,6 @@ def test_oidc_with_idp_ca_file_refresh(self, mock_ApiClient, mock_OAuth2Session)
active_context="expired_oidc_with_idp_ca_file",
)


self.assertTrue(loader._load_auth_provider_token())
self.assertEqual("Bearer abc123", loader.token)

Expand Down Expand Up @@ -1529,6 +1528,7 @@ def test_user_exec_auth_certificates(self, mock):
@mock.patch('kubernetes.config.kube_config.ExecProvider.run', autospec=True)
def test_user_exec_cwd(self, mock):
capture = {}

def capture_cwd(exec_provider):
capture['cwd'] = exec_provider.cwd
mock.side_effect = capture_cwd
Expand Down