This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
/
config.py
163 lines (142 loc) · 4.44 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Configuration code.
"""
import base64
import copy
import tempfile
import yaml
class KubeConfig(object):
"""
Main configuration class.
"""
def __init__(self, filename):
"""
Creates an instance of the KubeConfig class.
:Parameters:
- `filename`: The full path to the configuration file
"""
self.filename = filename
self.doc = None
self.current_context = None
def parse(self):
"""
Parses the configuration file.
"""
if self.doc is not None:
# TODO: Warn if there is nothing to parse?
return
with open(self.filename) as f:
self.doc = yaml.load(f.read())
if "current-context" in self.doc and self.doc["current-context"]:
self.set_current_context(self.doc["current-context"])
def set_current_context(self, value):
"""
Sets the context to the provided value.
:Parameters:
- `value`: The value for the current context
"""
self.current_context = value
@property
def clusters(self):
"""
Returns known clusters by exposing as a read-only property.
"""
if not hasattr(self, "_clusters"):
self.parse()
cs = {}
for cr in self.doc["clusters"]:
cs[cr["name"]] = c = copy.deepcopy(cr["cluster"])
if "server" not in c:
c["server"] = "http://localhost"
BytesOrFile.maybe_set(c, "certificate-authority")
self._clusters = cs
return self._clusters
@property
def users(self):
"""
Returns known users by exposing as a read-only property.
"""
if not hasattr(self, "_users"):
self.parse()
us = {}
for ur in self.doc["users"]:
us[ur["name"]] = u = copy.deepcopy(ur["user"])
BytesOrFile.maybe_set(u, "client-certificate")
BytesOrFile.maybe_set(u, "client-key")
self._users = us
return self._users
@property
def contexts(self):
"""
Returns known contexts by exposing as a read-only property.
"""
if not hasattr(self, "_contexts"):
self.parse()
cs = {}
for cr in self.doc["contexts"]:
cs[cr["name"]] = copy.deepcopy(cr["context"])
self._contexts = cs
return self._contexts
@property
def cluster(self):
"""
Returns the current selected cluster by exposing as a
read-only property.
"""
self.parse()
if self.current_context is None:
raise Exception("current context not set; call set_current_context")
return self.clusters[self.contexts[self.current_context]["cluster"]]
@property
def user(self):
"""
Returns the current user by exposing as a read-only property.
"""
self.parse()
if self.current_context is None:
raise Exception("current context not set; call set_current_context")
return self.users[self.contexts[self.current_context]["user"]]
class BytesOrFile(object):
"""
Implements the same interface for files and byte input.
"""
@classmethod
def maybe_set(cls, d, key):
file_key = key
data_key = "{}-data".format(key)
if data_key in d:
d[file_key] = cls(d[data_key])
del d[data_key]
elif file_key in d:
d[file_key] = cls(d[file_key])
def __init__(self, data):
"""
Creates a new instance of BytesOrFile.
:Parameters:
- `data`: A full path to a file or base64 encoded bytes
"""
self._filename = None
self._bytes = None
if data.startswith("/"):
self._filename = data
else:
self._bytes = base64.b64decode(data)
def bytes(self):
"""
Returns the provided data as bytes.
"""
if self._filename:
with open(self._filename, "rb") as f:
return f.read()
else:
return self._bytes
def filename(self):
"""
Returns the provided data as a file location.
"""
if self._filename:
return self._filename
else:
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(self._bytes)
return f.name