Skip to content

Commit

Permalink
Update non-working examples in Kamelet Catalog docs
Browse files Browse the repository at this point in the history
Fix #866

* There are some generated kamelet bindings are invalid, the auto
  generation cannot resolve all the required steps, so there is
  a need to create it manually
* Add a comment marker in kamelet binding examples for the auto
  generation to skip its creation
* The doc generation source the kamelet binding example file when it
  doesn't auto generate the example
* the camel-kamelets directory is hardcoded in kamelets.js as it is
  assumed this generation mechanism already assumes this directory
  • Loading branch information
claudio4j committed Apr 25, 2022
1 parent 5b9834e commit b35d9cd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 22 deletions.
95 changes: 75 additions & 20 deletions docs/modules/ROOT/examples/js/kamelets.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/

const util = require('camel-website-util')
const fs = require('fs')
const yaml = require('js-yaml');

const QUOTED_CHARS = /[$`"\\]/g

Expand All @@ -26,34 +28,48 @@ const QUOTE_REPLACEMENTS = {
'\\': '\\\\',
}

// marker added to the first line of kamelet binding files in templates/bindings/camel-k
// generator will not generate a kamelet binding example and will source this kamelet binding file into the generated doc
const DONT_OVERWRITE_MARKER = "dont_overwrite"

// regex to replace the sink type
const regex = new RegExp(`( sink:\\n\\s*ref:\\n)(\\s*kind:)(.*)(\\n\\s*apiVersion:)(.*)(\\n\\s*name:)(.*)`, 'g')

const svgb64Prefix = 'data:image/svg+xml;base64,'

module.exports = {
binding: (binding, apiVersion, kind, metadata_, spec_, refKind, refApiVersion, refName) => {
const name = metadata_.name
const metadata = {name: `${name}-binding`}
const kamelet = {
ref: {
kind,

genExample = shouldGenerateKameletBindingExample(metadata.name)
if (genExample) {
const kamelet = {
ref: {
kind,
apiVersion,
name,
},
properties: kameletPropertyList(spec_.definition)
}
const platform = {
ref: {
kind: refKind,
apiVersion: refApiVersion,
name: refName,
},
}
const base = {
apiVersion,
name,
},
properties: kameletPropertyList(spec_.definition)
}
const platform = {
ref: {
kind: refKind,
apiVersion: refApiVersion,
name: refName,
},
}
const base = {
apiVersion,
kind: 'KameletBinding',
metadata,
kind: 'KameletBinding',
metadata,
}
const fn = kameletBindings[binding] || (() => `unrecognized binding ${binding}`)
return fn(base, kamelet, platform)
} else {
content = readKameletBindingExample(metadata.name, refApiVersion, refKind, refName)
return content
}
const fn = kameletBindings[binding] || (() => `unrecognized binding ${binding}`)
return fn(base, kamelet, platform)
},

bindingCommand: (binding, name, definition, topic) => {
Expand Down Expand Up @@ -128,6 +144,45 @@ function kameletPropertyList (definition) {
)
}

// verify if the existing kamelet binding example should be automatically generated
// by checking if there is a comment marker in the first line
function shouldGenerateKameletBindingExample(file) {
f = "../camel-kamelets/templates/bindings/camel-k/" + file + ".yaml"
try {
bufContent = fs.readFileSync(f)
content = bufContent.toString()
line = content.split(/\r?\n/)[0]
return line.indexOf(DONT_OVERWRITE_MARKER) < 0
} catch (err) {
console.log("Error reading kamelet file " + file + ": " + err)
return true
}
}

// this is called from kamelet-options.adoc to source the kamelet binding example from a file
// skip the first line and replace the sink kind when the kind is a knative channel
function readKameletBindingExample(file, apiVersion, kind, name) {
f = "../camel-kamelets/templates/bindings/camel-k/" + file + ".yaml"
try {
bufContent = fs.readFileSync(f)
content = bufContent.toString()
lines = content.split(/\r?\n/)
klbContent = ""
// skip the first line, as it contains the comment marker
for (i = 1; i < lines.length; i++) {
klbContent += lines[i] + "\n"
}
// uses a knative channel sink
klbContent = klbContent.replace(regex, "$1$2 " + kind + "$4 " + apiVersion + "$6 " + name);
yamlDoc = yaml.load(klbContent);
return yamlDoc
} catch (err) {
console.log("Error reading kamelet file " + file + ": " + err)
return err
}
}


const kameletBindings = {
action: (base, kamelet, platform) => Object.assign(base, {
spec: {
Expand Down
17 changes: 16 additions & 1 deletion templates/bindings/camel-k/avro-deserialize-action-binding.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# dont_overwrite
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
Expand All @@ -9,14 +10,28 @@ spec:
apiVersion: camel.apache.org/v1alpha1
name: timer-source
properties:
message: "Hello"
message: '{"first":"Ada","last":"Lovelace"}'
steps:
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: json-deserialize-action
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: avro-serialize-action
properties:
schema: "{\"type\": \"record\", \"namespace\": \"com.example\", \"name\": \"FullName\", \"fields\": [{\"name\": \"first\", \"type\": \"string\"},{\"name\": \"last\", \"type\": \"string\"}]}"
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: avro-deserialize-action
properties:
schema: "{\"type\": \"record\", \"namespace\": \"com.example\", \"name\": \"FullName\", \"fields\": [{\"name\": \"first\", \"type\": \"string\"},{\"name\": \"last\", \"type\": \"string\"}]}"
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: json-serialize-action
sink:
ref:
kind: KafkaTopic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# dont_overwrite
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
Expand All @@ -9,8 +10,12 @@ spec:
apiVersion: camel.apache.org/v1alpha1
name: timer-source
properties:
message: "Hello"
message: '{"first":"Ada","last":"Lovelace"}'
steps:
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: json-deserialize-action
- ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
Expand Down

0 comments on commit b35d9cd

Please sign in to comment.