Merge pull request #2578 from aiordache/fix_context_load_without_orch

Make context orchestrator field optional
This commit is contained in:
Ulysses Souza 2020-06-02 15:10:33 +02:00 committed by GitHub
commit a5270865e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 17 deletions

View File

@ -14,11 +14,11 @@ class ContextAPI(object):
Contains methods for context management:
create, list, remove, get, inspect.
"""
DEFAULT_CONTEXT = Context("default")
DEFAULT_CONTEXT = Context("default", "swarm")
@classmethod
def create_context(
cls, name, orchestrator="swarm", host=None, tls_cfg=None,
cls, name, orchestrator=None, host=None, tls_cfg=None,
default_namespace=None, skip_tls_verify=False):
"""Creates a new context.
Returns:
@ -38,9 +38,7 @@ class ContextAPI(object):
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
"StackOrchestrator": "swarm"
},
"Metadata": {},
"Endpoints": {
"docker": {
"Host": "unix:///var/run/docker.sock",
@ -57,7 +55,9 @@ class ContextAPI(object):
ctx = Context.load_context(name)
if ctx:
raise errors.ContextAlreadyExists(name)
endpoint = "docker" if orchestrator == "swarm" else orchestrator
endpoint = "docker"
if orchestrator and orchestrator != "swarm":
endpoint = orchestrator
ctx = Context(name, orchestrator)
ctx.set_endpoint(
endpoint, host, tls_cfg,
@ -79,9 +79,7 @@ class ContextAPI(object):
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
"StackOrchestrator": "swarm"
},
"Metadata": {},
"Endpoints": {
"docker": {
"Host": "unix:///var/run/docker.sock",

View File

@ -11,7 +11,7 @@ from docker.context.config import get_context_host
class Context:
"""A context."""
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None,
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
tls=False):
if not name:
raise Exception("Name not provided")
@ -19,7 +19,7 @@ class Context:
self.orchestrator = orchestrator
if not endpoints:
default_endpoint = "docker" if (
orchestrator == "swarm"
not orchestrator or orchestrator == "swarm"
) else orchestrator
self.endpoints = {
default_endpoint: {
@ -85,7 +85,8 @@ class Context:
context {} : {}""".format(name, e))
return (
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
metadata["Name"],
metadata["Metadata"].get("StackOrchestrator", None),
metadata["Endpoints"])
return None, None, None
@ -162,7 +163,7 @@ class Context:
@property
def Host(self):
if self.orchestrator == "swarm":
if not self.orchestrator or self.orchestrator == "swarm":
return self.endpoints["docker"]["Host"]
return self.endpoints[self.orchestrator]["Host"]
@ -172,18 +173,19 @@ class Context:
@property
def Metadata(self):
meta = {}
if self.orchestrator:
meta = {"StackOrchestrator": self.orchestrator}
return {
"Name": self.name,
"Metadata": {
"StackOrchestrator": self.orchestrator
},
"Metadata": meta,
"Endpoints": self.endpoints
}
@property
def TLSConfig(self):
key = self.orchestrator
if key == "swarm":
if not key or key == "swarm":
key = "docker"
if key in self.tls_cfg.keys():
return self.tls_cfg[key]

View File

@ -50,3 +50,10 @@ class ContextLifecycleTest(BaseAPIIntegrationTest):
ContextAPI.remove_context("test")
with pytest.raises(errors.ContextNotFound):
ContextAPI.inspect_context("test")
def test_load_context_without_orchestrator(self):
ContextAPI.create_context("test")
ctx = ContextAPI.get_context("test")
assert ctx
assert ctx.Name == "test"
assert ctx.Orchestrator is None