Skip to content

Commit

Permalink
Merge pull request #193 from zeebe-io/os-zbchaos-namespace
Browse files Browse the repository at this point in the history
Allow overriding k8s namespace
  • Loading branch information
ChrisKujawa authored Oct 14, 2022
2 parents 314505f + 0106a02 commit baa7499
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 25 deletions.
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"]
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
},
}

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
24 changes: 24 additions & 0 deletions go-chaos/internal/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 internal

// defines whether the functions should print verbose output
var Verbosity bool = false

// defines if a custom kube config should be used instead of the default one found by k8s
var KubeConfigPath string

// sets the namespace to be used instead of the namespace from the current kube context
var Namespace string
39 changes: 23 additions & 16 deletions go-chaos/internal/k8helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package internal

import (
"flag"
"fmt"
"path/filepath"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"

// in order to authenticate with gcp
// https://github.com/kubernetes/client-go/issues/242#issuecomment-314642965
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand All @@ -40,15 +41,14 @@ func (c *K8Client) GetCurrentNamespace() string {

// Creates a kubernetes client, based on the local kubeconfig
func CreateK8Client() (K8Client, error) {
kubeconfig := findKubeconfigPath()

return createK8Client(kubeconfig)
settings := findKubernetesSettings()
return createK8Client(settings)
}

func createK8Client(kubeconfig *string) (K8Client, error) {
func createK8Client(settings KubernetesSettings) (K8Client, error) {
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfig},
&clientcmd.ConfigOverrides{})
&clientcmd.ClientConfigLoadingRules{ExplicitPath: settings.kubeConfigPath},
&clientcmd.ConfigOverrides{Context: api.Context{Namespace: settings.namespace}})

k8ClientConfig, err := clientConfig.ClientConfig()
if err != nil {
Expand All @@ -70,14 +70,21 @@ func createK8Client(kubeconfig *string) (K8Client, error) {
return K8Client{Clientset: clientset, ClientConfig: clientConfig}, nil
}

func findKubeconfigPath() *string {
//// based on https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
type KubernetesSettings struct {
kubeConfigPath string
namespace string
}

func findKubernetesSettings() KubernetesSettings {
kubeconfig := KubeConfigPath
if kubeconfig == "" {
// based on https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
}
return KubernetesSettings{
kubeConfigPath: kubeconfig,
namespace: Namespace,
}
flag.Parse()
return kubeconfig
}
28 changes: 22 additions & 6 deletions go-chaos/internal/k8helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

func Test_CreateK8ClientWithPath(t *testing.T) {
// given
path := "kubeconfigtest.yml"
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"}

// when
client, err := createK8Client(&path)
client, err := createK8Client(settings)

// then
assert.NoError(t, err)
Expand All @@ -39,8 +39,8 @@ func Test_CreateK8ClientWithPath(t *testing.T) {

func Test_ShouldReturnNamespace(t *testing.T) {
// given
path := "kubeconfigtest.yml"
client, err := createK8Client(&path)
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml"}
client, err := createK8Client(settings)
require.NoError(t, err)

// when
Expand All @@ -54,13 +54,29 @@ func Test_ShouldReturnNamespace(t *testing.T) {
assert.Equal(t, namespace, currentNamespace)
}

func Test_ShouldReturnNamespaceOverride(t *testing.T) {
// given
settings := KubernetesSettings{kubeConfigPath: "kubeconfigtest.yml", namespace: "namespace-override"}
client, err := createK8Client(settings)
require.NoError(t, err)

// when
currentNamespace := client.GetCurrentNamespace()
clientNamespace, _, err := client.ClientConfig.Namespace()
assert.NoError(t, err)

// then
assert.Equal(t, "namespace-override", currentNamespace)
assert.Equal(t, currentNamespace, clientNamespace)
}

func Test_ResolveDefaultKubePath(t *testing.T) {
// given
home := homedir.HomeDir()

// when
path := findKubeconfigPath()
settings := findKubernetesSettings()

// then
assert.Equal(t, strings.Join([]string{home, ".kube/config"}, "/"), *path)
assert.Equal(t, strings.Join([]string{home, ".kube/config"}, "/"), settings.kubeConfigPath)
}
3 changes: 0 additions & 3 deletions go-chaos/internal/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ import (
"k8s.io/client-go/transport/spdy"
)

// defines whether the functions should print verbose output
var Verbosity bool = false

func (c K8Client) GetBrokerPods() (*v1.PodList, error) {
listOptions := metav1.ListOptions{
LabelSelector: getSelfManagedBrokerLabels(),
Expand Down

0 comments on commit baa7499

Please sign in to comment.