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

Deploy models via zbchaos #240

Merged
merged 2 commits into from
Nov 18, 2022
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
81 changes: 81 additions & 0 deletions go-chaos/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 Camunda Services GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)

func init() {
rootCmd.AddCommand(deployCmd)

deployCmd.AddCommand(deployProcessModelCmd)

deployProcessModelCmd.Flags().StringVar(&processModelPath, "processModelPath", "",
"Specify the path to a BPMN process model, which should be deployed.")
deployProcessModelCmd.Flags().IntVar(&multipleVersions, "multipleVersions", 10,
"Specify how many different versions of a default BPMN and DMN model should be deployed. Useful for testing deployment distribution.")

deployProcessModelCmd.MarkFlagsMutuallyExclusive("processModelPath", "multipleVersions")
}

var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy certain resource",
Long: `Deploy certain resource, like process model(s) or kubernetes manifest.`,
}

var deployProcessModelCmd = &cobra.Command{
Use: "process",
Short: "Deploy a process model to Zeebe",
Long: `Deploy a process model to Zeebe.
Can be used to deploy a specific process model or multiple version of a default BPMN and DMN model.
Defaults to the later, which is useful for experimenting with deployment distribution.`,
Run: func(cmd *cobra.Command, args []string) {
k8Client, err := internal.CreateK8Client()
if err != nil {
panic(err)
}

port := 26500
closeFn := k8Client.MustGatewayPortForward(port, port)
defer closeFn()

zbClient, err := internal.CreateZeebeClient(port)
if err != nil {
panic(err.Error())
}
defer zbClient.Close()

if len(processModelPath) == 0 {
// deploy multi version
err := internal.DeployDifferentVersions(zbClient, int32(multipleVersions))
if err != nil {
panic(err.Error())
}
fmt.Printf("Deployed different process models of different types and versions to zeebe!\n")
} else {
processDefinitionKey, err := internal.DeployModel(zbClient, processModelPath)
if err != nil {
panic(err.Error())
}

fmt.Printf("Deployed given process model %s, under key %d!\n", processModelPath, processDefinitionKey)
}
},
}
4 changes: 3 additions & 1 deletion go-chaos/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package cmd

import (
"fmt"
"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"
"os"

"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"

"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)
Expand All @@ -28,6 +29,7 @@ var (
role string
nodeId int
processModelPath string
multipleVersions int
variables string
msgName string
awaitResult bool
Expand Down
28 changes: 28 additions & 0 deletions go-chaos/internal/bpmn/multi-version/fancyDecision_v2.dmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:dmndi="https://www.omg.org/spec/DMN/20191111/DMNDI/" xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_124x35b" name="DRD" namespace="http://camunda.org/schema/1.0/dmn" exporter="Camunda Modeler" exporterVersion="5.3.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.0.0">
<decision id="fancyDecision" name="FancyDecision">
<decisionTable id="DecisionTable_1ldntm8">
<input id="InputClause_0m7yn14">
<inputExpression id="LiteralExpression_1r91tez" typeRef="string">
<text>"test"</text>
</inputExpression>
</input>
<output id="Output_1" name="dummy" typeRef="string" />
<rule id="DecisionRule_1ld2c9k">
<inputEntry id="UnaryTests_1u3isaf">
<text></text>
</inputEntry>
<outputEntry id="LiteralExpression_17nti9y">
<text>"yolo"</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
<dmndi:DMNDI>
<dmndi:DMNDiagram>
<dmndi:DMNShape dmnElementRef="fancyDecision">
<dc:Bounds height="80" width="180" x="160" y="110" />
</dmndi:DMNShape>
</dmndi:DMNDiagram>
</dmndi:DMNDI>
</definitions>
66 changes: 66 additions & 0 deletions go-chaos/internal/zeebe.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,72 @@ func DeployModel(client zbc.Client, fileName string) (int64, error) {
return processDefinitionKey, nil
}

type models struct {
bpmnBytes []byte
bpmnFileName string
dmnBytes []byte
dmnFileName string
}

func readModels(bpmnFileName string, dmnFileName string) (*models, error) {
bpmnBytes, err := bpmnContent.ReadFile(bpmnFileName)
if err != nil {
return nil, err
}

dmnBytes, err := bpmnContent.ReadFile(dmnFileName)
if err != nil {
return nil, err
}

return &models{bpmnBytes: bpmnBytes, dmnBytes: dmnBytes, bpmnFileName: bpmnFileName, dmnFileName: dmnFileName}, nil
}

func deployModels(client zbc.Client, models *models) error {
_, err :=
client.NewDeployResourceCommand().AddResource(models.bpmnBytes, models.bpmnFileName).AddResource(models.dmnBytes, models.dmnFileName).Send(context.TODO())
if err != nil {
return err
}
return nil
}

func DeployDifferentVersions(client zbc.Client, versions int32) error {
firstModel, err := readModels("bpmn/multi-version/multiVersionModel.bpmn", "bpmn/multi-version/fancyDecision.dmn")
if err != nil {
return err
}

secondModel, err := readModels("bpmn/multi-version/multiVersionModel_v2.bpmn", "bpmn/multi-version/fancyDecision_v2.dmn")
if err != nil {
return err
}

if Verbosity {
fmt.Printf("Deploy %d versions of different type of models.\n", versions)
}

count := int32(0)
for count < versions {
err := deployModels(client, firstModel)
if err != nil {
return err
}

err = deployModels(client, secondModel)
if err != nil {
return err
}

count += 2
if Verbosity {
fmt.Printf("Deployed [%d/%d] versions.\n", count, versions)
}
}

return nil
}

// if file not exist we read our default BPMN process model and return the content
func readBPMNFileOrDefault(fileName string) ([]byte, string, error) {
var bpmnBytes []byte
Expand Down