Skip to content

Commit

Permalink
feat(component):Add observedGeneration to represent Component
Browse files Browse the repository at this point in the history
Add observedGeneration to represent Component

Fix crossplane#205

Signed-off-by: zhuhuijun <[email protected]>
  • Loading branch information
Ghostbaby committed Sep 16, 2020
1 parent 8b0d851 commit bc182db
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apis/core/v1alpha2/core_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,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"`
}

// DependencyStatus represents the observed state of the dependency of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ spec:
type: object
type: array
type: object
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 @@ -347,6 +347,10 @@ func (r *OAMApplicationReconciler) updateStatus(ctx context.Context, ac *v1alpha
}
ac.Status.Workloads = append(ac.Status.Workloads, revisionStatus...)

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

ac.SetConditions(v1alpha1.ReconcileSuccess())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import (
"testing"
"time"

"k8s.io/apimachinery/pkg/types"

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

runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/test"
"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"
Expand Down Expand Up @@ -1394,3 +1399,53 @@ func TestAddDataOutputsToDAG(t *testing.T) {
t.Errorf("didn't add conditions to source correctly: %s", diff)
}
}

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))

r.updateStatus(context.Background(), ac, []Workload{})
assert.Equal(t, ac.Status.ObservedGeneration, int64(1))

}
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

0 comments on commit bc182db

Please sign in to comment.