Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export egress cidrs to status #740

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/controller/infrastructure/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,17 @@ func (a *actuator) reconcile(ctx context.Context, log logr.Logger, infra *extens
if err != nil {
return err
}
egressCidrs, err := getEgressCidrs(state)
if err != nil {
return err
}

patch := client.MergeFrom(infra.DeepCopy())
infra.Status.ProviderStatus = &runtime.RawExtension{Object: status}
infra.Status.State = &runtime.RawExtension{Raw: stateByte}
if egressCidrs != nil {
infra.Status.EgressCIDRs = egressCidrs
}
return a.client.Status().Patch(ctx, infra, patch)
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/controller/infrastructure/actuator_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,43 @@ func (a *actuator) updateStatusProvider(ctx context.Context, infra *extensionsv1

patch := client.MergeFrom(infra.DeepCopy())
infra.Status.ProviderStatus = &runtime.RawExtension{Object: infrastructureStatus}
egressCidrs := getEgressIpCidrs(state)
if egressCidrs != nil {
infra.Status.EgressCIDRs = egressCidrs
}
return a.client.Status().Patch(ctx, infra, patch)

}

func getEgressIpCidrs(state *infraflow.PersistentState) []string {
if len(state.Data) == 0 {
return nil
}

cidrs := []string{}
prefix := infraflow.ChildIdZones + shared.Separator
for k, v := range state.Data {
if !shared.IsValidValue(v) {
continue
}
if strings.HasPrefix(k, prefix) {
parts := strings.Split(k, shared.Separator)
if len(parts) != 3 {
continue
}
if parts[2] == infraflow.ZoneNATGWElasticIPAddress {
cidrs = append(cidrs, v+"/32")
}
}
}
if len(cidrs) == 0 {
return nil
}

return cidrs

}

func computeProviderStatusFromFlowState(config *aliapi.InfrastructureConfig, state *infraflow.PersistentState) (*aliv1alpha1.InfrastructureStatus, error) {
if len(state.Data) == 0 {
return nil, nil
Expand Down
34 changes: 28 additions & 6 deletions pkg/controller/infrastructure/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var _ = Describe("Actuator", func() {
vpcCIDRString string
securityGroupID string
rawState *realterraformer.RawState
fakeTfState *realterraformer.RawState

serviceForNatGw string
serviceLinkedRoleForNatGw string
Expand Down Expand Up @@ -229,13 +230,28 @@ var _ = Describe("Actuator", func() {
serviceLinkedRoleForNatGw = "AliyunServiceRoleForNatgw"
serviceForNatGw = "nat.aliyuncs.com"

fakeTfState = &realterraformer.RawState{
Data: `
{
"resources": [
{
"mode": "managed",
"type": "alicloud_eip",
"instances": [
{
"attributes": {"ip_address": "139.196.40.2"}
}
]
}
]
}
`,
Encoding: "none",
}

})

It("should correctly reconcile the infrastructure", func() {
rawState = &realterraformer.RawState{
Data: "",
Encoding: "none",
}
describeNATGatewaysReq := vpc.CreateDescribeNatGatewaysRequest()
describeNATGatewaysReq.VpcId = vpcID

Expand Down Expand Up @@ -300,7 +316,7 @@ var _ = Describe("Actuator", func() {
TerraformerOutputKeyVPCCIDR: vpcCIDRString,
TerraformerOutputKeySecurityGroupID: securityGroupID,
}, nil),
terraformer.EXPECT().GetRawState(ctx).Return(rawState, nil),
terraformer.EXPECT().GetRawState(ctx).Return(fakeTfState, nil),
c.EXPECT().Status().Return(sw),
sw.EXPECT().Patch(ctx, &infra, gomock.Any()),
)
Expand All @@ -317,6 +333,9 @@ var _ = Describe("Actuator", func() {
},
},
}))
Expect(infra.Status.EgressCIDRs).To(Equal([]string{
"139.196.40.2/32",
}))
})

It("should correctly restore the infrastructure", func() {
Expand Down Expand Up @@ -390,7 +409,7 @@ var _ = Describe("Actuator", func() {
TerraformerOutputKeyVPCCIDR: vpcCIDRString,
TerraformerOutputKeySecurityGroupID: securityGroupID,
}, nil),
terraformer.EXPECT().GetRawState(ctx).Return(rawState, nil),
terraformer.EXPECT().GetRawState(ctx).Return(fakeTfState, nil),
c.EXPECT().Status().Return(sw),
sw.EXPECT().Patch(ctx, &infra, gomock.Any()),
)
Expand All @@ -408,6 +427,9 @@ var _ = Describe("Actuator", func() {
},
},
}))
Expect(infra.Status.EgressCIDRs).To(Equal([]string{
"139.196.40.2/32",
}))
})
})
})
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/infrastructure/tf_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,24 @@ func getTerraformerRawState(state *runtime.RawExtension) (*terraformer.RawState,
}
return tfRawState, nil
}

func getEgressCidrs(terraformState *terraformer.RawState) ([]string, error) {
tfState, err := shared.UnmarshalTerraformStateFromTerraformer(terraformState)
if err != nil {
return nil, err
}
resources := tfState.FindManagedResourcesByType("alicloud_eip")

egressCidrs := []string{}
for _, resource := range resources {
for _, instance := range resource.Instances {
rawIpAddress := instance.Attributes["ip_address"]
ipAddress, ok := rawIpAddress.(string)
if !ok {
return nil, fmt.Errorf("error parsing '%v' as IP-address from Terraform state", rawIpAddress)
}
egressCidrs = append(egressCidrs, ipAddress+"/32")
}
}
return egressCidrs, nil
}
Loading