Skip to content

Commit 0d5b234

Browse files
committed
add IAMRoleSelected condition
1 parent c55d9c0 commit 0d5b234

File tree

6 files changed

+83
-11
lines changed

6 files changed

+83
-11
lines changed

apis/core/v1alpha1/conditions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ const (
6666
// "False" status indicates that the resource references failed to resolve.
6767
// For Ex: When referenced resource is in terminal condition
6868
ConditionTypeReferencesResolved ConditionType = "ACK.ReferencesResolved"
69+
// ConditionTypeIAMRoleSelected indicates whether an IAMRoleSelector has been selected
70+
// to manage the AWSResource. If none are selected, this condition will be removed
71+
// and we'll use the custom role to manage the AWSResource
72+
ConditionTypeIAMRoleSelected ConditionType = "ACK.IAMRoleSelected"
6973
)
7074

7175
// Condition is the common struct used by all CRDs managed by ACK service

mocks/pkg/types/aws_resource.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/condition/condition.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ var (
3333
"annotations."
3434
UnknownSyncedMessage = "Unable to determine if desired resource state matches latest observed state"
3535
NotSyncedMessage = "Resource not synced"
36-
TerminalMessage = "Resource is "
36+
TerminalMessage = "Resource is "
3737
SyncedMessage = "Resource synced successfully"
3838
FailedReferenceResolutionMessage = "Reference resolution failed"
3939
UnavailableIAMRoleMessage = "IAM Role is not available"
40+
41+
IAMRoleSelectedReason = "Selected"
42+
IAMRoleSelectedMessage = "roleARN: %s, selectorName: %s, selectorResourceVersion: %s"
4043
)
4144

4245
// Ready returns the Condition in the resource's Conditions collection that is
@@ -81,6 +84,13 @@ func ReferencesResolved(subject acktypes.ConditionManager) *ackv1alpha1.Conditio
8184
return FirstOfType(subject, ackv1alpha1.ConditionTypeReferencesResolved)
8285
}
8386

