Skip to content

Commit

Permalink
Cherry-Pick for 1.13.8 (#156)
Browse files Browse the repository at this point in the history
* fix(bdd): adding retries to wait until job creation (#154)

Signed-off-by: shubhamchaudhary <shubham@chaosnative.com>

* feat(command): Pass command from experiment spec to runner (#153)

Closes #152

Signed-off-by: Julian Nodorp <jnodorp@jaconi.io>

* Add option to change default container registry during image build (#155)

Signed-off-by: Jakub Stejskal <xstejs24@gmail.com>

Co-authored-by: Shubham Chaudhary <shubham.chaudhary@mayadata.io>
Co-authored-by: Julian Nodorp <jnodorp@jaconi.io>
Co-authored-by: Jakub Stejskal <stejskinek@gmail.com>
  • Loading branch information
4 people authored Jul 15, 2021
1 parent 2baa49e commit 6f2b09a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 32 deletions.
15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Makefile for building Litmus and its tools
# Reference Guide - https://www.gnu.org/software/make/manual/make.html

REGISTRY ?= litmuschaos
IMG_NAME ?= chaos-runner
PACKAGE_VERSION ?= ci
DOCKER_REGISTRY ?= docker.io
DOCKER_REPO ?= litmuschaos
DOCKER_IMAGE ?= chaos-runner
DOCKER_TAG ?= ci
IS_DOCKER_INSTALLED = $(shell which docker >> /dev/null 2>&1; echo $$?)
HOME = $(shell echo $$HOME)

Expand Down Expand Up @@ -57,20 +58,20 @@ dockerops:
@echo "------------------"
@echo "--> Build Chaos-runner image..."
@echo "------------------"
@docker buildx build --file build/Dockerfile --progress plane --platform linux/arm64,linux/amd64 --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) .
@docker buildx build --file build/Dockerfile --progress plane --platform linux/arm64,linux/amd64 --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) .

.PHONY: dockerops-amd64
dockerops-amd64:
@echo "--------------------------------------------"
@echo "--> Build chaos-runner amd-64 docker image"
@echo "--------------------------------------------"
sudo docker build --file build/Dockerfile --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) . --build-arg TARGETARCH=amd64
sudo docker build --file build/Dockerfile --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) . --build-arg TARGETARCH=amd64
@echo "--------------------------------------------"
@echo "--> Push chaos-runner amd-64 docker image"
@echo "--------------------------------------------"
sudo docker push $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION)
sudo docker push $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG)

.PHONY: push
push:
@docker buildx build --file build/Dockerfile --progress plane --push --platform linux/arm64,linux/amd64 --tag $(REGISTRY)/$(IMG_NAME):$(PACKAGE_VERSION) .
@docker buildx build --file build/Dockerfile --progress plane --push --platform linux/arm64,linux/amd64 --tag $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG) .

2 changes: 1 addition & 1 deletion pkg/utils/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func buildContainerSpec(experiment *ExperimentDetails, envVars []corev1.EnvVar)
containerSpec := container.NewBuilder().
WithName(experiment.JobName).
WithImage(experiment.ExpImage).
WithCommandNew([]string{"/bin/bash"}).
WithCommandNew(experiment.ExpCommand).
WithArgumentsNew(experiment.ExpArgs).
WithImagePullPolicy(experiment.ExpImagePullPolicy).
WithEnvsNew(envVars)
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/experimentHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (expDetails *ExperimentDetails) SetDefaultAttributeValuesFromChaosExperimen
expDetails.SetImage(experimentSpec).
SetImagePullPolicy(experimentSpec).
SetArgs(experimentSpec).
SetCommand(experimentSpec).
SetLabels(experimentSpec, engine).
SetSecurityContext(experimentSpec).
SetHostPID(experimentSpec)
Expand Down Expand Up @@ -120,6 +121,12 @@ func (expDetails *ExperimentDetails) SetArgs(experimentSpec *litmuschaosv1alpha1
return expDetails
}

// SetCommand to execute inside the experiment image.
func (expDetails *ExperimentDetails) SetCommand(experimentSpec *litmuschaosv1alpha1.ChaosExperiment) *ExperimentDetails {
expDetails.ExpCommand = experimentSpec.Spec.Definition.Command
return expDetails
}

