def test_if_multiple_labels_are_set(self):
        definition = {
            'kind': 'Deployment',
            'spec': {
                'template': {
                    'spec': {
                        'containers': [
                            {
                                'name': 'nginx-container',
                                'image': 'nginx',
                            },
                        ],
                    },
                },
            },
        }
        pod_labels = {
            'app': 'nginx',
            'tier': 'frontend',
        }

        new_definition = definition_transformers.add_labels(definition, labels={}, pod_labels=pod_labels)

        new_labels = new_definition['spec']['template']['metadata']['labels']
        assert new_labels == {
            'app': 'nginx',
            'tier': 'frontend',
        }
    def test_with_existing_labels(self):
        definition = {
            'kind': 'Deployment',
            'spec': {
                'template': {
                    'metadata': {
                        'labels': {
                            'app': 'nginx',
                            'tier': 'frontend',
                        }
                    },
                    'spec': {
                        'containers': [
                            {
                                'name': 'nginx-container',
                                'image': 'nginx',
                            },
                        ],
                    },
                },
            },
        }
        pod_labels = {
            'tier': 'backend',
            'track': 'canary',
        }

        new_definition = definition_transformers.add_labels(definition, labels={}, pod_labels=pod_labels)

        new_labels = new_definition['spec']['template']['metadata']['labels']
        assert new_labels == {
            'app': 'nginx',
            'tier': 'backend',
            'track': 'canary',
        }
    def test_if_pod_label_is_set(self):
        definition = {
            'kind': 'Deployment',
            'spec': {
                'template': {
                    'spec': {
                        'containers': [
                            {
                                'name': 'nginx-container',
                                'image': 'nginx',
                            },
                        ],
                    },
                },
            },
        }
        pod_labels = {
            'app': 'nginx',
        }

        new_definition = definition_transformers.add_labels(definition, labels={}, pod_labels=pod_labels)

        new_metadata = new_definition['spec']['template']['metadata']
        assert 'labels' in new_metadata
        assert new_metadata['labels'] == {
            'app': 'nginx',
        }
Example #4
0
def transform_pod_definition(definition, options):
    new_definition = copy.deepcopy(definition)
    new_definition = definition_transformers.tag_untaged_images(
        new_definition, options.build_tag)
    new_definition = definition_transformers.set_environment(
        new_definition, options.environment)
    new_definition = definition_transformers.add_host_volumes(
        new_definition, options.host_volumes)
    new_definition = definition_transformers.add_labels(
        new_definition, options.labels, options.pod_labels)
    new_definition = definition_transformers.add_annotations(
        new_definition, options.annotations, options.pod_annotations)
    return new_definition