def containers(self):
     objs = []
     for c in self.model.spec.template.spec.containers:
         k8scontainer = K8sContainer(name=c.name, image=c.image)
         k8scontainer.model = c
         objs.append(k8scontainer)
     return objs
Beispiel #2
0
 def containers(self):
     _list = []
     for c in self.model.spec.containers:
         k8scontainer = K8sContainer(name=c.name, image=c.image)
         k8scontainer.model = c
         _list.append(k8scontainer)
     return _list
 def test_add_capabilities(self):
     name = "yomama"
     image = "redis"
     c = K8sContainer(name=name, image=image)
     c.add_capabilities(['NET_RAW'])
     self.assertIsInstance(c.capabilities, Capabilities)
     self.assertIsInstance(c.capabilities.add, list)
     self.assertEqual(c.capabilities.add, ['NET_RAW'])
Beispiel #4
0
 def test_struct_k8scontainer(self):
     name = "yomama"
     image = "redis:latest"
     c = K8sContainer(name=name, image=image)
     self.assertIsNotNone(c)
     self.assertIsInstance(c, K8sContainer)
     self.assertIsNotNone(c.model)
     self.assertIsInstance(c.model, Container)
Beispiel #5
0
 def test_struct_json(self):
     name = "yomama"
     image = "redis:latest"
     c = K8sContainer(name=name, image=image)
     j = c.serialize()
     self.assertIsInstance(j, dict)
     for i in ["image", "imagePullPolicy", "name"]:
         self.assertIn(i, j)
Beispiel #6
0
 def test_drop_capabilities(self):
     name = "yomama"
     image = "redis"
     c = K8sContainer(name=name, image=image)
     c.drop_capabilities(["NET_RAW"])
     self.assertIsInstance(c.capabilities, Capabilities)
     self.assertIsInstance(c.capabilities.drop, list)
     self.assertEqual(c.capabilities.drop, ["NET_RAW"])
Beispiel #7
0
 def test_init_with_name_and_image(self):
     name = "yomama"
     image = "redis"
     c = K8sContainer(name=name, image=image)
     self.assertIsNotNone(c)
     self.assertIsInstance(c, K8sContainer)
     self.assertEqual(c.model.name, name)
     self.assertEqual(c.model.image, image)
Beispiel #8
0
 def test_add_volume_mount_invalid_args(self):
     name = "yomama"
     image = "redis"
     vname = object()
     vmount = object()
     with self.assertRaises(SyntaxError):
         mount = K8sVolumeMount(name=vname, mount_path=vmount)
         c = K8sContainer(name=name, image=image)
         c.add_volume_mount(mount)
Beispiel #9
0
 def test_pod_add_container(self):
     name = "yoname"
     pod = _utils.create_pod(name=name)
     self.assertEqual(0, len(pod.model.spec.containers))
     name = "yopod"
     image = "redis"
     c = K8sContainer(name=name, image=image)
     pod.add_container(c)
     self.assertEqual(1, len(pod.model.spec.containers))
     self.assertEqual(c.model, pod.model.spec.containers[0])
