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

save egress ip addresses in the infrastructure.status #352

Draft
wants to merge 2 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
3 changes: 2 additions & 1 deletion pkg/apis/metal/types_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ type InfrastructureStatus struct {
}

type FirewallStatus struct {
MachineID string
MachineID string
ExternalEgressIPs []string
}
3 changes: 2 additions & 1 deletion pkg/apis/metal/v1alpha1/types_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ type InfrastructureStatus struct {
}

type FirewallStatus struct {
MachineID string `json:"machineID"`
MachineID string `json:"machineID"`
ExternalEgressIPs []string `json:"externalEgressIPs"`
}
2 changes: 2 additions & 0 deletions pkg/apis/metal/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/apis/metal/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/apis/metal/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pkg/controller/infrastructure/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ func decodeInfrastructure(infrastructure *extensionsv1alpha1.Infrastructure, dec
return infrastructureConfig, infrastructureStatus, nil
}

func updateProviderStatus(ctx context.Context, c client.Client, infrastructure *extensionsv1alpha1.Infrastructure, providerStatus *metalapi.InfrastructureStatus, nodeCIDR *string) error {
func updateProviderStatus(ctx context.Context, c client.Client, infrastructure *extensionsv1alpha1.Infrastructure, providerStatus *metalapi.InfrastructureStatus, nodeCIDR *string, externalEgressIPs *[]string) error {
patch := client.MergeFrom(infrastructure.DeepCopy())
infrastructure.Status.ProviderStatus = &runtime.RawExtension{Object: &metalv1alpha1.InfrastructureStatus{
TypeMeta: metav1.TypeMeta{
APIVersion: metalv1alpha1.SchemeGroupVersion.String(),
Kind: "InfrastructureStatus",
},
Firewall: metalv1alpha1.FirewallStatus{
MachineID: providerStatus.Firewall.MachineID,
MachineID: providerStatus.Firewall.MachineID,
ExternalEgressIPs: *externalEgressIPs,
},
}}
infrastructure.Status.NodesCIDR = nodeCIDR
Expand Down
44 changes: 41 additions & 3 deletions pkg/controller/infrastructure/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"time"

"github.com/go-logr/logr"
mn "github.com/metal-stack/metal-lib/pkg/net"
"github.com/metal-stack/metal-lib/pkg/tag"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"

metalclient "github.com/metal-stack/gardener-extension-provider-metal/pkg/metal/client"

metalapi "github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal"
"github.com/metal-stack/gardener-extension-provider-metal/pkg/apis/metal/helper"
metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metal-go/api/client/ip"
metalip "github.com/metal-stack/metal-go/api/client/ip"
"github.com/metal-stack/metal-go/api/client/network"
"github.com/metal-stack/metal-go/api/models"
Expand All @@ -23,6 +24,7 @@ import (
"github.com/gardener/gardener/pkg/controllerutils/reconciler"

extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
fcmv2 "github.com/metal-stack/firewall-controller-manager/api/v2"
)

type networkReconciler struct {
Expand Down Expand Up @@ -79,7 +81,16 @@ func (a *actuator) Reconcile(ctx context.Context, logger logr.Logger, infrastruc
}
}

err = updateProviderStatus(ctx, a.client, infrastructure, internalInfrastructureStatus, &nodeCIDR)
// get all egress ips from the firewall CRs and the infrastructure config and save them in the infrastructure status
externalEgressIPs, err := getFirewallEgressIPs(ctx, networkReconciler, a.client, cluster.Shoot.Status.TechnicalID, logger)
if err != nil {
return &reconciler.RequeueAfterError{
Cause: err,
RequeueAfter: 30 * time.Second,
}
}

err = updateProviderStatus(ctx, a.client, infrastructure, internalInfrastructureStatus, &nodeCIDR, &externalEgressIPs)
if err != nil {
return err
}
Expand All @@ -105,7 +116,7 @@ func (a *actuator) Reconcile(ctx context.Context, logger logr.Logger, infrastruc
func reconcileEgressIPs(ctx context.Context, r *egressIPReconciler) error {
currentEgressIPs := sets.NewString()

resp, err := r.mclient.IP().FindIPs(ip.NewFindIPsParams().WithBody(&models.V1IPFindRequest{
resp, err := r.mclient.IP().FindIPs(metalip.NewFindIPsParams().WithBody(&models.V1IPFindRequest{
Projectid: r.infrastructureConfig.ProjectID,
Tags: []string{r.egressTag},
Type: models.V1IPBaseTypeStatic,
Expand Down Expand Up @@ -227,3 +238,30 @@ func ensureNodeNetwork(ctx context.Context, r *networkReconciler) (string, error

return nodeCIDR, nil
}

// getFirewallEgressIPs returns all egress ips from the firewall CRs and the infrastructure config
func getFirewallEgressIPs(ctx context.Context, r *networkReconciler, c client.Client, namespace string, logger logr.Logger) ([]string, error) {
firewalls := &fcmv2.FirewallList{}
egressIPs := sets.NewString()

err := c.List(ctx, firewalls, client.InNamespace(namespace))
if err != nil {
return nil, fmt.Errorf("unable to list firewalls: %w", err)
}

for _, fw := range firewalls.Items {
fw := fw

for _, n := range fw.Status.FirewallNetworks {
if n.NetworkType != nil && *n.NetworkType == mn.External {
egressIPs.Insert(n.IPs...)
}
}
}

for _, egressRule := range r.infrastructureConfig.Firewall.EgressRules {
egressIPs.Insert(egressRule.IPs...)
}

return egressIPs.List(), nil
}