87+
// IAMRoleSelected returns the Condition in the resource's Conditions collection
88+
// that is of type ConditionTypeIAMRoleSelected. If no such condition is found,
89+
// returns nil.
90+
func IAMRoleSelected(subject acktypes.ConditionManager) *ackv1alpha1.Condition {
91+
return FirstOfType(subject, ackv1alpha1.ConditionTypeIAMRoleSelected)
92+
}
93+
8494
// FirstOfType returns the first Condition in the resource's Conditions
8595
// collection of the supplied type. If no such condition is found, returns nil.
8696
func FirstOfType(
@@ -252,6 +262,30 @@ func SetReferencesResolved(
252262
subject.ReplaceConditions(allConds)
253263
}
254264

265+
// SetIAMRoleSelected sets the resource's Condition of type ConditionTypeIAMRoleSelected
266+
// to the supplied status, optional message and reason.
267+
func SetIAMRoleSelected(
268+
subject acktypes.ConditionManager,
269+
status corev1.ConditionStatus,
270+
message *string,
271+
reason *string,
272+
) {
273+
allConds := subject.Conditions()
274+
var c *ackv1alpha1.Condition
275+
if c = ReferencesResolved(subject); c == nil {
276+
c = &ackv1alpha1.Condition{
277+
Type: ackv1alpha1.ConditionTypeIAMRoleSelected,
278+
}
279+
allConds = append(allConds, c)
280+
}
281+
now := metav1.Now()
282+
c.LastTransitionTime = &now
283+
c.Status = status
284+
c.Message = message
285+
c.Reason = reason
286+
subject.ReplaceConditions(allConds)
287+
}
288+
255289
// RemoveReferencesResolved removes the condition of type ConditionTypeReferencesResolved
256290
// from the resource's conditions
257291
func RemoveReferencesResolved(

pkg/condition/condition_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func TestConditionGetters(t *testing.T) {
4949
got = ackcond.Ready(r)
5050
assert.Nil(got)
5151

52+
got = ackcond.IAMRoleSelected(r)
53+
assert.Nil(got)
54+
5255
conds = append(conds, &ackv1alpha1.Condition{
5356
Type: ackv1alpha1.ConditionTypeResourceSynced,
5457
Status: corev1.ConditionFalse,
@@ -66,6 +69,9 @@ func TestConditionGetters(t *testing.T) {
6669
got = ackcond.Ready(r)
6770
assert.Nil(got)
6871

72+
got = ackcond.IAMRoleSelected(r)
73+
assert.Nil(got)
74+
6975
conds = append(conds, &ackv1alpha1.Condition{
7076
Type: ackv1alpha1.ConditionTypeTerminal,
7177
Status: corev1.ConditionFalse,
@@ -83,6 +89,9 @@ func TestConditionGetters(t *testing.T) {
8389
got = ackcond.Ready(r)
8490
assert.Nil(got)
8591

92+
got = ackcond.IAMRoleSelected(r)
93+
assert.Nil(got)
94+
8695
gotAll := ackcond.AllOfType(r, ackv1alpha1.ConditionTypeAdvisory)
8796
assert.Empty(gotAll)
8897

@@ -124,6 +133,15 @@ func TestConditionGetters(t *testing.T) {
124133
r.On("Conditions").Return(conds)
125134
got = ackcond.Ready(r)
126135
assert.NotNil(got)
136+
137+
conds = append(conds, &ackv1alpha1.Condition{
138+
Type: ackv1alpha1.ConditionTypeIAMRoleSelected,
139+
Status: corev1.ConditionTrue,
140+
})
141+
r = &ackmocks.AWSResource{}
142+
r.On("Conditions").Return(conds)
143+
got = ackcond.IAMRoleSelected(r)
144+
assert.NotNil(got)
127145
}
128146

129147
func TestConditionSetters(t *testing.T) {
@@ -329,4 +347,19 @@ func TestConditionSetters(t *testing.T) {
329347
}),
330348
)
331349
ackcond.SetReady(r, corev1.ConditionTrue, nil, nil)
350+
351+
// IAMRoleSelected condition
352+
r = &ackmocks.AWSResource{}
353+
r.On("Conditions").Return([]*ackv1alpha1.Condition{})
354+
r.On(
355+
"ReplaceConditions",
356+
mock.MatchedBy(func(subject []*ackv1alpha1.Condition) bool {
357+
if len(subject) != 1 {
358+
return false
359+
}
360+
return (subject[0].Type == ackv1alpha1.ConditionTypeIAMRoleSelected &&
361+
subject[0].Status == corev1.ConditionTrue)
362+
}),
363+
)
364+
ackcond.SetIAMRoleSelected(r, corev1.ConditionTrue, nil, nil)
332365
}

pkg/runtime/reconciler.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,21 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
353353
return ctrlrt.Result{}, err
354354
}
355355
latest, err = r.reconcile(ctx, rm, desired)
356-
if latest != nil && selector != nil {
357-
latest.SetIAMRoleSelector(selector)
356+
if latest != nil {
357+
setIAMRoleSelectorCondition(latest, selector)
358358
}
359359
return r.HandleReconcileError(ctx, desired, latest, err)
360360
}
361361

362+
func setIAMRoleSelectorCondition(r acktypes.ConditionManager, selector *ackv1alpha1.IAMRoleSelector) {
363+
if selector == nil {
364+
return
365+
}
366+
367+
message := fmt.Sprintf(condition.IAMRoleSelectedMessage, selector.Spec.ARN, selector.GetName(), selector.GetResourceVersion())
368+
condition.SetIAMRoleSelected(r, corev1.ConditionTrue, &condition.IAMRoleSelectedReason, &message)
369+
}
370+
362371
// regionDrifted return true if the desired resource region is different
363372
// from the target region. Target region can be derived from the two places
364373
// in the following order:

pkg/types/aws_resource.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,4 @@ type AWSResource interface {
5151
// PopulateResourceFromAnnotation will set the Spec or Status field that user
5252
// provided from annotations
5353
PopulateResourceFromAnnotation(fields map[string]string) error
54-
// Set the selected IAMRoleSelector information used to manage this resource
55-
// in the resource status
56-
SetIAMRoleSelector(*ackv1alpha1.IAMRoleSelector)
5754
}

0 commit comments

Comments
 (0)