• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

k8snetworkplumbingwg / sriov-network-operator / 3751025296

pending completion
3751025296

Pull #365

github

GitHub
Merge 421284b55 into 788d76f7e
Pull Request #365: Implementation for new systemd configuration method

958 of 958 new or added lines in 18 files covered. (100.0%)

1971 of 8330 relevant lines covered (23.66%)

0.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.33
/controllers/sriovibnetwork_controller.go
1
/*
2
Copyright 2021.
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 controllers
18

19
import (
20
        "context"
21
        "reflect"
22

23
        netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
24
        "k8s.io/apimachinery/pkg/api/errors"
25
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
        "k8s.io/apimachinery/pkg/runtime"
27
        "k8s.io/apimachinery/pkg/types"
28
        ctrl "sigs.k8s.io/controller-runtime"
29
        "sigs.k8s.io/controller-runtime/pkg/client"
30
        "sigs.k8s.io/controller-runtime/pkg/handler"
31
        "sigs.k8s.io/controller-runtime/pkg/log"
32
        "sigs.k8s.io/controller-runtime/pkg/reconcile"
33
        "sigs.k8s.io/controller-runtime/pkg/source"
34

35
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
36
)
37

38
// SriovIBNetworkReconciler reconciles a SriovIBNetwork object
39
type SriovIBNetworkReconciler struct {
40
        client.Client
41
        Scheme *runtime.Scheme
42
}
43

44
//+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovibnetworks,verbs=get;list;watch;create;update;patch;delete
45
//+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovibnetworks/status,verbs=get;update;patch
46
//+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovibnetworks/finalizers,verbs=update
47

48
// Reconcile is part of the main kubernetes reconciliation loop which aims to
49
// move the current state of the cluster closer to the desired state.
50
// TODO(user): Modify the Reconcile function to compare the state specified by
51
// the SriovIBNetwork object against the actual cluster state, and then
52
// perform operations to make the cluster state reflect the state specified by
53
// the user.
54
//
55
// For more details, check Reconcile and its Result here:
56
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
57
func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
1✔
58
        // The SriovNetwork CR shall only be defined in operator namespace.
1✔
59
        req.Namespace = namespace
1✔
60
        reqLogger := log.FromContext(ctx).WithValues("sriovnetwork", req.NamespacedName)
1✔
61
        reqLogger.Info("Reconciling SriovIBNetwork")
1✔
62
        var err error
1✔
63

1✔
64
        // Fetch the SriovNetwork instance
1✔
65
        instance := &sriovnetworkv1.SriovIBNetwork{}
1✔
66
        err = r.Get(context.TODO(), req.NamespacedName, instance)
1✔
67
        if err != nil {
2✔
68
                if errors.IsNotFound(err) {
2✔
69
                        // Request object not found, could have been deleted after reconcile request.
1✔
70
                        // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
1✔
71
                        // Return and don't requeue
1✔
72
                        return reconcile.Result{}, nil
1✔
73
                }
1✔
74
                // Error reading the object - requeue the request.
75
                return reconcile.Result{}, err
×
76
        }
77
        // name of our custom finalizer
78

79
        // examine DeletionTimestamp to determine if object is under deletion
80
        if instance.ObjectMeta.DeletionTimestamp.IsZero() {
2✔
81
                // The object is not being deleted, so if it does not have our finalizer,
1✔
82
                // then lets add the finalizer and update the object. This is equivalent
1✔
83
                // registering our finalizer.
1✔
84
                if !sriovnetworkv1.StringInArray(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers) {
2✔
85
                        instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, sriovnetworkv1.NETATTDEFFINALIZERNAME)
1✔
86
                        if err := r.Update(context.Background(), instance); err != nil {
1✔
87
                                return reconcile.Result{}, err
×
88
                        }
×
89
                }
90
        } else {
1✔
91
                // The object is being deleted
1✔
92
                if sriovnetworkv1.StringInArray(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers) {
2✔
93
                        // our finalizer is present, so lets handle any external dependency
1✔
94
                        reqLogger.Info("delete NetworkAttachmentDefinition CR", "Namespace", instance.Spec.NetworkNamespace, "Name", instance.Name)
1✔
95
                        if err := instance.DeleteNetAttDef(r.Client); err != nil {
1✔
96
                                // if fail to delete the external dependency here, return with error
×
97
                                // so that it can be retried
×
98
                                return reconcile.Result{}, err
×
99
                        }
×
100
                        // remove our finalizer from the list and update it.
101
                        var found bool
1✔
102
                        instance.ObjectMeta.Finalizers, found = sriovnetworkv1.RemoveString(sriovnetworkv1.NETATTDEFFINALIZERNAME, instance.ObjectMeta.Finalizers)
1✔
103
                        if found {
2✔
104
                                if err := r.Update(context.Background(), instance); err != nil {
1✔
105
                                        return reconcile.Result{}, err
×
106
                                }
×
107
                        }
108
                }
109
                return reconcile.Result{}, err
1✔
110
        }
111
        raw, err := instance.RenderNetAttDef()
1✔
112
        if err != nil {
1✔
113
                return reconcile.Result{}, err
×
114
        }
×
115
        netAttDef := &netattdefv1.NetworkAttachmentDefinition{}
1✔
116
        err = r.Scheme.Convert(raw, netAttDef, context.TODO())
1✔
117
        if err != nil {
1✔
118
                return reconcile.Result{}, err
×
119
        }
×
120
        if lnns, ok := instance.GetAnnotations()[sriovnetworkv1.LASTNETWORKNAMESPACE]; ok && netAttDef.GetNamespace() != lnns {
1✔
121
                err = r.Delete(context.TODO(), &netattdefv1.NetworkAttachmentDefinition{
×
122
                        ObjectMeta: metav1.ObjectMeta{
×
123
                                Name:      instance.GetName(),
×
124
                                Namespace: lnns,
×
125
                        },
×
126
                })
×
127
                if err != nil {
×
128
                        reqLogger.Error(err, "Couldn't delete NetworkAttachmentDefinition CR", "Namespace", instance.GetName(), "Name", lnns)
×
129
                        return reconcile.Result{}, err
×
130
                }
×
131
        }
132
        // Check if this NetworkAttachmentDefinition already exists
133
        found := &netattdefv1.NetworkAttachmentDefinition{}
1✔
134

1✔
135
        err = r.Get(context.TODO(), types.NamespacedName{Name: netAttDef.Name, Namespace: netAttDef.Namespace}, found)
1✔
136
        if err != nil {
2✔
137
                if errors.IsNotFound(err) {
2✔
138
                        reqLogger.Info("NetworkAttachmentDefinition CR not exist, creating")
1✔
139
                        err = r.Create(context.TODO(), netAttDef)
1✔
140
                        if err != nil {
1✔
141
                                reqLogger.Error(err, "Couldn't create NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
×
142
                                return reconcile.Result{}, err
×
143
                        }
×
144
                        anno := map[string]string{sriovnetworkv1.LASTNETWORKNAMESPACE: netAttDef.Namespace}
1✔
145
                        instance.SetAnnotations(anno)
1✔
146
                        if err := r.Update(context.Background(), instance); err != nil {
2✔
147
                                return reconcile.Result{}, err
1✔
148
                        }
1✔
149
                } else {
×
150
                        reqLogger.Error(err, "Couldn't get NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
×
151
                        return reconcile.Result{}, err
×
152
                }
×
153
        } else {
1✔
154
                reqLogger.Info("NetworkAttachmentDefinition CR already exist")
1✔
155
                if !reflect.DeepEqual(found.Spec, netAttDef.Spec) || !reflect.DeepEqual(found.GetAnnotations(), netAttDef.GetAnnotations()) {
2✔
156
                        reqLogger.Info("Update NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
1✔
157
                        netAttDef.SetResourceVersion(found.GetResourceVersion())
1✔
158
                        err = r.Update(context.TODO(), netAttDef)
1✔
159
                        if err != nil {
1✔
160
                                reqLogger.Error(err, "Couldn't update NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
×
161
                                return reconcile.Result{}, err
×
162
                        }
×
163
                }
164
        }
165
        return ctrl.Result{}, nil
1✔
166
}
167

168
// SetupWithManager sets up the controller with the Manager.
169
func (r *SriovIBNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
170
        return ctrl.NewControllerManagedBy(mgr).
1✔
171
                For(&sriovnetworkv1.SriovIBNetwork{}).
1✔
172
                Watches(&source.Kind{Type: &netattdefv1.NetworkAttachmentDefinition{}}, &handler.EnqueueRequestForObject{}).
1✔
173
                Complete(r)
1✔
174
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc