Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #789 from marun/unmanaged-dispatcher
Browse files Browse the repository at this point in the history
Factor unmanaged dispatcher out of the managed dispatcher
  • Loading branch information
k8s-ci-robot authored Apr 29, 2019
2 parents 90551b6 + ac605b0 commit 442e886
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 44 deletions.
53 changes: 10 additions & 43 deletions pkg/controller/sync/dispatch/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ type FederatedResourceForDispatch interface {
// ManagedDispatcher dispatches operations to member clusters for resources
// managed by a federated resource.
type ManagedDispatcher interface {
OperationDispatcher
UnmanagedDispatcher

Create(clusterName string)
Delete(clusterName string)
RemoveManagedLabel(clusterName string, clusterObj *unstructured.Unstructured)
Update(clusterName string, clusterObj *unstructured.Unstructured)
VersionMap() map[string]string
}

type managedDispatcherImpl struct {
sync.RWMutex

dispatcher *operationDispatcherImpl
fedResource FederatedResourceForDispatch
versionMap map[string]string
dispatcher *operationDispatcherImpl
unmanagedDispatcher *unmanagedDispatcherImpl
fedResource FederatedResourceForDispatch
versionMap map[string]string
}

func NewManagedDispatcher(clientAccessor clientAccessorFunc, fedResource FederatedResourceForDispatch) ManagedDispatcher {
Expand All @@ -68,6 +67,7 @@ func NewManagedDispatcher(clientAccessor clientAccessorFunc, fedResource Federat
versionMap: make(map[string]string),
}
d.dispatcher = newOperationDispatcher(clientAccessor, d)
d.unmanagedDispatcher = newUnmanagedDispatcher(d.dispatcher, d, fedResource.TargetKind(), fedResource.TargetName())
return d
}

Expand Down Expand Up @@ -157,56 +157,23 @@ func (d *managedDispatcherImpl) Update(clusterName string, clusterObj *unstructu
}

func (d *managedDispatcherImpl) Delete(clusterName string) {
d.dispatcher.incrementOperationsInitiated()
const op = "delete"
const opContinuous = "Deleting"
go d.dispatcher.clusterOperation(clusterName, op, func(client util.ResourceClient) util.ReconciliationStatus {
d.RecordEvent(clusterName, op, opContinuous)

qualifiedName := d.fedResource.TargetName()
err := client.Resources(qualifiedName.Namespace).Delete(qualifiedName.Name, &metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
if err != nil {
d.RecordError(clusterName, op, err)
return util.StatusError
}
return util.StatusAllOK
})
d.unmanagedDispatcher.Delete(clusterName)
}

func (d *managedDispatcherImpl) RemoveManagedLabel(clusterName string, clusterObj *unstructured.Unstructured) {
d.dispatcher.incrementOperationsInitiated()
const op = "remove managed label from"
const opContinuous = "Removing managed label from"
go d.dispatcher.clusterOperation(clusterName, op, func(client util.ResourceClient) util.ReconciliationStatus {
d.RecordEvent(clusterName, op, opContinuous)

// Avoid mutating the resource in the informer cache
updateObj := clusterObj.DeepCopy()

util.RemoveManagedLabel(updateObj)

_, err := client.Resources(updateObj.GetNamespace()).Update(updateObj, metav1.UpdateOptions{})
if err != nil {
d.RecordError(clusterName, op, err)
return util.StatusError
}
return util.StatusAllOK
})
d.unmanagedDispatcher.RemoveManagedLabel(clusterName, clusterObj)
}

func (d *managedDispatcherImpl) RecordError(clusterName, operation string, err error) {
args := []interface{}{operation, d.fedResource.TargetKind(), d.fedResource.TargetName(), clusterName}
eventType := fmt.Sprintf("%sInClusterFailed", strings.Replace(strings.Title(operation), " ", "", -1))
d.fedResource.RecordError(eventType, errors.Wrapf(err, "Failed to %s %s %q in cluster %q", args...))
d.fedResource.RecordError(eventType, errors.Wrapf(err, "Failed to "+eventTemplate, args...))
}

func (d *managedDispatcherImpl) RecordEvent(clusterName, operation, operationContinuous string) {
args := []interface{}{operationContinuous, d.fedResource.TargetKind(), d.fedResource.TargetName(), clusterName}
eventType := fmt.Sprintf("%sInCluster", strings.Replace(strings.Title(operation), " ", "", -1))
d.fedResource.RecordEvent(eventType, "%s %s %q in cluster %q", args...)
d.fedResource.RecordEvent(eventType, eventTemplate, args...)
}

func (d *managedDispatcherImpl) VersionMap() map[string]string {
Expand Down
9 changes: 8 additions & 1 deletion pkg/controller/sync/dispatch/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import (

"github.com/pkg/errors"

"k8s.io/apimachinery/pkg/util/runtime"

"github.com/kubernetes-sigs/federation-v2/pkg/controller/util"
)

type clientAccessorFunc func(clusterName string) (util.ResourceClient, error)

type DispatchRecorder interface {
RecordError(clusterName, operation string, err error)
RecordEvent(clusterName, operation, operationContinuous string)
}

// OperationDispatcher provides an interface to wait for operations
Expand Down Expand Up @@ -93,7 +96,11 @@ func (d *operationDispatcherImpl) clusterOperation(clusterName, op string, opFun
client, err := d.clientAccessor(clusterName)
if err != nil {
wrappedErr := errors.Wrapf(err, "Error retrieving client for cluster")
d.recorder.RecordError(clusterName, op, wrappedErr)
if d.recorder == nil {
runtime.HandleError(wrappedErr)
} else {
d.recorder.RecordError(clusterName, op, wrappedErr)
}
d.resultChan <- util.StatusError
return
}
Expand Down
129 changes: 129 additions & 0 deletions pkg/controller/sync/dispatch/unmanaged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dispatch

import (
"github.com/golang/glog"
"github.com/pkg/errors"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/runtime"

"github.com/kubernetes-sigs/federation-v2/pkg/controller/util"
)

const eventTemplate = "%s %s %q in cluster %q"

// UnmanagedDispatcher dispatches operations to member clusters for
// resources that are no longer managed by a federated resource.
type UnmanagedDispatcher interface {
OperationDispatcher

Delete(clusterName string)
RemoveManagedLabel(clusterName string, clusterObj *unstructured.Unstructured)
}

type unmanagedDispatcherImpl struct {
dispatcher *operationDispatcherImpl

targetName util.QualifiedName
targetKind string

recorder DispatchRecorder
}

func NewUnmanagedDispatcher(clientAccessor clientAccessorFunc, targetKind string, targetName util.QualifiedName) UnmanagedDispatcher {
dispatcher := newOperationDispatcher(clientAccessor, nil)
return newUnmanagedDispatcher(dispatcher, nil, targetKind, targetName)
}

func newUnmanagedDispatcher(dispatcher *operationDispatcherImpl, recorder DispatchRecorder, targetKind string, targetName util.QualifiedName) *unmanagedDispatcherImpl {
return &unmanagedDispatcherImpl{
dispatcher: dispatcher,
targetName: targetName,
targetKind: targetKind,
recorder: recorder,
}
}

func (d *unmanagedDispatcherImpl) Wait() (bool, error) {
return d.dispatcher.Wait()
}

func (d *unmanagedDispatcherImpl) Delete(clusterName string) {
d.dispatcher.incrementOperationsInitiated()
const op = "delete"
const opContinuous = "Deleting"
go d.dispatcher.clusterOperation(clusterName, op, func(client util.ResourceClient) util.ReconciliationStatus {
if d.recorder == nil {
glog.V(2).Infof(eventTemplate, opContinuous, d.targetKind, d.targetName, clusterName)
} else {
d.recorder.RecordEvent(clusterName, op, opContinuous)
}

err := client.Resources(d.targetName.Namespace).Delete(d.targetName.Name, &metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
if err != nil {
if d.recorder == nil {
wrappedErr := d.wrapOperationError(err, clusterName, op)
runtime.HandleError(wrappedErr)
} else {
d.recorder.RecordError(clusterName, op, err)
}
return util.StatusError
}
return util.StatusAllOK
})
}

func (d *unmanagedDispatcherImpl) RemoveManagedLabel(clusterName string, clusterObj *unstructured.Unstructured) {
d.dispatcher.incrementOperationsInitiated()
const op = "remove managed label from"
const opContinuous = "Removing managed label from"
go d.dispatcher.clusterOperation(clusterName, op, func(client util.ResourceClient) util.ReconciliationStatus {
if d.recorder == nil {
glog.V(2).Infof(eventTemplate, opContinuous, d.targetKind, d.targetName, clusterName)
} else {
d.recorder.RecordEvent(clusterName, op, opContinuous)
}

// Avoid mutating the resource in the informer cache
updateObj := clusterObj.DeepCopy()

util.RemoveManagedLabel(updateObj)

_, err := client.Resources(updateObj.GetNamespace()).Update(updateObj, metav1.UpdateOptions{})
if err != nil {
if d.recorder == nil {
wrappedErr := d.wrapOperationError(err, clusterName, op)
runtime.HandleError(wrappedErr)
} else {
d.recorder.RecordError(clusterName, op, err)
}
return util.StatusError
}
return util.StatusAllOK
})
}

func (d *unmanagedDispatcherImpl) wrapOperationError(err error, clusterName, operation string) error {
return errors.Wrapf(err, "Failed to "+eventTemplate, operation, d.targetKind, d.targetName, clusterName)
}

0 comments on commit 442e886

Please sign in to comment.