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

Added support for generating YAML binding template files #465

Merged
merged 2 commits into from
Aug 23, 2021
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
134 changes: 112 additions & 22 deletions script/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,80 @@ func main() {
os.Exit(1)
}

dir := os.Args[1]
out := os.Args[2]
projectBaseDir := os.Args[1]
docBaseDir := os.Args[2]

funcMap := template.FuncMap{
"ToCamel": strcase.ToCamel,
}

templateFile := path.Join(out, "kamelet.adoc.tmpl")
kameletBindingFile := path.Join(out, "kamelet-binding-sink-source.tmpl")
propertiesListFile := path.Join(out, "properties-list.tmpl")
templateFile := path.Join(docBaseDir, "kamelet.adoc.tmpl")
kameletBindingFile := path.Join(docBaseDir, "kamelet-binding-sink-source.tmpl")
propertiesListFile := path.Join(docBaseDir, "properties-list.tmpl")

t, err := template.New("kamelet.adoc.tmpl").Funcs(funcMap).ParseFiles(templateFile, kameletBindingFile, propertiesListFile)


docTemplate, err := template.New("kamelet.adoc.tmpl").Funcs(funcMap).ParseFiles(templateFile, kameletBindingFile, propertiesListFile)
handleGeneralError(fmt.Sprintf("cannot load template file from %s", templateFile), err)

kamelets := listKamelets(dir)
camelKYamlBindingsBaseDir := filepath.Join(projectBaseDir, "templates", "bindings", "camel-k")
yamlTemplateFile := path.Join(camelKYamlBindingsBaseDir, "kamelet.yaml.tmpl")

yamlTemplate, err := template.New("kamelet.yaml.tmpl").Funcs(funcMap).ParseFiles(yamlTemplateFile, kameletBindingFile, propertiesListFile)
handleGeneralError(fmt.Sprintf("cannot load template file from %s", templateFile), err)

coreYamlBindingsBaseDir := filepath.Join(projectBaseDir, "templates", "bindings", "core")
coreYamlTemplateFile := path.Join(coreYamlBindingsBaseDir, "kamelet-core-binding.yaml.tmpl")
parameterListFile := path.Join(coreYamlBindingsBaseDir, "parameter-list.tmpl")

coreYamlTemplate, err := template.New("kamelet-core-binding.yaml.tmpl").Funcs(funcMap).ParseFiles(coreYamlTemplateFile, kameletBindingFile, parameterListFile)
handleGeneralError(fmt.Sprintf("cannot load template file from %s", templateFile), err)

kamelets := listKamelets(projectBaseDir)

links := make([]string, 0)
for _, k := range kamelets {
img := saveImage(k, out)
img := saveImage(k, docBaseDir)

ctx := NewTemplateContext(k, img)

buffer := new(bytes.Buffer)
err = t.Execute(buffer, &ctx)
handleGeneralError("cannot process template", err)
processDocTemplate(k, docBaseDir, err, docTemplate, &ctx)
links = updateImageLink(k, img, links)

produceDocFile(k, out, buffer.String())
processYamlTemplate(k, projectBaseDir, err, yamlTemplate, &ctx)
processCoreYamlTemplate(k, projectBaseDir, err, coreYamlTemplate, &ctx)

links = append(links, fmt.Sprintf("* xref:ROOT:%s.adoc[%s %s]", k.Name, img, k.Spec.Definition.Title))
}

saveNav(links, out)
saveNav(links, docBaseDir)
}

func processDocTemplate(k camel.Kamelet, baseDir string, err error, docTemplate *template.Template, ctx *TemplateContext) {
buffer := new(bytes.Buffer)
err = docTemplate.Execute(buffer, &ctx)
handleGeneralError("cannot process documentation template", err)

produceDocFile(k, baseDir, buffer.String())
}

func processYamlTemplate(k camel.Kamelet, baseDir string, err error, yamlTemplate *template.Template, ctx *TemplateContext) {
buffer := new(bytes.Buffer)
err = yamlTemplate.Execute(buffer, ctx)
handleGeneralError("cannot process yaml binding template", err)

produceBindingFile(k, baseDir, "camel-k", buffer.String())
}

func processCoreYamlTemplate(k camel.Kamelet, baseDir string, err error, yamlTemplate *template.Template, ctx *TemplateContext) {
buffer := new(bytes.Buffer)
err = yamlTemplate.Execute(buffer, ctx)
handleGeneralError("cannot process yaml binding template", err)

produceBindingFile(k, baseDir, "core", buffer.String())
}

func updateImageLink(k camel.Kamelet, img string, links []string) []string {
return append(links, fmt.Sprintf("* xref:ROOT:%s.adoc[%s %s]", k.Name, img, k.Spec.Definition.Title))
}

