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

limited by one ns and no event #443

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# builder image
FROM golang:1.16-alpine3.14 as builder
FROM golang:1.17.8-alpine3.14 as builder

ENV CGO_ENABLED 0
ENV GO111MODULE on
Expand All @@ -13,7 +13,7 @@ RUN go build -o /bin/chaoskube -v \
-ldflags "-X main.version=$(git describe --tags --always --dirty) -w -s"

# final image
FROM alpine:3.14.3
FROM alpine:3.14.6
MAINTAINER Linki <[email protected]>

RUN apk --no-cache add ca-certificates dumb-init tzdata
Expand Down
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,29 @@ Use `UTC`, `Local` or pick a timezone name from the [(IANA) tz database](https:/

## Flags

| Option | Description | Default |
|---------------------------|----------------------------------------------------------------------|----------------------------|
| `--interval` | interval between pod terminations | 10m |
| `--labels` | label selector to filter pods by | (matches everything) |
| `--annotations` | annotation selector to filter pods by | (matches everything) |
| `--kinds` | owner's kind selector to filter pods by | (all kinds) |
| `--namespaces` | namespace selector to filter pods by | (all namespaces) |
| `--namespace-labels` | label selector to filter namespaces and its pods by | (all namespaces) |
| `--included-pod-names` | regular expression pattern for pod names to include | (all included) |
| `--excluded-pod-names` | regular expression pattern for pod names to exclude | (none excluded) |
| `--excluded-weekdays` | weekdays when chaos is to be suspended, e.g. "Sat,Sun" | (no weekday excluded) |
| `--excluded-times-of-day` | times of day when chaos is to be suspended, e.g. "22:00-08:00" | (no times of day excluded) |
| `--excluded-days-of-year` | days of a year when chaos is to be suspended, e.g. "Apr1,Dec24" | (no days of year excluded) |
| `--timezone` | timezone from tz database, e.g. "America/New_York", "UTC" or "Local" | (UTC) |
| `--max-runtime` | Maximum runtime before chaoskube exits | -1s (infinite time) |
| `--max-kill` | Specifies the maximum number of pods to be terminated per interval | 1 |
| `--minimum-age` | Minimum age to filter pods by | 0s (matches every pod) |
| `--dry-run` | don't kill pods, only log what would have been done | true |
| `--log-format` | specify the format of the log messages. Options are text and json | text |
| `--log-caller` | include the calling function name and location in the log messages | false |
| `--slack-webhook` | The address of the slack webhook for notifications | disabled |
| Option | Description | Default |
|-----------------------------|----------------------------------------------------------------------|----------------------------|
| `--interval` | interval between pod terminations | 10m |
| `--labels` | label selector to filter pods by | (matches everything) |
| `--annotations` | annotation selector to filter pods by | (matches everything) |
| `--kinds` | owner's kind selector to filter pods by | (all kinds) |
| `--namespaces` | namespace selector to filter pods by | (all namespaces) |
| `--namespace-labels` | label selector to filter namespaces and its pods by | (all namespaces) |
| `--included-pod-names` | regular expression pattern for pod names to include | (all included) |
| `--excluded-pod-names` | regular expression pattern for pod names to exclude | (none excluded) |
| `--excluded-weekdays` | weekdays when chaos is to be suspended, e.g. "Sat,Sun" | (no weekday excluded) |
| `--excluded-times-of-day` | times of day when chaos is to be suspended, e.g. "22:00-08:00" | (no times of day excluded) |
| `--excluded-days-of-year` | days of a year when chaos is to be suspended, e.g. "Apr1,Dec24" | (no days of year excluded) |
| `--timezone` | timezone from tz database, e.g. "America/New_York", "UTC" or "Local" | (UTC) |
| `--max-runtime` | Maximum runtime before chaoskube exits | -1s (infinite time) |
| `--max-kill` | Specifies the maximum number of pods to be terminated per interval | 1 |
| `--minimum-age` | Minimum age to filter pods by | 0s (matches every pod) |
| `--dry-run` | don't kill pods, only log what would have been done | true |
| `--log-format` | specify the format of the log messages. Options are text and json | text |
| `--log-caller` | include the calling function name and location in the log messages | false |
| `--slack-webhook` | The address of the slack webhook for notifications | disabled |
| `--limited-by-one-namespace`| limited by one namespace. If true then namespaces mast one namespace.| false |
| `--no-event` | no send event | false |

## Related work

Expand Down
80 changes: 49 additions & 31 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type Chaoskube struct {
Logger log.FieldLogger
// a terminator that terminates victim pods
Terminator terminator.Terminator
// limited by one namespace. if it is true then namespaces mast only one namespace.
LimitedByOneNamespace bool
// dry run will not allow any pod terminations
DryRun bool
// grace period to terminate the pods
Expand All @@ -72,6 +74,8 @@ type Chaoskube struct {
MaxKill int
// chaos events notifier
Notifier notifier.Notifier
// no event
NoEvent bool
}

var (
Expand All @@ -95,32 +99,41 @@ var (
// * a logger implementing logrus.FieldLogger to send log output to
// * what specific terminator to use to imbue chaos on victim pods
// * whether to enable/disable dry-run mode
func New(client kubernetes.Interface, labels, annotations, kinds, namespaces, namespaceLabels labels.Selector, includedPodNames, excludedPodNames *regexp.Regexp, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, logger log.FieldLogger, dryRun bool, terminator terminator.Terminator, maxKill int, notifier notifier.Notifier) *Chaoskube {
func New(client kubernetes.Interface, labels, annotations, kinds, namespaces, namespaceLabels labels.Selector, includedPodNames, excludedPodNames *regexp.Regexp, excludedWeekdays []time.Weekday, excludedTimesOfDay []util.TimePeriod, excludedDaysOfYear []time.Time, timezone *time.Location, minimumAge time.Duration, logger log.FieldLogger, limitedByOneNamespace bool, dryRun bool, terminator terminator.Terminator, maxKill int, notifier notifier.Notifier, noEvent bool) *Chaoskube {
var ns string
if limitedByOneNamespace {
ns = namespaces.String()
} else {
ns = v1.NamespaceAll
}

broadcaster := record.NewBroadcaster()
broadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: client.CoreV1().Events(v1.NamespaceAll)})
broadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: client.CoreV1().Events(ns)})
recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "chaoskube"})

