-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from daviddelucca/feat/flagger-canary-support
feat: add flagger canary support
- Loading branch information
Showing
9 changed files
with
805 additions
and
114 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
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,162 @@ | ||
package canary | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/werf/kubedog/pkg/tracker" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
watchtools "k8s.io/client-go/tools/watch" | ||
) | ||
|
||
type Feed interface { | ||
OnAdded(func() error) | ||
OnSucceeded(func() error) | ||
OnFailed(func(reason string) error) | ||
OnEventMsg(func(msg string) error) | ||
OnStatus(func(CanaryStatus) error) | ||
|
||
GetStatus() CanaryStatus | ||
Track(name, namespace string, kube kubernetes.Interface, opts tracker.Options) error | ||
} | ||
|
||
func NewFeed() Feed { | ||
return &feed{} | ||
} | ||
|
||
type feed struct { | ||
OnAddedFunc func() error | ||
OnSucceededFunc func() error | ||
OnFailedFunc func(string) error | ||
OnEventMsgFunc func(string) error | ||
OnStatusFunc func(CanaryStatus) error | ||
|
||
statusMux sync.Mutex | ||
status CanaryStatus | ||
} | ||
|
||
func (f *feed) OnAdded(function func() error) { | ||
f.OnAddedFunc = function | ||
} | ||
func (f *feed) OnSucceeded(function func() error) { | ||
f.OnSucceededFunc = function | ||
} | ||
func (f *feed) OnFailed(function func(string) error) { | ||
f.OnFailedFunc = function | ||
} | ||
func (f *feed) OnEventMsg(function func(string) error) { | ||
f.OnEventMsgFunc = function | ||
} | ||
func (f *feed) OnStatus(function func(CanaryStatus) error) { | ||
f.OnStatusFunc = function | ||
} | ||
|
||
func (f *feed) Track(name, namespace string, kube kubernetes.Interface, opts tracker.Options) error { | ||
errorChan := make(chan error, 0) | ||
doneChan := make(chan struct{}, 0) | ||
|
||
parentContext := opts.ParentContext | ||
if parentContext == nil { | ||
parentContext = context.Background() | ||
} | ||
ctx, cancel := watchtools.ContextWithOptionalTimeout(parentContext, opts.Timeout) | ||
defer cancel() | ||
|
||
canary := NewTracker(name, namespace, kube, opts) | ||
|
||
go func() { | ||
err := canary.Track(ctx) | ||
if err != nil { | ||
errorChan <- err | ||
} else { | ||
doneChan <- struct{}{} | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case status := <-canary.Added: | ||
f.setStatus(status) | ||
|
||
if f.OnAddedFunc != nil { | ||
err := f.OnAddedFunc() | ||
if err == tracker.StopTrack { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
case status := <-canary.Succeeded: | ||
f.setStatus(status) | ||
|
||
if f.OnSucceededFunc != nil { | ||
err := f.OnSucceededFunc() | ||
if err == tracker.StopTrack { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
case status := <-canary.Failed: | ||
f.setStatus(status) | ||
|
||
if f.OnFailedFunc != nil { | ||
err := f.OnFailedFunc(status.FailedReason) | ||
if err == tracker.StopTrack { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
case msg := <-canary.EventMsg: | ||
if f.OnEventMsgFunc != nil { | ||
err := f.OnEventMsgFunc(msg) | ||
if err == tracker.StopTrack { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
case status := <-canary.Status: | ||
f.setStatus(status) | ||
|
||
if f.OnStatusFunc != nil { | ||
err := f.OnStatusFunc(status) | ||
if err == tracker.StopTrack { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
case err := <-errorChan: | ||
return err | ||
case <-doneChan: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func (f *feed) setStatus(status CanaryStatus) { | ||
f.statusMux.Lock() | ||
defer f.statusMux.Unlock() | ||
|
||
if status.StatusGeneration > f.status.StatusGeneration { | ||
f.status = status | ||
} | ||
} | ||
|
||
func (f *feed) GetStatus() CanaryStatus { | ||
f.statusMux.Lock() | ||
defer f.statusMux.Unlock() | ||
return f.status | ||
} |
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,54 @@ | ||
package canary | ||
|
||
import ( | ||
v1beta1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" | ||
"github.com/werf/kubedog/pkg/tracker/indicators" | ||
"github.com/werf/kubedog/pkg/utils" | ||
) | ||
|
||
type CanaryStatus struct { | ||
v1beta1.CanaryStatus | ||
|
||
StatusGeneration uint64 | ||
|
||
StatusIndicator *indicators.StringEqualConditionIndicator | ||
|
||
Duration string | ||
Age string | ||
|
||
IsSucceeded bool | ||
IsFailed bool | ||
FailedReason string | ||
} | ||
|
||
func NewCanaryStatus(object *v1beta1.Canary, statusGeneration uint64, isTrackerFailed bool, trackerFailedReason string, canariesStatuses map[string]v1beta1.CanaryStatus) CanaryStatus { | ||
res := CanaryStatus{ | ||
CanaryStatus: object.Status, | ||
StatusGeneration: statusGeneration, | ||
StatusIndicator: &indicators.StringEqualConditionIndicator{}, | ||
Age: utils.TranslateTimestampSince(object.CreationTimestamp), | ||
} | ||
|
||
switch object.Status.Phase { | ||
case v1beta1.CanaryPhaseInitialized, v1beta1.CanaryPhaseSucceeded: | ||
res.IsSucceeded = true | ||
case v1beta1.CanaryPhaseFailed: | ||
if !res.IsFailed { | ||
errorMessage := "Failed - " | ||
for _, condition := range object.Status.Conditions { | ||
errorMessage += condition.Message | ||
} | ||
res.IsFailed = true | ||
res.FailedReason = errorMessage | ||
} | ||
default: | ||
res.StatusIndicator.Value = string(object.Status.Phase) | ||
} | ||
|
||
if !res.IsSucceeded && !res.IsFailed { | ||
res.IsFailed = isTrackerFailed | ||
res.FailedReason = trackerFailedReason | ||
} | ||
|
||
return res | ||
} |
Oops, something went wrong.