/
references.go
200 lines (188 loc) · 6.19 KB
/
references.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.
// Code generated by ack-generate. DO NOT EDIT.
package integration
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
svcapitypes "github.com/aws-controllers-k8s/apigatewayv2-controller/apis/v1alpha1"
)
// ResolveReferences finds if there are any Reference field(s) present
// inside AWSResource passed in the parameter and attempts to resolve
// those reference field(s) into target field(s).
// It returns an AWSResource with resolved reference(s), and an error if the
// passed AWSResource's reference field(s) cannot be resolved.
// This method also adds/updates the ConditionTypeReferencesResolved for the
// AWSResource.
func (rm *resourceManager) ResolveReferences(
ctx context.Context,
apiReader client.Reader,
res acktypes.AWSResource,
) (acktypes.AWSResource, error) {
namespace := res.MetaObject().GetNamespace()
ko := rm.concreteResource(res).ko.DeepCopy()
err := validateReferenceFields(ko)
if err == nil {
err = resolveReferenceForAPIID(ctx, apiReader, namespace, ko)
}
if err == nil {
err = resolveReferenceForConnectionID(ctx, apiReader, namespace, ko)
}
// If there was an error while resolving any reference, reset all the
// resolved values so that they do not get persisted inside etcd
if err != nil {
ko = rm.concreteResource(res).ko.DeepCopy()
}
if hasNonNilReferences(ko) {
return ackcondition.WithReferencesResolvedCondition(&resource{ko}, err)
}
return &resource{ko}, err
}
// validateReferenceFields validates the reference field and corresponding
// identifier field.
func validateReferenceFields(ko *svcapitypes.Integration) error {
if ko.Spec.APIRef != nil && ko.Spec.APIID != nil {
return ackerr.ResourceReferenceAndIDNotSupportedFor("APIID", "APIRef")
}
if ko.Spec.APIRef == nil && ko.Spec.APIID == nil {
return ackerr.ResourceReferenceOrIDRequiredFor("APIID", "APIRef")
}
if ko.Spec.ConnectionRef != nil && ko.Spec.ConnectionID != nil {
return ackerr.ResourceReferenceAndIDNotSupportedFor("ConnectionID", "ConnectionRef")
}
return nil
}
// hasNonNilReferences returns true if resource contains a reference to another
// resource
func hasNonNilReferences(ko *svcapitypes.Integration) bool {
return false || (ko.Spec.APIRef != nil) || (ko.Spec.ConnectionRef != nil)
}
// resolveReferenceForAPIID reads the resource referenced
// from APIRef field and sets the APIID
// from referenced resource
func resolveReferenceForAPIID(
ctx context.Context,
apiReader client.Reader,
namespace string,
ko *svcapitypes.Integration,
) error {
if ko.Spec.APIRef != nil &&
ko.Spec.APIRef.From != nil {
arr := ko.Spec.APIRef.From
if arr == nil || arr.Name == nil || *arr.Name == "" {
return fmt.Errorf("provided resource reference is nil or empty")
}
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: *arr.Name,
}
obj := svcapitypes.API{}
err := apiReader.Get(ctx, namespacedName, &obj)
if err != nil {
return err
}
var refResourceSynced, refResourceTerminal bool
for _, cond := range obj.Status.Conditions {
if cond.Type == ackv1alpha1.ConditionTypeResourceSynced &&
cond.Status == corev1.ConditionTrue {
refResourceSynced = true
}
if cond.Type == ackv1alpha1.ConditionTypeTerminal &&
cond.Status == corev1.ConditionTrue {
refResourceTerminal = true
}
}
if refResourceTerminal {
return ackerr.ResourceReferenceTerminalFor(
"API",
namespace, *arr.Name)
}
if !refResourceSynced {
return ackerr.ResourceReferenceNotSyncedFor(
"API",
namespace, *arr.Name)
}
if obj.Status.APIID == nil {
return ackerr.ResourceReferenceMissingTargetFieldFor(
"API",
namespace, *arr.Name,
"Status.APIID")
}
referencedValue := string(*obj.Status.APIID)
ko.Spec.APIID = &referencedValue
}
return nil
}
// resolveReferenceForConnectionID reads the resource referenced
// from ConnectionRef field and sets the ConnectionID
// from referenced resource
func resolveReferenceForConnectionID(
ctx context.Context,
apiReader client.Reader,
namespace string,
ko *svcapitypes.Integration,
) error {
if ko.Spec.ConnectionRef != nil &&
ko.Spec.ConnectionRef.From != nil {
arr := ko.Spec.ConnectionRef.From
if arr == nil || arr.Name == nil || *arr.Name == "" {
return fmt.Errorf("provided resource reference is nil or empty")
}
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: *arr.Name,
}
obj := svcapitypes.VPCLink{}
err := apiReader.Get(ctx, namespacedName, &obj)
if err != nil {
return err
}
var refResourceSynced, refResourceTerminal bool
for _, cond := range obj.Status.Conditions {
if cond.Type == ackv1alpha1.ConditionTypeResourceSynced &&
cond.Status == corev1.ConditionTrue {
refResourceSynced = true
}
if cond.Type == ackv1alpha1.ConditionTypeTerminal &&
cond.Status == corev1.ConditionTrue {
refResourceTerminal = true
}
}
if refResourceTerminal {
return ackerr.ResourceReferenceTerminalFor(
"VPCLink",
namespace, *arr.Name)
}
if !refResourceSynced {
return ackerr.ResourceReferenceNotSyncedFor(
"VPCLink",
namespace, *arr.Name)
}
if obj.Status.VPCLinkID == nil {
return ackerr.ResourceReferenceMissingTargetFieldFor(
"VPCLink",
namespace, *arr.Name,
"Status.VPCLinkID")
}
referencedValue := string(*obj.Status.VPCLinkID)
ko.Spec.ConnectionID = &referencedValue
}
return nil
}