-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add barebones termination controller
A very bare bones termination controller to watch Consoles, determine why their pods fail and update conditions on their status.
- Loading branch information
Theo Barber-Bany
committed
Feb 17, 2022
1 parent
e0d9ffb
commit 15c9484
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
controllers/workloads/console/termination/handler/handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package handler | ||
|
||
import ( | ||
workloadsv1alpha1 "github.com/gocardless/theatre/v3/apis/workloads/v1alpha1" | ||
"github.com/gocardless/theatre/v3/controllers/workloads/console/termination/status" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func main() { | ||
} | ||
|
||
type TerminationHandler struct { | ||
client client.Client | ||
} | ||
|
||
type Options struct{} | ||
|
||
func NewTerminationHandler(c client.Client, opts *Options) *TerminationHandler { | ||
return &TerminationHandler{ | ||
client: c, | ||
} | ||
} | ||
|
||
func (h *TerminationHandler) Handle(instance *workloadsv1alpha1.Console) (*status.Result, error) { | ||
switch instance.Status.Phase { | ||
default: | ||
return nil, nil | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
controllers/workloads/console/termination/status/status.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package status | ||
|
||
import ( | ||
workloadsv1alpha1 "github.com/gocardless/theatre/v3/apis/workloads/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func UpdateStatus(c client.Client, instance *workloadsv1alpha1.Console, result *Result) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package status | ||
|
||
import ( | ||
workloadsv1alpha1 "github.com/gocardless/theatre/v3/apis/workloads/v1alpha1" | ||
) | ||
|
||
// Result is the basis for updating the status of a Console with termination | ||
// events | ||
type Result struct { | ||
Phase *workloadsv1alpha1.ConsolePhase | ||
Condition workloadsv1alpha1.ConsoleConditionType | ||
Reason workloadsv1alpha1.ConsoleConditionReason | ||
} |
94 changes: 94 additions & 0 deletions
94
controllers/workloads/console/termination/termination_controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package termination | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
workloadsv1alpha1 "github.com/gocardless/theatre/v3/apis/workloads/v1alpha1" | ||
|
||
"github.com/gocardless/theatre/v3/controllers/workloads/console/termination/handler" | ||
"github.com/gocardless/theatre/v3/controllers/workloads/console/termination/status" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
watchhandler "sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// newReconciler returns a new reconcile.Reconciler | ||
func newReconciler(mgr manager.Manager) reconcile.Reconciler { | ||
h := handler.NewTerminationHandler(mgr.GetClient(), &handler.Options{}) | ||
return &TerminationReconciler{Client: mgr.GetClient(), handler: h, scheme: mgr.GetScheme()} | ||
} | ||
|
||
func add(mgr manager.Manager, r reconcile.Reconciler) error { | ||
// create the controller | ||
c, err := controller.New("console-termination-controller", mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Watch for Console changes | ||
err = c.Watch(&source.Kind{Type: &workloadsv1alpha1.Console{}}, &watchhandler.EnqueueRequestForOwner{ | ||
IsController: true, | ||
}) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
// Watch for job changes | ||
err = c.Watch(&source.Kind{Type: &batchv1.JobP{}}, &watchhandler.EnqueueRequestForOwner{ | ||
OwnerType: &workloadsv1alpha1.Console{}, | ||
IsController: true, | ||
}) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
// err = mgr.GetCache().IndexField(&batchv1.Job{}, "metadata.labels" ,func(obj runtime.Object) []string | ||
|
||
return nil | ||
} | ||
|
||
var _ reconcile.Reconciler = &TerminationReconciler{} | ||
|
||
type TerminationReconciler struct { | ||
client.Client | ||
handler *handler.TerminationHandler | ||
scheme *runtime.Scheme | ||
} | ||
|
||
func (r *TerminationReconciler) SetupWithManager(ctx context.Context, mgr manager.Manager) error { | ||
return add(mgr, newReconciler(mgr)) | ||
} | ||
|
||
func (r *TerminationReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) { | ||
instance := &workloadsv1alpha1.Console{} | ||
err := r.Get(context.Background(), request.NamespacedName, instance) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, err | ||
} | ||
|
||
result, err := r.handler.Handle(instance) | ||
if err != nil { | ||
statusErr := status.UpdateStatus(r.Client, instance, result) | ||
if statusErr != nil { | ||
// todo: logging good | ||
panic("failed to update status") | ||
} | ||
|
||
return reconcile.Result{}, fmt.Errorf("error handling console %s: %v", instance.GetName(), err) | ||
} | ||
|
||
err = status.UpdateStatus(r.Client, instance, result) | ||
if err != nil { | ||
return reconcile.Result{}, fmt.Errorf("error updating status: %v", err) | ||
} | ||
return reconcile.Result{}, nil | ||
} |