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

feat: Add serviceaccount permissions for <username>-che as default namespace #469

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ export class KubeHelper {
}
}

async createClusterRoleBindingFromFile(filePath: string, namespace: string) {
const yamlRoleBinding = this.safeLoadFromYamlFile(filePath) as V1ClusterRoleBinding
if (yamlRoleBinding.subjects && namespace) {
for (const subject of yamlRoleBinding.subjects) {
subject.namespace = namespace
}
}

const k8sRbacAuthApi = this.kc.makeApiClient(RbacAuthorizationV1Api)
try {
return await k8sRbacAuthApi.createClusterRoleBinding(yamlRoleBinding)
} catch (e) {
throw this.wrapK8sClientError(e)
}
}

async createClusterRoleBinding(name: string, saName: string, saNamespace = '', roleName = '') {
const clusterRoleBinding = {
apiVersion: 'rbac.authorization.k8s.io/v1',
Expand Down Expand Up @@ -367,6 +383,25 @@ export class KubeHelper {
}
}

async replaceClusterRoleBindingFromFile(filePath: string, namespace: string) {
const yamlClusterRoleBinding = this.safeLoadFromYamlFile(filePath) as V1ClusterRoleBinding
if (!yamlClusterRoleBinding.metadata || !yamlClusterRoleBinding.metadata.name) {
throw new Error(`Cluster role binding read from ${filePath} must have name specified`)
}

const k8sRbacAuthApi = this.kc.makeApiClient(RbacAuthorizationV1Api)
try {
if (yamlClusterRoleBinding.subjects && namespace) {
for (const subject of yamlClusterRoleBinding.subjects) {
subject.namespace = namespace
}
}
return await k8sRbacAuthApi.replaceClusterRoleBinding(yamlClusterRoleBinding.metadata.name, yamlClusterRoleBinding)
} catch (e) {
throw this.wrapK8sClientError(e)
}
}

async deleteRoleBinding(name = '', namespace = '') {
const k8sRbacAuthApi = this.kc.makeApiClient(RbacAuthorizationV1Api)
try {
Expand Down
157 changes: 157 additions & 0 deletions src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class OperatorTasks {
operatorClusterRole = 'che-operator'
operatorRoleBinding = 'che-operator'
operatorClusterRoleBinding = 'che-operator'

cheClusterRoleCreateNamespaces = 'che-create-namespaces'
cheOperatorClusterRoleBindingCreateNamespaces = 'che-operator-create-namespaces'

cheClusterRole = 'che-manage-namespaces'
cheClusterRoleBinding = 'che-operator-che'

cheClusterCrd = 'checlusters.org.eclipse.che'
operatorName = 'che-operator'
operatorCheCluster = 'eclipse-che'
Expand Down Expand Up @@ -105,6 +112,64 @@ export class OperatorTasks {
}
}
},
{
title: `Create ClusterRole ${this.cheClusterRoleCreateNamespaces}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleExist(this.cheClusterRoleCreateNamespaces)
if (exist) {
task.title = `${task.title}...It already exists.`
} else {
const yamlFilePath = this.resourcesPath + 'cluster_role_createns.yaml'
const statusCode = await kube.createClusterRoleFromFile(yamlFilePath)
if (statusCode === 403) {
command.error('ERROR: It looks like you don\'t have enough privileges. You need to grant more privileges to current user or use a different user. If you are using minishift you can "oc login -u system:admin"')
}
task.title = `${task.title}...done.`
}
}
},
{
title: `Create ClusterRoleBinding ${this.cheOperatorClusterRoleBindingCreateNamespaces} in namespace ${flags.chenamespace}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleBindingExist(this.cheOperatorClusterRoleBindingCreateNamespaces)
if (exist) {
task.title = `${task.title}...It already exists.`
} else {
const yamlFilePath = this.resourcesPath + 'cluster_role_binding_createns.yaml'
await kube.createClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...done.`
}
}
},
{
title: `Create Cluster role ${this.cheClusterRole}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleExist(this.cheClusterRole)
if (exist) {
task.title = `${task.title}...It already exists.`
} else {
const yamlFilePath = this.resourcesPath + 'cluster_role_che.yaml'
const statusCode = await kube.createClusterRoleFromFile(yamlFilePath)
if (statusCode === 403) {
command.error('ERROR: It looks like you don\'t have enough privileges. You need to grant more privileges to current user or use a different user. If you are using minishift you can "oc login -u system:admin"')
}
task.title = `${task.title}...done.`
}
}
},
{
title: `Create RoleBinding ${this.cheClusterRoleBinding} in namespace ${flags.chenamespace}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleBindingExist(this.cheClusterRoleBinding)
if (exist) {
task.title = `${task.title}...It already exists.`
} else {
const yamlFilePath = this.resourcesPath + 'cluster_role_binding_che.yaml'
await kube.createClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...done.`
}
}
},
{
title: `Create RoleBinding ${this.operatorRoleBinding} in namespace ${flags.chenamespace}`,
task: async (_ctx: any, task: any) => {
Expand Down Expand Up @@ -301,6 +366,62 @@ export class OperatorTasks {
}
}
},
{
title: `Update ClusterRole ${this.cheClusterRoleCreateNamespaces}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleExist(this.cheClusterRoleCreateNamespaces)
const yamlFilePath = this.resourcesPath + 'cluster_role_createns.yaml'
if (exist) {
await kube.replaceClusterRoleFromFile(yamlFilePath)
task.title = `${task.title}...updated.`
} else {
await kube.createClusterRoleFromFile(yamlFilePath)
task.title = `${task.title}...created a new one.`
}
}
},
{
title: `Update ClusterRoleBinding ${this.cheOperatorClusterRoleBindingCreateNamespaces} in namespace ${flags.chenamespace}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleBindingExist(this.cheOperatorClusterRoleBindingCreateNamespaces)
const yamlFilePath = this.resourcesPath + 'cluster_role_binding_createns.yaml'
if (exist) {
await kube.replaceClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...updated.`
} else {
await kube.createClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...created new one.`
}
}
},
{
title: `Update ClusterRole ${this.cheClusterRole}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleExist(this.cheClusterRole)
const yamlFilePath = this.resourcesPath + 'cluster_role_che.yaml'
if (exist) {
await kube.replaceClusterRoleFromFile(yamlFilePath)
task.title = `${task.title}...updated.`
} else {
await kube.createClusterRoleFromFile(yamlFilePath)
task.title = `${task.title}...created a new one.`
}
}
},
{
title: `Update ClusterRoleBinding ${this.cheClusterRoleBinding} in namespace ${flags.chenamespace}`,
task: async (_ctx: any, task: any) => {
const exist = await kube.clusterRoleBindingExist(this.cheClusterRoleBinding)
const yamlFilePath = this.resourcesPath + 'cluster_role_binding_che.yaml'
if (exist) {
await kube.replaceClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...updated.`
} else {
await kube.createClusterRoleBindingFromFile(yamlFilePath, flags.chenamespace)
task.title = `${task.title}...created new one.`
}
}
},
{
title: `Updating Che Cluster CRD ${this.cheClusterCrd}`,
task: async (_ctx: any, task: any) => {
Expand Down Expand Up @@ -412,6 +533,42 @@ export class OperatorTasks {
task.title = await `${task.title}...OK`
}
},
{
title: `Delete cluster role binding ${this.cheOperatorClusterRoleBindingCreateNamespaces}`,
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleBindingExist(this.cheOperatorClusterRoleBindingCreateNamespaces)) {
await kh.deleteClusterRoleBinding(this.cheOperatorClusterRoleBindingCreateNamespaces)
}
task.title = await `${task.title}...OK`
}
},
{
title: `Delete cluster role ${this.cheClusterRoleCreateNamespaces}`,
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleExist(this.cheClusterRoleCreateNamespaces)) {
await kh.deleteClusterRole(this.cheClusterRoleCreateNamespaces)
}
task.title = await `${task.title}...OK`
}
},
{
title: `Delete cluster role binding ${this.cheClusterRoleBinding}`,
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleBindingExist(this.cheClusterRoleBinding)) {
await kh.deleteClusterRoleBinding(this.cheClusterRoleBinding)
}
task.title = await `${task.title}...OK`
}
},
{
title: `Delete cluster role ${this.cheClusterRole}`,
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleExist(this.cheClusterRole)) {
await kh.deleteClusterRole(this.cheClusterRole)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete server and workspace rolebindings',
task: async (_ctx: any, task: any) => {
Expand Down