Beispiel #10
0
 def test_add_volume_mount(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     volname = "vol1"
     volpath = "/path/on/container"
     mount = K8sVolumeMount(name=volname, mount_path=volpath)
     c.add_volume_mount(mount)
     self.assertEqual(1, len(c.volume_mounts))
     self.assertIn(mount.model, c.volume_mounts)
Beispiel #11
0
 def test_serialize(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     volname = "vol1"
     volpath = "/path/on/container"
     mount = K8sVolumeMount(name=volname, mount_path=volpath)
     c.add_volume_mount(mount)
     data = c.serialize()
     self.assertIsInstance(data, dict)
Beispiel #12
0
 def test_add_readiness_probe(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     probe = {"httpGet": {"path": "/admin/health", "port": 8086, "scheme": "HTTP"}}
     c.add_readiness_probe(**probe)
     self.assertIsInstance(c.readiness_probe, Probe)
     self.assertEqual(c.readiness_probe.http_get_action.path, probe["httpGet"]["path"])
     self.assertEqual(c.readiness_probe.http_get_action.port, probe["httpGet"]["port"])
     data = c.readiness_probe.serialize()
     self.assertEqual(probe, data)
Beispiel #13
0
 def test_as_json(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     volname = "vol1"
     volpath = "/path/on/container"
     mount = K8sVolumeMount(name=volname, mount_path=volpath)
     c.add_volume_mount(mount)
     j = c.as_json()
     self.assertIsInstance(j, str)
     d = json.loads(j)
     self.assertIsInstance(d, dict)
Beispiel #14
0
 def test_add_liveness_probe(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     probe = {"initialDelaySeconds": 15, "tcpSocket": {"port": "8086"}, "timeoutSeconds": 1}
     c.add_liveness_probe(**probe)
     self.assertIsInstance(c.liveness_probe, Probe)
     self.assertEqual(c.liveness_probe.initial_delay_seconds, probe["initialDelaySeconds"])
     self.assertEqual(c.liveness_probe.tcp_socket_action.port, str(probe["tcpSocket"]["port"]))
     self.assertEqual(c.liveness_probe.timeout_seconds, probe["timeoutSeconds"])
     data = c.liveness_probe.serialize()
     self.assertEqual(probe, data)
Beispiel #15
0
 def test_as_yaml(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     volname = "vol1"
     volpath = "/path/on/container"
     mount = K8sVolumeMount(name=volname, mount_path=volpath)
     c.add_volume_mount(mount)
     y = c.as_yaml()
     self.assertIsInstance(y, str)
     d = yaml.load(y)
     self.assertIsInstance(d, dict)
Beispiel #16
0
    def test_get_containers(self):
        name = "yoname"
        pod = _utils.create_pod(name=name)

        count = 3
        for i in range(0, 3):
            name = "yocontainer_{0}".format(i)
            image = "redis"
            c = K8sContainer(name=name, image=image)
            pod.add_container(c)

        self.assertIsNotNone(pod.containers)
        self.assertEqual(count, len(pod.containers))
        [self.assertIsInstance(x, K8sContainer) for x in pod.containers]
 def test_add_readiness_probe(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     probe = {
         'httpGet': {
             'path': '/admin/health',
             'port': 8086,
             'scheme': 'HTTP'
         }
     }
     c.add_readiness_probe(**probe)
     self.assertIsInstance(c.readiness_probe, Probe)
     self.assertEqual(c.readiness_probe.http_get_action.path,
                      probe['httpGet']['path'])
     self.assertEqual(c.readiness_probe.http_get_action.port,
                      probe['httpGet']['port'])
     data = c.readiness_probe.serialize()
     self.assertEqual(probe, data)
 def test_add_liveness_probe(self):
     name = "redis"
     image = "redis:3.0.7"
     c = K8sContainer(name=name, image=image)
     probe = {
         'initialDelaySeconds': 15,
         'tcpSocket': {
             'port': '8086'
         },
         'timeoutSeconds': 1
     }
     c.add_liveness_probe(**probe)
     self.assertIsInstance(c.liveness_probe, Probe)
     self.assertEqual(c.liveness_probe.initial_delay_seconds,
                      probe['initialDelaySeconds'])
     self.assertEqual(c.liveness_probe.tcp_socket_action.port,
                      str(probe['tcpSocket']['port']))
     self.assertEqual(c.liveness_probe.timeout_seconds,
                      probe['timeoutSeconds'])
     data = c.liveness_probe.serialize()
     self.assertEqual(probe, data)
Beispiel #19
0
    def __init__(self, config=None, name=None, image=None, replicas=0):

        super(K8sDeployment, self).__init__(config=config,
                                            obj_type="Deployment",
                                            name=name)

        self.desired_replicas = replicas

        labels = {"name": name}
        sel = LabelSelector()
        sel.match_labels = labels

        self.selector = sel
        self.labels = labels
        self.pod_labels = labels

        if image is not None:
            container = K8sContainer(name=name, image=image)
            self.add_container(container)

        if self.config.pull_secret is not None:
            self.add_image_pull_secrets(self.config.pull_secret)
Beispiel #20
0
def create_container(name=None, image="redis"):
    return K8sContainer(name=name, image=image)
Beispiel #21
0
 def test_add_volume_mount_no_args(self):
     name = "yomama"
     image = "redis"
     c = K8sContainer(name=name, image=image)
     with self.assertRaises(SyntaxError):
         c.add_volume_mount()
Beispiel #22
0
 def test_drop_capabilities_invalid_args(self):
     name = "yomama"
     image = "redis"
     with self.assertRaises(SyntaxError):
         c = K8sContainer(name=name, image=image)
         c.drop_capabilities("NET_RAW")
Beispiel #23
0
 def test_init_image_only(self):
     image = "redis"
     with self.assertRaises(SyntaxError):
         K8sContainer(image=image)
Beispiel #24
0
 def test_init_name_only(self):
     name = "yomama"
     with self.assertRaises(SyntaxError):
         K8sContainer(name=name)
Beispiel #25
0
 def test_init_no_args(self):
     with self.assertRaises(SyntaxError):
         K8sContainer()