Skip to content

Commit 493fbf0

Browse files
sync-tokenlukas.koszegy@t-systems.com
authored andcommitted
Add flags to adjust polling behavior for the apinet nic status (ironcore-dev#564)
1 parent fa5b8b0 commit 493fbf0

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

internal/networkinterfaceplugin/apinet.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ package networkinterfaceplugin
55

66
import (
77
"fmt"
8+
"time"
9+
10+
"github.com/spf13/pflag"
811

912
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
1013
providernetworkinterface "github.com/ironcore-dev/libvirt-provider/internal/plugins/networkinterface"
1114
"github.com/ironcore-dev/libvirt-provider/internal/plugins/networkinterface/apinet"
12-
"github.com/spf13/pflag"
15+
1316
"k8s.io/apimachinery/pkg/runtime"
1417
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1518
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -18,19 +21,19 @@ import (
1821
"sigs.k8s.io/controller-runtime/pkg/client"
1922
)
2023

21-
var (
22-
scheme = runtime.NewScheme()
23-
)
24+
var scheme = runtime.NewScheme()
2425

2526
func init() {
2627
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
2728
utilruntime.Must(apinetv1alpha1.AddToScheme(scheme))
2829
}
2930

3031
type apinetOptions struct {
31-
APInetNodeName string
32-
ApinetKubeconfig string
33-
APInetCleanup bool
32+
APInetNodeName string
33+
ApinetKubeconfig string
34+
APInetPollingDuration time.Duration
35+
APInetPollingInterval time.Duration
36+
APInetCleanup bool
3437
}
3538

3639
func (o *apinetOptions) PluginName() string {
@@ -40,6 +43,8 @@ func (o *apinetOptions) PluginName() string {
4043
func (o *apinetOptions) AddFlags(fs *pflag.FlagSet) {
4144
fs.StringVar(&o.APInetNodeName, "apinet-node-name", "", "APInet node name")
4245
fs.StringVar(&o.ApinetKubeconfig, "apinet-kubeconfig", "", "Path to the kubeconfig file for the apinet-cluster.")
46+
fs.DurationVar(&o.APInetPollingDuration, "apinet-polling-duration", time.Second*30, "The maximum time the apinet plugin will wait until the networkinterface becomes ready.")
47+
fs.DurationVar(&o.APInetPollingInterval, "apinet-polling-interval", time.Second*1, "The polling interval the apinet plugin uses to check if the networkinterface became ready.")
4348
fs.BoolVar(&o.APInetCleanup, "apinet-cleanup", false, "Cleanup orphan apinet interfaces during startup.")
4449
}
4550

@@ -69,7 +74,7 @@ func (o *apinetOptions) NetworkInterfacePlugin() (providernetworkinterface.Plugi
6974
return nil, nil, fmt.Errorf("failed to initialize api-net client: %w", err)
7075
}
7176

72-
return apinet.NewPlugin(o.APInetNodeName, apinetClient, o.APInetCleanup), nil, nil
77+
return apinet.NewPlugin(o.APInetNodeName, apinetClient, o.APInetPollingDuration, o.APInetPollingInterval, o.APInetCleanup), nil, nil
7378
}
7479

7580
func init() {

internal/plugins/networkinterface/apinet/apinet.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import (
1515
"time"
1616

1717
"github.com/google/uuid"
18+
1819
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
1920
apinet "github.com/ironcore-dev/ironcore-net/apimachinery/api/net"
2021
"github.com/ironcore-dev/ironcore-net/apinetlet/provider"
2122
"github.com/ironcore-dev/libvirt-provider/api"
2223
providerhost "github.com/ironcore-dev/libvirt-provider/internal/host"
2324
providernetworkinterface "github.com/ironcore-dev/libvirt-provider/internal/plugins/networkinterface"
25+
2426
corev1 "k8s.io/api/core/v1"
2527
apierrors "k8s.io/apimachinery/pkg/api/errors"
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,25 +38,30 @@ const (
3638

3739
defaultAPINetConfigFile = "api-net.json"
3840

39-
permFile = 0640
40-
permFolder = 0750
41+
permFile = 0o640
42+
permFolder = 0o750
43+
4144
pluginAPInet = "apinet"
4245

4346
labelLibvirtProviderHostname = "libvirt-provider/hostname"
4447
)
4548

4649
type Plugin struct {
47-
nodeName string
48-
host providerhost.LibvirtHost
49-
apinetClient client.Client
50-
enableCleanup bool
50+
nodeName string
51+
host providerhost.LibvirtHost
52+
apinetClient client.Client
53+
pollingInterval time.Duration
54+
pollingDuration time.Duration
55+
enableCleanup bool
5156
}
5257

53-
func NewPlugin(nodeName string, client client.Client, cleanup bool) providernetworkinterface.Plugin {
58+
func NewPlugin(nodeName string, client client.Client, duration, interval time.Duration, cleanup bool) providernetworkinterface.Plugin {
5459
return &Plugin{
55-
nodeName: nodeName,
56-
apinetClient: client,
57-
enableCleanup: cleanup,
60+
nodeName: nodeName,
61+
apinetClient: client,
62+
pollingDuration: duration,
63+
pollingInterval: interval,
64+
enableCleanup: cleanup,
5865
}
5966
}
6067

@@ -150,11 +157,11 @@ type apiNetNetworkInterfaceConfig struct {
150157
Namespace string `json:"namespace"`
151158
}
152159

153-
func (p *Plugin) apiNetNetworkInterfaceConfigFile(machineID string, networkInterfaceName string) string {
160+
func (p *Plugin) apiNetNetworkInterfaceConfigFile(machineID, networkInterfaceName string) string {
154161
return filepath.Join(p.host.MachineNetworkInterfaceDir(machineID, networkInterfaceName), defaultAPINetConfigFile)
155162
}
156163

157-
func (p *Plugin) writeAPINetNetworkInterfaceConfig(machineID string, networkInterfaceName string, cfg *apiNetNetworkInterfaceConfig) error {
164+
func (p *Plugin) writeAPINetNetworkInterfaceConfig(machineID, networkInterfaceName string, cfg *apiNetNetworkInterfaceConfig) error {
158165
data, err := json.Marshal(cfg)
159166
if err != nil {
160167
return err
@@ -163,7 +170,7 @@ func (p *Plugin) writeAPINetNetworkInterfaceConfig(machineID string, networkInte
163170
return os.WriteFile(p.apiNetNetworkInterfaceConfigFile(machineID, networkInterfaceName), data, permFile)
164171
}
165172

166-
func (p *Plugin) readAPINetNetworkInterfaceConfig(machineID string, networkInterfaceName string) (*apiNetNetworkInterfaceConfig, error) {
173+
func (p *Plugin) readAPINetNetworkInterfaceConfig(machineID, networkInterfaceName string) (*apiNetNetworkInterfaceConfig, error) {
167174
data, err := os.ReadFile(p.apiNetNetworkInterfaceConfigFile(machineID, networkInterfaceName))
168175
if err != nil {
169176
return nil, err
@@ -176,7 +183,7 @@ func (p *Plugin) readAPINetNetworkInterfaceConfig(machineID string, networkInter
176183
return cfg, nil
177184
}
178185

179-
func (p *Plugin) APInetNicName(machineID string, networkInterfaceName string) string {
186+
func (p *Plugin) APInetNicName(machineID, networkInterfaceName string) string {
180187
return uuid.NewHash(sha256.New(), uuid.Nil, []byte(fmt.Sprintf("%s/%s", machineID, networkInterfaceName)), 5).String()
181188
}
182189

@@ -258,7 +265,7 @@ func (p *Plugin) Apply(ctx context.Context, spec *api.NetworkInterfaceSpec, mach
258265

259266
log.V(1).Info("Waiting for apinet network interface to become ready")
260267
apinetNicKey := client.ObjectKeyFromObject(apinetNic)
261-
if err := wait.PollUntilContextTimeout(ctx, 50*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (done bool, err error) {
268+
if err := wait.PollUntilContextTimeout(ctx, p.pollingInterval, p.pollingDuration, true, func(ctx context.Context) (done bool, err error) {
262269
if err := p.apinetClient.Get(ctx, apinetNicKey, apinetNic); err != nil {
263270
return false, fmt.Errorf("error getting apinet nic %s: %w", apinetNicKey, err)
264271
}
@@ -341,7 +348,7 @@ func getHostDevice(apinetNic *apinetv1alpha1.NetworkInterface) (*providernetwork
341348
}
342349
}
343350

344-
func (p *Plugin) Delete(ctx context.Context, computeNicName string, machineID string) error {
351+
func (p *Plugin) Delete(ctx context.Context, computeNicName, machineID string) error {
345352
log := ctrl.LoggerFrom(ctx)
346353

347354
log.V(1).Info("Reading APINet network interface config file")

0 commit comments

Comments
 (0)