|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "math/rand" |
| 22 | + "os" |
| 23 | + "time" |
| 24 | + |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + api "sigs.k8s.io/controller-runtime/examples/crd/pkg" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + setupLog = ctrl.Log.WithName("setup") |
| 37 | + recLog = ctrl.Log.WithName("reconciler") |
| 38 | +) |
| 39 | + |
| 40 | +type reconciler struct { |
| 41 | + client.Client |
| 42 | + scheme *runtime.Scheme |
| 43 | +} |
| 44 | + |
| 45 | +func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { |
| 46 | + log := recLog.WithValues("chaospod", req.NamespacedName) |
| 47 | + log.V(1).Info("reconciling chaos pod") |
| 48 | + ctx := context.Background() |
| 49 | + |
| 50 | + var chaospod api.ChaosPod |
| 51 | + if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil { |
| 52 | + log.Error(err, "unable to get chaosctl") |
| 53 | + return ctrl.Result{}, err |
| 54 | + } |
| 55 | + |
| 56 | + var pod corev1.Pod |
| 57 | + podFound := true |
| 58 | + if err := r.Get(ctx, req.NamespacedName, &pod); err != nil { |
| 59 | + if !apierrors.IsNotFound(err) { |
| 60 | + log.Error(err, "unable to get pod") |
| 61 | + return ctrl.Result{}, err |
| 62 | + } |
| 63 | + podFound = false |
| 64 | + } |
| 65 | + |
| 66 | + if podFound { |
| 67 | + shouldStop := chaospod.Spec.NextStop.Time.Before(time.Now()) |
| 68 | + if !shouldStop { |
| 69 | + return ctrl.Result{RequeueAfter: chaospod.Spec.NextStop.Sub(time.Now()) + 1*time.Second}, nil |
| 70 | + } |
| 71 | + |
| 72 | + if err := r.Delete(ctx, &pod); err != nil { |
| 73 | + log.Error(err, "unable to delete pod") |
| 74 | + return ctrl.Result{}, err |
| 75 | + } |
| 76 | + |
| 77 | + return ctrl.Result{Requeue: true}, nil |
| 78 | + } |
| 79 | + |
| 80 | + templ := chaospod.Spec.Template.DeepCopy() |
| 81 | + pod.ObjectMeta = templ.ObjectMeta |
| 82 | + pod.Name = req.Name |
| 83 | + pod.Namespace = req.Namespace |
| 84 | + pod.Spec = templ.Spec |
| 85 | + |
| 86 | + if err := ctrl.SetControllerReference(&chaospod, &pod, r.scheme); err != nil { |
| 87 | + log.Error(err, "unable to set pod's owner reference") |
| 88 | + return ctrl.Result{}, err |
| 89 | + } |
| 90 | + |
| 91 | + if err := r.Create(ctx, &pod); err != nil { |
| 92 | + log.Error(err, "unable to create pod") |
| 93 | + return ctrl.Result{}, err |
| 94 | + } |
| 95 | + |
| 96 | + chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second) |
| 97 | + chaospod.Status.LastRun = pod.CreationTimestamp |
| 98 | + if err := r.Update(ctx, &chaospod); err != nil { |
| 99 | + log.Error(err, "unable to update chaosctl status") |
| 100 | + return ctrl.Result{}, err |
| 101 | + } |
| 102 | + return ctrl.Result{}, nil |
| 103 | +} |
| 104 | + |
| 105 | +func main() { |
| 106 | + ctrl.SetLogger(zap.Logger(true)) |
| 107 | + |
| 108 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{}) |
| 109 | + if err != nil { |
| 110 | + setupLog.Error(err, "unable to start manager") |
| 111 | + os.Exit(1) |
| 112 | + } |
| 113 | + |
| 114 | + // in a real controller, we'd create a new scheme for this |
| 115 | + err = api.AddToScheme(mgr.GetScheme()) |
| 116 | + if err != nil { |
| 117 | + setupLog.Error(err, "unable to add scheme") |
| 118 | + os.Exit(1) |
| 119 | + } |
| 120 | + |
| 121 | + err = ctrl.NewControllerManagedBy(mgr). |
| 122 | + For(&api.ChaosPod{}). |
| 123 | + Owns(&corev1.Pod{}). |
| 124 | + Complete(&reconciler{ |
| 125 | + Client: mgr.GetClient(), |
| 126 | + scheme: mgr.GetScheme(), |
| 127 | + }) |
| 128 | + |
| 129 | + if err != nil { |
| 130 | + setupLog.Error(err, "unable to create controller") |
| 131 | + os.Exit(1) |
| 132 | + } |
| 133 | + |
| 134 | + setupLog.Info("starting manager") |
| 135 | + if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { |
| 136 | + setupLog.Error(err, "problem running manager") |
| 137 | + os.Exit(1) |
| 138 | + } |
| 139 | +} |
0 commit comments