-
Notifications
You must be signed in to change notification settings - Fork 42
NETOBSERV-2484: improve ovn subnets detection #2169
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
jotak
wants to merge
7
commits into
netobserv:main
Choose a base branch
from
jotak:ovn-subnets
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.
+260
−146
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9276b13
NETOBSERV-2484: improve ovn subnets detection
jotak 796a30c
add configurable masquerade subnet
jotak 304ea73
Fetch kube apiserver IP, small refactoring (moving files)
jotak 07f9579
Avoid configuring cidrs already covered for apiserver endpoint
jotak 9a73fa2
Refactor detect subnets
jotak adfa563
Add role for reading networks
jotak 1c64baf
Use EXT:* pattern for external ips
jotak 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
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,183 @@ | ||
| package flp | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
|
|
||
| flowslatest "github.com/netobserv/network-observability-operator/api/flowcollector/v1beta2" | ||
| "github.com/netobserv/network-observability-operator/internal/pkg/cluster" | ||
| "github.com/netobserv/network-observability-operator/internal/pkg/helper" | ||
| configv1 "github.com/openshift/api/config/v1" | ||
| operatorv1 "github.com/openshift/api/operator/v1" | ||
| "gopkg.in/yaml.v2" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| func (r *Reconciler) getOpenShiftSubnets(ctx context.Context) ([]flowslatest.SubnetLabel, error) { | ||
| if !r.mgr.ClusterInfo.HasCNO() { | ||
| return nil, nil | ||
| } | ||
| var errs []error | ||
| var svcMachineCIDRs []*net.IPNet | ||
|
|
||
| pods, services, extIPs, err := readNetworkConfig(ctx, r) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| for _, strCIDR := range services { | ||
| if _, parsed, err := net.ParseCIDR(strCIDR); err == nil { | ||
| svcMachineCIDRs = append(svcMachineCIDRs, parsed) | ||
| } | ||
| } | ||
|
|
||
| machines, err := readClusterConfig(ctx, r) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| for _, strCIDR := range machines { | ||
| if _, parsed, err := net.ParseCIDR(strCIDR); err == nil { | ||
| svcMachineCIDRs = append(svcMachineCIDRs, parsed) | ||
| } | ||
| } | ||
|
|
||
| // API server | ||
| if apiserverIPs, err := cluster.GetAPIServerEndpointIPs(ctx, r, r.mgr.ClusterInfo); err == nil { | ||
| // Check if this isn't already an IP covered in Services or Machines subnets | ||
| for _, ip := range apiserverIPs { | ||
| if parsed := net.ParseIP(ip); parsed != nil { | ||
| var alreadyCovered bool | ||
| for _, cidr := range svcMachineCIDRs { | ||
| if cidr.Contains(parsed) { | ||
| alreadyCovered = true | ||
| break | ||
| } | ||
| } | ||
| if !alreadyCovered { | ||
| cidr := helper.IPToCIDR(ip) | ||
| services = append(services, cidr) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| errs = append(errs, fmt.Errorf("can't get API server endpoint IPs: %w", err)) | ||
| } | ||
|
|
||
| // Additional OVN subnets | ||
| moreMachines, err := readNetworkOperatorConfig(ctx, r) | ||
| if err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| machines = append(machines, moreMachines...) | ||
|
|
||
| var subnets []flowslatest.SubnetLabel | ||
| if len(machines) > 0 { | ||
| subnets = append(subnets, flowslatest.SubnetLabel{ | ||
| Name: "Machines", | ||
| CIDRs: machines, | ||
| }) | ||
| } | ||
| if len(pods) > 0 { | ||
| subnets = append(subnets, flowslatest.SubnetLabel{ | ||
| Name: "Pods", | ||
| CIDRs: pods, | ||
| }) | ||
| } | ||
| if len(services) > 0 { | ||
| subnets = append(subnets, flowslatest.SubnetLabel{ | ||
| Name: "Services", | ||
| CIDRs: services, | ||
| }) | ||
| } | ||
| if len(extIPs) > 0 { | ||
| subnets = append(subnets, flowslatest.SubnetLabel{ | ||
| Name: "EXT:ExternalIP", | ||
| CIDRs: extIPs, | ||
| }) | ||
| } | ||
| return subnets, errors.Join(errs...) | ||
| } | ||
|
|
||
| func readNetworkConfig(ctx context.Context, cl client.Client) ([]string, []string, []string, error) { | ||
| // Pods and Services subnets are found in CNO config | ||
| var pods, services, extIPs []string | ||
| network := &configv1.Network{} | ||
| if err := cl.Get(ctx, types.NamespacedName{Name: "cluster"}, network); err != nil { | ||
| return nil, nil, nil, fmt.Errorf("can't get Network (config) information: %w", err) | ||
| } | ||
| for _, podsNet := range network.Spec.ClusterNetwork { | ||
| pods = append(pods, podsNet.CIDR) | ||
| } | ||
| services = network.Spec.ServiceNetwork | ||
| if network.Spec.ExternalIP != nil && len(network.Spec.ExternalIP.AutoAssignCIDRs) > 0 { | ||
| extIPs = network.Spec.ExternalIP.AutoAssignCIDRs | ||
| } | ||
| return pods, services, extIPs, nil | ||
| } | ||
|
|
||
| func readClusterConfig(ctx context.Context, cl client.Client) ([]string, error) { | ||
| // Nodes subnet found in CM cluster-config-v1 (kube-system) | ||
| cm := &corev1.ConfigMap{} | ||
| if err := cl.Get(ctx, types.NamespacedName{Name: "cluster-config-v1", Namespace: "kube-system"}, cm); err != nil { | ||
| return nil, fmt.Errorf(`can't read "cluster-config-v1" ConfigMap: %w`, err) | ||
| } | ||
| return readMachineFromConfig(cm) | ||
| } | ||
|
|
||
| func readMachineFromConfig(cm *corev1.ConfigMap) ([]string, error) { | ||
| type ClusterConfig struct { | ||
| Networking struct { | ||
| MachineNetwork []struct { | ||
| CIDR string `yaml:"cidr"` | ||
| } `yaml:"machineNetwork"` | ||
| } `yaml:"networking"` | ||
| } | ||
|
|
||
| var rawConfig string | ||
| var ok bool | ||
| if rawConfig, ok = cm.Data["install-config"]; !ok { | ||
| return nil, fmt.Errorf(`can't find key "install-config" in "cluster-config-v1" ConfigMap`) | ||
| } | ||
| var config ClusterConfig | ||
| if err := yaml.Unmarshal([]byte(rawConfig), &config); err != nil { | ||
| return nil, fmt.Errorf(`can't deserialize content of "cluster-config-v1" ConfigMap: %w`, err) | ||
| } | ||
|
|
||
| var cidrs []string | ||
| for _, cidr := range config.Networking.MachineNetwork { | ||
| cidrs = append(cidrs, cidr.CIDR) | ||
| } | ||
|
|
||
| return cidrs, nil | ||
| } | ||
|
|
||
| func readNetworkOperatorConfig(ctx context.Context, cl client.Client) ([]string, error) { | ||
| // Additional OVN subnets: https://github.com/openshift/cluster-network-operator/blob/fda7a9f07ab6f78d032d310cdd77f21d04f1289a/pkg/network/ovn_kubernetes.go#L76-L77 | ||
| var machines []string | ||
| networkOp := &operatorv1.Network{} | ||
| if err := cl.Get(ctx, types.NamespacedName{Name: "cluster"}, networkOp); err != nil { | ||
| return nil, fmt.Errorf("can't get Network (operator) information: %w", err) | ||
| } | ||
| internalSubnet := "100.64.0.0/16" | ||
| transitSwitchSubnet := "100.88.0.0/16" | ||
| masqueradeSubnet := "169.254.0.0/17" | ||
| ovnk := networkOp.Spec.DefaultNetwork.OVNKubernetesConfig | ||
| if ovnk != nil { | ||
| if ovnk.V4InternalSubnet != "" { | ||
| internalSubnet = ovnk.V4InternalSubnet | ||
| } | ||
| if ovnk.IPv4 != nil && ovnk.IPv4.InternalTransitSwitchSubnet != "" { | ||
| transitSwitchSubnet = ovnk.IPv4.InternalTransitSwitchSubnet | ||
| } | ||
| if ovnk.GatewayConfig != nil && ovnk.GatewayConfig.IPv4.InternalMasqueradeSubnet != "" { | ||
| masqueradeSubnet = ovnk.GatewayConfig.IPv4.InternalMasqueradeSubnet | ||
| } | ||
| } | ||
| machines = append(machines, internalSubnet) | ||
| machines = append(machines, transitSwitchSubnet) | ||
| machines = append(machines, masqueradeSubnet) | ||
| return machines, 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
26 changes: 0 additions & 26 deletions
26
internal/controller/networkpolicy/apiserver_endpoint_test.go
This file was deleted.
Oops, something went wrong.
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.
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.
Can't we import https://raw.githubusercontent.com/openshift/cluster-network-operator/refs/heads/master/pkg/network/ovn_kubernetes.go here ?
So at some point if their defaults or CR changes, we'll be updated automatically.