Skip to content

Commit

Permalink
fix: filter 0.9 api events bug (#8630)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Dec 13, 2024
1 parent 799677f commit b5981d1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 26 deletions.
32 changes: 15 additions & 17 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,24 +429,22 @@ func main() {
os.Exit(1)
}

if !viper.GetBool(constant.DualOperatorsMode) {
if err = (&appscontrollers.OpsDefinitionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ops-definition-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpsDefinition")
os.Exit(1)
}
if err = (&appscontrollers.OpsDefinitionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ops-definition-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpsDefinition")
os.Exit(1)
}

if err = (&appscontrollers.OpsRequestReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ops-request-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpsRequest")
os.Exit(1)
}
if err = (&appscontrollers.OpsRequestReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ops-request-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpsRequest")
os.Exit(1)
}

if !viper.GetBool(constant.DualOperatorsMode) {
Expand Down
4 changes: 3 additions & 1 deletion controllers/apps/componentdefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (r *ComponentDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.
if err := r.Client.Get(rctx.Ctx, rctx.Req.NamespacedName, cmpd); err != nil {
return intctrlutil.CheckedRequeueWithError(err, rctx.Log, "")
}

if !intctrlutil.ObjectAPIVersionSupported(cmpd) {
return intctrlutil.Reconciled()
}
return r.reconcile(rctx, cmpd)
}

Expand Down
3 changes: 3 additions & 0 deletions controllers/apps/opsrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ func (r *OpsRequestReconciler) handleOpsReqDeletedDuringRunning(reqCtx intctrlut
return err
}
for _, cluster := range clusterList.Items {
if !intctrlutil.ObjectAPIVersionSupported(&cluster) {
continue
}
if err := r.cleanupOpsAnnotationForCluster(reqCtx, &cluster); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion controllers/apps/transformer_cluster_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func (t *clusterInitTransformer) Transform(ctx graph.TransformContext, dag *grap
graphCli, _ := transCtx.Client.(model.GraphClient)

// init dag
graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionStatusPtr())
if !intctrlutil.ObjectAPIVersionSupported(transCtx.Cluster) {
graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionNoopPtr())
return graph.ErrPrematureStop
}
graphCli.Root(dag, transCtx.OrigCluster, transCtx.Cluster, model.ActionStatusPtr())
return nil
}
1 change: 1 addition & 0 deletions controllers/apps/transformer_component_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (t *componentInitTransformer) Transform(ctx graph.TransformContext, dag *gr
transCtx.Context = intoContext(transCtx.Context, placement(transCtx.Component))

if !intctrlutil.ObjectAPIVersionSupported(transCtx.Component) {
rootVertex.Action = model.ActionNoopPtr()
return graph.ErrPrematureStop
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/instanceset/reconciler_api_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (r *apiVersionReconciler) PreCondition(tree *kubebuilderx.ObjectTree) *kube
}

func (r *apiVersionReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (kubebuilderx.Result, error) {
if intctrlutil.ObjectAPIVersionSupported(tree.GetRoot()) {
return kubebuilderx.Continue, nil
if !intctrlutil.ObjectAPIVersionSupported(tree.GetRoot()) {
return kubebuilderx.Commit, nil
}
return kubebuilderx.Commit, nil
return kubebuilderx.Continue, nil
}

func NewAPIVersionReconciler() kubebuilderx.Reconciler {
Expand Down
20 changes: 18 additions & 2 deletions pkg/controller/kubebuilderx/plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,31 @@ func (b *PlanBuilder) Build() (graph.Plan, error) {
}

func buildOrderedVertices(ctx context.Context, currentTree *ObjectTree, desiredTree *ObjectTree) []*model.ObjectVertex {
getStatusField := func(obj client.Object) interface{} {
objValue := reflect.ValueOf(obj)
if objValue.Kind() != reflect.Ptr || objValue.Elem().Kind() != reflect.Struct {
return nil
}
field := objValue.Elem().FieldByName("Status")
if !field.IsValid() {
return nil
}
return field.Interface()
}

var vertices []*model.ObjectVertex

// handle root object
if desiredTree.GetRoot() == nil {
root := model.NewObjectVertex(currentTree.GetRoot(), currentTree.GetRoot(), model.ActionDeletePtr())
vertices = append(vertices, root)
} else {
root := model.NewObjectVertex(currentTree.GetRoot(), desiredTree.GetRoot(), model.ActionStatusPtr())
vertices = append(vertices, root)
currentStatus := getStatusField(currentTree.GetRoot())
desiredStatus := getStatusField(desiredTree.GetRoot())
if !reflect.DeepEqual(currentStatus, desiredStatus) {
root := model.NewObjectVertex(currentTree.GetRoot(), desiredTree.GetRoot(), model.ActionStatusPtr())
vertices = append(vertices, root)
}
// if annotations, labels or finalizers updated, do both meta patch and status update.
if !reflect.DeepEqual(currentTree.GetRoot().GetAnnotations(), desiredTree.GetRoot().GetAnnotations()) ||
!reflect.DeepEqual(currentTree.GetRoot().GetLabels(), desiredTree.GetRoot().GetLabels()) ||
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/kubebuilderx/plan_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ var _ = Describe("plan builder test", func() {
env := builder.NewConfigMapBuilder(namespace, name+"-env").GetObject()

var verticesExpected []*model.ObjectVertex
verticesExpected = append(verticesExpected, newVertex(its.DeepCopy(), its, model.ActionStatusPtr()))
itsCopy := its.DeepCopy()
itsCopy.Status.Replicas = *itsCopy.Spec.Replicas
verticesExpected = append(verticesExpected, newVertex(its, itsCopy, model.ActionStatusPtr()))
verticesExpected = append(verticesExpected, newVertex(nil, pod, model.ActionCreatePtr()))
verticesExpected = append(verticesExpected, newVertex(nil, headlessSvc, model.ActionCreatePtr()))
verticesExpected = append(verticesExpected, newVertex(nil, svc, model.ActionCreatePtr()))
verticesExpected = append(verticesExpected, newVertex(nil, env, model.ActionCreatePtr()))

// build ordered vertices
currentTree.SetRoot(its)
desiredTree.SetRoot(its)
desiredTree.SetRoot(itsCopy)
Expect(desiredTree.Add(pod, headlessSvc, svc, env)).Should(Succeed())
vertices := buildOrderedVertices(ctx, currentTree, desiredTree)

Expand Down

0 comments on commit b5981d1

Please sign in to comment.