From 2bc08dd2980b6f11cfe3655c05649db60a326afb Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Thu, 17 Nov 2022 15:09:11 +0100 Subject: [PATCH] feat: new subcmd deploy process models Add new sub command to deploy process models, either specified by the user via processModelPath or if not given it default process models BPMN and DMN are be deployed. The user is able to specify the count of versions which should be deployed for each. This allows to experiment with deployment distribution. --- go-chaos/cmd/deploy.go | 81 ++++++++++++++++++++++++++++++++++++++ go-chaos/cmd/root.go | 4 +- go-chaos/internal/zeebe.go | 66 +++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 go-chaos/cmd/deploy.go diff --git a/go-chaos/cmd/deploy.go b/go-chaos/cmd/deploy.go new file mode 100644 index 000000000..69e61fc71 --- /dev/null +++ b/go-chaos/cmd/deploy.go @@ -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) + } + }, +} diff --git a/go-chaos/cmd/root.go b/go-chaos/cmd/root.go index 1e07df760..6b6a55daa 100644 --- a/go-chaos/cmd/root.go +++ b/go-chaos/cmd/root.go @@ -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" ) @@ -28,6 +29,7 @@ var ( role string nodeId int processModelPath string + multipleVersions int variables string msgName string awaitResult bool diff --git a/go-chaos/internal/zeebe.go b/go-chaos/internal/zeebe.go index a17688f90..ac0ba799d 100644 --- a/go-chaos/internal/zeebe.go +++ b/go-chaos/internal/zeebe.go @@ -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