Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

minor fix datarace #257

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions pkg/controller/v1alpha2/applicationconfiguration/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,38 +133,39 @@ func newTrue() *bool {

func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj runtime.Object) bool {
curComp := obj.(*v1alpha2.Component)
diff, curRevision := c.IsRevisionDiff(mt, curComp)
comp := curComp.DeepCopy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the controller runtime already returns a copy from the informer queue(client go, tools/cache/delta_fifo.go:486), is it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid not, currently vela complains the error: https://github.com/oam-dev/kubevela/runs/1272425015

After debug, I found out it because of this not copy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the event handler passes the object reference directly:

// OnAdd creates CreateEvent and calls Create on EventHandler
func (e EventHandler) OnAdd(obj interface{}) {
	...
	// Pull the runtime.Object out of the object
	if o, ok := obj.(runtime.Object); ok {
		c.Object = o
	} 

diff, curRevision := c.IsRevisionDiff(mt, comp)
if !diff {
// No difference, no need to create new revision.
return false
}
nextRevision := curRevision + 1
revisionName := ConstructRevisionName(mt.GetName(), nextRevision)

curComp.Status.LatestRevision = &v1alpha2.Revision{
comp.Status.LatestRevision = &v1alpha2.Revision{
Name: revisionName,
Revision: nextRevision,
}
// set annotation to component
revision := appsv1.ControllerRevision{
ObjectMeta: metav1.ObjectMeta{
Name: revisionName,
Namespace: curComp.Namespace,
Namespace: comp.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.ComponentKind,
Name: curComp.Name,
UID: curComp.UID,
Name: comp.Name,
UID: comp.UID,
Controller: newTrue(),
},
},
Labels: map[string]string{
ControllerRevisionComponentLabel: curComp.Name,
ControllerRevisionComponentLabel: comp.Name,
},
},
Revision: nextRevision,
Data: runtime.RawExtension{Object: curComp},
Data: runtime.RawExtension{Object: comp},
}

err := c.Client.Create(context.TODO(), &revision)
Expand All @@ -173,18 +174,18 @@ func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj runtim
return false
}

if curComp.Status.ObservedGeneration != curComp.Generation {
curComp.Status.ObservedGeneration = curComp.Generation
if comp.Status.ObservedGeneration != comp.Generation {
comp.Status.ObservedGeneration = comp.Generation
}

err = c.Client.Status().Update(context.Background(), curComp)
err = c.Client.Status().Update(context.Background(), comp)
if err != nil {
c.Logger.Info(fmt.Sprintf("update component status latestRevision %s err %v", revisionName, err), "componentName", mt.GetName())
return false
}
c.Logger.Info(fmt.Sprintf("ControllerRevision %s created", revisionName))
if int64(c.RevisionLimit) < nextRevision {
if err := c.cleanupControllerRevision(curComp); err != nil {
if err := c.cleanupControllerRevision(comp); err != nil {
c.Logger.Info(fmt.Sprintf("failed to clean up revisions of Component %v.", err))
}
}
Expand Down