Skip to content
Closed
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
30 changes: 22 additions & 8 deletions examples/crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
Expand All @@ -48,8 +49,11 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu

var chaospod api.ChaosPod
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {
log.Error(err, "unable to get chaosctl")
return ctrl.Result{}, err
if !apierrors.IsNotFound(err) {
log.Error(err, "unable to get chaosctl")
}

return ctrl.Result{}, client.IgnoreNotFound(err)
}

var pod corev1.Pod
Expand Down Expand Up @@ -93,27 +97,37 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}

chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second)
chaospod.Status.LastRun = pod.CreationTimestamp
if err := r.Update(ctx, &chaospod); err != nil {
log.Error(err, "unable to update chaosctl status")
return ctrl.Result{}, err
}

chaospod.Status.LastRun = pod.CreationTimestamp
err := r.Status().Update(ctx, &chaospod)
if err != nil {
log.Error(err, "unable to update chaosctl status")
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

func main() {
ctrl.SetLogger(zap.New())

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
scheme := runtime.NewScheme()
utilruntime.Must(corev1.AddToScheme(scheme))
err := api.AddToScheme(scheme)
if err != nil {
setupLog.Error(err, "unable to start manager")
setupLog.Error(err, "unable to add scheme")
os.Exit(1)
}

// in a real controller, we'd create a new scheme for this
err = api.AddToScheme(mgr.GetScheme())
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
})
if err != nil {
setupLog.Error(err, "unable to add scheme")
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

Expand Down
1 change: 1 addition & 0 deletions examples/crd/pkg/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

// +kubebuilder:object:generate=true
// +groupName=chaosapps.metamagical.io
// +versionName=v1
package pkg

import (
Expand Down
1 change: 1 addition & 0 deletions examples/crd/pkg/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ChaosPodStatus struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ChaosPod is the Schema for the randomjobs API
// +kubebuilder:printcolumn:name="next stop",type="string",JSONPath=".spec.nextStop",format="date"
Expand Down