return &Chaoskube{
Client: client,
Labels: labels,
Annotations: annotations,
Kinds: kinds,
Namespaces: namespaces,
NamespaceLabels: namespaceLabels,
IncludedPodNames: includedPodNames,
ExcludedPodNames: excludedPodNames,
ExcludedWeekdays: excludedWeekdays,
ExcludedTimesOfDay: excludedTimesOfDay,
ExcludedDaysOfYear: excludedDaysOfYear,
Timezone: timezone,
MinimumAge: minimumAge,
Logger: logger,
DryRun: dryRun,
Terminator: terminator,
EventRecorder: recorder,
Now: time.Now,
MaxKill: maxKill,
Notifier: notifier,
Client: client,
Labels: labels,
Annotations: annotations,
Kinds: kinds,
Namespaces: namespaces,
NamespaceLabels: namespaceLabels,
IncludedPodNames: includedPodNames,
ExcludedPodNames: excludedPodNames,
ExcludedWeekdays: excludedWeekdays,
ExcludedTimesOfDay: excludedTimesOfDay,
ExcludedDaysOfYear: excludedDaysOfYear,
Timezone: timezone,
MinimumAge: minimumAge,
Logger: logger,
LimitedByOneNamespace: limitedByOneNamespace,
DryRun: dryRun,
Terminator: terminator,
NoEvent: noEvent,
EventRecorder: recorder,
Now: time.Now,
MaxKill: maxKill,
Notifier: notifier,
}
}

Expand Down Expand Up @@ -210,8 +223,13 @@ func (c *Chaoskube) Victims(ctx context.Context) ([]v1.Pod, error) {
// It returns all pods that match the configured label, annotation and namespace selectors.
func (c *Chaoskube) Candidates(ctx context.Context) ([]v1.Pod, error) {
listOptions := metav1.ListOptions{LabelSelector: c.Labels.String()}

podList, err := c.Client.CoreV1().Pods(v1.NamespaceAll).List(ctx, listOptions)
var ns string
if c.LimitedByOneNamespace {
ns = c.Namespaces.String()
} else {
ns = v1.NamespaceAll
}
podList, err := c.Client.CoreV1().Pods(ns).List(ctx, listOptions)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -263,17 +281,17 @@ func (c *Chaoskube) DeletePod(ctx context.Context, victim v1.Pod) error {

metrics.PodsDeletedTotal.WithLabelValues(victim.Namespace).Inc()

ref, err := reference.GetReference(scheme.Scheme, &victim)
if err != nil {
return err
}

c.EventRecorder.Event(ref, v1.EventTypeNormal, "Killing", "Pod was terminated by chaoskube to introduce chaos.")
if !c.NoEvent {
ref, err := reference.GetReference(scheme.Scheme, &victim)
if err != nil {
return err
}

c.EventRecorder.Event(ref, v1.EventTypeNormal, "Killing", "Pod was terminated by chaoskube to introduce chaos.")
}
if err := c.Notifier.NotifyPodTermination(victim); err != nil {
c.Logger.WithField("err", err).Warn("failed to notify pod termination")
}

return nil
}

Expand Down
Loading