Skip to content

Commit b043eff

Browse files
committed
Introduce watch and cleanup of NICs
Signed-off-by: Lukas Frank <lukas.frank@sap.com>
1 parent 45e55ce commit b043eff

9 files changed

Lines changed: 255 additions & 111 deletions

File tree

cmd/libvirt-provider/app/app.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ func Run(ctx context.Context, opts Options) error {
286286
}
287287

288288
setupLog.Info("Initializing network interface plugin")
289-
290-
if err := nicPlugin.Init(providerHost); err != nil {
289+
if err := nicPlugin.Init(ctx, providerHost); err != nil {
291290
setupLog.Error(err, "failed to initialize network plugin")
292291
return err
293292
}

internal/controllers/controllers_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ var _ = BeforeSuite(func() {
184184
if cleanup != nil {
185185
DeferCleanup(cleanup)
186186
}
187-
Expect(networkPlugin.Init(providerHost)).To(Succeed())
187+
Expect(networkPlugin.Init(pluginCtx, providerHost)).To(Succeed())
188188

189189
By("setting up resource claimer")
190190
resClaimer, err = claim.NewResourceClaimer(

internal/controllers/machine_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ func (r *MachineReconciler) Start(ctx context.Context) error {
173173
},
174174
})
175175

176+
r.networkInterfacePlugin.AddEventHandler(providernetworkinterface.EventHandlerFuncs{
177+
HandleNICEventFunc: func(machineID string) {
178+
log.V(1).Info("NIC event: Requeue machine", "Machine", machineID)
179+
r.queue.Add(machineID)
180+
},
181+
})
182+
176183
imgEventReg, err := r.machineEvents.AddHandler(event.HandlerFunc[*api.Machine](func(evt event.Event[*api.Machine]) {
177184
r.queue.Add(evt.Object.ID)
178185
}))

internal/controllers/machine_controller_nics.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,28 @@ func (r *MachineReconciler) attachDetachNetworkInterfaces(
164164
mountedNic, err := r.reconcileDesiredNetworkInterface(ctx, machine, domain, mountedNics, desiredNic)
165165
if err != nil {
166166
errs = append(errs, fmt.Errorf("[network interface %s] error reconciling: %w", nicName, err))
167-
} else {
168-
log.V(1).Info("Successfully reconciled desired network interface", "NetworkInterfaceName", nicName)
169-
mountedNics[nicName] = *mountedNic
170-
nicStates = append(nicStates, api.NetworkInterfaceStatus{
171-
Name: nicName,
172-
Handle: mountedNic.networkInterface.Handle,
173-
State: api.NetworkInterfaceStateAttached,
174-
})
167+
continue
175168
}
169+
if mountedNic == nil {
170+
continue
171+
}
172+
log.V(1).Info("Successfully reconciled desired network interface", "NetworkInterfaceName", nicName)
173+
mountedNics[nicName] = *mountedNic
174+
nicStates = append(nicStates, api.NetworkInterfaceStatus{
175+
Name: nicName,
176+
Handle: mountedNic.networkInterface.Handle,
177+
State: api.NetworkInterfaceStateAttached,
178+
})
176179
}
177180

178181
for nicName, machineNic := range machineNicByName {
179182
if _, ok := mountedNics[nicName]; ok {
180183
continue
181184
}
185+
if _, ok := desiredNics[nicName]; ok {
186+
log.V(1).Info("Skipping not ready NICs", "NetworkInterfaceName", nicName)
187+
continue
188+
}
182189

183190
log.V(1).Info("Tearing down network interface", "NetworkInterfaceName", nicName)
184191
if err := r.deleteNetworkInterface(ctx, machine, machineNic); err != nil {
@@ -210,6 +217,9 @@ func (r *MachineReconciler) reconcileDesiredNetworkInterface(
210217
nic *api.NetworkInterfaceSpec,
211218
) (*mountedNetworkInterface, error) {
212219
providerNic, err := r.networkInterfacePlugin.Apply(ctx, nic, machine)
220+
if errors.Is(err, providernetworkinterface.ErrNotReady) {
221+
return nil, nil
222+
}
213223
if err != nil {
214224
return nil, err
215225
}

internal/networkinterfaceplugin/apinet.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"crypto/x509"
99
"crypto/x509/pkix"
1010
"fmt"
11-
"time"
1211

1312
apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
1413
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
@@ -21,7 +20,6 @@ import (
2120
"k8s.io/apimachinery/pkg/runtime"
2221
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2322
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
24-
"sigs.k8s.io/controller-runtime/pkg/client"
2523
)
2624

2725
var scheme = runtime.NewScheme()
@@ -32,9 +30,7 @@ func init() {
3230
}
3331

3432
type apinetOptions struct {
35-
APInetNodeName string
36-
PollingDuration time.Duration
37-
PollingInterval time.Duration
33+
APInetNodeName string
3834

3935
GetConfigOptions config.GetConfigOptions
4036
}
@@ -45,8 +41,6 @@ func (o *apinetOptions) PluginName() string {
4541

4642
func (o *apinetOptions) AddFlags(fs *pflag.FlagSet) {
4743
fs.StringVar(&o.APInetNodeName, "apinet-node-name", "", "APInet node name")
48-
fs.DurationVar(&o.PollingDuration, "apinet-polling-duration", 30*time.Second, "Duration to poll for apinet network interface readiness")
49-
fs.DurationVar(&o.PollingInterval, "apinet-polling-interval", 1*time.Second, "Interval between apinet network interface readiness polls")
5044
o.GetConfigOptions.BindFlags(fs, config.WithNamePrefix("apinet-"))
5145
}
5246

@@ -80,12 +74,7 @@ func (o *apinetOptions) NetworkInterfacePlugin(ctx context.Context) (
8074
return nil, nil, nil, fmt.Errorf("error getting apinet config: %w", err)
8175
}
8276

83-
apinetClient, err := client.New(cfg, client.Options{Scheme: scheme})
84-
if err != nil {
85-
return nil, nil, nil, fmt.Errorf("error creating apinet client: %w", err)
86-
}
87-
88-
return apinet.NewPlugin(o.APInetNodeName, apinetClient, o.PollingDuration, o.PollingInterval), configCtrl, nil, nil
77+
return apinet.NewPlugin(o.APInetNodeName, cfg), configCtrl, nil, nil
8978
}
9079

9180
func init() {

0 commit comments

Comments
 (0)