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

Improve current state #231

Merged
merged 7 commits into from
Nov 15, 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
51 changes: 51 additions & 0 deletions go-chaos/internal/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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

import (
"context"
"errors"
"fmt"

v12 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (c K8Client) getGatewayDeployment() (*v12.Deployment, error) {
listOptions := metav1.ListOptions{
LabelSelector: getSelfManagedGatewayLabels(),
}
deploymentList, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions)
if err != nil {
return nil, err
}

if deploymentList == nil || len(deploymentList.Items) <= 0 {
// lets check for SaaS setup
listOptions.LabelSelector = getSaasGatewayLabels()
deploymentList, err = c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions)
if err != nil {
return nil, err
}

// here it is currently hard to distingush between not existing and embedded gateway;
// since we don't use embedded gateway in our current chaos setup I would not support it right now here
if deploymentList == nil || len(deploymentList.Items) <= 0 {
return nil, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found!", c.GetCurrentNamespace()))
}
}

return &deploymentList.Items[0], err
}
96 changes: 96 additions & 0 deletions go-chaos/internal/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_ShouldReturnTrueForRunningGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSelfManagedGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.NoError(t, err)
assert.Equal(t, true, running)
}

func Test_ShouldReturnTrueForRunningSaaSGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.NoError(t, err)
assert.Equal(t, true, running)
}

func Test_ShouldReturnErrorForNonExistingDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.Error(t, err)
require.Contains(t, err.Error(), "Expected to find standalone gateway deployment")
assert.Equal(t, false, running)
}

func Test_ShouldReturnGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSelfManagedGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
deployment, err := k8Client.getGatewayDeployment()

// then
require.NoError(t, err)
assert.Equal(t, "gateway", deployment.Name)
}

func Test_ShouldReturnSaaSGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
deployment, err := k8Client.getGatewayDeployment()

// then
require.NoError(t, err)
assert.Equal(t, "gateway", deployment.Name)
}
14 changes: 12 additions & 2 deletions go-chaos/internal/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,22 @@ func (c K8Client) CreatePodWithLabelsAndName(t *testing.T, selector *metav1.Labe
require.NoError(t, err)
}

func (c K8Client) CreateDeploymentWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, podName string) {
func (c K8Client) CreateDeploymentWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) {
_, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).Create(context.TODO(), &v12.Deployment{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: podName},
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: name},
Spec: v12.DeploymentSpec{},
Status: v12.DeploymentStatus{},
}, metav1.CreateOptions{})

require.NoError(t, err)
}

func (c K8Client) CreateStatefulSetWithLabelsAndName(t *testing.T, selector *metav1.LabelSelector, name string) {
_, err := c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).Create(context.TODO(), &v12.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Labels: selector.MatchLabels, Name: name},
Spec: v12.StatefulSetSpec{},
Status: v12.StatefulSetStatus{},
}, metav1.CreateOptions{})

require.NoError(t, err)
}
40 changes: 29 additions & 11 deletions go-chaos/internal/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,39 @@ import (

func (c K8Client) ApplyNetworkPatch() error {
Copy link
Member

Choose a reason for hiding this comment

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

💭

Nice that you got this working for SaaS too. I think the only thing missing would be to pause reconciliation. Same for all other functionality that modifies any of the resources managed by the ZeebeCluster CRD.

Copy link
Member Author

Choose a reason for hiding this comment

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

Jep right now it is only this I think the other stuff is inside the container like disconnecting


// todo support cloud
listOptions := metav1.ListOptions{
LabelSelector: "app=camunda-platform",
}

statefulSetList, err := c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).List(context.TODO(), listOptions)
statefulSet, err := c.GetZeebeStatefulSet()
if err != nil {
return err
}

if len(statefulSetList.Items) <= 0 {
return errors.New(fmt.Sprintf("Expected to find the Zeebe statefulset but nothing was found in namespace %s", c.GetCurrentNamespace()))
}
patch := []byte(`{
"spec":{
"template":{
"spec":{
"containers":[
{
"name": "zeebe",
"securityContext":{
"capabilities":{
"add":["NET_ADMIN"]
}
}
}]
}
}
}
}`)

_, err = c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).Patch(context.TODO(), statefulSet.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
return err
}

