Esempio n. 1
0
 def run(file_path):
     for template_body in get_template_contexts(file_path):
         if template_body.get('kind') == 'Secret':
             log.info(f'Skipping secret {template_body.get("metadata", {}).get("name")}')
             continue
         kube_client = Adapter.get_instance(template_body)
         new = yaml.safe_dump(template_body)
         k8s_object = kube_client.get()
         if k8s_object is None:
             current_dict = {}
         else:
             current_dict = to_dict(k8s_object)
         for field_path in IGNORE_FIELDS:
             try:
                 apply_filter(current_dict, field_path)
             except KeyError:
                 pass
         metadata = current_dict.get('metadata', {})
         if 'annotations' in metadata and metadata['annotations'] == {}:
             del metadata['annotations']
         current = yaml.safe_dump(current_dict)
         if new == current:
             log.info(f' Kind: "{template_body.get("kind")}", '
                      f'name: "{template_body.get("metadata", {}).get("name")}" : NO CHANGES')
         else:
             diff = ndiff(current.splitlines(keepends=True), new.splitlines(keepends=True))
             log.info(f' Kind: "{template_body.get("kind")}", '
                      f'name: "{template_body.get("metadata", {}).get("name")}"')
             sys.stdout.write(''.join(diff))
Esempio n. 2
0
 def run(self, file_path: str):
     for template_body in get_template_contexts(file_path):
         if not self._is_available_kind(template_body.get('apiVersion'),
                                        template_body.get('kind')):
             raise ResourceNotAvailableError(
                 "The resource with kind {} is not supported with version {}. File: {}"
                 .format(template_body.get('kind'),
                         template_body.get('apiVersion'), file_path))
Esempio n. 3
0
    def test_get_template_contexts(self):
        with self.assertRaises(StopIteration):
            next(get_template_contexts('k8s_handle/k8s/fixtures/empty.yaml'))

        with self.assertRaises(RuntimeError) as context:
            next(get_template_contexts('k8s_handle/k8s/fixtures/nokind.yaml'))
        self.assertTrue(
            'Field "kind" not found (or empty) in file "k8s_handle/k8s/fixtures/nokind.yaml"'
            in str(context.exception), context.exception)

        with self.assertRaises(RuntimeError) as context:
            next(
                get_template_contexts(
                    'k8s_handle/k8s/fixtures/nometadata.yaml'))
        self.assertTrue(
            'Field "metadata" not found (or empty) in file "k8s_handle/k8s/fixtures/nometadata.yaml"'
            in str(context.exception), context.exception)

        with self.assertRaises(RuntimeError) as context:
            next(
                get_template_contexts(
                    'k8s_handle/k8s/fixtures/nometadataname.yaml'))
        self.assertTrue(
            'Field "metadata->name" not found (or empty) in file "k8s_handle/k8s/fixtures/nometadataname.yaml"'
            in str(context.exception), context.exception)

        context = next(
            get_template_contexts('k8s_handle/k8s/fixtures/valid.yaml'))
        self.assertEqual(context.get('kind'), 'Service')
        self.assertEqual(context.get('apiVersion'), 'v1')
        self.assertEqual(context.get('metadata').get('name'), 'my-service')
        self.assertEqual(
            context.get('spec').get('selector').get('app'), 'my-app')

        context = next(
            get_template_contexts(
                'k8s_handle/k8s/fixtures/deployment_wo_replicas.yaml'))
        self.assertEqual(context.get('spec').get('replicas'), 1)
Esempio n. 4
0
 def run(self, file_path):
     for template_body in get_template_contexts(file_path):
         self._is_deprecated(
             template_body.get('apiVersion'),
             template_body.get('kind'),
         )
Esempio n. 5
0
 def _destroy_all(self, file_path):
     for template_body in get_template_contexts(file_path):
         self._destroy(template_body, file_path)