/
references.go
236 lines (220 loc) · 7.6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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 stage
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 = resolveReferenceForDeploymentID(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.Stage) 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.DeploymentRef != nil && ko.Spec.DeploymentID != nil {
return ackerr.ResourceReferenceAndIDNotSupportedFor("DeploymentID", "DeploymentRef")
}
return nil
}
// hasNonNilReferences returns true if resource contains a reference to another
// resource
func hasNonNilReferences(ko *svcapitypes.Stage) bool {
return false || (ko.Spec.APIRef != nil) || (ko.Spec.DeploymentRef != 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.Stage,
) 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: APIRef")
}
obj := &svcapitypes.API{}
if err := getReferencedResourceState_API(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
return err
}
ko.Spec.APIID = (*string)(obj.Status.APIID)
}
return nil
}
// getReferencedResourceState_API looks up whether a referenced resource
// exists and is in a ACK.ResourceSynced=True state. If the referenced resource does exist and is
// in a Synced state, returns nil, otherwise returns `ackerr.ResourceReferenceTerminalFor` or
// `ResourceReferenceNotSyncedFor` depending on if the resource is in a Terminal state.
func getReferencedResourceState_API(
ctx context.Context,
apiReader client.Reader,
obj *svcapitypes.API,
name string, // the Kubernetes name of the referenced resource
namespace string, // the Kubernetes namespace of the referenced resource
) error {
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
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 {
return ackerr.ResourceReferenceTerminalFor(
"API",
namespace, name)
}
}
if refResourceTerminal {
return ackerr.ResourceReferenceTerminalFor(
"API",
namespace, name)
}
if !refResourceSynced {
return ackerr.ResourceReferenceNotSyncedFor(
"API",
namespace, name)
}
if obj.Status.APIID == nil {
return ackerr.ResourceReferenceMissingTargetFieldFor(
"API",
namespace, name,
"Status.APIID")
}
return nil
}
// resolveReferenceForDeploymentID reads the resource referenced
// from DeploymentRef field and sets the DeploymentID
// from referenced resource
func resolveReferenceForDeploymentID(
ctx context.Context,
apiReader client.Reader,
namespace string,
ko *svcapitypes.Stage,
) error {
if ko.Spec.DeploymentRef != nil && ko.Spec.DeploymentRef.From != nil {
arr := ko.Spec.DeploymentRef.From
if arr == nil || arr.Name == nil || *arr.Name == "" {
return fmt.Errorf("provided resource reference is nil or empty: DeploymentRef")
}
obj := &svcapitypes.Deployment{}
if err := getReferencedResourceState_Deployment(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
return err
}
ko.Spec.DeploymentID = (*string)(obj.Status.DeploymentID)
}
return nil
}
// getReferencedResourceState_Deployment looks up whether a referenced resource
// exists and is in a ACK.ResourceSynced=True state. If the referenced resource does exist and is
// in a Synced state, returns nil, otherwise returns `ackerr.ResourceReferenceTerminalFor` or
// `ResourceReferenceNotSyncedFor` depending on if the resource is in a Terminal state.
func getReferencedResourceState_Deployment(
ctx context.Context,
apiReader client.Reader,
obj *svcapitypes.Deployment,
name string, // the Kubernetes name of the referenced resource
namespace string, // the Kubernetes namespace of the referenced resource
) error {
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
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 {
return ackerr.ResourceReferenceTerminalFor(
"Deployment",
namespace, name)
}
}
if refResourceTerminal {
return ackerr.ResourceReferenceTerminalFor(
"Deployment",
namespace, name)
}
if !refResourceSynced {
return ackerr.ResourceReferenceNotSyncedFor(
"Deployment",
namespace, name)
}
if obj.Status.DeploymentID == nil {
return ackerr.ResourceReferenceMissingTargetFieldFor(
"Deployment",
namespace, name,
"Status.DeploymentID")
}
return nil
}