Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iniitialize status conditions #191

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
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
13 changes: 5 additions & 8 deletions pkg/apis/servicebinding/v1alpha3/servicebinding_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const (
ServiceBindingConditionReady = "Ready"
ServiceBindingConditionServiceAvailable = "ServiceAvailable"
ServiceBindingConditionProjectionReady = "ProjectionReady"
InitializeConditionReason = "Unknown"
)

func (bs *ServiceBindingStatus) InitializeConditions() {
ready := metav1.Condition{Type: ServiceBindingConditionReady}
serviceAvailable := metav1.Condition{Type: ServiceBindingConditionServiceAvailable}
projectionReady := metav1.Condition{Type: ServiceBindingConditionProjectionReady}
func (bs *ServiceBindingStatus) InitializeConditions(now metav1.Time) {
ready := metav1.Condition{Type: ServiceBindingConditionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason}
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if it would be simpler to set the metav1.time inside the method. Perhaps the unit test will be challenging!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup you guessed it right

serviceAvailable := metav1.Condition{Type: ServiceBindingConditionServiceAvailable, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason}
projectionReady := metav1.Condition{Type: ServiceBindingConditionProjectionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason}
for _, c := range bs.Conditions {
switch c.Type {
case ServiceBindingConditionReady:
Expand Down Expand Up @@ -112,10 +113,6 @@ func (bs *ServiceBindingStatus) aggregateReadyCondition(now metav1.Time) {
bs.Conditions[0].Status = metav1.ConditionUnknown
bs.Conditions[0].Reason = fmt.Sprintf("%s%s", ServiceBindingConditionProjectionReady, bs.Conditions[2].Reason)
bs.Conditions[0].Message = bs.Conditions[2].Message
} else {
bs.Conditions[0].Status = metav1.ConditionUnknown
bs.Conditions[0].Reason = "Unknown"
bs.Conditions[0].Message = ""
}

// update time when the status changes
Expand Down
79 changes: 52 additions & 27 deletions pkg/apis/servicebinding/v1alpha3/servicebinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
projection: nil,
expected: &ServiceBindingStatus{
Conditions: []metav1.Condition{
{Type: ServiceBindingConditionReady},
{Type: ServiceBindingConditionServiceAvailable},
{Type: ServiceBindingConditionProjectionReady},
{Type: ServiceBindingConditionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason},
{Type: ServiceBindingConditionServiceAvailable, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason},
{Type: ServiceBindingConditionProjectionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: InitializeConditionReason},
},
},
},
Expand All @@ -301,10 +301,15 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
{
Type: ServiceBindingConditionReady,
Status: metav1.ConditionUnknown,
Reason: "ProjectionReadyUnknown",
Reason: "ServiceAvailableUnknown",
LastTransitionTime: now,
},
{Type: ServiceBindingConditionServiceAvailable},
{
Type: ServiceBindingConditionServiceAvailable,
LastTransitionTime: now,
Status: metav1.ConditionUnknown,
Reason: InitializeConditionReason,
},
{
Type: ServiceBindingConditionProjectionReady,
Status: metav1.ConditionUnknown,
Expand Down Expand Up @@ -334,11 +339,14 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
{
Type: ServiceBindingConditionReady,
Status: metav1.ConditionUnknown,
Reason: "Unknown",
Reason: "ServiceAvailableUnknown",
LastTransitionTime: now,
},
{
Type: ServiceBindingConditionServiceAvailable,
Type: ServiceBindingConditionServiceAvailable,
LastTransitionTime: now,
Status: metav1.ConditionUnknown,
Reason: InitializeConditionReason,
},
{
Type: ServiceBindingConditionProjectionReady,
Expand Down Expand Up @@ -376,7 +384,10 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
LastTransitionTime: now,
},
{
Type: ServiceBindingConditionServiceAvailable,
Type: ServiceBindingConditionServiceAvailable,
LastTransitionTime: now,
Status: metav1.ConditionUnknown,
Reason: InitializeConditionReason,
},
{
Type: ServiceBindingConditionProjectionReady,
Expand Down Expand Up @@ -407,11 +418,14 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
{
Type: ServiceBindingConditionReady,
Status: metav1.ConditionUnknown,
Reason: "ProjectionReadyUnknown",
Reason: "ServiceAvailableUnknown",
LastTransitionTime: now,
},
{
Type: ServiceBindingConditionServiceAvailable,
Type: ServiceBindingConditionServiceAvailable,
LastTransitionTime: now,
Status: metav1.ConditionUnknown,
Reason: InitializeConditionReason,
},
{
Type: ServiceBindingConditionProjectionReady,
Expand All @@ -426,9 +440,9 @@ func TestServiceBindingStatus_PropagateServiceBindingProjectionStatus(t *testing
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
actual := c.seed.DeepCopy()
actual.InitializeConditions()
actual.InitializeConditions(now)
actual.PropagateServiceBindingProjectionStatus(c.projection, now)
if diff := cmp.Diff(c.expected, actual); diff != "" {
if diff := cmp.Diff(c.expected, actual, cmpopts.IgnoreTypes(metav1.Time{})); diff != "" {
t.Errorf("%s: PropagateServiceBindingProjectionStatus() (-expected, +actual): %s", c.name, diff)
}
})
Expand All @@ -442,7 +456,7 @@ func TestServiceBindingStatus_MarkServiceAvailable(t *testing.T) {
{
Type: ServiceBindingConditionReady,
Status: metav1.ConditionUnknown,
Reason: "Unknown",
Reason: "ProjectionReadyUnknown",
Copy link
Member

Choose a reason for hiding this comment

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

Is that Reason ProjectionReadyUnknown correct? Can you please help me with some detail for this reason value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aggregare func builds the reason string with sub condition type + condition reason. Same for other comment

LastTransitionTime: now,
},
{
Expand All @@ -451,14 +465,14 @@ func TestServiceBindingStatus_MarkServiceAvailable(t *testing.T) {
Reason: "Available",
LastTransitionTime: now,
},
{Type: ServiceBindingConditionProjectionReady},
{Type: ServiceBindingConditionProjectionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: "Unknown"},
},
}
actual := &ServiceBindingStatus{}
actual.InitializeConditions()
actual.InitializeConditions(now)
actual.MarkServiceAvailable(now)

if diff := cmp.Diff(expected, actual); diff != "" {
if diff := cmp.Diff(expected, actual, cmpopts.IgnoreTypes(metav1.Time{})); diff != "" {
t.Errorf("MarkServiceAvailable() (-expected, +actual): %s", diff)
}
}
Expand All @@ -481,19 +495,20 @@ func TestServiceBindingStatus_MarkServiceUnavailable(t *testing.T) {
Message: "the message",
LastTransitionTime: now,
},
{Type: ServiceBindingConditionProjectionReady},
{Type: ServiceBindingConditionProjectionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: "Unknown"},
},
}
actual := &ServiceBindingStatus{}
actual.InitializeConditions()
actual.InitializeConditions(now)
actual.MarkServiceUnavailable("TheReason", "the message", now)

if diff := cmp.Diff(expected, actual); diff != "" {
if diff := cmp.Diff(expected, actual, cmpopts.IgnoreTypes(metav1.Time{})); diff != "" {
t.Errorf("MarkServiceUnavailable() (-expected, +actual): %s", diff)
}
}

func TestServiceBindingStatus_InitializeConditions(t *testing.T) {
now := metav1.Now()
tests := []struct {
name string
seed *ServiceBindingStatus
Expand All @@ -504,9 +519,9 @@ func TestServiceBindingStatus_InitializeConditions(t *testing.T) {
seed: &ServiceBindingStatus{},
expected: &ServiceBindingStatus{
Conditions: []metav1.Condition{
{Type: ServiceBindingConditionReady},
{Type: ServiceBindingConditionServiceAvailable},
{Type: ServiceBindingConditionProjectionReady},
{Type: ServiceBindingConditionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: "Unknown"},
{Type: ServiceBindingConditionServiceAvailable, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: "Unknown"},
{Type: ServiceBindingConditionProjectionReady, Status: metav1.ConditionUnknown, LastTransitionTime: now, Reason: "Unknown"},
},
},
},
Expand Down Expand Up @@ -549,7 +564,7 @@ func TestServiceBindingStatus_InitializeConditions(t *testing.T) {
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
actual := c.seed.DeepCopy()
actual.InitializeConditions()
actual.InitializeConditions(now)
if diff := cmp.Diff(c.expected, actual, cmpopts.IgnoreTypes(metav1.Time{})); diff != "" {
t.Errorf("%s: InitializeConditions() (-expected, +actual): %s", c.name, diff)
}
Expand All @@ -572,11 +587,21 @@ func TestServiceBindingStatus_aggregateReadyCondition(t *testing.T) {
{
Type: ServiceBindingConditionReady,
Status: "Unknown",
Reason: "ServiceAvailableUnknown",
Copy link
Member

Choose a reason for hiding this comment

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

ServiceAvailableUnknown - same question here as well

LastTransitionTime: now,
},
{
Type: ServiceBindingConditionServiceAvailable,
Status: "Unknown",
Reason: "Unknown",
LastTransitionTime: now,
},
{
Type: ServiceBindingConditionProjectionReady,
Status: "Unknown",
Reason: "Unknown",
LastTransitionTime: now,
},
{Type: ServiceBindingConditionServiceAvailable},
{Type: ServiceBindingConditionProjectionReady},
},
},
},
Expand Down Expand Up @@ -769,9 +794,9 @@ func TestServiceBindingStatus_aggregateReadyCondition(t *testing.T) {
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
actual := c.seed.DeepCopy()
actual.InitializeConditions()
actual.InitializeConditions(now)
actual.aggregateReadyCondition(now)
if diff := cmp.Diff(c.expected, actual); diff != "" {
if diff := cmp.Diff(c.expected, actual, cmpopts.IgnoreTypes(metav1.Time{})); diff != "" {
t.Errorf("%s: aggregateReadyCondition() (-expected, +actual): %s", c.name, diff)
}
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/servicebinding/servicebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, binding *servicebindingv
return nil
}

binding.Status.InitializeConditions()
now := r.now()
binding.Status.InitializeConditions(now)

secretRef, err := r.provisionedSecret(ctx, logger, binding)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions pkg/reconciler/servicebinding/servicebinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,63 @@ func TestReconcile(t *testing.T) {
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "Reconciled", "ServiceBinding reconciled: %q", key),
},
}, {
Name: "missing referenced service with no service projection",
Key: key,
Objects: []runtime.Object{
&servicebindingv1alpha3.ServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Generation: 1,
},
Spec: servicebindingv1alpha3.ServiceBindingSpec{
Name: name,
Workload: &workloadRef,
Service: &serviceRef,
},
},
},
WantErr: true,
WantEvents: []string{
Eventf(corev1.EventTypeWarning, "InternalError", "failed to get resource for bindings.labs.vmware.com/v1alpha1, Resource=provisionedservices: provisionedservices.bindings.labs.vmware.com %q not found", serviceRef.Name),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: &servicebindingv1alpha3.ServiceBinding{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Generation: 1,
},
Spec: servicebindingv1alpha3.ServiceBindingSpec{
Name: name,
Workload: &workloadRef,
Service: &serviceRef,
},
Status: servicebindingv1alpha3.ServiceBindingStatus{
Conditions: []metav1.Condition{
{
Type: servicebindingv1alpha3.ServiceBindingConditionReady,
Status: metav1.ConditionUnknown,
LastTransitionTime: now,
Reason: "Unknown",
},
{
Type: servicebindingv1alpha3.ServiceBindingConditionServiceAvailable,
Status: metav1.ConditionUnknown,
LastTransitionTime: now,
Reason: "Unknown",
},
{
Type: servicebindingv1alpha3.ServiceBindingConditionProjectionReady,
Status: metav1.ConditionUnknown,
LastTransitionTime: now,
Reason: "Unknown",
},
},
},
},
}},
}, {
Name: "missing referenced service",
Key: key,
Expand Down