Skip to content

Commit

Permalink
(feat): Add support for induce chaos of statefulset (#83)
Browse files Browse the repository at this point in the history
* Add support for induce chaos for statefulset

Signed-off-by: chandan kumar <[email protected]>
  • Loading branch information
Chandan Kumar authored and Karthik Satchitanand committed Oct 25, 2019
1 parent 415d315 commit 4346f14
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
38 changes: 25 additions & 13 deletions pkg/controller/resource/deployment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package resource

import (
"errors"
"fmt"
"k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

Expand All @@ -10,23 +11,34 @@ import (

// CheckDeploymentAnnotation will check the annotation of deployment
func CheckDeploymentAnnotation(clientSet *kubernetes.Clientset, ce *chaosTypes.EngineInfo) (*chaosTypes.EngineInfo, error) {
targetAppList, err := clientSet.AppsV1().Deployments(ce.AppInfo.Namespace).List(metav1.ListOptions{LabelSelector: ce.Instance.Spec.Appinfo.Applabel, FieldSelector: ""})
targetAppList, err := getDeploymentLists(clientSet, ce)
if err != nil {
return ce, errors.New("unable to list apps matching labels")
return ce, err
}
chaosCandidates := 0
if len(targetAppList.Items) == 0 {
return ce, errors.New("no app deployments with matching labels")
}
for _, app := range targetAppList.Items {
ce.AppName = app.ObjectMeta.Name
ce.AppUUID = app.ObjectMeta.UID
annotationValue := app.ObjectMeta.GetAnnotations()[ChaosAnnotationKey]
chaosCandidates, err = ValidateAnnotation(annotationValue, chaosCandidates)
chaosEnabledDeployment := 0
for _, deployment := range targetAppList.Items {
ce.AppName = deployment.ObjectMeta.Name
ce.AppUUID = deployment.ObjectMeta.UID
annotationValue := deployment.ObjectMeta.GetAnnotations()[ChaosAnnotationKey]
chaosEnabledDeployment, err = ValidateAnnotation(annotationValue, chaosEnabledDeployment)
if err != nil {
return ce, err
}
chaosTypes.Log.Info("chaos candidate : ", "appName", ce.AppName, "appUUID", ce.AppUUID)
chaosTypes.Log.Info("Deployment chaos candidate, appName:", ce.AppName," appUUID: ", ce.AppUUID)
}
return ce, nil
}

// getDeploymentLists will list the deployments which having the chaos label
func getDeploymentLists(clientSet *kubernetes.Clientset, ce *chaosTypes.EngineInfo) (*v1.DeploymentList, error) {
targetAppList, err := clientSet.AppsV1().Deployments(ce.AppInfo.Namespace).List(metav1.ListOptions{
LabelSelector: ce.Instance.Spec.Appinfo.Applabel,
FieldSelector: ""})
if err != nil {
return nil, fmt.Errorf("error while listing deployments with matching labels %s", ce.Instance.Spec.Appinfo.Applabel)
}
if len(targetAppList.Items) == 0 {
return nil, fmt.Errorf("no deployments apps with matching labels %s", ce.Instance.Spec.Appinfo.Applabel)
}
return targetAppList, err
}
9 changes: 7 additions & 2 deletions pkg/controller/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ func CheckChaosAnnotation(ce *chaosTypes.EngineInfo) (*chaosTypes.EngineInfo, er
case "deployment", "deployments":
ce, err = CheckDeploymentAnnotation(clientSet, ce)
if err != nil {
return ce, fmt.Errorf("no deployement found with required annotation, err: %+v", err)
return ce, fmt.Errorf("resource type 'deployment', err: %+v", err)
}
case "statefulset", "statefulsets":
ce, err = CheckStatefulSetAnnotation(clientSet, ce)
if err != nil {
return ce, fmt.Errorf("resource type 'statefulset', err: %+v", err)
}
default:
return ce, fmt.Errorf("resource type not supported for induce chaos")
return ce, fmt.Errorf("resource type '%s' not supported for induce chaos", ce.AppInfo.Kind)
}
return ce, nil
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/controller/resource/statefulset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package resource

import (
"fmt"
"k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

chaosTypes "github.com/litmuschaos/chaos-operator/pkg/controller/types"
)

// CheckStatefulSetAnnotation will check the annotation of StatefulSet
func CheckStatefulSetAnnotation(clientSet *kubernetes.Clientset, ce *chaosTypes.EngineInfo) (*chaosTypes.EngineInfo, error) {
targetAppList, err := getStatefulSetLists(clientSet, ce)
if err != nil {
return ce, err
}
chaosEnabledStatefulset := 0
for _, statefulset := range targetAppList.Items {
ce.AppName = statefulset.ObjectMeta.Name
ce.AppUUID = statefulset.ObjectMeta.UID
annotationValue := statefulset.ObjectMeta.GetAnnotations()[ChaosAnnotationKey]
chaosEnabledStatefulset, err = ValidateAnnotation(annotationValue, chaosEnabledStatefulset)
if err != nil {
return ce, err
}
chaosTypes.Log.Info("Statefulset chaos candidate, appName:", ce.AppName," appUUID: ", ce.AppUUID)
}
return ce, nil
}

// getStatefulSetLists will list the statefulset which having the chaos label
func getStatefulSetLists(clientSet *kubernetes.Clientset, ce *chaosTypes.EngineInfo) (*v1.StatefulSetList, error) {
targetAppList, err := clientSet.AppsV1().StatefulSets(ce.AppInfo.Namespace).List(metav1.ListOptions{
LabelSelector: ce.Instance.Spec.Appinfo.Applabel,
FieldSelector: ""})
if err != nil {
return nil, fmt.Errorf("error while listing statefulsets with matching labels %s", ce.Instance.Spec.Appinfo.Applabel)
}
if len(targetAppList.Items) == 0 {
return nil, fmt.Errorf("no statefulset apps with matching labels %s", ce.Instance.Spec.Appinfo.Applabel)
}
return targetAppList, err
}

0 comments on commit 4346f14

Please sign in to comment.