// SetSecurityContext sets the security context, in Experiment Structure
func (expDetails *ExperimentDetails) SetSecurityContext(experimentSpec *litmuschaosv1alpha1.ChaosExperiment) *ExperimentDetails {
expDetails.SecurityContext = experimentSpec.Spec.Definition.SecurityContext
Expand Down
56 changes: 56 additions & 0 deletions pkg/utils/experimentHelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func TestSetDefaultAttributeValuesFromChaosExperiment(t *testing.T) {
envMap: map[string]v1.EnvVar{},
ExpLabels: map[string]string{},
ExpArgs: []string{},
ExpCommand: []string{},
ConfigMaps: []v1alpha1.ConfigMap{},
Secrets: []v1alpha1.Secret{},
ExpImage: "",
Expand Down Expand Up @@ -637,6 +638,61 @@ func TestSetArgs(t *testing.T) {
}
}

func TestSetCommand(t *testing.T) {
fakeExperimentCommand := "fake-exp-command"
experiment := ExperimentDetails{
Name: "Fake-Exp-Name",
Namespace: "Fake NameSpace",
JobName: "fake-job-name",
StatusCheckTimeout: 10,
envMap: map[string]v1.EnvVar{},
ExpLabels: map[string]string{},
ExpArgs: []string{},
}

tests := map[string]struct {
chaosexperiment *v1alpha1.ChaosExperiment
}{
"Test Positive-1": {
chaosexperiment: &v1alpha1.ChaosExperiment{
ObjectMeta: metav1.ObjectMeta{
Name: experiment.Name,
Namespace: experiment.Namespace,
},
Spec: v1alpha1.ChaosExperimentSpec{
Definition: v1alpha1.ExperimentDef{
Command: []string{
fakeExperimentCommand,
},
},
},
},
},
}

for name, mock := range tests {
t.Run(name, func(t *testing.T) {
client := CreateFakeClient(t)

_, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosExperiments(mock.chaosexperiment.Namespace).Create(mock.chaosexperiment)
if err != nil {
t.Fatalf("experiment not created for %v test, err: %v", name, err)
}
experimentSpec, err := client.LitmusClient.LitmuschaosV1alpha1().ChaosExperiments(mock.chaosexperiment.Namespace).Get(mock.chaosexperiment.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("fail to get the chaosexperiment for %v test, err: %v", name, err)
}

expDetails := experiment.SetCommand(experimentSpec)
expectedResult := expDetails.ExpCommand
actualResult := mock.chaosexperiment.Spec.Definition.Command
if !reflect.DeepEqual(expectedResult, actualResult) {
t.Fatalf("Test %q failed to set the command from experiment", name)
}
})
}
}

func TestSetSecurityContext(t *testing.T) {
experiment := ExperimentDetails{
Name: "Fake-Exp-Name",
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ExperimentDetails struct {
ExpImage string
ExpImagePullPolicy v1.PullPolicy
ExpArgs []string
ExpCommand []string
JobName string
Namespace string
ConfigMaps []v1alpha1.ConfigMap
Expand Down
64 changes: 40 additions & 24 deletions tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

import (
"flag"
"fmt"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -243,39 +244,54 @@ var _ = Describe("BDD on chaos-runner", func() {
When("Check if the Job is spawned by chaos-runner", func() {
It("Should create a Pod delete Job", func() {

var jobName string
jobs, _ := clients.KubeClient.BatchV1().Jobs("litmus").List(metav1.ListOptions{})

for _, job := range jobs.Items {
matched, _ := regexp.MatchString("pod-delete-.*", job.Name)
if matched {
jobName = job.Name
break
}
}
err := retry.
Times(uint(180 / 2)).
Wait(time.Duration(2) * time.Second).
Try(func(attempt uint) error {
var jobName string
jobs, err := clients.KubeClient.BatchV1().Jobs("litmus").List(metav1.ListOptions{})
if err != nil {
return err
}
regExpr, err := regexp.Compile("pod-delete-.*")
if err != nil {
return err
}
for _, job := range jobs.Items {
matched := regExpr.MatchString(job.Name)
if matched {
jobName = job.Name
break
}
}
if jobName == "" {
return fmt.Errorf("unable to get the job, might be something wrong with chaos-runner")
}
return nil
})

Expect(jobName).To(
Not(BeEmpty()),
"unable to get the job, might be something wrong with chaos-runner",
Expect(err).To(
BeNil(),
"while listing experiment job in namespace litmus",
)
})
})

})

// This is a workaround to prevent a condition where operator expects presence of chaosresult to update target revert status
// Also, this minikube is a transient cluster brought up in the pipeline VM, so we cna skip the cleanup
/*
//Deleting all unused resources
var _ = AfterSuite(func() {
By("Deleting all CRDs")
crdDeletion := exec.Command("kubectl", "delete", "-f", "https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/chaos_crds.yaml").Run()

By("Deleting chaosengine CRD")
ceDeleteCRDs := exec.Command("kubectl", "delete", "crds", "chaosengines.litmuschaos.io").Run()
Expect(ceDeleteCRDs).To(BeNil())

By("Deleting other CRDs")
crdDeletion := exec.Command("kubectl", "delete", "crds", "chaosresults.litmuschaos.io", "chaosexperiments.litmuschaos.io").Run()
Expect(crdDeletion).To(BeNil())
By("Deleting RBAC Permissions")
rbacDeletion := exec.Command("kubectl", "delete", "-f", "https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/rbac.yaml").Run()

By("Deleting namespace litmus")
rbacDeletion := exec.Command("kubectl", "delete", "ns", "litmus").Run()
Expect(rbacDeletion).To(BeNil())
log.Info("deleted CRD and RBAC")
log.Info("deleted CRD and Namespace")
})
*/

0 comments on commit 6f2b09a

Please sign in to comment.