-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
140 additions
and
1 deletion.
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
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,139 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
type Kluster struct { | ||
Name string | ||
Pool []string | ||
} | ||
|
||
var KLUSTER = []Kluster{ | ||
Kluster{"fabus", []string{"mypool"}}, | ||
Kluster{"esther", []string{"swimmingpool"}}, | ||
Kluster{"michi", []string{"poo", "pool"}}, | ||
Kluster{"marian", []string{"schwarz", "weiss"}}, | ||
} | ||
|
||
type Controller interface { | ||
Run() | ||
} | ||
|
||
type Reconciler interface { | ||
Reconcile(Kluster) (bool, error) | ||
} | ||
|
||
type controller struct { | ||
Name string | ||
Reconciler Reconciler | ||
Logger log.Logger | ||
} | ||
|
||
func (c *controller) Run() { | ||
c.Logger.Log("msg", "running") | ||
|
||
for _, kluster := range KLUSTER { | ||
requeue, _ := c.Reconciler.Reconcile(kluster) | ||
if requeue { | ||
c.Logger.Log("msg", "requeue requested", "kluster", kluster.Name) | ||
c.Reconciler.Reconcile(kluster) | ||
} | ||
} | ||
} | ||
|
||
type launchReconciler struct { | ||
Logger log.Logger | ||
} | ||
|
||
type groundReconciler struct { | ||
Logger log.Logger | ||
} | ||
|
||
func NewLaunchControl(logger log.Logger) Controller { | ||
logger = log.With(logger, "controller", "launch") | ||
return &controller{ | ||
Name: "launch", | ||
Reconciler: &launchReconciler{logger}, | ||
Logger: logger, | ||
} | ||
} | ||
|
||
func NewGroundControl(logger log.Logger) Controller { | ||
logger = log.With(logger, "controller", "ground") | ||
return &controller{ | ||
Name: "ground", | ||
Reconciler: &groundReconciler{logger}, | ||
Logger: logger, | ||
} | ||
} | ||
|
||
func (r *launchReconciler) Reconcile(kluster Kluster) (requeue bool, err error) { | ||
defer func(begin time.Time) { | ||
r.Logger.Log( | ||
"msg", "reconciled kluster", | ||
"kluster", kluster.Name, | ||
"reqeue", requeue, | ||
"took", time.Since(begin), | ||
"err", err) | ||
}(time.Now()) | ||
|
||
if kluster.Name == "michi" { | ||
return false, fmt.Errorf("reconciliation failed") | ||
} | ||
|
||
for _, pool := range kluster.Pool { | ||
if err = r.ReconcilePool(kluster, pool); err != nil { | ||
return | ||
} | ||
} | ||
|
||
if kluster.Name == "fabus" { | ||
return true, nil | ||
} | ||
|
||
return | ||
} | ||
|
||
func (r *launchReconciler) ReconcilePool(kluster Kluster, pool string) (err error) { | ||
defer func(begin time.Time) { | ||
r.Logger.Log( | ||
"msg", "reconciled pool", | ||
"kluster", kluster.Name, | ||
"pool", pool, | ||
"took", time.Since(begin), | ||
"err", err) | ||
}(time.Now()) | ||
|
||
return nil | ||
} | ||
|
||
func (r *groundReconciler) Reconcile(kluster Kluster) (requeue bool, err error) { | ||
defer func(begin time.Time) { | ||
r.Logger.Log( | ||
"msg", "reconciled kluster", | ||
"kluster", kluster.Name, | ||
"reqeue", requeue, | ||
"took", time.Since(begin), | ||
"err", err) | ||
}(time.Now()) | ||
|
||
if kluster.Name == "michi" { | ||
return false, fmt.Errorf("reconciliation failed") | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func main() { | ||
w := log.NewSyncWriter(os.Stderr) | ||
logger := log.NewLogfmtLogger(w) | ||
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller) | ||
|
||
NewLaunchControl(logger).Run() | ||
NewGroundControl(logger).Run() | ||
} |