Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a new namespace in a project using this client? #16

Closed
JeffreyDD opened this issue Oct 30, 2020 · 3 comments
Closed

How to create a new namespace in a project using this client? #16

JeffreyDD opened this issue Oct 30, 2020 · 3 comments

Comments

@JeffreyDD
Copy link

I'm trying to create a namespace inside a project, but have not been able to find the proper method to do so. I have been able to create projects, apply actions, but I can't seem to find any way to create a namespace, either as an action on a project or cluster, nor using client.create.

I'm using the master branch of this client with rancher API version 3

Some of the things I've tried so far:

rancher_namespace = self.rancher_client.create(type='namespace', clusterId=rancher_cluster.id, projectId=rancher_project.id, name=name)
# KeyError: 'namespace'

rancher_cluster = self.rancher_client.by_id_cluster(rancher_project.clusterId);
rancher_namespace = rancher_cluster.namespaces().create()

rancher_namespace = self.rancher_client.create_namespace()

I've noticed that namespaces are managed as a subresource of clusters, and are accessible through the API as such (rancher_namespace = self.rancher_client.), but how can I get these through this client? I've read through the source code and can't seem to find any way to accomplish this, can someone maybe point me in the right direction?

@rule88
Copy link

rule88 commented Nov 1, 2020

Hi @JeffreyDD ,

I see that it isn't very straight forward to create a namespace with this client, but figured out a way to do this by 'abusing' the _post function of the client (directly posting to the Rancher API trough the client).

I have found that the create function, which is only available at the root, has access to some sort of types schema:

client-python/rancher.py

Lines 433 to 435 in c357e7a

def create(self, type, *args, **kw):
collection_url = self.schema.types[type].links.collection
return self._post(collection_url, data=self._to_dict(*args, **kw))

(for a complete list of types, try piece of python: [ type for type in client.schema.types ] )

You would expect namespace to be one of them, but unfortunately this isn't the case. These types are not part of this client, but are provided by the rancher API (thus if desired to have it available an issue at Rancher itself should be filed).

However, in that same block you see how the Post method is build up, to get an understanding of what information could be provided as data you can explore the API trough the Rancher GUI (e.g. https:///v3/cluster//namespaces --> Create Operation).

I came up with the following:

cluster = client.by_id_cluster('c-6lhz6')
namespaces=cluster.namespaces()
client._post(namespaces.createTypes[namespaces.resourceType],data={'name':'new','projectId':'c-6lhz6:p-xvqq2'})

That created a namespace named 'new' in my 'default' project, whereas off-course the id's will differ in your case.

@dramich
Copy link
Contributor

dramich commented Mar 4, 2021

It looks like this is answered so going to close this issue, if not feel free to reopen

@dramich dramich closed this as completed Mar 4, 2021
@ADLebrero
Copy link

ADLebrero commented Mar 29, 2022

Based on what @rule88 said, I´ve found this:

client2 = rancher.Client(url='<<your_server_URL>>/v3/cluster/<<CLUSTER_ID>, access_key=access_key, secret_key=secret_key)

client2.create_namespace(name="NAMESPACE_NAME" projectId="CLUSTER_ID:PROJECT_ID")

The trick is connect the client to the right api url, on which the namespace object type exists

Hope this help others like me trying to use the python client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants