java icon indicating copy to clipboard operation
java copied to clipboard

Getting io.kubernetes.client.ApiException: Forbidden while using AppsV1beta1Api.listNamespacedDeployment in Cluster

Open GiriManas opened this issue 7 years ago • 3 comments

I am using io.kubernetes --> client-java --> version --> 4.0.0,

I am trying to list all the deployments using AppsV1beta1Api v1beta1Api = new AppsV1beta1Api(); AppsV1beta1DeploymentList deploymentsList = v1beta1Api.listNamespacedDeployment("default", null, "true", null,null, null, 1000, null, null, null);

I am getting proper result in my local machine where I have configured KUBECONFIG.

But when I am running as a service in Kubernates creating a POD and inside the POD its trying to invoke the code using TestNG.XML then its giving Forbidden error Error Below: io.kubernetes.client.ApiException: Forbidden Exception Occured while build verification for SDP_UI: Forbidden at io.kubernetes.client.ApiClient.handleResponse(ApiClient.java:882) at io.kubernetes.client.ApiClient.execute(ApiClient.java:798) at io.kubernetes.client.apis.AppsV1beta1Api.listNamespacedDeploymentWithHttpInfo(AppsV1beta1Api.java:2539) at io.kubernetes.client.apis.AppsV1beta1Api.listNamespacedDeployment(AppsV1beta1Api.java:2516)

Have setup rbac in roles.yaml rules:

  • apiGroups: [""] resources: ["pods","pods/exec"] verbs: ["get", "list", "watch", "create"]
  • apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"]
  • apiGroups: [""] resources: ["deployments"] verbs: ["get", "list", "watch", "create","update","patch","delete"] {{- end }}

Still facing the same problem

GiriManas avatar Jul 23 '19 11:07 GiriManas

But when I am running as a service in Kubernates creating a POD and inside the POD its trying to invoke the code using TestNG.XML then its giving Forbidden error

when requesting inside a container, it's actually identified as service-account, you are supposed to properly setup rbac permission for the service-account as well.

yue9944882 avatar Jul 23 '19 12:07 yue9944882

Thanks for your quick response ... I have configured rbac but still I am facing the same issue

---ROLEBINDING.YAML--- {{- if .Values.rbac.create }} kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: {{ template "xyz.fullname" . }} namespace: default labels: app: {{ template "xyz.name" . }} chart: {{ template "xyz.chart" . }} release: {{ .Release.Name }} heritage: {{ .Release.Service }} subjects:

  • kind: ServiceAccount name: {{ template "xyz.fullname" . }} roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: {{ template "xyz.fullname" . }} {{- end }}

---ROLE.YAML---

{{- if .Values.rbac.create }} apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: {{ template "xyz.fullname" . }} namespace: default labels: app: {{ template "xyz.name" . }} chart: {{ template "xyz.chart" . }} release: {{ .Release.Name }} heritage: {{ .Release.Service }} rules:

  • apiGroups: [""] resources: ["pods","pods/exec"] verbs: ["get", "list", "watch", "create"]
  • apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"]
  • apiGroups: [""] resources: ["deployments"] verbs: ["get", "list", "watch", "create","update","patch","delete"] {{- end }}

GiriManas avatar Jul 23 '19 12:07 GiriManas

I don't think your service account name is correct, if you aren't setting a service account when you create the pods, it will use the default service account. Without seeing your pod definition, it's hard to know, but I don't think this is an issue in the library, it is an issue with your usage of Kubernetes RBAC. I'm going to close this issue, please re-open if you have further problems.

brendandburns avatar Jul 23 '19 23:07 brendandburns