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

Allow overriding k8s namespace #193

Merged
merged 9 commits into from
Oct 14, 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
29 changes: 29 additions & 0 deletions .github/workflows/publishZbchaosImage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release zbchaos Docker Image
on:
pull_request: {}
release:
types: [published]

jobs:
publish-image:
runs-on: ubuntu-latest
env:
VERSION: ${{ github.event.release.name || github.sha }}
steps:
- uses: actions/checkout@v3
- name: Setup BuildKit
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: gcr.io
username: _json_key
password: ${{ secrets.DOCKER_GCR }}
- name: Publish Docker Image
uses: docker/build-push-action@v3
with:
context: go-chaos
push: true
tags: gcr.io/zeebe-io/zbchaos:${{ env.VERSION }}
cache-from: type=gha
cache-to: type=gha,mode=max
6 changes: 6 additions & 0 deletions go-chaos/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.*
!cmd/
!internal
!main.go
!go.mod
!go.sum
21 changes: 21 additions & 0 deletions go-chaos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# syntax=docker/dockerfile:1.4
FROM golang:alpine as builder

WORKDIR /app

COPY --link go.mod go.sum .
RUN go mod download

COPY --link cmd/ ./cmd
COPY --link internal ./internal
COPY --link main.go ./
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o zbchaos main.go

FROM scratch

WORKDIR /app

COPY --from=builder /app/zbchaos /usr/local/bin/

ENTRYPOINT ["zbchaos"]
CMD ["worker"]
2 changes: 1 addition & 1 deletion go-chaos/cmd/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package cmd
import (
"fmt"

"github.com/camunda-cloud/zeebe/clients/go/pkg/zbc"
"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"
"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
v1 "k8s.io/api/core/v1"
Expand Down
7 changes: 7 additions & 0 deletions go-chaos/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ var (
)

var Verbose bool
var KubeConfigPath string
var Namespace string

var rootCmd = &cobra.Command{
Use: "zbchaos",
Short: "Zeebe chaos is a chaos experiment tool for Zeebe",
Long: `A chaos experimenting toolkit for Zeebe.
Perfect to inject some chaos into your brokers and gateways.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
internal.Verbosity = Verbose
internal.Namespace = Namespace
internal.KubeConfigPath = KubeConfigPath
Comment on lines +52 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need also some defaults ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVar(&KubeConfigPath, "kubeconfig", "", "path the the kube config that will be used")
rootCmd.PersistentFlags().StringVarP(&Namespace, "namespace", "n", "", "connect to the given namespace")
}

func NewCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion go-chaos/cmd/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"os"
"text/tabwriter"

"github.com/camunda-cloud/zeebe/clients/go/pkg/pb"
"github.com/camunda/zeebe/clients/go/v8/pkg/pb"
"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)
Expand Down
2 changes: 1 addition & 1 deletion go-chaos/cmd/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"bytes"
"testing"

"github.com/camunda-cloud/zeebe/clients/go/pkg/pb"
"github.com/camunda/zeebe/clients/go/v8/pkg/pb"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion go-chaos/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"time"

"github.com/camunda-cloud/zeebe/clients/go/pkg/zbc"
"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"
"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)
Expand Down
112 changes: 112 additions & 0 deletions go-chaos/cmd/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 (
"context"
"os"
"strings"
"time"

"github.com/camunda/zeebe/clients/go/v8/pkg/entities"
"github.com/camunda/zeebe/clients/go/v8/pkg/worker"
"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"
"github.com/spf13/cobra"
)

const jobType = "zbchaos"

func init() {
rootCmd.AddCommand(workerCommand)
}

var workerCommand = &cobra.Command{
Use: "worker",
Short: "Starts a worker for zbchaos jobs",
Long: "Starts a worker for zbchaos jobs that executes zbchaos commands",
Run: start_worker,
}

type ChaosProvider struct {
path string
args []string
timeout int64
}

type ZbChaosVariables struct {
clusterId string
provider ChaosProvider
}

func start_worker(cmd *cobra.Command, args []string) {
credsProvider, err := zbc.NewOAuthCredentialsProvider(&zbc.OAuthProviderConfig{
ClientID: os.Getenv("TESTBENCH_CLIENT_ID"),
ClientSecret: os.Getenv("TESTBENCH_CLIENT_SECRET"),
Audience: strings.TrimSuffix(os.Getenv("TESTBENCH_ADDRESS"), ":443"),
AuthorizationServerURL: os.Getenv("TESTBENCH_AUTHORIZATION_SERVER_URL"),
})
if err != nil {
panic(err)
}

client, err := zbc.NewClient(&zbc.ClientConfig{
GatewayAddress: os.Getenv("TESTBENCH_ADDRESS"),
CredentialsProvider: credsProvider,
})

if err != nil {
panic(err)
}

jobWorker := client.NewJobWorker().JobType(jobType).Handler(handleZbChaosJob).Open()
jobWorker.AwaitClose()
}

func handleZbChaosJob(client worker.JobClient, job entities.Job) {
ctx := context.Background()

jobVariables := ZbChaosVariables{
provider: ChaosProvider{
timeout: 15 * 60, // 15 minute default timeout
},
}
err := job.GetVariablesAs(&jobVariables)
if err != nil {
// Can't parse variables, no sense in retrying
_, _ = client.NewFailJobCommand().JobKey(job.Key).Retries(0).Send(ctx)
return
}

timeout := time.Duration(jobVariables.provider.timeout) * time.Second
commandCtx, cancelCommand := context.WithTimeout(ctx, timeout)
defer cancelCommand()

err = runZbChaosCommand(jobVariables.provider.args, commandCtx)
if err != nil {
_, _ = client.NewFailJobCommand().JobKey(job.Key).Retries(job.Retries - 1).Send(ctx)
return
}

_, _ = client.NewCompleteJobCommand().JobKey(job.Key).Send(ctx)
}

func runZbChaosCommand(args []string, ctx context.Context) error {
rootCmd.SetArgs(args)
_, err := rootCmd.ExecuteContextC(ctx)
if err != nil {
return err
}
return nil
}
10 changes: 5 additions & 5 deletions go-chaos/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/zeebe-io/zeebe-chaos/go-chaos

go 1.17
go 1.19

require (
github.com/camunda-cloud/zeebe/clients/go v1.3.14
github.com/camunda/zeebe/clients/go/v8 v8.1.0
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
google.golang.org/grpc v1.50.0
Expand All @@ -28,7 +28,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand All @@ -50,8 +50,8 @@ require (
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading