Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
| 16 | from xossynchronizer.steps.syncstep import SyncStep |
| 17 | from xossynchronizer.modelaccessor import KubernetesService |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | log = create_logger(Config().get('logging')) |
| 23 | |
| 24 | class SyncK8Service(SyncStep): |
| 25 | provides = [KubernetesService] |
| 26 | observes = KubernetesService |
| 27 | |
| 28 | max_version = "1.14" |
| 29 | [expected_major, expected_minor] = max_version.split(".") |
| 30 | |
| 31 | def __init__(self, *args, **kwargs): |
| 32 | super(SyncK8Service, self).__init__(*args, **kwargs) |
| 33 | self.init_kubernetes_client() |
| 34 | |
| 35 | def init_kubernetes_client(self): |
| 36 | from kubernetes.client.rest import ApiException |
| 37 | from kubernetes import client as kubernetes_client, config as kubernetes_config |
| 38 | kubernetes_config.load_incluster_config() |
| 39 | self.api_instance = kubernetes_client.VersionApi(kubernetes_client.ApiClient()) |
| 40 | self.ApiException = ApiException |
| 41 | |
| 42 | def sync_record(self, o): |
| 43 | log.info("[K8Service SyncStep] Sync'ing model", model=o, name=o.name) |
| 44 | |
| 45 | res = self.api_instance.get_code() |
| 46 | major = res.major |
| 47 | minor = res.minor |
Matteo Scandolo | f6a60e5 | 2019-05-24 15:11:52 -0700 | [diff] [blame] | 48 | log.debug("[K8Service SyncStep] API response", res=res) |
Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -0700 | [diff] [blame] | 49 | log.info("[K8Service SyncStep] API Code", major=major, minor=minor, |
| 50 | expected_major=self.expected_major, expected_minor=self.expected_minor) |
| 51 | |
Matteo Scandolo | f6a60e5 | 2019-05-24 15:11:52 -0700 | [diff] [blame] | 52 | if int(major) != int(self.expected_major) or int(minor) > int(self.expected_minor): |
Matteo Scandolo | 5f2840f | 2019-05-16 16:08:23 -0700 | [diff] [blame] | 53 | raise Exception("Kubernetes cluster of version %s is not supported by the kubernetes-services" % res.git_version+ |
| 54 | "the maximum supported version is %s" % self.max_version) |
| 55 | |
| 56 | def delete_record(self, o): |
| 57 | pass |