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

whats the canonical YAML way to add a Role and a RoleBinding for a ServiceAccount into a Template? #11566

Closed
jstrachan opened this issue Oct 25, 2016 · 16 comments

Comments

@jstrachan
Copy link
Contributor

I've seen how to add roles via the CLI, but how should we define the required Role and RoleBinding for a ServiceAccount inside an OpenShift Template?

I could create a role OK:


---
apiVersion: "v1"
kind: "Role"
metadata:
  labels:
    provider: "fabric8"
    project: "jenkins-openshift"
  name: "jenkins"
rules:
- resources:
  - "pod"
  verbs:
  - "create"
  - "delete"
  - "deletecollection"
  - "get"
  - "list"
  - "watch"

then struggled to refer to the role in the RoleBinding


---
apiVersion: "v1"
kind: "RoleBinding"
metadata:
  labels:
    provider: "fabric8"
    project: "jenkins-openshift"
  name: "jenkins"
roleRef:
  name: "jenkins"
subjects:
- kind: "ServiceAccount"
  name: "jenkins"

Is there a way to refer to a role in the current namespace? I've seen system:admin to refer to global roles but haven't seen the magic prefix to refer to a local role inside a namespace.

Any ideas?

@jstrachan jstrachan changed the title whats the canonical YAML to add a Role and a RoleBinding for a ServiceAccount into a Template? whats the canonical YAML way to add a Role and a RoleBinding for a ServiceAccount into a Template? Oct 25, 2016
@liggitt
Copy link
Contributor

liggitt commented Oct 25, 2016

specify a namespace in the roleRef to refer to a locally defined role. note that a policybinding for for the local namespace must exist before you can bind to locally defined roles. You can create that with oc create policybinding <myns>

@jstrachan
Copy link
Contributor Author

thanks @liggitt

So I need to do something like this...

---
apiVersion: "v1"
kind: "RoleBinding"
metadata:
  labels:
    provider: "fabric8"
    project: "jenkins-openshift"
  name: "jenkins"
roleRef:
  name: "${NAMESPACE}:jenkins"

then define the NAMESPACE expression in a Template parameter or something? I was hoping there was a notation to refer to a namespace local role - I guess not?

@liggitt
Copy link
Contributor

liggitt commented Oct 26, 2016

No,

roleRef:
  name: myrole
  namespace: myns

@liggitt
Copy link
Contributor

liggitt commented Oct 26, 2016

Ah, the issue with not knowing the destination namespace when processing a template.

@jstrachan
Copy link
Contributor Author

jstrachan commented Oct 26, 2016

@liggitt yeah ;) I was hoping we could get a template parameter defined for the namespace the template is being created inside; like the downward API on env vars can be used to define an env var for a namespace - but I don't see how.

If the namespace is missed off the roleRef I would have thought it'd assume the current namespace though? If it was a global or system role I thought you'd have to specify that explicitly using some magic name like "system" or something?

e.g. when you create a kubernetes resource there's metadata.namespace and you leave it blank if you want the current namespace & things just work - then kubernetes will fill in empty namespaces with the current one. It feels like the RoleBinding and PolicyBinding should work the same really

@bparees
Copy link
Contributor

bparees commented Feb 22, 2017

you can now set the namespace field in terms of a parameter value. There is still no automatic/implicit parameter that tells you the current namespace though, you have to collect that value from the user as a parameter.

Might be good enough to close this, though.

@jim-minter
Copy link
Contributor

@liggitt @smarterclayton there was not unreasonable reluctance recently (#16021) to add the capability for templates to have a defaulting NAMESPACE parameter, and I think this is the number one use case. To get the current namespace into pod environment variables, downward API (env/valueFrom/fieldRef/fieldPath/metadata.namespace) can be used. Would it be possible to add an equivalent to the RoleBinding roleRef field? It would probably mean we could do away with having NAMESPACE as a parameter in quite a lot of templates.

@jim-minter
Copy link
Contributor

@jorgemoralespou fyi

@liggitt
Copy link
Contributor

liggitt commented Sep 12, 2017

If you target 3.7+, you can use kubernetes RBAC objects for roles/bindings, and when specifying a roleRef, you must specify kind of ClusterRole or Role, and a ref to a Role is automatically to the rolebinding's namespace

@smarterclayton
Copy link
Contributor

smarterclayton commented Sep 13, 2017 via email

@bparees
Copy link
Contributor

bparees commented Sep 13, 2017

sounds closeable.

@bparees bparees closed this as completed Sep 13, 2017
@jim-minter
Copy link
Contributor

This already works on 3.4+ - use the ref with no namespace and kind service
account.

@smarterclayton I'm not following - doesn't that just avoid the need to specify the namespace of the SA? I'm talking about the namespace of the Role. Every time I don't specify it, OpenShift interprets it as meaning a ClusterRole.

@liggitt
Copy link
Contributor

liggitt commented Sep 13, 2017

Every time I don't specify it, OpenShift interprets it as meaning a ClusterRole.

Right. The kube RBAC types specify kind: Role in the roleRef

@anderssv
Copy link

A bit unclear to me. Anyone verified this pre 3.7? We're on 3.6 and the following does not work without specifying the namespace:

apiVersion: v1
kind: RoleBinding
metadata:
  name: openshift-cluster-basetest
roleRef:
  kind: Role
  name: openshift-cluster-basetest
  namespace: cluster-test
subjects:
- kind: ServiceAccount
  name: openshift-cluster-basetest

@parberge
Copy link

@anderssv On 3.7 I still need to specify namespace when doing the RoleBinding

@Asgoret
Copy link

Asgoret commented Jun 9, 2018

Hi @jstrachan, can you help with my case?)
Whell....i catch erorr after you fix this errot) Now, you didnt specify namespaceinsubject, BUT if you dont write namespace RoleBinding end without error, but past user without namespace. See file.

Full code:

kind: ServiceAccount
apiVersion: v1
metadata:
  name: test
secrets:
- fieldPath: metadata.namespace

kind: RoleBinding
apiVersion: v1
metadata:
 name: test
roleRef:
 apiGroup: v1
 kind: Role
 name: admin
subjects:
- apiVersion: v1
  fieldPath: metadata.namespace
  kind: ServiceAccount
  name: test

image

And if i add namespace: test to subjects:
image

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

No branches or pull requests

9 participants