statefulSet := statefulSetList.Items[0]
func (c K8Client) ApplyNetworkPatchOnGateway() error {

deployment, err := c.getGatewayDeployment()
if err != nil {
return err
}

patch := []byte(`{
"spec":{
Expand All @@ -61,7 +79,7 @@ func (c K8Client) ApplyNetworkPatch() error {
}
}`)

_, err = c.Clientset.AppsV1().StatefulSets(c.GetCurrentNamespace()).Patch(context.TODO(), statefulSet.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
_, err = c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).Patch(context.TODO(), deployment.Name, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
return err
}

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

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Test_ShouldApplyNetworkPatchOnStatefulSet(t *testing.T) {
// given
k8Client := CreateFakeClient()
k8Client.CreateStatefulSetWithLabelsAndName(t, &metav1.LabelSelector{}, "zeebe")

// when
err := k8Client.ApplyNetworkPatch()

// then
require.NoError(t, err)

statefulSet, err := k8Client.GetZeebeStatefulSet()
require.NoError(t, err)

require.NotNil(t, statefulSet)
assert.Equal(t, v1.Capability("NET_ADMIN"), statefulSet.Spec.Template.Spec.Containers[0].SecurityContext.Capabilities.Add[0], "Expected to add capability to statefulset")
}

func Test_ShouldApplyNetworkPatchOnDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
err = k8Client.ApplyNetworkPatchOnGateway()

// then
require.NoError(t, err)

deployment, err := k8Client.getGatewayDeployment()
require.NoError(t, err)

require.NotNil(t, deployment)
assert.Equal(t, v1.Capability("NET_ADMIN"), deployment.Spec.Template.Spec.Containers[0].SecurityContext.Capabilities.Add[0], "Expected to add capability to deployment")
}
22 changes: 1 addition & 21 deletions go-chaos/internal/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,11 @@ func (c K8Client) checkIfBrokersAreRunning() (bool, error) {
}

func (c K8Client) checkIfGatewaysAreRunning() (bool, error) {
listOptions := metav1.ListOptions{
LabelSelector: getSelfManagedGatewayLabels(),
}
deploymentList, err := c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions)
deployment, err := c.getGatewayDeployment()
if err != nil {
return false, err
}

if deploymentList == nil || len(deploymentList.Items) <= 0 {
// lets check for SaaS setup
listOptions.LabelSelector = getSaasGatewayLabels()
deploymentList, err = c.Clientset.AppsV1().Deployments(c.GetCurrentNamespace()).List(context.TODO(), listOptions)
if err != nil {
return false, err
}

// here it is currently hard to distingush between not existing and embedded gateway;
// since we don't use embedded gateway in our current chaos setup I would not support it right now here
if deploymentList == nil || len(deploymentList.Items) <= 0 {
return false, errors.New(fmt.Sprintf("Expected to find standalone gateway deployment in namespace %s, but none found!", c.GetCurrentNamespace()))
}
}

deployment := deploymentList.Items[0]

if deployment.Status.UnavailableReplicas > 0 {
if Verbosity {
fmt.Printf("Gateway deployment not fully available. [Available replicas: %d/%d]\n", deployment.Status.AvailableReplicas, deployment.Status.Replicas)
Expand Down
43 changes: 0 additions & 43 deletions go-chaos/internal/pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,46 +201,3 @@ func Test_GetEmbeddedGateway(t *testing.T) {
require.NotEmpty(t, names)
assert.Equal(t, "broker", names[0], "Expected to retrieve broker")
}

func Test_ShouldReturnTrueForRunningGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSelfManagedGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.NoError(t, err)
assert.Equal(t, true, running)
}

func Test_ShouldReturnTrueForRunningSaaSGatewayDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()
selector, err := metav1.ParseToLabelSelector(getSaasGatewayLabels())
require.NoError(t, err)
k8Client.CreateDeploymentWithLabelsAndName(t, selector, "gateway")

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.NoError(t, err)
assert.Equal(t, true, running)
}

func Test_ShouldReturnErrorForNonExistingDeployment(t *testing.T) {
// given
k8Client := CreateFakeClient()

// when
running, err := k8Client.checkIfGatewaysAreRunning()

// then
require.Error(t, err)
require.Contains(t, err.Error(), "Expected to find standalone gateway deployment")
assert.Equal(t, false, running)
}
Loading