Skip to content

Commit 28ea35c

Browse files
authored
Merge pull request #141 from fiaas/poddnsconfig
Add PodDNSConfig.
2 parents f6d6653 + e6bf53f commit 28ea35c

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

k8s/fields.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@ def __set__(self, instance, value):
137137
class ListField(Field):
138138
"""ListField is a list (array) of a single type on a model"""
139139

140-
def __init__(self, field_type, default_value=None, name='__unset__'):
140+
def __init__(self, field_type, default_value=None, name='__unset__', empty_as_none=False):
141141
if default_value is None:
142142
default_value = []
143143
super(ListField, self).__init__(field_type, default_value, name=name)
144+
self._empty_as_none = empty_as_none
144145

145146
def dump(self, instance):
146-
return [self._as_dict(v) for v in getattr(instance, self.attr_name)]
147+
v = [self._as_dict(v) for v in getattr(instance, self.attr_name)]
148+
if self._empty_as_none and not v:
149+
return None
150+
return v
147151

148152
def load(self, instance, value):
149153
if value is None:

k8s/models/pod.py

+12
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,25 @@ class Volume(Model):
202202
secret = Field(SecretVolumeSource)
203203

204204

205+
class PodDNSConfigOption(Model):
206+
name = Field(str)
207+
value = Field(str)
208+
209+
210+
class PodDNSConfig(Model):
211+
nameservers = ListField(str, empty_as_none=True)
212+
searches = ListField(str, empty_as_none=True)
213+
options = ListField(PodDNSConfigOption, empty_as_none=True)
214+
215+
205216
class PodSpec(Model):
206217
volumes = ListField(Volume)
207218
containers = ListField(Container)
208219
restartPolicy = Field(str, "Always")
209220
terminationGracePeriodSeconds = Field(int)
210221
activeDeadlineSeconds = Field(int)
211222
dnsPolicy = Field(str, "ClusterFirst")
223+
dnsConfig = Field(PodDNSConfig, None)
212224
nodeName = Field(str)
213225
nodeSelector = Field(dict)
214226
selector = Field(dict)

tests/k8s/test_pod.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# -*- coding: utf-8
33

44
# Copyright 2017-2019 The FIAAS Authors
5-
#
5+
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,8 +20,17 @@
2020

2121
from k8s.client import NotFound
2222
from k8s.models.common import ObjectMeta
23-
from k8s.models.pod import Pod, ContainerPort, Container, LocalObjectReference, PodSpec, Volume, VolumeMount, \
24-
SecretVolumeSource
23+
from k8s.models.pod import (
24+
Pod,
25+
ContainerPort,
26+
Container,
27+
LocalObjectReference,
28+
PodSpec,
29+
PodDNSConfig,
30+
Volume,
31+
VolumeMount,
32+
SecretVolumeSource,
33+
)
2534

2635
NAME = "my-name"
2736
NAMESPACE = "my-namespace"
@@ -34,6 +43,14 @@ def test_create_blank_pod(self):
3443
pod = _create_pod()
3544
assert pod.metadata.name == NAME
3645
assert pod.as_dict()["metadata"]["name"] == NAME
46+
assert "dnsOptions" not in pod.as_dict()["spec"]
47+
48+
def test_create_pod_with_dns_options(self):
49+
pod = _create_pod()
50+
pod.spec.dnsConfig = PodDNSConfig(
51+
searches=["other-namespace.svc.cluster.local"],
52+
)
53+
assert pod.as_dict()["spec"]["dnsConfig"]["searches"] == ["other-namespace.svc.cluster.local"]
3754

3855
def test_pod_created_if_not_exists(self, post, api_get):
3956
api_get.side_effect = NotFound()

0 commit comments

Comments
 (0)