mirror of
https://github.com/fluxcd/flagger.git
synced 2026-04-15 06:57:34 +00:00
kubectl annotation support
rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller add unit tests for finalizing introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller run fmt to clean up formatting review changes add kubectl annotation add kubectl annotation support introduction of finalizer introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller add unit tests for finalizing introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller run fmt to clean up formatting review changes introduction of finalizer introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller add unit tests for finalizing introduction of finalizer rebase and squash fix fmt issues revert Dockerfile revert go.mod and go.sum introduction of finalizer introduction of finalizer remove test for finalizer add istio tests fix fmt issues revert go.mod and go.sum revert Dockerfile and main.go fmt deployment controller run fmt to clean up formatting review changes
This commit is contained in:
@@ -128,6 +128,21 @@ func NewController(
|
||||
}
|
||||
|
||||
ctrl.enqueue(new)
|
||||
} else if !newCanary.DeletionTimestamp.IsZero() && hasFinalizer(&newCanary, finalizer) ||
|
||||
!hasFinalizer(&newCanary, finalizer) && newCanary.Spec.RevertOnDeletion {
|
||||
//If this was marked for deletion and has finalizers enqueue for finalizing or
|
||||
//If this canary doesn't have finalizers and RevertOnDeletion is true updated speck enqueue
|
||||
ctrl.enqueue(new)
|
||||
}
|
||||
|
||||
//If canary no longer desires reverting, finalizers should be removed
|
||||
if oldCanary.Spec.RevertOnDeletion && !newCanary.Spec.RevertOnDeletion {
|
||||
ctrl.logger.Infof("%s.%s opting out, deleting finalizers", newCanary.Name, newCanary.Namespace)
|
||||
err := ctrl.removeFinalizer(&newCanary, finalizer)
|
||||
if err != nil {
|
||||
ctrl.logger.Warnf("Failed to finalizers for %s.%s", oldCanary.Name, oldCanary.Namespace)
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
DeleteFunc: func(old interface{}) {
|
||||
@@ -217,6 +232,33 @@ func (c *Controller) syncHandler(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//Finalize if canary has been marked for deletion and revert is desired
|
||||
if cd.Spec.RevertOnDeletion && cd.ObjectMeta.DeletionTimestamp != nil {
|
||||
|
||||
//If finalizers have been previously removed proceed
|
||||
if !hasFinalizer(cd, finalizer) {
|
||||
c.logger.Infof("Canary %s.%s has been finalized", cd.Name, cd.Namespace)
|
||||
return nil
|
||||
}
|
||||
|
||||
if cd.Status.Phase != flaggerv1.CanaryPhaseTerminated {
|
||||
if err := c.finalize(cd); err != nil {
|
||||
return fmt.Errorf("unable to finalize to canary %s.%s error %s", cd.Name, cd.Namespace, err)
|
||||
}
|
||||
}
|
||||
|
||||
//Remove finalizer from Canary
|
||||
if err := c.removeFinalizer(cd, finalizer); err != nil {
|
||||
return fmt.Errorf("unable to remove finalizer for canary %s.%s", cd.Name, cd.Namespace)
|
||||
}
|
||||
|
||||
//record event
|
||||
c.recordEventInfof(cd, "Terminated canary %s.%s", cd.Name, cd.Namespace)
|
||||
|
||||
c.logger.Infof("Canary %s.%s has been successfully processed and marked for deletion", cd.Name, cd.Namespace)
|
||||
return nil
|
||||
}
|
||||
|
||||
// set status condition for new canaries
|
||||
if cd.Status.Conditions == nil {
|
||||
if ok, conditions := canary.MakeStatusConditions(cd, flaggerv1.CanaryPhaseInitializing); ok {
|
||||
@@ -233,6 +275,14 @@ func (c *Controller) syncHandler(key string) error {
|
||||
}
|
||||
|
||||
c.canaries.Store(fmt.Sprintf("%s.%s", cd.Name, cd.Namespace), cd)
|
||||
|
||||
//If opt in for revertOnDeletion add finaliers if not present
|
||||
if cd.Spec.RevertOnDeletion && !hasFinalizer(cd, finalizer) {
|
||||
if err := c.addFinalizer(cd, finalizer); err != nil {
|
||||
return fmt.Errorf("unable to add finalizer to canary %s.%s", cd.Name, cd.Namespace)
|
||||
}
|
||||
|
||||
}
|
||||
c.logger.Infof("Synced %s", key)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
ex "github.com/pkg/errors"
|
||||
flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1"
|
||||
"github.com/weaveworks/flagger/pkg/canary"
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
"k8s.io/client-go/util/retry"
|
||||
)
|
||||
|
||||
const finalizer = "finalizer.flagger.com"
|
||||
const finalizer = "finalizer.flagger.app"
|
||||
|
||||
func (c *Controller) finalize(old interface{}) error {
|
||||
var r *flaggerv1.Canary
|
||||
@@ -39,7 +40,7 @@ func (c *Controller) finalize(old interface{}) error {
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
//No reason to wait not found
|
||||
c.logger.Warnf("%s.%s failed due to %s not found", r.Name, r.Namespace, r.Kind)
|
||||
c.logger.Warnf("%s.%s failed due to %s not found", r.Name, r.Namespace, r.Spec.TargetRef.Kind)
|
||||
return nil
|
||||
}
|
||||
c.logger.Errorf("%s.%s failed due to %s", r.Name, r.Namespace, err)
|
||||
@@ -55,7 +56,6 @@ func (c *Controller) finalize(old interface{}) error {
|
||||
}
|
||||
|
||||
c.logger.Infof("%s.%s moving forward with router finalizing", r.Name, r.Namespace)
|
||||
//TODO if I can't revert continue on?
|
||||
labelSelector, ports, err := canaryController.GetMetadata(r)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s.%s failed to get metadata for router finalizing", r.Name, r.Namespace)
|
||||
@@ -82,20 +82,12 @@ func (c *Controller) finalize(old interface{}) error {
|
||||
c.logger.Infof("Finalization complete for %s.%s", r.Name, r.Namespace)
|
||||
|
||||
return nil
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (c *Controller) revertTargetRef(ctrl canary.Controller, r *flaggerv1.Canary) error {
|
||||
if err := ctrl.Finalize(r); err != nil {
|
||||
return err
|
||||
}
|
||||
/*if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return false, err
|
||||
}
|
||||
return true, fmt.Errorf("%s.%s failed to revert deployment to original replicas", r.Name, r.Namespace)
|
||||
}*/
|
||||
c.logger.Infof("%s.%s kind %s reverted", r.Name, r.Namespace, r.Spec.TargetRef.Kind)
|
||||
return nil
|
||||
}
|
||||
@@ -107,9 +99,6 @@ func (c *Controller) revertRouter(r *flaggerv1.Canary, labelSelector string, por
|
||||
c.logger.Errorf("%s.%s router failed with error %s", r.Name, r.Namespace, err)
|
||||
return err
|
||||
}
|
||||
/*if err != nil {
|
||||
return fmt.Errorf("%s.%s failed to revert service to original state", r.Name, r.Namespace)
|
||||
}*/
|
||||
c.logger.Infof("Service %s.%s reverted", r.Name, r.Namespace)
|
||||
return nil
|
||||
}
|
||||
@@ -158,7 +147,7 @@ func (c *Controller) addFinalizer(canary *flaggerv1.Canary, finalizerString stri
|
||||
|
||||
var selErr error
|
||||
if !firstTry {
|
||||
canary, selErr = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Get(canary.GetName(),metav1.GetOptions{})
|
||||
canary, selErr = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Get(canary.GetName(), metav1.GetOptions{})
|
||||
if selErr != nil {
|
||||
return selErr
|
||||
}
|
||||
@@ -167,7 +156,7 @@ func (c *Controller) addFinalizer(canary *flaggerv1.Canary, finalizerString stri
|
||||
copy := canary.DeepCopy()
|
||||
copy.ObjectMeta.Finalizers = append(copy.ObjectMeta.Finalizers, finalizerString)
|
||||
|
||||
_, err = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Update(copy)
|
||||
_, err = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Update(copy)
|
||||
|
||||
firstTry = false
|
||||
return
|
||||
@@ -182,13 +171,13 @@ func (c *Controller) addFinalizer(canary *flaggerv1.Canary, finalizerString stri
|
||||
//removeFinalizer removes a provided finalizer to the specified canary resource.
|
||||
//If failures occur the error will be returned otherwise the action is deemed successful
|
||||
//and error will be nil.
|
||||
func (c *Controller) removeFinalizer(canary *flaggerv1.Canary, finalizerString string) error {
|
||||
func (c *Controller) removeFinalizer(canary *flaggerv1.Canary, finalizerString string) error {
|
||||
firstTry := true
|
||||
err := retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) {
|
||||
|
||||
var selErr error
|
||||
if !firstTry {
|
||||
canary, selErr = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Get(canary.GetName(),metav1.GetOptions{})
|
||||
canary, selErr = c.flaggerClient.FlaggerV1beta1().Canaries(canary.Namespace).Get(canary.GetName(), metav1.GetOptions{})
|
||||
if selErr != nil {
|
||||
return selErr
|
||||
}
|
||||
@@ -217,5 +206,4 @@ func (c *Controller) removeFinalizer(canary *flaggerv1.Canary, finalizerString s
|
||||
return ex.Wrap(err, "Remove finalizer failed")
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1"
|
||||
fakeFlagger "github.com/weaveworks/flagger/pkg/client/clientset/versioned/fake"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -13,15 +14,15 @@ import (
|
||||
//Test has finalizers
|
||||
func TestFinalizer_hasFinalizer(t *testing.T) {
|
||||
|
||||
withFinalizer := newTestCanary()
|
||||
withFinalizer := newDeploymentTestCanary()
|
||||
withFinalizer.Finalizers = append(withFinalizer.Finalizers, finalizer)
|
||||
|
||||
tables := []struct{
|
||||
tables := []struct {
|
||||
canary *flaggerv1.Canary
|
||||
result bool
|
||||
}{
|
||||
{newTestCanary(), false},
|
||||
{ withFinalizer, true},
|
||||
{newDeploymentTestCanary(), false},
|
||||
{withFinalizer, true},
|
||||
}
|
||||
|
||||
for _, table := range tables {
|
||||
@@ -35,24 +36,23 @@ func TestFinalizer_hasFinalizer(t *testing.T) {
|
||||
func TestFinalizer_addFinalizer(t *testing.T) {
|
||||
|
||||
mockError := fmt.Errorf("failed to add finalizer to canary %s", "testCanary")
|
||||
cs := fakeFlagger.NewSimpleClientset(newTestCanary())
|
||||
cs := fakeFlagger.NewSimpleClientset(newDeploymentTestCanary())
|
||||
//prepend so it is evaluated over the catch all *
|
||||
cs.PrependReactor("update", "canaries", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error){
|
||||
cs.PrependReactor("update", "canaries", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return true, nil, mockError
|
||||
})
|
||||
m := Mocks{
|
||||
canary:newTestCanary(),
|
||||
m := fixture{
|
||||
canary: newDeploymentTestCanary(),
|
||||
flaggerClient: cs,
|
||||
ctrl:&Controller{flaggerClient:cs},
|
||||
ctrl: &Controller{flaggerClient: cs},
|
||||
}
|
||||
|
||||
|
||||
tables := []struct{
|
||||
mock Mocks
|
||||
tables := []struct {
|
||||
mock fixture
|
||||
canary *flaggerv1.Canary
|
||||
error error
|
||||
error error
|
||||
}{
|
||||
{SetupMocks(nil), newTestCanary(), nil},
|
||||
{newDeploymentFixture(nil), newDeploymentTestCanary(), nil},
|
||||
{m, m.canary, mockError},
|
||||
}
|
||||
|
||||
@@ -70,27 +70,27 @@ func TestFinalizer_addFinalizer(t *testing.T) {
|
||||
|
||||
func TestFinalizer_removeFinalizer(t *testing.T) {
|
||||
|
||||
withFinalizer := newTestCanary()
|
||||
withFinalizer := newDeploymentTestCanary()
|
||||
withFinalizer.Finalizers = append(withFinalizer.Finalizers, finalizer)
|
||||
|
||||
mockError := fmt.Errorf("failed to add finalizer to canary %s", "testCanary")
|
||||
cs := fakeFlagger.NewSimpleClientset(newTestCanary())
|
||||
cs := fakeFlagger.NewSimpleClientset(newDeploymentTestCanary())
|
||||
//prepend so it is evaluated over the catch all *
|
||||
cs.PrependReactor("update", "canaries", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error){
|
||||
cs.PrependReactor("update", "canaries", func(action k8sTesting.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return true, nil, mockError
|
||||
})
|
||||
m := Mocks{
|
||||
canary:withFinalizer,
|
||||
m := fixture{
|
||||
canary: withFinalizer,
|
||||
flaggerClient: cs,
|
||||
ctrl:&Controller{flaggerClient:cs},
|
||||
ctrl: &Controller{flaggerClient: cs},
|
||||
}
|
||||
|
||||
tables := []struct{
|
||||
mock Mocks
|
||||
tables := []struct {
|
||||
mock fixture
|
||||
canary *flaggerv1.Canary
|
||||
error error
|
||||
error error
|
||||
}{
|
||||
{SetupMocks(nil), withFinalizer, nil},
|
||||
{newDeploymentFixture(nil), withFinalizer, nil},
|
||||
{m, m.canary, mockError},
|
||||
}
|
||||
|
||||
@@ -104,4 +104,4 @@ func TestFinalizer_removeFinalizer(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user