-
Notifications
You must be signed in to change notification settings - Fork 2
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
[WIP]SKS-1825: Use the default IPPool as the global IPPool #37
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -130,26 +130,28 @@ func (m *Metal3IPAM) ReleaseIPs(ctx goctx.Context, owner metav1.Object, pool ipa | |
|
||
func (m *Metal3IPAM) GetAvailableIPPool(ctx goctx.Context, poolMatchLabels map[string]string, clusterMeta metav1.ObjectMeta) (ipam.IPPool, error) { | ||
poolNamespace := getIPPoolNamespace(poolMatchLabels, clusterMeta) | ||
poolName := poolMatchLabels[ipam.ClusterIPPoolNameKey] | ||
|
||
// if the specific ip-pool name is provided use that to get the ip-pool | ||
var ipPool ipamv1.IPPool | ||
if poolName, ok := poolMatchLabels[ipam.ClusterIPPoolNameKey]; ok && poolName != "" { | ||
if err := m.Get(ctx, apitypes.NamespacedName{ | ||
Namespace: poolNamespace, | ||
Name: poolName, | ||
}, &ipPool); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil, nil | ||
} | ||
|
||
return nil, errors.Wrapf(err, "failed to get IPPool %s/%s", poolNamespace, poolName) | ||
} | ||
|
||
return toIPPool(ipPool), nil | ||
// 1. If the specific ip-pool name is provided use that to get the ip-pool. | ||
if poolName != "" { | ||
return m.getIPPoolByName(ctx, poolNamespace, poolName) | ||
} | ||
|
||
matchLabels := map[string]string{} | ||
// 2. Otherwise use default ip-pool | ||
|
||
ipPoolList := &ipamv1.IPPoolList{} | ||
if err := m.List( | ||
ctx, | ||
ipPoolList, | ||
client.InNamespace(clusterMeta.Namespace), | ||
client.MatchingLabels(map[string]string{ipam.DefaultIPPoolKey: "true"})); err != nil { | ||
return nil, err | ||
} | ||
if len(ipPoolList.Items) > 0 { | ||
return toIPPool(ipPoolList.Items[0]), nil | ||
} | ||
|
||
matchLabels := map[string]string{ipam.DefaultIPPoolKey: "true"} | ||
// use labels 'ip-pool-group' & 'network-name' to select the ip-pool | ||
if label, ok := poolMatchLabels[ipam.ClusterIPPoolGroupKey]; ok && label != "" { | ||
matchLabels[ipam.ClusterIPPoolGroupKey] = label | ||
|
@@ -158,30 +160,50 @@ func (m *Metal3IPAM) GetAvailableIPPool(ctx goctx.Context, poolMatchLabels map[s | |
matchLabels[ipam.ClusterNetworkNameKey] = label | ||
} | ||
|
||
// use default ip-pool | ||
if len(matchLabels) == 0 { | ||
poolNamespace = ipam.DefaultIPPoolNamespace | ||
matchLabels[ipam.DefaultIPPoolKey] = "true" | ||
} | ||
|
||
ipPoolList := &ipamv1.IPPoolList{} | ||
ipPoolList = &ipamv1.IPPoolList{} | ||
if err := m.List( | ||
ctx, | ||
ipPoolList, | ||
client.InNamespace(poolNamespace), | ||
client.InNamespace(ipam.DefaultIPPoolNamespace), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果DefaultIPPoolNamespace和clusterMeta.Namespace 一样,这个if 是不是就不用执行了? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 后面还有 ClusterIPPoolGroupKey & ClusterNetworkNameKey,这两个没有用到,可以考虑删了。 |
||
client.MatchingLabels(matchLabels)); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(ipPoolList.Items) == 0 { | ||
m.logger.Info("failed to get a matching IPPool") | ||
return nil, nil | ||
} | ||
ipPool = ipPoolList.Items[0] | ||
|
||
m.logger.Info(fmt.Sprintf("IPPool %s is available", ipPool.Name)) | ||
return toIPPool(ipPoolList.Items[0]), nil | ||
} | ||
|
||
// getIPPoolByName returns the IPPool with the specified name. | ||
// | ||
// If IPPool is not found in the specified namespace, will try to find from the | ||
// default namespace. | ||
func (m *Metal3IPAM) getIPPoolByName(ctx goctx.Context, poolNamespace, poolName string) (ipam.IPPool, error) { | ||
var ipPool ipamv1.IPPool | ||
err := m.Get(ctx, apitypes.NamespacedName{ | ||
Namespace: poolNamespace, | ||
Name: poolName, | ||
}, &ipPool) | ||
if err == nil { | ||
return toIPPool(ipPool), nil | ||
} else if !apierrors.IsNotFound(err) { | ||
return nil, errors.Wrapf(err, "failed to get IPPool %s/%s", poolNamespace, poolName) | ||
} | ||
|
||
// Try the ip-pool default namespace. | ||
err = m.Get(ctx, apitypes.NamespacedName{ | ||
Namespace: ipam.DefaultIPPoolNamespace, | ||
Name: poolName, | ||
}, &ipPool) | ||
if err == nil { | ||
return toIPPool(ipPool), nil | ||
} else if apierrors.IsNotFound(err) { | ||
return nil, nil | ||
} | ||
|
||
return toIPPool(ipPool), nil | ||
return nil, errors.Wrapf(err, "failed to get IPPool %s/%s", poolNamespace, poolName) | ||
} | ||
|
||
func (m *Metal3IPAM) getIPClaim(ctx goctx.Context, pool ipam.IPPool, claimName string) (*ipamv1.IPClaim, error) { | ||
|
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.
建议这里改成一个func:getIPPoolByLabels()