This repository was archived by the owner on Mar 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
Add proper GCP config loader and refresher #22
Merged
+247
−22
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add proper GCP config loader and refresher
commit 824c03c7eee71dd5ac52fada8d7d36aecf81a781
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2017 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import datetime | ||
import math | ||
import re | ||
|
||
|
||
class TimezoneInfo(datetime.tzinfo): | ||
def __init__(self, h, m): | ||
self._name = "UTC" | ||
if h != 0 and m != 0: | ||
self._name += "%+03d:%2d" % (h, m) | ||
self._delta = datetime.timedelta(hours=h, minutes=math.copysign(m, h)) | ||
|
||
def utcoffset(self, dt): | ||
return self._delta | ||
|
||
def tzname(self, dt): | ||
return self._name | ||
|
||
def dst(self, dt): | ||
return datetime.timedelta(0) | ||
|
||
|
||
UTC = TimezoneInfo(0, 0) | ||
|
||
# ref https://www.ietf.org/rfc/rfc3339.txt | ||
_re_rfc3339 = re.compile(r"(\d\d\d\d)-(\d\d)-(\d\d)" # full-date | ||
r"[ Tt]" # Separator | ||
r"(\d\d):(\d\d):(\d\d)([.,]\d+)?" # partial-time | ||
r"([zZ ]|[-+]\d\d?:\d\d)?", # time-offset | ||
re.VERBOSE + re.IGNORECASE) | ||
_re_timezone = re.compile(r"([-+])(\d\d?):?(\d\d)?") | ||
|
||
|
||
def parse_rfc3339(s): | ||
if isinstance(s, datetime.datetime): | ||
# no need to parse it, just make sure it has a timezone. | ||
if not s.tzinfo: | ||
return s.replace(tzinfo=UTC) | ||
return s | ||
groups = _re_rfc3339.search(s).groups() | ||
dt = [0] * 7 | ||
for x in range(6): | ||
dt[x] = int(groups[x]) | ||
if groups[6] is not None: | ||
dt[6] = int(groups[6]) | ||
tz = UTC | ||
if groups[7] is not None and groups[7] != 'Z' and groups[7] != 'z': | ||
tz_groups = _re_timezone.search(groups[7]).groups() | ||
hour = int(tz_groups[1]) | ||
minute = 0 | ||
if tz_groups[0] == "-": | ||
hour *= -1 | ||
if tz_groups[2]: | ||
minute = int(tz_groups[2]) | ||
tz = TimezoneInfo(hour, minute) | ||
return datetime.datetime( | ||
year=dt[0], month=dt[1], day=dt[2], | ||
hour=dt[3], minute=dt[4], second=dt[5], | ||
microsecond=dt[6], tzinfo=tz) | ||
|
||
|
||
def format_rfc3339(date_time): | ||
if date_time.tzinfo is None: | ||
date_time = date_time.replace(tzinfo=UTC) | ||
date_time = date_time.astimezone(UTC) | ||
return date_time.strftime('%Y-%m-%dT%H:%M:%SZ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
from datetime import datetime | ||
|
||
from .dateutil import UTC, TimezoneInfo, format_rfc3339, parse_rfc3339 | ||
|
||
|
||
class DateUtilTest(unittest.TestCase): | ||
|
||
def _parse_rfc3339_test(self, st, y, m, d, h, mn, s): | ||
actual = parse_rfc3339(st) | ||
expected = datetime(y, m, d, h, mn, s, 0, UTC) | ||
self.assertEqual(expected, actual) | ||
|
||
def test_parse_rfc3339(self): | ||
self._parse_rfc3339_test("2017-07-25T04:44:21Z", | ||
2017, 7, 25, 4, 44, 21) | ||
self._parse_rfc3339_test("2017-07-25 04:44:21Z", | ||
2017, 7, 25, 4, 44, 21) | ||
self._parse_rfc3339_test("2017-07-25T04:44:21", | ||
2017, 7, 25, 4, 44, 21) | ||
self._parse_rfc3339_test("2017-07-25T04:44:21z", | ||
2017, 7, 25, 4, 44, 21) | ||
self._parse_rfc3339_test("2017-07-25T04:44:21+03:00", | ||
2017, 7, 25, 1, 44, 21) | ||
self._parse_rfc3339_test("2017-07-25T04:44:21-03:00", | ||
2017, 7, 25, 7, 44, 21) | ||
|
||
def test_format_rfc3339(self): | ||
self.assertEqual( | ||
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, UTC)), | ||
"2017-07-25T04:44:21Z") | ||
self.assertEqual( | ||
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, | ||
TimezoneInfo(2, 0))), | ||
"2017-07-25T02:44:21Z") | ||
self.assertEqual( | ||
format_rfc3339(datetime(2017, 7, 25, 4, 44, 21, 0, | ||
TimezoneInfo(-2, 30))), | ||
"2017-07-25T07:14:21Z") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why not directly raising the exception?
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.
Why do you mean? I am passing this function as a function pointer to config loader and expect the function pointer (that suppose to update GCE token) never been called in the test. I was using lambda syntax to return a dummy token before, but you cannot raise exception in lambda syntax.
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.
Ok, I didn't now that you can not raise inside lambda (always learning from codereview).
Forget about it.