-
Notifications
You must be signed in to change notification settings - Fork 0
Update gardener infrastructure egress cidrs for ACL extension. #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d57201c
Update gardener infrastructure egress cidrs for ACL extension.
Gerrit91 63f5e24
Test with CIDRs already set.
Gerrit91 fb333ff
Improve test a bit.
Gerrit91 c625900
Skip update if already up-to-date.
Gerrit91 ef22b35
Include egress rules.
Gerrit91 920033e
Sort CIDRs.
Gerrit91 8eca522
Move logic to firewall deployment controller.
Gerrit91 e7f2708
Small refactor.
Gerrit91 57b7c08
Also trigger ACL extension reconcile.
Gerrit91 d14d7b2
Fix.
Gerrit91 d571163
Simplify.
Gerrit91 19fc65b
Simplify.
Gerrit91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package deployment | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/netip" | ||
"slices" | ||
"strings" | ||
|
||
v2 "github.com/metal-stack/firewall-controller-manager/api/v2" | ||
"github.com/metal-stack/firewall-controller-manager/controllers" | ||
"github.com/metal-stack/metal-lib/pkg/pointer" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (c *controller) updateInfrastructureStatus(r *controllers.Ctx[*v2.FirewallDeployment], infrastructureName string, ownedFirewalls []*v2.Firewall) error { | ||
infraObj := &unstructured.Unstructured{} | ||
|
||
infraObj.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "extensions.gardener.cloud", | ||
Kind: "Infrastructure", | ||
Version: "v1alpha1", | ||
}) | ||
|
||
err := c.c.GetSeedClient().Get(r.Ctx, client.ObjectKey{ | ||
Namespace: c.c.GetSeedNamespace(), | ||
Name: infrastructureName, | ||
}, infraObj) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
type infrastructure struct { | ||
Spec struct { | ||
ProviderConfig struct { | ||
Firewall struct { | ||
EgressRules []struct { | ||
IPs []string `json:"ips"` | ||
} `json:"egressRules"` | ||
} `json:"firewall"` | ||
} `json:"providerConfig"` | ||
} `json:"spec"` | ||
Status struct { | ||
EgressCIDRs []string `json:"egressCIDRs"` | ||
} `json:"status"` | ||
} | ||
|
||
infraRaw, err := json.Marshal(infraObj) | ||
if err != nil { | ||
return fmt.Errorf("unable to convert gardener infrastructure object: %w", err) | ||
} | ||
|
||
var typedInfra infrastructure | ||
err = json.Unmarshal(infraRaw, &typedInfra) | ||
if err != nil { | ||
return fmt.Errorf("unable to convert gardener infrastructure object: %w", err) | ||
} | ||
|
||
var egressCIDRs []string | ||
|
||
for _, fw := range ownedFirewalls { | ||
for _, network := range fw.Status.FirewallNetworks { | ||
if pointer.SafeDeref(network.NetworkType) != "external" { | ||
continue | ||
} | ||
|
||
for _, ip := range network.IPs { | ||
parsed, err := netip.ParseAddr(ip) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
egressCIDRs = append(egressCIDRs, fmt.Sprintf("%s/%d", ip, parsed.BitLen())) | ||
} | ||
} | ||
} | ||
|
||
for _, rule := range typedInfra.Spec.ProviderConfig.Firewall.EgressRules { | ||
for _, ip := range rule.IPs { | ||
parsed, err := netip.ParseAddr(ip) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
egressCIDRs = append(egressCIDRs, fmt.Sprintf("%s/%d", ip, parsed.BitLen())) | ||
} | ||
} | ||
|
||
slices.Sort(egressCIDRs) | ||
slices.Sort(typedInfra.Status.EgressCIDRs) | ||
|
||
// check if an update is required or not | ||
if slices.Equal(egressCIDRs, typedInfra.Status.EgressCIDRs) { | ||
c.log.Info("found gardener infrastructure resource, egress cidrs already up-to-date", "infrastructure-name", infraObj.GetName(), "egress-cidrs", egressCIDRs) | ||
return nil | ||
} | ||
|
||
infraStatusPatch := map[string]any{ | ||
"status": map[string]any{ | ||
"egressCIDRs": egressCIDRs, | ||
}, | ||
} | ||
|
||
jsonPatch, err := json.Marshal(infraStatusPatch) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshal infrastructure status patch: %w", err) | ||
} | ||
|
||
err = c.c.GetSeedClient().Status().Patch(r.Ctx, infraObj, client.RawPatch(types.MergePatchType, jsonPatch)) | ||
if err != nil { | ||
return fmt.Errorf("error patching infrastructure status egress cidrs field: %w", err) | ||
} | ||
|
||
c.log.Info("found gardener infrastructure resource and patched egress cidrs for acl extension", "infrastructure-name", infraObj.GetName(), "egress-cidrs", egressCIDRs) | ||
|
||
aclObj := &unstructured.Unstructured{} | ||
|
||
aclObj.SetGroupVersionKind(schema.GroupVersionKind{ | ||
Group: "extensions.gardener.cloud", | ||
Kind: "Extension", | ||
Version: "v1alpha1", | ||
}) | ||
|
||
err = c.c.GetSeedClient().Get(r.Ctx, client.ObjectKey{ | ||
Namespace: c.c.GetSeedNamespace(), | ||
Name: "acl", | ||
}, aclObj) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
err = v2.AddAnnotation(r.Ctx, c.c.GetSeedClient(), aclObj, "gardener.cloud/operation", "reconcile") | ||
if err != nil { | ||
return fmt.Errorf("error annotating acl extension with reconcile operation: %w", err) | ||
} | ||
|
||
c.log.Info("added reconcile annotation to gardener acl extension object") | ||
|
||
return nil | ||
} | ||
|
||
func extractInfrastructureNameFromSeedNamespace(namespace string) (string, bool) { | ||
if !strings.HasPrefix(namespace, "shoot--") { | ||
return "", false | ||
} | ||
|
||
parts := strings.Split(namespace, "--") | ||
if len(parts) < 3 { | ||
return "", false | ||
} | ||
|
||
return strings.Join(parts[2:], "--"), true | ||
majst01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.