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 ValidationWebhookConfiguration #2247

Merged
merged 2 commits into from
Sep 6, 2022
Merged
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
34 changes: 33 additions & 1 deletion src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
V1ServiceAccount,
V1ServiceList,
Watch,
V1CustomResourceDefinition,
V1CustomResourceDefinition, V1ValidatingWebhookConfiguration,
} from '@kubernetes/client-node'
import { Cluster } from '@kubernetes/client-node/dist/config_types'
import axios, { AxiosRequestConfig } from 'axios'
Expand Down Expand Up @@ -374,6 +374,38 @@ export class KubeHelper {
}
}

async isValidatingWebhookConfigurationExists(name: string): Promise<boolean> {
const k8sAdmissionApi = this.kubeConfig.makeApiClient(AdmissionregistrationV1Api)
try {
await k8sAdmissionApi.readValidatingWebhookConfiguration(name)
return true
} catch (e: any) {
if (e.response && e.response.statusCode === 404) {
return false
}

throw this.wrapK8sClientError(e)
}
}

async replaceValidatingWebhookConfiguration(name: string, webhook: V1ValidatingWebhookConfiguration): Promise<void> {
const k8sAdmissionApi = this.kubeConfig.makeApiClient(AdmissionregistrationV1Api)
try {
await k8sAdmissionApi.replaceValidatingWebhookConfiguration(name, webhook)
} catch (e: any) {
throw this.wrapK8sClientError(e)
}
}

async createValidatingWebhookConfiguration(webhook: V1ValidatingWebhookConfiguration): Promise<void> {
const k8sAdmissionApi = this.kubeConfig.makeApiClient(AdmissionregistrationV1Api)
try {
await k8sAdmissionApi.createValidatingWebhookConfiguration(webhook)
} catch (e: any) {
throw this.wrapK8sClientError(e)
}
}

async deleteValidatingWebhookConfiguration(name: string): Promise<void> {
const k8sAdmissionApi = this.kubeConfig.makeApiClient(AdmissionregistrationV1Api)
try {
Expand Down
54 changes: 53 additions & 1 deletion src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
V1Role,
V1RoleBinding,
V1Service,
V1CustomResourceDefinition,
V1CustomResourceDefinition, V1ValidatingWebhookConfiguration,
} from '@kubernetes/client-node'
import {cli} from 'cli-ux'
import * as fs from 'fs'
Expand All @@ -40,6 +40,7 @@ import {V1Certificate} from '../../api/types/cert-manager'
import {OpenShiftHelper} from '../../api/openshift'

export class OperatorTasks {
private static readonly VALIDATING_WEBHOOK = 'org.eclipse.che'
private static readonly WEBHOOK_SERVICE = 'che-operator-service'
private static readonly CERTIFICATE = 'che-operator-serving-cert'
private static readonly ISSUER = 'che-operator-selfsigned-issuer'
Expand Down Expand Up @@ -282,6 +283,25 @@ export class OperatorTasks {
title: 'Operator pod bootstrap',
task: () => kubeTasks.podStartTasks(CHE_OPERATOR_SELECTOR, this.flags.chenamespace),
},
{
title: `Create ValidatingWebhookConfiguration ${OperatorTasks.VALIDATING_WEBHOOK}`,
task: async (ctx: any, task: any) => {
const exists = await this.kh.isValidatingWebhookConfigurationExists(OperatorTasks.VALIDATING_WEBHOOK)
if (exists) {
task.title = `${task.title}...[Exists]`
} else {
const webhookPath = this.getResourcePath('org.eclipse.che.ValidatingWebhookConfiguration.yaml')
if (fs.existsSync(webhookPath)) {
const webhook = this.kh.safeLoadFromYamlFile(webhookPath) as V1ValidatingWebhookConfiguration
webhook!.webhooks![0].clientConfig.service!.namespace = this.flags.chenamespace
await this.kh.createValidatingWebhookConfiguration(webhook)
task.title = `${task.title}...[OK: created]`
} else {
task.title = `${task.title}...[Not found]`
}
}
},
},
createEclipseCheClusterTask(this.flags, kube),
]
}
Expand Down Expand Up @@ -461,6 +481,27 @@ export class OperatorTasks {
await this.kh.waitLatestReplica(OPERATOR_DEPLOYMENT_NAME, this.flags.chenamespace)
},
},
{
title: `Update ValidatingWebhookConfiguration ${OperatorTasks.VALIDATING_WEBHOOK}`,
task: async (ctx: any, task: any) => {
const webhookPath = this.getResourcePath('org.eclipse.che.ValidatingWebhookConfiguration.yaml')
if (fs.existsSync(webhookPath)) {
const webhook = this.kh.safeLoadFromYamlFile(webhookPath) as V1ValidatingWebhookConfiguration
webhook!.webhooks![0].clientConfig.service!.namespace = this.flags.chenamespace

const exists = await this.kh.isValidatingWebhookConfigurationExists(OperatorTasks.VALIDATING_WEBHOOK)
if (exists) {
await this.kh.replaceValidatingWebhookConfiguration(OperatorTasks.VALIDATING_WEBHOOK, webhook)
task.title = `${task.title}...[Ok: updated]`
} else {
await this.kh.createValidatingWebhookConfiguration(webhook)
task.title = `${task.title}...[OK: created]`
}
} else {
task.title = `${task.title}...[Not found]`
}
},
},
patchingEclipseCheCluster(this.flags, this.kh),
]
}
Expand Down Expand Up @@ -532,6 +573,17 @@ export class OperatorTasks {
getDeleteTasks(flags: any): ReadonlyArray<Listr.ListrTask> {
const kh = new KubeHelper(flags)
return [
{
title: `Delete ValidatingWebhookConfiguration ${OperatorTasks.VALIDATING_WEBHOOK}`,
task: async (_ctx: any, task: any) => {
try {
await kh.deleteValidatingWebhookConfiguration(OperatorTasks.VALIDATING_WEBHOOK)
task.title = `${task.title}...[Ok]`
} catch (e: any) {
task.title = `${task.title}...[Failed: ${e.message}]`
}
},
},
{
title: 'Delete CRDs',
task: async (_ctx: any, task: any) => {
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/e2e-upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ describe('Test Che upgrade', () => {
it(`Deploy Che using ${INSTALLER} installer and self signed certificates`, async () => {
// uses installed chectl (from a stable channel)
// see github workflow
const deployCommand = `chectl server:deploy --batch --platform=${PLATFORM} --installer=${INSTALLER} --chenamespace=${NAMESPACE} --telemetry=off`
let deployCommand = `chectl server:deploy --batch --platform=${PLATFORM} --installer=${INSTALLER} --chenamespace=${NAMESPACE} --telemetry=off`
if (PLATFORM === 'minikube') {
deployCommand += ' --che-operator-cr-patch-yaml=test/e2e/resources/minikube-checluster-patch.yaml'
}
await helper.runCliCommand(deployCommand)
})
})
Expand Down