diff --git a/pkg/harvester/mixins/harvester-vm/impl.js b/pkg/harvester/mixins/harvester-vm/impl.js index 3c2ae101..8fd2fa92 100644 --- a/pkg/harvester/mixins/harvester-vm/impl.js +++ b/pkg/harvester/mixins/harvester-vm/impl.js @@ -38,6 +38,10 @@ export const SSH_EXISTING_TYPE = { export default { methods: { hasCloudConfigComment(userScript) { + if (typeof userScript === 'string' && /^\s*#cloud-config(\s|$)/.test(userScript)) { + return true; + } + // Check that userData contains: #cloud-config const userDataDoc = userScript ? YAML.parseDocument(userScript) : YAML.parseDocument({}); const items = userDataDoc?.contents?.items || []; @@ -52,6 +56,14 @@ export default { exist = true; } + if (userDataDoc?.contents?.commentBefore === 'cloud-config' || userDataDoc?.contents?.commentBefore?.includes('cloud-config\n')) { + exist = true; + } + + if (userDataDoc?.contents?.comment === 'cloud-config' || userDataDoc?.contents?.comment?.includes('cloud-config\n')) { + exist = true; + } + items.map((item) => { const key = item.key; diff --git a/pkg/harvester/mixins/harvester-vm/index.js b/pkg/harvester/mixins/harvester-vm/index.js index e6b3b917..95951619 100644 --- a/pkg/harvester/mixins/harvester-vm/index.js +++ b/pkg/harvester/mixins/harvester-vm/index.js @@ -1211,9 +1211,10 @@ export default { userDataDoc = config.installAgent ? this.mergeQGA({ userDataDoc, ...config }) : this.deleteQGA({ userDataDoc, ...config }); const userDataYaml = userDataDoc.toString(); + const userDataValue = userDataDoc.toJSON(); - if (userDataYaml === '{}\n') { - // When the YAML parsed value is '{}\n', it means that the userData is empty, then undefined is returned. + if (userDataValue === null || (typeof userDataValue === 'object' && !Array.isArray(userDataValue) && isEmpty(userDataValue))) { + // Empty userData should not create cloud-init content like `null`. return undefined; }