|
| 1 | +// /* |
| 2 | +// Copyright 2025 IQiYi Inc. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// */ |
| 16 | + |
| 17 | +package actioner |
| 18 | + |
| 19 | +/* |
| 20 | +KernelRouteAddDelVerdict Actioner Params: |
| 21 | +------------------------------------------------- |
| 22 | +name value |
| 23 | +------------------------------------------------- |
| 24 | +ifname network interface name |
| 25 | +with-route also add a host route |
| 26 | +
|
| 27 | +------------------------------------------------- |
| 28 | +*/ |
| 29 | + |
| 30 | +import ( |
| 31 | + "context" |
| 32 | + "fmt" |
| 33 | + "time" |
| 34 | + |
| 35 | + "github.com/golang/glog" |
| 36 | + "github.com/iqiyi/dpvs/tools/healthcheck/pkg/types" |
| 37 | + "github.com/iqiyi/dpvs/tools/healthcheck/pkg/utils" |
| 38 | + "github.com/vishvananda/netlink" |
| 39 | +) |
| 40 | + |
| 41 | +var _ ActionMethod = (*KernelRouteVerdictAction)(nil) |
| 42 | +var _ ActionMethodWithVerdict = (*KernelRouteVerdictAction)(nil) |
| 43 | + |
| 44 | +const kernelRouteVerdictActionerName = "KernelRouteAddDelVerdict" |
| 45 | + |
| 46 | +func init() { |
| 47 | + registerMethod(kernelRouteVerdictActionerName, &KernelRouteVerdictAction{}) |
| 48 | +} |
| 49 | + |
| 50 | +// KernelRouteVerdictAction is the same as KernelRouteAction except it also implements |
| 51 | +// ActionMethodWithVerdict interface. |
| 52 | +type KernelRouteVerdictAction struct { |
| 53 | + KernelRouteAction |
| 54 | +} |
| 55 | + |
| 56 | +func (a *KernelRouteVerdictAction) Act(signal types.State, timeout time.Duration, |
| 57 | + data ...interface{}) (interface{}, error) { |
| 58 | + return a.KernelRouteAction.Act(signal, timeout, data) |
| 59 | +} |
| 60 | + |
| 61 | +func (a *KernelRouteVerdictAction) create(target *utils.L3L4Addr, params map[string]string, |
| 62 | + extras ...interface{}) (ActionMethod, error) { |
| 63 | + embeded, err := a.KernelRouteAction.create(target, params, extras) |
| 64 | + if embededObj, ok := embeded.(*KernelRouteAction); ok { |
| 65 | + method := &KernelRouteVerdictAction{ |
| 66 | + KernelRouteAction: *embededObj, |
| 67 | + } |
| 68 | + return method, err |
| 69 | + } |
| 70 | + return nil, fmt.Errorf("failed to create %s embeded action for %s", kernelRouteVerdictActionerName, |
| 71 | + target.IP.String()) |
| 72 | +} |
| 73 | + |
| 74 | +func (a *KernelRouteVerdictAction) validate(params map[string]string) error { |
| 75 | + return a.KernelRouteAction.validate(params) |
| 76 | +} |
| 77 | + |
| 78 | +func (a *KernelRouteVerdictAction) Verdict(timeout time.Duration) (types.State, error) { |
| 79 | + targetIP := a.target.IP |
| 80 | + if timeout <= 0 { |
| 81 | + return types.Unknown, fmt.Errorf("zero verdict timeout on %s actioner %v", |
| 82 | + kernelRouteVerdictActionerName, targetIP) |
| 83 | + } |
| 84 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + result := types.Unknown |
| 88 | + done := make(chan error, 1) |
| 89 | + |
| 90 | + go func() { |
| 91 | + link, err := netlink.LinkByName(a.ifname) |
| 92 | + if err != nil { |
| 93 | + done <- fmt.Errorf("failed to get link by name: %w", err) |
| 94 | + return |
| 95 | + } |
| 96 | + addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL) |
| 97 | + if err != nil { |
| 98 | + done <- fmt.Errorf("failed to get addrs on %s: %w", a.ifname, err) |
| 99 | + return |
| 100 | + } |
| 101 | + for _, addr := range addrs { |
| 102 | + if targetIP.Equal(addr.IP) { |
| 103 | + result = types.Healthy |
| 104 | + done <- nil |
| 105 | + return |
| 106 | + } |
| 107 | + } |
| 108 | + result = types.Unhealthy |
| 109 | + done <- nil |
| 110 | + }() |
| 111 | + |
| 112 | + select { |
| 113 | + case <-ctx.Done(): |
| 114 | + glog.Warningf("%s actioner %v verdict timeout", kernelRouteVerdictActionerName, targetIP) |
| 115 | + return types.Unknown, ctx.Err() |
| 116 | + case err := <-done: |
| 117 | + if err != nil { |
| 118 | + return types.Unknown, err |
| 119 | + } |
| 120 | + return result, nil |
| 121 | + } |
| 122 | + return types.Unknown, nil |
| 123 | +} |
0 commit comments