Skip to content

Commit e35b0fa

Browse files
authored
vm static ip validate should use vm name (#5569)
* vm static ip validate should use vm name Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com> * name should always be equal, no contains Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com> --------- Signed-off-by: zbb88888 <jmdxjsjgcxy@gmail.com>
1 parent d9473a3 commit e35b0fa

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pkg/webhook/static_ip.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,16 @@ func (v *ValidatingHook) PodCreateHook(ctx context.Context, req admission.Reques
117117
if v.allowLiveMigration(o.GetAnnotations()) {
118118
return ctrlwebhook.Allowed("by pass")
119119
}
120-
return v.validateIP(ctx, o.GetAnnotations(), o.Kind, o.GetName(), o.GetNamespace())
120+
name := o.GetName()
121+
// If the pod is created by a VM, we need to get the VM name from owner references
122+
for _, owner := range o.GetOwnerReferences() {
123+
if owner.Kind == util.VMInstance && strings.HasPrefix(owner.APIVersion, "kubevirt.io") {
124+
name = owner.Name
125+
klog.V(3).Infof("pod %s is created by vm %s", o.GetName(), name)
126+
break
127+
}
128+
}
129+
return v.validateIP(ctx, o.GetAnnotations(), o.Kind, name, o.GetNamespace())
121130
}
122131

123132
func (v *ValidatingHook) allowLiveMigration(annotations map[string]string) bool {
@@ -197,7 +206,7 @@ func (v *ValidatingHook) checkIPConflict(ipAddress, annoSubnet, name string, ipL
197206
if ipAddr.String() == v4IP || ipAddr.String() == v6IP {
198207
// The IP's spec podName does not equal the Pod name in the request;
199208
// The two names have a containment relationship.
200-
if name == ipCR.Spec.PodName || ipCR.Spec.PodType == util.VM && strings.Contains(ipCR.Spec.PodName, name) {
209+
if name == ipCR.Spec.PodName {
201210
klog.Infof("get same ip crd for %s", name)
202211
} else {
203212
err := fmt.Errorf("annotation static-ip %s is conflict with ip crd %s, ip %s", ipAddr.String(), ipCR.Name, ipCR.Spec.IPAddress)

0 commit comments

Comments
 (0)