Skip to content

Commit

Permalink
manager: add iochaos and support fuse sidecar (chaos-mesh#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwen0 authored Nov 21, 2019
1 parent a6b3325 commit ed4c88b
Show file tree
Hide file tree
Showing 56 changed files with 2,727 additions and 176 deletions.
26 changes: 20 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ endif

all: yaml build image

build: chaosdaemon manager
build: chaosdaemon manager chaosfs

# Run tests
test: generate fmt vet manifests
Expand All @@ -49,15 +49,18 @@ chaosdaemon: generate fmt vet

# Build manager binary
manager: generate fmt vet
$(GO) build -ldflags '$(LDFLAGS)' -o images/chaos-operator/bin/chaos-controller-manager ./cmd/controller-manager/main.go
$(GO) build -ldflags '$(LDFLAGS)' -o images/chaos-operator/bin/chaos-controller-manager ./cmd/controller-manager/*.go

chaosfs: generate fmt vet
$(GO) build -ldflags '$(LDFLAGS)' -o images/chaosfs/bin/chaosfs ./cmd/chaosfs/*.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
$(GO) run ./cmd/controller-manager/main.go

# Install CRDs into a cluster
install: manifests
kubectl apply -f manifests/
kubectl apply -f manifests/crd.yaml
helm install helm/chaos-operator --name=chaos-operator --namespace=chaos-testing

# Generate manifests e.g. CRD, RBAC etc.
Expand All @@ -78,10 +81,16 @@ tidy:
git diff --quiet go.mod go.sum

image:
docker build -t pingcap/chaos-operator images/chaos-operator
docker build -t ${DOCKER_REGISTRY}/pingcap/chaos-operator images/chaos-operator
docker build -t ${DOCKER_REGISTRY}/pingcap/chaos-fs images/chaosfs
cp -R hack images/chaos-scripts
docker build -t ${DOCKER_REGISTRY}/pingcap/chaos-scripts images/chaos-scripts
rm -rf images/chaos-scripts/hack

docker-push: docker
docker-push:
docker push "${DOCKER_REGISTRY}/pingcap/chaos-operator:latest"
docker push "${DOCKER_REGISTRY}/pingcap/chaos-fs:latest"
docker push "${DOCKER_REGISTRY}/pingcap/chaos-scripts:latest"

lint:
@echo "linting"
Expand All @@ -102,4 +111,9 @@ CONTROLLER_GEN=$(shell which controller-gen)
endif

yaml: manifests
kustomize build config/default > manifests/config.yaml
kustomize build config/default > manifests/crd.yaml


.PHONY: all build test install manifests fmt vet tidy image \
docker-push lint generate controller-gen yaml \
manager chaosfs chaosdaemon
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ resources:
- group: chaosoperator
version: v1alpha1
kind: NetworkChaos
- group: chaosoperator
version: v1alpha1
kind: IoChaos
236 changes: 236 additions & 0 deletions api/v1alpha1/iochaos_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
// Copyright 2019 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"time"

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

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// IOChaosAction represents the chaos action about I/O action.
type IOChaosAction string

const (
IODelayAction IOChaosAction = "delay"
IOErrnoAction = "errno"
IOMixedAction = "mixed"
)

const (
// TODO: add config file
WebhookNamespaceLabelKey = "admission-webhook"
WebhookNamespaceLabelValue = "enabled"
WebhookPodAnnotationKey = "admission-webhook.pingcap.com/request"
)

// IOLayer represents the layer of I/O system.
type IOLayer string

const (
FileSystemLayer = "fs"
BlockLayer = "block"
DeviceLayer = "device"
)

const (
DefaultChaosfsAddr = ":65534"
)

// IoChaosSpec defines the desired state of IoChaos
type IoChaosSpec struct {
// Selector is used to select pods that are used to inject chaos action.
Selector SelectorSpec `json:"selector"`

// Scheduler defines some schedule rules to
// control the running time of the chaos experiment about pods.
Scheduler SchedulerSpec `json:"scheduler"`

// Action defines the specific pod chaos action.
// Supported action: delay / errno / mixed
// Default action: delay
Action IOChaosAction `json:"action"`

// Mode defines the mode to run chaos action.
// Supported mode: one / all / fixed / fixed-percent / random-max-percent
Mode PodMode `json:"mode"`

// Value is required when the mode is set to `FixedPodMode` / `FixedPercentPodMod` / `RandomMaxPercentPodMod`.
// If `FixedPodMode`, provide an integer of pods to do chaos action.
// If `FixedPercentPodMod`, provide a number from 0-100 to specify the max % of pods the server can do chaos action.
// IF `RandomMaxPercentPodMod`, provide a number from 0-100 to specify the % of pods to do chaos action
// +optional
Value string `json:"value"`

// Duration represents the duration of the chaos action.
// It is required when the action is `PodFailureAction`.
// A duration string is a possibly signed sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// +optional
Duration string `json:"duration"`

// Layer represents the layer of the I/O action.
// Supported value: fs / errno / mixed.
// Default layer: fs
Layer IOLayer `json:"layer"`

// Delay defines the value of I/O chaos action delay.
// It is required when the action is `PodFailureAction`.
// A delay string is a possibly signed sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "300ms".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
//
// If `Delay` is empty, the operator will generate a value for it randomly.
// +optional
Delay string `json:"delay,omitempty"`

// Errno defines the error code that returned by I/O action.
// refer to: https://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html
//
// If `Errno` is empty, the operator will generate a error code for it randomly.
// +optional
Errno string `json:"errno,omitempty"`

// Percent defines the percentage of injection errors and provides a number from 0-100.
// If `Percent` is empty, the operator will generate a value for it randomly.
// +optional
Percent string `json:"percent,omitempty"`

// Path defines the path of files for injecting I/O chaos action.
// +optional
Path string `json:"path,omitempty"`

// Methods defines the I/O methods for injecting I/O chaos action.
// default: all I/O methods.
// +optional
Methods []string `json:"methods,omitempty"`

// Addr defines the address for sidecar container.
// +optional
Addr string `json:"addr,omitempty"`

// ConfigName defines the config name which used to inject pod.
// +required
ConfigName string `json:"configName"`

// Next time when this action will be applied again
// +optional
NextStart *metav1.Time `json:"nextStart,omitempty"`

// Next time when this action will be recovered
// +optional
NextRecover *metav1.Time `json:"nextRecover,omitempty"`
}

func (in *IoChaosSpec) GetSelector() SelectorSpec {
return in.Selector
}

func (in *IoChaosSpec) GetMode() PodMode {
return in.Mode
}

func (in *IoChaosSpec) GetValue() string {
return in.Value
}

// IoChaosStatus defines the observed state of IoChaos
type IoChaosStatus struct {
ChaosStatus `json:",inline"`
}

// +kubebuilder:object:root=true

// IoChaos is the Schema for the iochaos API
type IoChaos struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec IoChaosSpec `json:"spec,omitempty"`
Status IoChaosStatus `json:"status,omitempty"`
}

func (in *IoChaos) IsDeleted() bool {
return !in.DeletionTimestamp.IsZero()
}

func (in *IoChaos) GetDuration() (time.Duration, error) {
duration, err := time.ParseDuration(in.Spec.Duration)
if err != nil {
return time.Hour * 0, err
}

return duration, nil
}

func (in *IoChaos) GetNextStart() time.Time {
if in.Spec.NextStart == nil {
return time.Time{}
}
return in.Spec.NextStart.Time
}

func (in *IoChaos) SetNextStart(t time.Time) {
if t.IsZero() {
in.Spec.NextStart = nil
return
}

if in.Spec.NextStart == nil {
in.Spec.NextStart = &metav1.Time{}
}
in.Spec.NextStart.Time = t
}

func (in *IoChaos) GetNextRecover() time.Time {
if in.Spec.NextRecover == nil {
return time.Time{}
}
return in.Spec.NextRecover.Time
}

func (in *IoChaos) SetNextRecover(t time.Time) {
if t.IsZero() {
in.Spec.NextRecover = nil
return
}

if in.Spec.NextRecover == nil {
in.Spec.NextRecover = &metav1.Time{}
}
in.Spec.NextRecover.Time = t
}

func (in *IoChaos) GetScheduler() SchedulerSpec {
return in.Spec.Scheduler
}

// +kubebuilder:object:root=true

// IoChaosList contains a list of IoChaos
type IoChaosList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []IoChaos `json:"items"`
}

func init() {
SchemeBuilder.Register(&IoChaos{}, &IoChaosList{})
}
26 changes: 12 additions & 14 deletions api/v1alpha1/networkchaos_types.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
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.
*/
// Copyright 2019 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

Expand Down
Loading

0 comments on commit ed4c88b

Please sign in to comment.