Skip to content

Commit

Permalink
more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhprasad-sap committed Aug 22, 2024
1 parent 355b0f4 commit bf98925
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 72 deletions.
29 changes: 23 additions & 6 deletions bin/cap-op-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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'))
}

Expand Down
10 changes: 1 addition & 9 deletions files/chartFlexibleTemplates/templates/cap-operator-cros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}}
52 changes: 2 additions & 50 deletions files/chartFlexibleTemplates/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -171,7 +132,7 @@
],
"type": "object"
},
"chartValueSimplified": {
"flexibleChartValue": {
"additionalProperties": true,
"properties": {
"app": {
Expand All @@ -180,12 +141,6 @@
"btp": {
"$ref": "#/$defs/btp"
},
"contentJobs": {
"items": {
"type": "string"
},
"type": "array"
},
"hanaInstanceId": {
"type": "string"
},
Expand All @@ -207,9 +162,6 @@
},
"type": "object"
},
"tenantOperations": {
"$ref": "#/$defs/TenantOperations"
},
"workloads": {
"additionalProperties": {
"$ref": "#/$defs/workloadDefinition"
Expand Down Expand Up @@ -361,6 +313,6 @@
"type": "object"
}
},
"$ref": "#/$defs/chartValueSimplified",
"$ref": "#/$defs/flexibleChartValue",
"$schema": "https://json-schema.org/draft/2020-12/schema"
}
12 changes: 5 additions & 7 deletions hack/schema-generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ 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"`
HanaInstanceId string `json:"hanaInstanceId,omitempty"`
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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit bf98925

Please sign in to comment.