-
Notifications
You must be signed in to change notification settings - Fork 850
/
Copy pathtypes.go
86 lines (76 loc) · 2.78 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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 iochaos
import (
"fmt"
"github.com/go-logr/logr"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/pingcap/chaos-mesh/api/v1alpha1"
"github.com/pingcap/chaos-mesh/controllers/common"
"github.com/pingcap/chaos-mesh/controllers/iochaos/fs"
"github.com/pingcap/chaos-mesh/controllers/twophase"
)
type Reconciler struct {
client.Client
record.EventRecorder
Log logr.Logger
}
// Reconcile reconciles an IOChaos resource
func (r *Reconciler) Reconcile(req ctrl.Request, chaos *v1alpha1.IoChaos) (ctrl.Result, error) {
r.Log.Info("Reconciling iochaos")
scheduler := chaos.GetScheduler()
duration, err := chaos.GetDuration()
if err != nil {
msg := fmt.Sprintf("unable to get iochaos[%s/%s]'s duration",
req.Namespace, req.Name)
r.Log.Error(err, msg)
return ctrl.Result{}, err
}
if scheduler == nil && duration == nil {
return r.commonIOChaos(chaos, req)
} else if scheduler != nil && duration != nil {
return r.scheduleIOChaos(chaos, req)
}
// This should be ensured by admission webhook in the future
err = fmt.Errorf("iochaos[%s/%s] spec invalid", req.Namespace, req.Name)
r.Log.Error(err, "scheduler and duration should be omitted or defined at the same time")
return ctrl.Result{}, err
}
func (r *Reconciler) commonIOChaos(iochaos *v1alpha1.IoChaos, req ctrl.Request) (ctrl.Result, error) {
var cr *common.Reconciler
switch iochaos.Spec.Layer {
case v1alpha1.FileSystemLayer:
cr = fs.NewCommonReconciler(r.Client, r.Log.WithValues("reconciler", "chaosfs"),
req, r.EventRecorder)
default:
return r.invalidActionResponse(iochaos)
}
return cr.Reconcile(req)
}
func (r *Reconciler) scheduleIOChaos(iochaos *v1alpha1.IoChaos, req ctrl.Request) (ctrl.Result, error) {
var sr *twophase.Reconciler
switch iochaos.Spec.Layer {
case v1alpha1.FileSystemLayer:
sr = fs.NewTwoPhaseReconciler(r.Client, r.Log.WithValues("reconciler", "chaosfs"),
req, r.EventRecorder)
default:
return r.invalidActionResponse(iochaos)
}
return sr.Reconcile(req)
}
func (r *Reconciler) invalidActionResponse(iochaos *v1alpha1.IoChaos) (ctrl.Result, error) {
r.Log.Error(nil, "unknown file system I/O layer", "action", iochaos.Spec.Action)
return ctrl.Result{}, fmt.Errorf("unknown file system I/O layer")
}