type TemplateContext struct {
Expand Down Expand Up @@ -172,6 +214,42 @@ func (ctx *TemplateContext) PropertyList() string {
return props
}

func (ctx *TemplateContext) ParameterList() string {
tp := ctx.Kamelet.ObjectMeta.Labels["camel.apache.org/kamelet.type"]
propDefs := getSortedProps(ctx.Kamelet)

sampleConfig := make([]string, 0)
for _, propDef := range propDefs {
if !propDef.Required {
continue
}
key := propDef.Name
if propDef.Default == nil {
ex := propDef.GetSampleValue()
sampleConfig = append(sampleConfig, fmt.Sprintf("%s: %s", key, ex))
}
}

props := ""

if len(sampleConfig) > 0 {
paddingSpace := ""
switch tp {
case "sink":
props = fmt.Sprintf("\n%10s%s:\n%12s%s", "", "parameters",
"", strings.Join(sampleConfig, fmt.Sprintf("\n%12s", "")))
case "source":
props = fmt.Sprintf("\n%6s%s:\n%8s%s", "", "parameters",
"", strings.Join(sampleConfig, fmt.Sprintf("\n%8s", "")))
case "action":
props = fmt.Sprintf("\n%10s%s:\n%12s%s", paddingSpace, "parameters",
"", strings.Join(sampleConfig, fmt.Sprintf("\n%8s", "")))
}
}

return props
}

func (ctx *TemplateContext) SetVal(key, val string) string {
ctx.TemplateProperties[key] = val
return ""
Expand Down Expand Up @@ -293,15 +371,27 @@ func saveImage(k camel.Kamelet, out string) string {
return ""
}

func produceDocFile(k camel.Kamelet, out string, content string) {
docFile := filepath.Join(out, "pages", k.Name+".adoc")
if _, err := os.Stat(docFile); err == nil {
err = os.Remove(docFile)
handleGeneralError(fmt.Sprintf("cannot remove file %q", docFile), err)
func produceDocFile(k camel.Kamelet, baseDir string, content string) {
outputDir := filepath.Join(baseDir, "pages")

produceOutputFile(k, outputDir, content,".adoc")
}

func produceBindingFile(k camel.Kamelet, baseDir string, projectName string, content string) {
camelKOutputDir := filepath.Join(baseDir, "templates", "bindings", projectName)

produceOutputFile(k, camelKOutputDir, content,"-binding.yaml")
}

func produceOutputFile(k camel.Kamelet, outputDir string, content string, extension string) {
outputFile := filepath.Join(outputDir, k.Name + extension)
if _, err := os.Stat(outputFile); err == nil {
err = os.Remove(outputFile)
handleGeneralError(fmt.Sprintf("cannot remove file %q", outputFile), err)
}
err := ioutil.WriteFile(docFile, []byte(content), 0666)
handleGeneralError(fmt.Sprintf("cannot write to file %q", docFile), err)
fmt.Printf("%q written\n", docFile)
err := ioutil.WriteFile(outputFile, []byte(content), 0666)
handleGeneralError(fmt.Sprintf("cannot write to file %q", outputFile), err)
fmt.Printf("%q written\n", outputFile)
}

func tableLine(val ...string) string {
Expand Down
24 changes: 24 additions & 0 deletions templates/bindings/camel-k/avro-deserialize-action-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: avro-deserialize-action-binding
spec:
source:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: timer-source
properties:
message: "Hello"
steps:
- 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\"}]}"
sink:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
24 changes: 24 additions & 0 deletions templates/bindings/camel-k/avro-serialize-action-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: avro-serialize-action-binding
spec:
source:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: timer-source
properties:
message: "Hello"
steps:
- 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\"}]}"
sink:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-cloudwatch-sink-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-cloudwatch-sink-binding
spec:
source:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-cloudwatch-sink
properties:
accessKey: "The Access Key"
cw_namespace: "The Cloud Watch Namespace"
region: "eu-west-1"
secretKey: "The Secret Key"

21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-ddb-streams-source-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-ddb-streams-source-binding
spec:
source:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-ddb-streams-source
properties:
accessKey: "The Access Key"
region: "eu-west-1"
secretKey: "The Secret Key"
table: "The Table"
sink:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic

20 changes: 20 additions & 0 deletions templates/bindings/camel-k/aws-ec2-sink-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-ec2-sink-binding
spec:
source:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-ec2-sink
properties:
accessKey: "The Access Key"
region: "eu-west-1"
secretKey: "The Secret Key"

21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-kinesis-firehose-sink-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-kinesis-firehose-sink-binding
spec:
source:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-kinesis-firehose-sink
properties:
accessKey: "The Access Key"
region: "eu-west-1"
secretKey: "The Secret Key"
streamName: "The Stream Name"

21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-kinesis-sink-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-kinesis-sink-binding
spec:
source:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-kinesis-sink
properties:
accessKey: "The Access Key"
region: "eu-west-1"
secretKey: "The Secret Key"
stream: "The Stream Name"

21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-kinesis-source-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-kinesis-source-binding
spec:
source:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-kinesis-source
properties:
accessKey: "The Access Key"
region: "eu-west-1"
secretKey: "The Secret Key"
stream: "The Stream Name"
sink:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic

21 changes: 21 additions & 0 deletions templates/bindings/camel-k/aws-lambda-sink-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: camel.apache.org/v1alpha1
kind: KameletBinding
metadata:
name: aws-lambda-sink-binding
spec:
source:
ref:
kind: KafkaTopic
apiVersion: kafka.strimzi.io/v1beta1
name: my-topic
sink:
ref:
kind: Kamelet
apiVersion: camel.apache.org/v1alpha1
name: aws-lambda-sink
properties:
accessKey: "The Access Key"
function: "The Function Name"
region: "eu-west-1"
secretKey: "The Secret Key"

Loading