-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates the response API to allow targeting and conditions.
Signed-off-by: dalton hill <[email protected]>
- Loading branch information
1 parent
95d3207
commit d685d66
Showing
7 changed files
with
614 additions
and
381 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2024 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package response | ||
|
||
import ( | ||
"github.com/crossplane/function-sdk-go/proto/v1beta1" | ||
) | ||
|
||
// Condition to be applied to the Composite Resource and sometimes the | ||
// Claim. For detailed information on proper usage of Conditions, please see | ||
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties. | ||
type Condition struct { | ||
*v1beta1.Condition | ||
} | ||
|
||
// ConditionTrue will create a condition with the status of true and add the | ||
// condition to the supplied RunFunctionResponse. | ||
func ConditionTrue(rsp *v1beta1.RunFunctionResponse, typ, reason string) *Condition { | ||
return newCondition(rsp, typ, reason, v1beta1.Status_STATUS_CONDITION_TRUE) | ||
} | ||
|
||
// ConditionFalse will create a condition with the status of false and add the | ||
// condition to the supplied RunFunctionResponse. | ||
func ConditionFalse(rsp *v1beta1.RunFunctionResponse, typ, reason string) *Condition { | ||
return newCondition(rsp, typ, reason, v1beta1.Status_STATUS_CONDITION_FALSE) | ||
} | ||
|
||
// ConditionUnknown will create a condition with the status of unknown and add | ||
// the condition to the supplied RunFunctionResponse. | ||
func ConditionUnknown(rsp *v1beta1.RunFunctionResponse, typ, reason string) *Condition { | ||
return newCondition(rsp, typ, reason, v1beta1.Status_STATUS_CONDITION_UNKNOWN) | ||
} | ||
|
||
func newCondition(rsp *v1beta1.RunFunctionResponse, typ, reason string, s v1beta1.Status) *Condition { | ||
if rsp.GetConditions() == nil { | ||
rsp.Conditions = make([]*v1beta1.Condition, 0, 1) | ||
} | ||
c := &Condition{ | ||
Condition: &v1beta1.Condition{ | ||
Type: typ, | ||
Status: s, | ||
Reason: reason, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
}, | ||
} | ||
rsp.Conditions = append(rsp.GetConditions(), c.Condition) | ||
return c | ||
} | ||
|
||
// TargetComposite updates the condition to target the composite resource. | ||
func (c *Condition) TargetComposite() { | ||
c.Target = v1beta1.Target_TARGET_COMPOSITE.Enum() | ||
} | ||
|
||
// TargetCompositeAndClaim updates the condition to target both the composite | ||
// resource and claim. | ||
func (c *Condition) TargetCompositeAndClaim() { | ||
c.Target = v1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum() | ||
} | ||
|
||
// WithMessage adds the message to the condition. | ||
func (c *Condition) WithMessage(message string) { | ||
c.Message = &message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
Copyright 2024 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package response_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
"k8s.io/utils/ptr" | ||
|
||
"github.com/crossplane/function-sdk-go/proto/v1beta1" | ||
"github.com/crossplane/function-sdk-go/response" | ||
) | ||
|
||
// Condition types. | ||
const ( | ||
typeDatabaseReady = "DatabaseReady" | ||
) | ||
|
||
// Condition reasons. | ||
const ( | ||
reasonAvailable = "ReasonAvailable" | ||
reasonCreating = "ReasonCreating" | ||
reasonPriorFailure = "ReasonPriorFailure" | ||
reasonUnauthorized = "ReasonUnauthorized" | ||
) | ||
|
||
func TestCondition(t *testing.T) { | ||
type testFn func(*v1beta1.RunFunctionResponse) | ||
type args struct { | ||
fns []testFn | ||
} | ||
type want struct { | ||
conditions []*v1beta1.Condition | ||
} | ||
cases := map[string]struct { | ||
reason string | ||
args args | ||
want want | ||
}{ | ||
"CreateBasicRecords": { | ||
reason: "Correctly adds conditions to the response.", | ||
args: args{ | ||
fns: []testFn{ | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable) | ||
}, | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionFalse(rsp, typeDatabaseReady, reasonCreating) | ||
}, | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionUnknown(rsp, typeDatabaseReady, reasonPriorFailure) | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
conditions: []*v1beta1.Condition{ | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_TRUE, | ||
Reason: reasonAvailable, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
}, | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_FALSE, | ||
Reason: reasonCreating, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
}, | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_UNKNOWN, | ||
Reason: reasonPriorFailure, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
"SetTargets": { | ||
reason: "Correctly sets targets on condition and adds it to the response.", | ||
args: args{ | ||
fns: []testFn{ | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).TargetComposite() | ||
}, | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).TargetCompositeAndClaim() | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
conditions: []*v1beta1.Condition{ | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_TRUE, | ||
Reason: reasonAvailable, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
}, | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_TRUE, | ||
Reason: reasonAvailable, | ||
Target: v1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
"SetMessage": { | ||
reason: "Correctly sets message on condition and adds it to the response.", | ||
args: args{ | ||
fns: []testFn{ | ||
func(rsp *v1beta1.RunFunctionResponse) { | ||
response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).WithMessage("a test message") | ||
}, | ||
}, | ||
}, | ||
want: want{ | ||
conditions: []*v1beta1.Condition{ | ||
{ | ||
Type: typeDatabaseReady, | ||
Status: v1beta1.Status_STATUS_CONDITION_TRUE, | ||
Reason: reasonAvailable, | ||
Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), | ||
Message: ptr.To("a test message"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
rsp := &v1beta1.RunFunctionResponse{} | ||
for _, f := range tc.args.fns { | ||
f(rsp) | ||
} | ||
|
||
if diff := cmp.Diff(tc.want.conditions, rsp.GetConditions(), protocmp.Transform()); diff != "" { | ||
t.Errorf("\n%s\nFrom(...): -want, +got:\n%s", tc.reason, diff) | ||
} | ||
|
||
}) | ||
} | ||
} |
Oops, something went wrong.