| |
| # Copyright 2017-present Open Networking Foundation |
| # |
| # 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. |
| |
| """ |
| sync_secret.py |
| |
| Synchronize Secrets. |
| """ |
| |
| import json |
| from synchronizers.new_base.syncstep import SyncStep |
| from synchronizers.new_base.modelaccessor import KubernetesSecret |
| |
| from xosconfig import Config |
| from multistructlog import create_logger |
| |
| from kubernetes.client.rest import ApiException |
| from kubernetes import client as kubernetes_client, config as kubernetes_config |
| |
| log = create_logger(Config().get('logging')) |
| |
| class SyncKubernetesSecret(SyncStep): |
| |
| """ |
| SyncKubernetesSecret |
| |
| Implements sync step for syncing Secrets. |
| """ |
| |
| provides = [KubernetesSecret] |
| observes = KubernetesSecret |
| requested_interval = 0 |
| |
| def __init__(self, *args, **kwargs): |
| super(SyncKubernetesSecret, self).__init__(*args, **kwargs) |
| kubernetes_config.load_incluster_config() |
| self.v1 = kubernetes_client.CoreV1Api() |
| |
| def get_secret(self, o): |
| """ Given an XOS KubernetesSecret object, read the corresponding Secret from Kubernetes. |
| return None if no Secret exists. |
| """ |
| try: |
| secret = self.v1.read_namespaced_secret(o.name, o.trust_domain.name) |
| except ApiException, e: |
| if e.status == 404: |
| return None |
| raise |
| return secret |
| |
| def sync_record(self, o): |
| secret = self.get_secret(o) |
| if not secret: |
| secret = kubernetes_client.V1Secret() |
| secret.data = json.loads(o.data) |
| secret.metadata = kubernetes_client.V1ObjectMeta(name=o.name) |
| |
| secret = self.v1.create_namespaced_secret(o.trust_domain.name, secret) |
| else: |
| secret.data = json.loads(o.data) |
| self.v1.patch_namespaced_secret(o.name, o.trust_domain.name, secret) |
| |
| if (not o.backend_handle): |
| o.backend_handle = secret.metadata.self_link |
| o.save(update_fields=["backend_handle"]) |
| |
| def delete_record(self, port): |
| # TODO(smbaker): Implement delete step |
| pass |
| |