Skip to content
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

webhook: fix ip validation when pod is annotated with an ippool name #3284

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkg/webhook/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand Down Expand Up @@ -162,14 +163,14 @@ func (v *ValidatingHook) validateIP(ctx context.Context, annotations map[string]
if err := v.cache.List(ctx, ipList); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
if err := v.validateIPConflict(annotations, name, ipList.Items); err != nil {
if err := v.validateIPConflict(ctx, annotations, name, ipList.Items); err != nil {
return ctrlwebhook.Denied(err.Error())
}

return ctrlwebhook.Allowed("by pass")
}

func (v *ValidatingHook) validateIPConflict(annotations map[string]string, name string, ipList []ovnv1.IP) error {
func (v *ValidatingHook) validateIPConflict(ctx context.Context, annotations map[string]string, name string, ipList []ovnv1.IP) error {
annoSubnet := annotations[util.LogicalSwitchAnnotation]
if annotations[util.LogicalSwitchAnnotation] == "" {
annoSubnet = util.DefaultSubnet
Expand All @@ -183,7 +184,12 @@ func (v *ValidatingHook) validateIPConflict(annotations map[string]string, name

ipPool := annotations[util.IPPoolAnnotation]
if ipPool != "" {
if err := v.checkIPConflict(ipPool, annoSubnet, name, ipList); err != nil {
if !strings.ContainsRune(ipPool, ',') && net.ParseIP(ipPool) == nil {
pool := &ovnv1.IPPool{}
if err := v.cache.Get(ctx, types.NamespacedName{Name: ipPool}, pool); err != nil {
return fmt.Errorf("ippool %q not found", ipPool)
}
} else if err := v.checkIPConflict(ipPool, annoSubnet, name, ipList); err != nil {
return err
}
}
Expand All @@ -198,6 +204,9 @@ func (v *ValidatingHook) checkIPConflict(ipAddress, annoSubnet, name string, ipL
} else {
ipAddr = net.ParseIP(strings.TrimSpace(ip))
}
if ipAddr == nil {
return fmt.Errorf("invalid static ip/ippool annotation value: %s", ipAddress)
}

for _, ipCr := range ipList {
if annoSubnet != "" && ipCr.Spec.Subnet != annoSubnet {
Expand Down
Loading