fabric8io/fabric8

Null pointer exception while deploying ClusterRoleBinding using io/fabric8 kubernetes api.

q2w opened this issue · 3 comments

q2w commented

I am using io.fabric8:kubernetes-client:3.2.0.
There is a ClusterRoleBinding type available in io/fabric8 but, no handler for the same is available. It leads to
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException.

The below yaml, i am deploying.

apiVersion: rbac.authorization.k8s.io/v1beta
kind: ClusterRoleBinding
metadata:
labels:
app: horovod-1
name: horovod-1
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: horovod-1
namespace: kubeflow

    List<HasMetadata> metadataResponse = client.load(bais).inNamespace(
            namespace).createOrReplace();

If there is another way to create ClusterRoleBinding from a java program, please let me know. It would be of great help for my use case.

Thanks.

q2w commented

I did it using kubernetes java-client itself.

@q2w
Hi,
I am using io.fabric8:kubernetes-client:3.2.0. I am facing the same issue of null pointer exception. The below yaml, i am deploying.

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: cluster-admin-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:

  • kind: ServiceAccount
    name: test
    namespace: test

Can you please explain how you did it using kubernetes java-client?

Thanks,

q2w commented

@ArundathiGV
Sure.
First you need to create an object V1beta1ClusterRoleBinding (present in kubernetes java-client) and then submit to kubernetes master.

Like for this case....

    V1beta1ClusterRoleBinding v1beta1ClusterRoleBinding = new V1beta1ClusterRoleBinding();
    v1beta1ClusterRoleBinding.setApiVersion("rbac.authorization.k8s.io/v1beta1");
    v1beta1ClusterRoleBinding.setKind("ClusterRoleBinding");
    V1ObjectMeta v1ObjectMeta = new V1ObjectMeta();
    v1ObjectMeta.setName("cluster-admin-binding");
    v1beta1ClusterRoleBinding.setMetadata(v1ObjectMeta);
    V1beta1RoleRef v1beta1RoleRef = new V1beta1RoleRef();
    v1beta1RoleRef.setApiGroup("rbac.authorization.k8s.io");
    v1beta1RoleRef.setKind("ClusterRole");
    v1beta1RoleRef.setName("cluster-admin");
    v1beta1ClusterRoleBinding.setRoleRef(v1beta1RoleRef);
    V1beta1Subject v1beta1Subject = new V1beta1Subject();
    v1beta1Subject.setName("test");
    v1beta1Subject.setKind("ServiceAccount");
    v1beta1Subject.setNamespace("test");
    List<V1beta1Subject> list = new ArrayList<>();
    list.add(v1beta1Subject);
    v1beta1ClusterRoleBinding.setSubjects(list);

try {
rbacAuthorizationV1beta1Api.createClusterRoleBinding(v1beta1ClusterRoleBinding,
"true");
} catch (ApiException e) {
e.printStackTrace();
}