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

feat(component):Add observedGeneration to represent Component #205

Merged
merged 9 commits into from
Sep 28, 2020
8 changes: 8 additions & 0 deletions apis/core/v1alpha2/core_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ type ComponentSpec struct {

// A ComponentStatus represents the observed state of a Component.
type ComponentStatus struct {
// The generation observed by the component controller.
// +optional
ObservedGeneration int64 `json:"observedGeneration"`
Ghostbaby marked this conversation as resolved.
Show resolved Hide resolved

runtimev1alpha1.ConditionedStatus `json:",inline"`

// LatestRevision of component
Expand Down Expand Up @@ -412,6 +416,10 @@ type ApplicationConfigurationStatus struct {
// Workloads created by this ApplicationConfiguration.
Workloads []WorkloadStatus `json:"workloads,omitempty"`

// The generation observed by the appConfig controller.
// +optional
ObservedGeneration int64 `json:"observedGeneration"`

// HistoryWorkloads will record history but still working revision workloads.
HistoryWorkloads []HistoryWorkload `json:"historyWorkloads"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ spec:
type: object
type: object
type: array
observedGeneration:
description: The generation observed by the appConfig controller.
format: int64
type: integer
status:
description: Status is a place holder for a customized controller
to fill if it needs a single place to summarize the status of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ spec:
- name
- revision
type: object
observedGeneration:
description: The generation observed by the component controller.
format: int64
type: integer
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (result reco

// execute the posthooks at the end no matter what
defer func() {
updateObservedGeneration(ac)
Copy link
Member

Choose a reason for hiding this comment

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

An afterthought: I don't think this is the right place to update ObservedGeneration. Because this will always update even if the reconciliation failed.

Note that I will be fixing this shortly.

@wonderflow @Ghostbaby

Copy link
Member

Choose a reason for hiding this comment

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

Reconcile failed also need to add observed

for name, hook := range r.postHooks {
exeResult, err := hook.Exec(ctx, ac, log)
if err != nil {
Expand Down Expand Up @@ -348,6 +349,12 @@ func (r *OAMApplicationReconciler) updateStatus(ctx context.Context, ac, acPatch
ac.SetConditions(v1alpha1.ReconcileSuccess())
}

func updateObservedGeneration(ac *v1alpha2.ApplicationConfiguration) {
if ac.Status.ObservedGeneration != ac.Generation {
ac.Status.ObservedGeneration = ac.Generation
Ghostbaby marked this conversation as resolved.
Show resolved Hide resolved
}
}

func patchExtraStatusField(acStatus *v1alpha2.ApplicationConfigurationStatus, acPatchStatus v1alpha2.ApplicationConfigurationStatus) {
// patch the extra status back
for i := range acStatus.Workloads {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -1577,3 +1580,53 @@ func TestPatchExtraField(t *testing.T) {
})
}
}

func TestUpdateStatus(t *testing.T) {

mockGetAppConfigFn := func(_ context.Context, key client.ObjectKey, obj runtime.Object) error {
if o, ok := obj.(*v1alpha2.ApplicationConfiguration); ok {
*o = v1alpha2.ApplicationConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "example-appconfig",
Generation: 1,
},
Spec: v1alpha2.ApplicationConfigurationSpec{
Components: []v1alpha2.ApplicationConfigurationComponent{
{
ComponentName: "example-component",
ParameterValues: []v1alpha2.ComponentParameterValue{
{
Name: "image",
Value: intstr.IntOrString{
StrVal: "wordpress:php7.3",
},
},
},
},
},
},
Status: v1alpha2.ApplicationConfigurationStatus{
ObservedGeneration: 0,
},
}
}
return nil
}

m := &mock.Manager{
Client: &test.MockClient{
MockGet: mockGetAppConfigFn,
},
}

r := NewReconciler(m)

ac := &v1alpha2.ApplicationConfiguration{}
err := r.client.Get(context.Background(), types.NamespacedName{Name: "example-appconfig"}, ac)
assert.Equal(t, err, nil)
assert.Equal(t, ac.Status.ObservedGeneration, int64(0))

updateObservedGeneration(ac)
assert.Equal(t, ac.Status.ObservedGeneration, int64(1))

}
6 changes: 6 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,17 @@ func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj runtim
Revision: nextRevision,
Data: runtime.RawExtension{Object: curComp},
}

err := c.Client.Create(context.TODO(), &revision)
if err != nil {
c.Logger.Info(fmt.Sprintf("error create controllerRevision %v", err), "componentName", mt.GetName())
return false
}

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

err = c.Client.Status().Update(context.Background(), curComp)
if err != nil {
c.Logger.Info(fmt.Sprintf("update component status latestRevision %s err %v", revisionName, err), "componentName", mt.GetName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (

func TestComponentHandler(t *testing.T) {
q := controllertest.Queue{Interface: workqueue.New()}
var curComp = &v1alpha2.Component{}
var curComp = &v1alpha2.Component{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
}
var createdRevisions = []appsv1.ControllerRevision{}
var instance = ComponentHandler{
Client: &test.MockClient{
Expand Down Expand Up @@ -109,7 +111,7 @@ func TestComponentHandler(t *testing.T) {
RevisionLimit: 2,
}
comp := &v1alpha2.Component{
ObjectMeta: metav1.ObjectMeta{Namespace: "biz", Name: "comp1"},
ObjectMeta: metav1.ObjectMeta{Namespace: "biz", Name: "comp1", Generation: 1},
Spec: v1alpha2.ComponentSpec{Workload: runtime.RawExtension{Object: &v1.Deployment{Spec: v1.DeploymentSpec{Template: v12.PodTemplateSpec{Spec: v12.PodSpec{Containers: []v12.Container{{Image: "nginx:v1"}}}}}}}},
}

Expand Down Expand Up @@ -137,6 +139,8 @@ func TestComponentHandler(t *testing.T) {
// check component's status saved in corresponding controllerRevision
assert.Equal(t, gotComp.Status.LatestRevision.Name, revisions.Items[0].Name)
assert.Equal(t, gotComp.Status.LatestRevision.Revision, revisions.Items[0].Revision)
// check component's status ObservedGeneration
assert.Equal(t, gotComp.Status.ObservedGeneration, comp.Generation)
q.Done(item)
// ============ Test Create Event End ===================

Expand Down