Skip to content

Commit

Permalink
chore(resourceRequirements): Adding resource requirements in chaos pod (
Browse files Browse the repository at this point in the history
#93)

Signed-off-by: shubhamchaudhary <[email protected]>
  • Loading branch information
ispeakc0de authored Sep 29, 2020
1 parent 8198727 commit d0055cc
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/_output/
.idea/
vendor/modules.txt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test:
@echo "------------------"
@echo "Aquire YAML for performing tests"
@echo "------------------"
@mkdir -p ./build/_output/test;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/chaos_crds.yaml -P ./build/_output/test ;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/rbac.yaml -P ./build/_output/test; wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/operator.yaml -P ./build/_output/test
@mkdir -p ./build/_output/test;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/chaos_crds.yaml -P ./build/_output/test ;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/rbac.yaml -P ./build/_output/test;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/tests/manifest/pod_delete_rbac.yaml -P ./build/_output/test;wget -q -N https://raw.githubusercontent.com/litmuschaos/chaos-operator/master/deploy/operator.yaml -P ./build/_output/test
@echo "------------------"
@echo "--> Run Go Test"
@echo "------------------"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.13
require (
github.com/imdario/mergo v0.3.9 // indirect
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef
github.com/litmuschaos/chaos-operator v0.0.0-20200917035646-2633098da932
github.com/litmuschaos/elves v0.0.0-20200704104545-672722932140
github.com/litmuschaos/chaos-operator v0.0.0-20200929102701-8805f49fa9cf
github.com/litmuschaos/elves v0.0.0-20200929113647-fbf07cc3600f
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/pkg/errors v0.9.1
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,11 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/libopenstorage/openstorage v1.0.0/go.mod h1:Sp1sIObHjat1BeXhfMqLZ14wnOzEhNx2YQedreMcUyc=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/litmuschaos/chaos-operator v0.0.0-20200917035646-2633098da932 h1:hYL8VZ+40N3TKBPiEwFxbfhzzTLXU1Qvr6yc2u3lA60=
github.com/litmuschaos/chaos-operator v0.0.0-20200917035646-2633098da932/go.mod h1:QHmeCgV1yDMBnNk5tz0c9OVGUUC7MWrVIAlrPWMRhK4=
github.com/litmuschaos/elves v0.0.0-20200502084504-13be0e6937ee h1:k0ry/r6fbUXp48eGioI9SbVfbSQPBJdhNbkHg7RpHB0=
github.com/litmuschaos/chaos-operator v0.0.0-20200929102701-8805f49fa9cf h1:itW05EDZv/G8cNFiXmXTryWT6fezwVBTWYiiDH0u4j0=
github.com/litmuschaos/chaos-operator v0.0.0-20200929102701-8805f49fa9cf/go.mod h1:QHmeCgV1yDMBnNk5tz0c9OVGUUC7MWrVIAlrPWMRhK4=
github.com/litmuschaos/elves v0.0.0-20200502084504-13be0e6937ee/go.mod h1:DsbHGNUq/78NZozWVVI9Q6eBei4I+JjlkkD5aibJ3MQ=
github.com/litmuschaos/elves v0.0.0-20200704104545-672722932140 h1:H6xJtfYX++HR0FB58KDs73PTU1Nt2UiOj3glmv9uHy0=
github.com/litmuschaos/elves v0.0.0-20200704104545-672722932140/go.mod h1:DsbHGNUq/78NZozWVVI9Q6eBei4I+JjlkkD5aibJ3MQ=
github.com/litmuschaos/elves v0.0.0-20200929113647-fbf07cc3600f h1:t/DDHFV9aVDCwzW13+1evM6DiycUmGqNh+H76NvXTlA=
github.com/litmuschaos/elves v0.0.0-20200929113647-fbf07cc3600f/go.mod h1:DsbHGNUq/78NZozWVVI9Q6eBei4I+JjlkkD5aibJ3MQ=
github.com/lpabon/godbc v0.1.1/go.mod h1:Jo9QV0cf3U6jZABgiJ2skINAXb9j8m51r07g4KI92ZA=
github.com/lucas-clemente/aes12 v0.0.0-20171027163421-cd47fb39b79f/go.mod h1:JpH9J1c9oX6otFSgdUHwUBUizmKlrMjxWnIAjff4m04=
github.com/lucas-clemente/quic-clients v0.1.0/go.mod h1:y5xVIEoObKqULIKivu+gD/LU90pL73bTdtQjPBvtCBk=
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func buildContainerSpec(experiment *ExperimentDetails, envVar []corev1.EnvVar) (

}

if !reflect.DeepEqual(experiment.ResourceRequirements, corev1.ResourceRequirements{}) {

containerSpec.WithResourceRequirements(experiment.ResourceRequirements)

}

if experiment.VolumeOpts.VolumeMounts != nil {
containerSpec.WithVolumeMountsNew(experiment.VolumeOpts.VolumeMounts)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/experimentHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func (expDetails *ExperimentDetails) SetValueFromChaosEngine(engine *EngineDetai
if err := expDetails.SetExpNodeSelectorFromEngine(engine.Name, clients); err != nil {
return err
}
if err := expDetails.SetResourceRequirementsFromEngine(engine.Name, clients); err != nil {
return err
}
return nil
}

Expand All @@ -262,6 +265,23 @@ func (expDetails *ExperimentDetails) SetExpAnnotationFromEngine(engineName strin
return nil
}

// SetResourceRequirementsFromEngine will add the resource requirements provided inside chaosengine
func (expDetails *ExperimentDetails) SetResourceRequirementsFromEngine(engineName string, clients ClientSets) error {

engineSpec, err := clients.LitmusClient.LitmuschaosV1alpha1().ChaosEngines(expDetails.Namespace).Get(engineName, metav1.GetOptions{})
if err != nil {
return errors.Errorf("Unable to get ChaosEngine Resource in namespace: %v", expDetails.Namespace)
}

expRefList := engineSpec.Spec.Experiments
for i := range expRefList {
if expRefList[i].Name == expDetails.Name {
expDetails.ResourceRequirements = expRefList[i].Spec.Components.Resources
}
}
return nil
}

// SetExpNodeSelectorFromEngine will add the nodeSelector attribute based the key/value provided in the chaosEngine
func (expDetails *ExperimentDetails) SetExpNodeSelectorFromEngine(engineName string, clients ClientSets) error {

Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
clientV1alpha1 "github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned"
volume "github.com/litmuschaos/elves/kubernetes/volume/v1alpha1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -51,7 +52,8 @@ type ExperimentDetails struct {
HostPID bool
// InstanceID is passed as env inside chaosengine
// It is separately specified here because this attribute is common for all experiment.
InstanceID string
InstanceID string
ResourceRequirements v1.ResourceRequirements
}

//VolumeOpts is a strcuture for all volume related operations
Expand Down
23 changes: 14 additions & 9 deletions tests/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ var (
)

func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "path to kubeconfig to invoke kubernetes API calls")
flag.Parse()
flag.StringVar(&kubeconfig, "kubeconfig", os.Getenv("HOME")+"/.kube/config", "path to kubeconfig to invoke kubernetes API calls")
}
func TestChaos(t *testing.T) {

Expand All @@ -57,7 +56,9 @@ func TestChaos(t *testing.T) {
}

var _ = BeforeSuite(func() {
kubeconfig = os.Getenv("HOME") + "/.kube/config"

flag.Parse()

var err error
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
Expand All @@ -83,20 +84,20 @@ var _ = BeforeSuite(func() {

//Creating crds
By("Installing Litmus CRDs")
err = exec.Command("kubectl", "create", "-f", "../build/_output/test/chaos_crds.yaml").Run()
err = exec.Command("kubectl", "apply", "-f", "../build/_output/test/chaos_crds.yaml").Run()
if err != nil {
klog.Infof("Unable to create Litmus CRD's, due to error: %v", err)
}

//Creating rbacs
err = exec.Command("kubectl", "create", "-f", "../build/_output/test/rbac.yaml").Run()
err = exec.Command("kubectl", "apply", "-f", "../build/_output/test/rbac.yaml").Run()
if err != nil {
klog.Infof("Unable to create RBAC Permissions, due to error: %v", err)
}

//Creating Chaos-Operator
By("Installing Chaos-Operator")
err = exec.Command("kubectl", "create", "-f", "../build/_output/test/operator.yaml").Run()
err = exec.Command("kubectl", "apply", "-f", "../build/_output/test/operator.yaml").Run()
if err != nil {
klog.Infof("Unable to create Chaos-operator, due to error: %v", err)
}
Expand All @@ -114,10 +115,15 @@ var _ = BeforeSuite(func() {
break
}

err = exec.Command("kubectl", "create", "-f", "https://hub.litmuschaos.io/api/chaos/master?file=charts/generic/experiments.yaml", "-n", "litmus").Run()
err = exec.Command("kubectl", "apply", "-f", "https://hub.litmuschaos.io/api/chaos/master?file=charts/generic/experiments.yaml", "-n", "litmus").Run()
if err != nil {
klog.Infof("Unable to create Pod-Delete Experiment, due to error: %v", err)
}

err = exec.Command("kubectl", "apply", "-f", "../build/_output/test/pod_delete_rbac.yaml", "-n", "litmus").Run()
if err != nil {
klog.Infof("Unable to create pod-delete rbac, due to error: %v", err)
}
})

//BDD Tests to check secondary resources
Expand Down Expand Up @@ -153,7 +159,6 @@ var _ = Describe("BDD on chaos-runner", func() {
},
},
Spec: v1.PodSpec{
ServiceAccountName: "litmus",
Containers: []v1.Container{
{
Name: "nginx",
Expand Down Expand Up @@ -194,7 +199,7 @@ var _ = Describe("BDD on chaos-runner", func() {
AppKind: "deployment",
},
EngineState: "active",
ChaosServiceAccount: "litmus",
ChaosServiceAccount: "pod-delete-sa",
Components: v1alpha1.ComponentParams{
Runner: v1alpha1.RunnerInfo{
Image: "litmuschaos/chaos-runner:ci",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/litmuschaos/elves/kubernetes/container/build.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ github.com/imdario/mergo
github.com/jpillora/go-ogle-analytics
# github.com/json-iterator/go v1.1.9
github.com/json-iterator/go
# github.com/litmuschaos/chaos-operator v0.0.0-20200813024250-a5d1a83bb863
# github.com/konsorten/go-windows-terminal-sequences v1.0.2
github.com/konsorten/go-windows-terminal-sequences
# github.com/litmuschaos/chaos-operator v0.0.0-20200929102701-8805f49fa9cf
github.com/litmuschaos/chaos-operator/pkg/apis/litmuschaos/v1alpha1
github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned
github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/scheme
github.com/litmuschaos/chaos-operator/pkg/client/clientset/versioned/typed/litmuschaos/v1alpha1
# github.com/litmuschaos/elves v0.0.0-20200704104545-672722932140
# github.com/litmuschaos/elves v0.0.0-20200929113647-fbf07cc3600f
github.com/litmuschaos/elves/kubernetes/container
github.com/litmuschaos/elves/kubernetes/job
github.com/litmuschaos/elves/kubernetes/jobspec
Expand Down Expand Up @@ -99,6 +101,8 @@ github.com/onsi/gomega/matchers/support/goraph/util
github.com/onsi/gomega/types
# github.com/pkg/errors v0.9.1
github.com/pkg/errors
# github.com/sirupsen/logrus v1.4.2
github.com/sirupsen/logrus
# github.com/spf13/pflag v1.0.5
github.com/spf13/pflag
# golang.org/x/crypto v0.0.0-20191028145041-f83a4685e152
Expand Down

0 comments on commit d0055cc

Please sign in to comment.