Skip to content

Commit 018b77b

Browse files
committed
Harmonize infra status creation
1 parent 12fbc4e commit 018b77b

3 files changed

Lines changed: 111 additions & 184 deletions

File tree

pkg/controller/infrastructure/flow_reconciler.go

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
1212
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
1313
"github.com/go-logr/logr"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1514
"k8s.io/apimachinery/pkg/runtime"
1615
"k8s.io/apimachinery/pkg/util/sets"
1716
"k8s.io/client-go/rest"
@@ -20,7 +19,6 @@ import (
2019

2120
awsapi "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws"
2221
"github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws/helper"
23-
awsv1alpha1 "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws/v1alpha1"
2422
"github.com/gardener/gardener-extension-provider-aws/pkg/aws"
2523
"github.com/gardener/gardener-extension-provider-aws/pkg/controller/infrastructure/infraflow"
2624
"github.com/gardener/gardener-extension-provider-aws/pkg/controller/infrastructure/infraflow/shared"
@@ -185,8 +183,11 @@ func (f *FlowReconciler) migrateFromTerraform(ctx context.Context, infra *extens
185183
return nil, fmt.Errorf("migration from terraform state failed: %w", err)
186184
}
187185

188-
// TODO duplication of computeProviderStatusFromFlowState and fctx.computeInfrastructureStatus
189-
infrastructureStatus := computeProviderStatusFromFlowState(infrastructureConfig, state)
186+
whiteboard := shared.NewWhiteboard()
187+
if state != nil {
188+
whiteboard.ImportFromFlatMap(state.Data)
189+
}
190+
infrastructureStatus := infraflow.BuildInfrastructureStatus(whiteboard, infrastructureConfig)
190191

191192
if err := infraflow.PatchProviderStatusAndState(ctx, f.client, infra, networking, infrastructureStatus, &runtime.RawExtension{Object: state}, nil, nil, nil); err != nil {
192193
return nil, fmt.Errorf("updating status state failed: %w", err)
@@ -195,94 +196,6 @@ func (f *FlowReconciler) migrateFromTerraform(ctx context.Context, infra *extens
195196
return state, nil
196197
}
197198

198-
func computeProviderStatusFromFlowState(config *awsapi.InfrastructureConfig, state *awsapi.InfrastructureState) *awsv1alpha1.InfrastructureStatus {
199-
if len(state.Data) == 0 {
200-
return nil
201-
}
202-
status := &awsv1alpha1.InfrastructureStatus{
203-
TypeMeta: metav1.TypeMeta{
204-
APIVersion: awsv1alpha1.SchemeGroupVersion.String(),
205-
Kind: "InfrastructureStatus",
206-
},
207-
}
208-
209-
vpcID := ""
210-
if config.Networks.VPC.ID != nil {
211-
vpcID = *config.Networks.VPC.ID
212-
} else {
213-
vpcID = state.Data[infraflow.IdentifierVPC]
214-
if !shared.IsValidValue(vpcID) {
215-
vpcID = ""
216-
}
217-
}
218-
219-
if vpcID != "" {
220-
var subnets []awsv1alpha1.Subnet
221-
prefix := infraflow.ChildIdZones + shared.Separator
222-
for k, v := range state.Data {
223-
if !shared.IsValidValue(v) {
224-
continue
225-
}
226-
if strings.HasPrefix(k, prefix) {
227-
parts := strings.Split(k, shared.Separator)
228-
if len(parts) != 3 {
229-
continue
230-
}
231-
var purpose string
232-
switch parts[2] {
233-
case infraflow.IdentifierZoneSubnetPublic:
234-
purpose = awsapi.PurposePublic
235-
case infraflow.IdentifierZoneSubnetWorkers:
236-
purpose = awsapi.PurposeNodes
237-
default:
238-
continue
239-
}
240-
subnets = append(subnets, awsv1alpha1.Subnet{
241-
ID: v,
242-
Purpose: purpose,
243-
Zone: parts[1],
244-
})
245-
}
246-
}
247-
248-
status.VPC = awsv1alpha1.VPCStatus{
249-
ID: vpcID,
250-
Subnets: subnets,
251-
}
252-
if groupID := state.Data[infraflow.IdentifierNodesSecurityGroup]; shared.IsValidValue(groupID) {
253-
status.VPC.SecurityGroups = []awsv1alpha1.SecurityGroup{
254-
{
255-
Purpose: awsapi.PurposeNodes,
256-
ID: groupID,
257-
},
258-
}
259-
}
260-
}
261-
262-
if keyName := state.Data[infraflow.NameKeyPair]; shared.IsValidValue(keyName) {
263-
status.EC2.KeyName = keyName
264-
}
265-
266-
if name := state.Data[infraflow.NameIAMInstanceProfile]; shared.IsValidValue(name) {
267-
status.IAM.InstanceProfiles = []awsv1alpha1.InstanceProfile{
268-
{
269-
Purpose: awsapi.PurposeNodes,
270-
Name: name,
271-
},
272-
}
273-
}
274-
if arn := state.Data[infraflow.ARNIAMRole]; shared.IsValidValue(arn) {
275-
status.IAM.Roles = []awsv1alpha1.Role{
276-
{
277-
Purpose: awsapi.PurposeNodes,
278-
ARN: arn,
279-
},
280-
}
281-
}
282-
283-
return status
284-
}
285-
286199
func migrateTerraformStateToFlowState(rawExtension *runtime.RawExtension, zones []awsapi.Zone) (*awsapi.InfrastructureState, error) {
287200
var (
288201
flowState = &awsapi.InfrastructureState{

pkg/controller/infrastructure/infraflow/context.go

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1616
"k8s.io/apimachinery/pkg/runtime"
1717
"k8s.io/apimachinery/pkg/util/sets"
18-
"k8s.io/utils/ptr"
1918
"sigs.k8s.io/controller-runtime/pkg/client"
2019

2120
awsapi "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws"
@@ -238,97 +237,7 @@ func PatchProviderStatusAndState(
238237
}
239238

240239
func (c *FlowContext) computeInfrastructureStatus() *awsv1alpha1.InfrastructureStatus {
241-
status := &awsv1alpha1.InfrastructureStatus{
242-
TypeMeta: metav1.TypeMeta{
243-
APIVersion: awsv1alpha1.SchemeGroupVersion.String(),
244-
Kind: "InfrastructureStatus",
245-
},
246-
}
247-
248-
vpcID := ptr.Deref(c.state.Get(IdentifierVPC), "")
249-
groupID := ptr.Deref(c.state.Get(IdentifierNodesSecurityGroup), "")
250-
ec2KeyName := ptr.Deref(c.state.Get(NameKeyPair), "")
251-
iamInstanceProfileName := ptr.Deref(c.state.Get(NameIAMInstanceProfile), "")
252-
arnIAMRole := ptr.Deref(c.state.Get(ARNIAMRole), "")
253-
efsID := ptr.Deref(c.state.Get(IdentifierManagedEfsID), "")
254-
// check if user provided a custom EFS ID
255-
if c.config.ElasticFileSystem != nil && c.config.ElasticFileSystem.ID != nil {
256-
efsID = *c.config.ElasticFileSystem.ID
257-
}
258-
259-
if c.config.Networks.VPC.ID != nil {
260-
vpcID = *c.config.Networks.VPC.ID
261-
}
262-
263-
if vpcID != "" {
264-
var subnets []awsv1alpha1.Subnet
265-
prefix := ChildIdZones + shared.Separator
266-
for k, v := range c.state.ExportAsFlatMap() {
267-
if !shared.IsValidValue(v) {
268-
continue
269-
}
270-
if strings.HasPrefix(k, prefix) {
271-
parts := strings.Split(k, shared.Separator)
272-
if len(parts) != 3 {
273-
continue
274-
}
275-
var purpose string
276-
switch parts[2] {
277-
case IdentifierZoneSubnetPublic:
278-
purpose = awsapi.PurposePublic
279-
case IdentifierZoneSubnetWorkers:
280-
purpose = awsapi.PurposeNodes
281-
default:
282-
continue
283-
}
284-
subnets = append(subnets, awsv1alpha1.Subnet{
285-
ID: v,
286-
Purpose: purpose,
287-
Zone: parts[1],
288-
})
289-
}
290-
}
291-
292-
status.VPC = awsv1alpha1.VPCStatus{
293-
ID: vpcID,
294-
Subnets: subnets,
295-
}
296-
if groupID != "" {
297-
status.VPC.SecurityGroups = []awsv1alpha1.SecurityGroup{
298-
{
299-
Purpose: awsapi.PurposeNodes,
300-
ID: groupID,
301-
},
302-
}
303-
}
304-
}
305-
306-
if ec2KeyName != "" {
307-
status.EC2.KeyName = ec2KeyName
308-
}
309-
310-
if iamInstanceProfileName != "" {
311-
status.IAM.InstanceProfiles = []awsv1alpha1.InstanceProfile{
312-
{
313-
Purpose: awsapi.PurposeNodes,
314-
Name: iamInstanceProfileName,
315-
},
316-
}
317-
}
318-
if arnIAMRole != "" {
319-
status.IAM.Roles = []awsv1alpha1.Role{
320-
{
321-
Purpose: awsapi.PurposeNodes,
322-
ARN: arnIAMRole,
323-
},
324-
}
325-
}
326-
327-
if efsID != "" {
328-
status.ElasticFileSystem.ID = efsID
329-
}
330-
331-
return status
240+
return BuildInfrastructureStatus(c.state, c.config)
332241
}
333242

334243
func (c *FlowContext) computeInfrastructureState() *runtime.RawExtension {

pkg/controller/infrastructure/infraflow/utils.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ import (
1818
"github.com/gardener/gardener/pkg/utils/flow"
1919
"github.com/go-logr/logr"
2020
"go.uber.org/atomic"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/utils/ptr"
2123

24+
awsapi "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws"
25+
awsv1alpha1 "github.com/gardener/gardener-extension-provider-aws/pkg/apis/aws/v1alpha1"
2226
awsclient "github.com/gardener/gardener-extension-provider-aws/pkg/aws/client"
27+
"github.com/gardener/gardener-extension-provider-aws/pkg/controller/infrastructure/infraflow/shared"
2328
)
2429

2530
// ErrorMultipleMatches is returned when multiple matches are found
@@ -208,3 +213,103 @@ func mountTargetsContainSubnet(mountTargets []efstypes.MountTargetDescription, s
208213
}
209214
return false, ""
210215
}
216+
217+
// BuildInfrastructureStatus constructs an InfrastructureStatus from flow state and config.
218+
func BuildInfrastructureStatus(
219+
state shared.Whiteboard,
220+
cfg *awsapi.InfrastructureConfig,
221+
) *awsv1alpha1.InfrastructureStatus {
222+
status := &awsv1alpha1.InfrastructureStatus{
223+
TypeMeta: metav1.TypeMeta{
224+
APIVersion: awsv1alpha1.SchemeGroupVersion.String(),
225+
Kind: "InfrastructureStatus",
226+
},
227+
}
228+
229+
vpcID := ptr.Deref(state.Get(IdentifierVPC), "")
230+
groupID := ptr.Deref(state.Get(IdentifierNodesSecurityGroup), "")
231+
ec2KeyName := ptr.Deref(state.Get(NameKeyPair), "")
232+
iamInstanceProfileName := ptr.Deref(state.Get(NameIAMInstanceProfile), "")
233+
arnIAMRole := ptr.Deref(state.Get(ARNIAMRole), "")
234+
efsID := ptr.Deref(state.Get(IdentifierManagedEfsID), "")
235+
236+
// config overrides
237+
if cfg != nil {
238+
if cfg.ElasticFileSystem != nil && cfg.ElasticFileSystem.ID != nil {
239+
efsID = *cfg.ElasticFileSystem.ID
240+
}
241+
if cfg.Networks.VPC.ID != nil {
242+
vpcID = *cfg.Networks.VPC.ID
243+
}
244+
}
245+
246+
if vpcID != "" {
247+
var subnets []awsv1alpha1.Subnet
248+
prefix := ChildIdZones + shared.Separator
249+
for k, v := range state.ExportAsFlatMap() {
250+
if !shared.IsValidValue(v) {
251+
continue
252+
}
253+
if strings.HasPrefix(k, prefix) {
254+
parts := strings.Split(k, shared.Separator)
255+
if len(parts) != 3 {
256+
continue
257+
}
258+
var purpose string
259+
switch parts[2] {
260+
case IdentifierZoneSubnetPublic:
261+
purpose = awsapi.PurposePublic
262+
case IdentifierZoneSubnetWorkers:
263+
purpose = awsapi.PurposeNodes
264+
default:
265+
continue
266+
}
267+
subnets = append(subnets, awsv1alpha1.Subnet{
268+
ID: v,
269+
Purpose: purpose,
270+
Zone: parts[1],
271+
})
272+
}
273+
}
274+
275+
status.VPC = awsv1alpha1.VPCStatus{
276+
ID: vpcID,
277+
Subnets: subnets,
278+
}
279+
if groupID != "" {
280+
status.VPC.SecurityGroups = []awsv1alpha1.SecurityGroup{
281+
{
282+
Purpose: awsapi.PurposeNodes,
283+
ID: groupID,
284+
},
285+
}
286+
}
287+
}
288+
289+
if ec2KeyName != "" {
290+
status.EC2.KeyName = ec2KeyName
291+
}
292+
293+
if iamInstanceProfileName != "" {
294+
status.IAM.InstanceProfiles = []awsv1alpha1.InstanceProfile{
295+
{
296+
Purpose: awsapi.PurposeNodes,
297+
Name: iamInstanceProfileName,
298+
},
299+
}
300+
}
301+
if arnIAMRole != "" {
302+
status.IAM.Roles = []awsv1alpha1.Role{
303+
{
304+
Purpose: awsapi.PurposeNodes,
305+
ARN: arnIAMRole,
306+
},
307+
}
308+
}
309+
310+
if efsID != "" {
311+
status.ElasticFileSystem.ID = efsID
312+
}
313+
314+
return status
315+
}

0 commit comments

Comments
 (0)