• 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.0
/controllers/sriovnetwork_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

29
        ctrl "sigs.k8s.io/controller-runtime"
30
        "sigs.k8s.io/controller-runtime/pkg/client"
31
        "sigs.k8s.io/controller-runtime/pkg/handler"
32
        "sigs.k8s.io/controller-runtime/pkg/log"
33
        "sigs.k8s.io/controller-runtime/pkg/reconcile"
34
        "sigs.k8s.io/controller-runtime/pkg/source"
35

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

39
// SriovNetworkReconciler reconciles a SriovNetwork object
40
type SriovNetworkReconciler struct {
41
        client.Client
42
        Scheme *runtime.Scheme
43
}
44

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

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

1✔
62
        reqLogger.Info("Reconciling SriovNetwork")
1✔
63
        var err error
1✔
64

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

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

166
        return ctrl.Result{}, nil
1✔
167
}
168

169
// SetupWithManager sets up the controller with the Manager.
170
func (r *SriovNetworkReconciler) SetupWithManager(mgr ctrl.Manager) error {
1✔
171
        return ctrl.NewControllerManagedBy(mgr).
1✔
172
                For(&sriovnetworkv1.SriovNetwork{}).
1✔
173
                Watches(&source.Kind{Type: &netattdefv1.NetworkAttachmentDefinition{}}, &handler.EnqueueRequestForObject{}).
1✔
174
                Complete(r)
1✔
175
}
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