-
Notifications
You must be signed in to change notification settings - Fork 42
Network policy fix for HyperShift #2157
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
Open
leandroberetta
wants to merge
7
commits into
netobserv:main
Choose a base branch
from
leandroberetta:np_fix_hypershift
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−17
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e360e5f
Network policy fix for HyperShift
leandroberetta 3476b6a
enhance network policy egress rule when using external control planes…
leandroberetta 66b3a0c
fix linting issues
leandroberetta c948a47
bundle update
leandroberetta e113268
addressing feedback
leandroberetta f3ee168
bundle
leandroberetta 57e31c2
Merge branch 'main' into np_fix_hypershift
leandroberetta 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
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
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
100 changes: 100 additions & 0 deletions
100
internal/controller/networkpolicy/apiserver_endpoint.go
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,100 @@ | ||
| package networkpolicy | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| discoveryv1 "k8s.io/api/discovery/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| const ( | ||
| kubernetesServiceName = "kubernetes" | ||
| kubernetesServiceNamespace = "default" | ||
| ) | ||
|
|
||
| // GetAPIServerEndpointIPs retrieves the API server endpoint IP addresses. | ||
| // It first tries to use EndpointSlice API (v1), and falls back to Endpoints API if unavailable. | ||
| func GetAPIServerEndpointIPs(ctx context.Context, cl client.Client) ([]string, error) { | ||
| logger := log.FromContext(ctx) | ||
|
|
||
| // Try EndpointSlice first (discovery.k8s.io/v1, available since k8s 1.21) | ||
| ips, err := getEndpointIPsFromEndpointSlice(ctx, cl) | ||
| if err == nil && len(ips) > 0 { | ||
| logger.V(1).Info("Retrieved API server endpoint IPs from EndpointSlice", "ips", ips) | ||
| return ips, nil | ||
| } | ||
|
|
||
| if err != nil { | ||
| logger.V(1).Info("Failed to get EndpointSlice, falling back to Endpoints API", "error", err) | ||
| } | ||
|
|
||
| // Fallback to Endpoints API (core/v1, deprecated but widely available) | ||
| ips, err = getEndpointIPsFromEndpoints(ctx, cl) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get API server endpoint IPs: %w", err) | ||
| } | ||
|
|
||
| if len(ips) == 0 { | ||
| return nil, fmt.Errorf("no API server endpoint IPs found") | ||
| } | ||
|
|
||
| logger.V(1).Info("Retrieved API server endpoint IPs from Endpoints", "ips", ips) | ||
| return ips, nil | ||
| } | ||
|
|
||
| // getEndpointIPsFromEndpointSlice retrieves endpoint IPs using the EndpointSlice API | ||
| func getEndpointIPsFromEndpointSlice(ctx context.Context, cl client.Client) ([]string, error) { | ||
| // Get the EndpointSlice directly by name | ||
| endpointSlice := &discoveryv1.EndpointSlice{} | ||
| err := cl.Get(ctx, types.NamespacedName{ | ||
| Name: kubernetesServiceName, | ||
| Namespace: kubernetesServiceNamespace, | ||
| }, endpointSlice) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return nil, fmt.Errorf("EndpointSlice for kubernetes service not found") | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| var ips []string | ||
| for j := range endpointSlice.Endpoints { | ||
| endpoint := &endpointSlice.Endpoints[j] | ||
| // Only use ready endpoints | ||
| if endpoint.Conditions.Ready != nil && *endpoint.Conditions.Ready { | ||
| ips = append(ips, endpoint.Addresses...) | ||
| } | ||
| } | ||
|
|
||
| return ips, nil | ||
| } | ||
|
|
||
| // getEndpointIPsFromEndpoints retrieves endpoint IPs using the legacy Endpoints API | ||
| func getEndpointIPsFromEndpoints(ctx context.Context, cl client.Client) ([]string, error) { | ||
| //nolint:staticcheck // SA1019: Endpoints is deprecated but used as fallback for k8s < 1.21 | ||
| endpoints := &corev1.Endpoints{} | ||
| err := cl.Get(ctx, types.NamespacedName{ | ||
| Name: kubernetesServiceName, | ||
| Namespace: kubernetesServiceNamespace, | ||
| }, endpoints) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return nil, fmt.Errorf("endpoints for kubernetes service not found") | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| var ips []string | ||
| for _, subset := range endpoints.Subsets { | ||
| for _, address := range subset.Addresses { | ||
| ips = append(ips, address.IP) | ||
| } | ||
| } | ||
|
|
||
| return ips, nil | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is specific to Openshift? Is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I see, this policy rule is only created for openshift; forget my comment :-)