Skip to content

Commit

Permalink
demo controller as ut
Browse files Browse the repository at this point in the history
  • Loading branch information
WeichengWang1 committed Sep 20, 2023
1 parent 3ba5a5a commit a2e0925
Show file tree
Hide file tree
Showing 7 changed files with 1,027 additions and 227 deletions.
4 changes: 0 additions & 4 deletions pkg/controllers/alibabacloudslb/alibabacloudslb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ func (r *ReconcileAdapter) DeleteEmployer(employer client.Object, toDelete []res
return nil, nil, nil
}

func (r *ReconcileAdapter) RecordEmployer(succCreate, succUpdate, succDelete []resourceconsist.IEmployer) error {
return nil
}

func (r *ReconcileAdapter) GetExpectEmployee(ctx context.Context, employer client.Object) ([]resourceconsist.IEmployee, error) {
svc, ok := employer.(*corev1.Service)
if !ok {
Expand Down
207 changes: 0 additions & 207 deletions pkg/controllers/demoresourceconsist/event_handler.go

This file was deleted.

29 changes: 16 additions & 13 deletions pkg/controllers/resourceconsist/consister.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,19 @@ func (r *Consist) syncEmployer(ctx context.Context, employer client.Object, expe
if err != nil {
return false, false, fmt.Errorf("diff employer failed, err: %s", err.Error())
}
succCreate, failCreate, err := r.adapter.CreateEmployer(employer, toCudEmployer.ToCreate)
_, failCreate, err := r.adapter.CreateEmployer(employer, toCudEmployer.ToCreate)
if err != nil {
return false, false, fmt.Errorf("syncCreate failed, err: %s", err.Error())
}
succUpdate, failUpdate, err := r.adapter.UpdateEmployer(employer, toCudEmployer.ToUpdate)
_, failUpdate, err := r.adapter.UpdateEmployer(employer, toCudEmployer.ToUpdate)
if err != nil {
return false, false, fmt.Errorf("syncUpdate failed, err: %s", err.Error())
}
succDelete, failDelete, err := r.adapter.DeleteEmployer(employer, toCudEmployer.ToDelete)
_, failDelete, err := r.adapter.DeleteEmployer(employer, toCudEmployer.ToDelete)
if err != nil {
return false, false, fmt.Errorf("syncDelete failed, err: %s", err.Error())
}

err = r.adapter.RecordEmployer(succCreate, succUpdate, succDelete)
if err != nil {
return false, false, fmt.Errorf("record employer failed, err: %s", err.Error())
}

isClean := len(toCudEmployer.Unchanged) == 0 && len(toCudEmployer.ToCreate) == 0 && len(toCudEmployer.ToUpdate) == 0 && len(toCudEmployer.ToDelete) == 0
cudFailedExist := len(failCreate) > 0 || len(failUpdate) > 0 || len(failDelete) > 0
return isClean, cudFailedExist, nil
Expand Down Expand Up @@ -85,7 +80,7 @@ func (r *Consist) diffEmployer(expectEmployer, currentEmployer []IEmployer) (ToC
toCreateIdx++
continue
}
equal, err := expect.EmployerEqual(current.GetEmployerStatuses())
equal, err := expect.EmployerEqual(current)
if err != nil {
return ToCUDEmployer{}, err
}
Expand Down Expand Up @@ -190,10 +185,6 @@ func (r *Consist) syncEmployees(ctx context.Context, employer client.Object, exp
return false, false, err
}

// todo, to be removed, for demo
r.Recorder.Eventf(employer, corev1.EventTypeNormal, "diffEmployees", "toCreate: %v, toUpdate: %v, toDelete: %v, unchanged: %v",
toCudEmployees.ToCreate, toCudEmployees.ToUpdate, toCudEmployees.ToDelete, toCudEmployees.Unchanged)

succCreate, failCreate, err := r.adapter.CreateEmployees(employer, toCudEmployees.ToCreate)
if err != nil {
return false, false, fmt.Errorf("syncCreate failed, err: %s", err.Error())
Expand Down Expand Up @@ -255,6 +246,12 @@ func (r *Consist) ensureExpectedFinalizer(ctx context.Context, employer client.O
}
patch := client.MergeFrom(employer.DeepCopyObject().(client.Object))
annos := employer.GetAnnotations()
if annos == nil {
annos = make(map[string]string)
}
if annos[expectedFinalizerAddedAnnoKey] == strings.Join(notDeletedPodNames, ",") {
return len(notDeletedPodNames) == 0, nil
}
annos[expectedFinalizerAddedAnnoKey] = strings.Join(notDeletedPodNames, ",")
employer.SetAnnotations(annos)
return len(notDeletedPodNames) == 0, r.Client.Patch(ctx, employer, patch)
Expand Down Expand Up @@ -302,6 +299,12 @@ func (r *Consist) ensureExpectedFinalizer(ctx context.Context, employer client.O

patch := client.MergeFrom(employer.DeepCopyObject().(client.Object))
annos := employer.GetAnnotations()
if annos == nil {
annos = make(map[string]string)
}
if annos[expectedFinalizerAddedAnnoKey] == strings.Join(addedNames, ",") {
return len(addedNames) == 0, nil
}
annos[expectedFinalizerAddedAnnoKey] = strings.Join(addedNames, ",")
employer.SetAnnotations(annos)
return len(addedNames) == 0, r.Client.Patch(ctx, employer, patch)
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/resourceconsist/resourceconsist_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ type Consist struct {
}

// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods/status,verbs=get;update;patch

func (r *Consist) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
var employer client.Object
Expand Down
Loading

0 comments on commit a2e0925

Please sign in to comment.