|
102 | 102 | "list": Template("/api/v1/namespaces/${namespace}/pods"), |
103 | 103 | "list-all": "/api/v1/pods", |
104 | 104 | }, |
| 105 | + "Secret": { |
| 106 | + "single": Template("/api/v1/namespaces/${namespace}/secrets/${name}"), |
| 107 | + "list": Template("/api/v1/namespaces/${namespace}/secrets"), |
| 108 | + "list-all": "/api/v1/secrets" |
| 109 | + }, |
105 | 110 | "ReplicaSet": { |
106 | 111 | "single": Template("/apis/apps/v1/namespaces/${namespace}/replicasets/${name}"), |
107 | 112 | "list": Template("/apis/apps/v1/namespaces/${namespace}/replicasets"), |
@@ -518,6 +523,16 @@ def __repr__(self): |
518 | 523 | return str(self.__dict__) |
519 | 524 |
|
520 | 525 |
|
| 526 | +class Secret(object): |
| 527 | + def __init__(self, name, namespace, data, kind, string_data, type): |
| 528 | + self.name = name |
| 529 | + self.namespace = namespace |
| 530 | + self.data = data |
| 531 | + self.kind = kind |
| 532 | + self.string_data = string_data |
| 533 | + self.type = type |
| 534 | + |
| 535 | + |
521 | 536 | class Controller(object): |
522 | 537 | """ |
523 | 538 | General class for all cached Controller objects |
@@ -1012,6 +1027,19 @@ def process_object(self, k8s, obj, query_options=None): |
1012 | 1027 | return result |
1013 | 1028 |
|
1014 | 1029 |
|
| 1030 | +class SecretProcessor(_K8sProcessor): |
| 1031 | + def process_object(self, k8s, obj, query_options=None): |
| 1032 | + metadata = obj.get("metadata", {}) |
| 1033 | + kind = obj.get("kind", "") |
| 1034 | + namespace = metadata.get("namespace", "") |
| 1035 | + name = metadata.get("name", "") |
| 1036 | + data = obj.get("data", {}) |
| 1037 | + string_data = obj.get("stringData", {}) |
| 1038 | + type = obj.get("type", "") |
| 1039 | + |
| 1040 | + return Secret(name, namespace, data, kind, string_data, type) |
| 1041 | + |
| 1042 | + |
1015 | 1043 | class ControllerProcessor(_K8sProcessor): |
1016 | 1044 | def process_object(self, k8s, obj, query_options=None): |
1017 | 1045 | """Generate a Controller object from a JSON object |
@@ -1292,6 +1320,10 @@ def __init__( |
1292 | 1320 | self._pod_processor = PodProcessor(self._controllers) |
1293 | 1321 | self._pods_cache = _K8sCache(self._pod_processor, "Pod") |
1294 | 1322 |
|
| 1323 | + # create the secret cache |
| 1324 | + self._secret_processor = SecretProcessor() |
| 1325 | + self._secrets_cache = _K8sCache(self._secret_processor, "Secret") |
| 1326 | + |
1295 | 1327 | self._cluster_name = None |
1296 | 1328 | self._api_server_version = None |
1297 | 1329 | # The last time (in seconds since epoch) we updated the K8s version number via a query |
@@ -1571,6 +1603,7 @@ def update_cache(self, run_state): |
1571 | 1603 | scalyr_logging.DEBUG_LEVEL_1, "Marking unused pods as expired" |
1572 | 1604 | ) |
1573 | 1605 | self._pods_cache.mark_as_expired(current_time) |
| 1606 | + self._secrets_cache.mark_as_expired(current_time) |
1574 | 1607 |
|
1575 | 1608 | self._update_cluster_name(local_state.k8s) |
1576 | 1609 | self._update_api_server_version_if_necessary( |
@@ -1614,6 +1647,39 @@ def update_cache(self, run_state): |
1614 | 1647 | local_state.cache_expiry_secs - fuzz_factor |
1615 | 1648 | ) |
1616 | 1649 |
|
| 1650 | + def secret( |
| 1651 | + self, |
| 1652 | + namespace, |
| 1653 | + name, |
| 1654 | + current_time=None, |
| 1655 | + allow_expired=False |
| 1656 | + ): |
| 1657 | + """Returns pod info for the pod specified by namespace and name or None if no pad matches. |
| 1658 | +
|
| 1659 | + Warning: Failure to pass current_time leads to incorrect recording of last access times, which will |
| 1660 | + lead to these objects being refreshed prematurely (potential source of bugs) |
| 1661 | +
|
| 1662 | + Querying the pod information is thread-safe, but the returned object should |
| 1663 | + not be written to. |
| 1664 | +
|
| 1665 | + @param allow_expired: If True, an object is considered present in cache even if it is expired. |
| 1666 | + @type allow_expired: bool |
| 1667 | + """ |
| 1668 | + local_state = self._state.copy_state() |
| 1669 | + |
| 1670 | + if local_state.k8s is None: |
| 1671 | + return |
| 1672 | + |
| 1673 | + return self._secrets_cache.lookup( |
| 1674 | + local_state.k8s, |
| 1675 | + current_time, |
| 1676 | + namespace, |
| 1677 | + name, |
| 1678 | + kind="Secret", |
| 1679 | + allow_expired=allow_expired, |
| 1680 | + ignore_k8s_api_exception=ignore_k8s_api_exception, |
| 1681 | + ) |
| 1682 | + |
1617 | 1683 | def pod( |
1618 | 1684 | self, |
1619 | 1685 | namespace, |
|
0 commit comments