From bf989257e5cca7d0951bf583a5d68ecc8a3cf32c Mon Sep 17 00:00:00 2001 From: i325261 Date: Thu, 22 Aug 2024 10:30:56 +0200 Subject: [PATCH] more updates --- bin/cap-op-plugin.js | 29 ++++++++--- .../templates/cap-operator-cros.yaml | 10 +--- .../chartFlexibleTemplates/values.schema.json | 52 +------------------ hack/schema-generation.go | 12 ++--- 4 files changed, 31 insertions(+), 72 deletions(-) diff --git a/bin/cap-op-plugin.js b/bin/cap-op-plugin.js index 166fb1d..274a0f6 100755 --- a/bin/cap-op-plugin.js +++ b/bin/cap-op-plugin.js @@ -61,8 +61,12 @@ EXAMPLES } async function convertToFlexibleTemplateChart(option) { - if (!((cds.utils.exists('chart') && isCAPOperatorChart(cds.utils.path.join(cds.root,'chart'))))) { + if (!((cds.utils.exists('chart') && isCAPOperatorChart(cds.utils.path.join(cds.root,'chart'))))) throw new Error("No CAP Operator chart found in the project. Please run 'cds add cap-operator --force' to add the CAP Operator chart folder.") + + if (isFlexibleTemplateChart(cds.utils.path.join(cds.root,'chart'))){ + console.log("Exisiting chart is already a flexible chart. No need for conversion. Exiting...") + return } // Copy templates @@ -84,17 +88,18 @@ async function populateFromValuesYaml() { const valuesYaml = yaml.parse(await cds.utils.read(cds.utils.path.join(cds.root, 'chart/values.yaml'))) const capOpCROYaml = fs.readFileSync(cds.utils.path.join(cds.root, 'chart/templates/cap-operator-cros.yaml'), 'utf8') + // Update cap-operator-cro.yaml with existing values let workloadArray = [] for (const [workloadKey, workloadDetails] of Object.entries(valuesYaml.workloads)) workloadArray.push(workloadDetails) let updatedCapOpCROObj = { 'workloads': workloadArray } - // if (valuesYaml['tenantOperations']) - // updatedCapOpCROObj['tenantOperations'] = valuesYaml['tenantOperations'] + if (valuesYaml['tenantOperations']) + updatedCapOpCROObj['tenantOperations'] = valuesYaml['tenantOperations'] - // if (valuesYaml['contentJobs']) - // updatedCapOpCROObj['contentJobs'] = valuesYaml['contentJobs'] + if (valuesYaml['contentJobs']) + updatedCapOpCROObj['contentJobs'] = valuesYaml['contentJobs'] const updatedCapOpCROYaml = capOpCROYaml.replace( /workloads:\n(.*\n)*?(?=\n\s{2,}- name|spec:|$)/gm, @@ -105,13 +110,25 @@ async function populateFromValuesYaml() { // transform values.yaml let newWorkloadObj = {} + let serverEnv = [] for (const [workloadKey, workloadDetails] of Object.entries(valuesYaml.workloads)) { newWorkloadObj[workloadKey] = { - "image": workloadDetails.deploymentDefinition ? workloadDetails.deploymentDefinition.image : workloadDetails.jobDefinition.image + "image": workloadDetails.deploymentDefinition ? workloadDetails.deploymentDefinition.image ?? null : workloadDetails.jobDefinition.image ?? null } + + if (workloadDetails?.deploymentDefinition?.type === 'CAP') + serverEnv = workloadDetails.deploymentDefinition?.env } valuesYaml['workloads'] = newWorkloadObj + // extract hanaInstanceId from serverEnv + for (const i in serverEnv) { + if (serverEnv[i].name = 'CDS_CONFIG') { + const envValueJson = JSON.parse(serverEnv[i].value) + valuesYaml['hanaInstanceId'] = envValueJson?.requires['cds.xt.DeploymentService']?.hdi?.create?.database_id + } + } + await cds.utils.write(yaml.stringify(valuesYaml)).to(cds.utils.path.join(cds.root, 'chart/values.yaml')) } diff --git a/files/chartFlexibleTemplates/templates/cap-operator-cros.yaml b/files/chartFlexibleTemplates/templates/cap-operator-cros.yaml index 111fe4c..8ec3019 100644 --- a/files/chartFlexibleTemplates/templates/cap-operator-cros.yaml +++ b/files/chartFlexibleTemplates/templates/cap-operator-cros.yaml @@ -16,7 +16,7 @@ spec: {{- range $k, $v := .Values.app.istioIngressGatewayLabels }} - name: {{ $k }} value: {{ $v | default "invalidValue"}} - {{ end }} + {{- end }} btpAppName: {{ include "appName" $ }} globalAccountId: {{.Values.btp.globalAccountId}} provider: @@ -124,11 +124,3 @@ spec: jobDefinition: type: Content image: {{ .Values.workloads.contentDeploy.image }} - - {{- if .Values.tenantOperations }} - tenantOperations : {{ .Values.tenantOperations }} - {{- end}} - - {{- if .Values.contentJobs }} - contentJobs : {{ .Values.contentJobs }} - {{- end}} diff --git a/files/chartFlexibleTemplates/values.schema.json b/files/chartFlexibleTemplates/values.schema.json index 096ca5f..0930a57 100644 --- a/files/chartFlexibleTemplates/values.schema.json +++ b/files/chartFlexibleTemplates/values.schema.json @@ -73,45 +73,6 @@ ], "type": "object" }, - "TenantOperationWorkloadReference": { - "additionalProperties": false, - "properties": { - "continueOnFailure": { - "type": "boolean" - }, - "workloadName": { - "type": "string" - } - }, - "required": [ - "workloadName" - ], - "type": "object" - }, - "TenantOperations": { - "additionalProperties": false, - "properties": { - "deprovisioning": { - "items": { - "$ref": "#/$defs/TenantOperationWorkloadReference" - }, - "type": "array" - }, - "provisioning": { - "items": { - "$ref": "#/$defs/TenantOperationWorkloadReference" - }, - "type": "array" - }, - "upgrade": { - "items": { - "$ref": "#/$defs/TenantOperationWorkloadReference" - }, - "type": "array" - } - }, - "type": "object" - }, "UserInfo": { "additionalProperties": false, "properties": { @@ -171,7 +132,7 @@ ], "type": "object" }, - "chartValueSimplified": { + "flexibleChartValue": { "additionalProperties": true, "properties": { "app": { @@ -180,12 +141,6 @@ "btp": { "$ref": "#/$defs/btp" }, - "contentJobs": { - "items": { - "type": "string" - }, - "type": "array" - }, "hanaInstanceId": { "type": "string" }, @@ -207,9 +162,6 @@ }, "type": "object" }, - "tenantOperations": { - "$ref": "#/$defs/TenantOperations" - }, "workloads": { "additionalProperties": { "$ref": "#/$defs/workloadDefinition" @@ -361,6 +313,6 @@ "type": "object" } }, - "$ref": "#/$defs/chartValueSimplified", + "$ref": "#/$defs/flexibleChartValue", "$schema": "https://json-schema.org/draft/2020-12/schema" } diff --git a/hack/schema-generation.go b/hack/schema-generation.go index 23eb5c4..f0a6564 100644 --- a/hack/schema-generation.go +++ b/hack/schema-generation.go @@ -58,7 +58,7 @@ type workloadDefinition struct { Image string `json:"image"` } -type chartValueSimplified struct { +type flexibleChartValue struct { App app `json:"app"` Btp btp `json:"btp"` ImagePullSecrets []string `json:"imagePullSecrets,omitempty"` @@ -66,8 +66,6 @@ type chartValueSimplified struct { ServiceInstances map[string]serviceInstanceExt `json:"serviceInstances"` ServiceBindings map[string]serviceBindingExt `json:"serviceBindings"` Workloads map[string]workloadDefinition `json:"workloads"` - TenantOperations v1alpha1.TenantOperations `json:"tenantOperations,omitempty"` - ContentJobs []string `json:"contentJobs,omitempty"` } func updateProperties(data []byte) []byte { @@ -115,9 +113,9 @@ func updatePropertiesFlexibleChart(data []byte) []byte { workloadDefinition["additionalProperties"] = true m["$defs"].(map[string]interface{})["workloadDefinition"] = workloadDefinition - chartValueSimplified := m["$defs"].(map[string]interface{})["chartValueSimplified"].(map[string]interface{}) - chartValueSimplified["additionalProperties"] = true - m["$defs"].(map[string]interface{})["chartValueSimplified"] = chartValueSimplified + flexibleChartValue := m["$defs"].(map[string]interface{})["flexibleChartValue"].(map[string]interface{}) + flexibleChartValue["additionalProperties"] = true + m["$defs"].(map[string]interface{})["flexibleChartValue"] = flexibleChartValue data, _ = json.Marshal(m) @@ -149,7 +147,7 @@ func main() { // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - sV2 := jsonschema.Reflect(&chartValueSimplified{}) + sV2 := jsonschema.Reflect(&flexibleChartValue{}) dataV2, errV2 := json.MarshalIndent(sV2, "", " ") if errV2 != nil { panic(errV2.Error())