Skip to content

Commit

Permalink
Merge pull request #669 from SchSeba/remove_DP_from_nodestate_spec
Browse files Browse the repository at this point in the history
Remove DPversion from nodeState
  • Loading branch information
e0ne authored Apr 8, 2024
2 parents c03c62f + 3d72c77 commit 46062c3
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 106 deletions.
3 changes: 1 addition & 2 deletions api/v1/sriovnetworknodestate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (

// SriovNetworkNodeStateSpec defines the desired state of SriovNetworkNodeState
type SriovNetworkNodeStateSpec struct {
DpConfigVersion string `json:"dpConfigVersion,omitempty"`
Interfaces Interfaces `json:"interfaces,omitempty"`
Interfaces Interfaces `json:"interfaces,omitempty"`
}

type Interfaces []Interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ spec:
spec:
description: SriovNetworkNodeStateSpec defines the desired state of SriovNetworkNodeState
properties:
dpConfigVersion:
type: string
interfaces:
items:
properties:
Expand Down
14 changes: 8 additions & 6 deletions controllers/sriovnetworknodepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import (
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/featuregate"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)

Expand Down Expand Up @@ -260,7 +259,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncAllSriovNetworkNodeStates(ctx con
ns.Namespace = vars.Namespace
j, _ := json.Marshal(ns)
logger.V(2).Info("SriovNetworkNodeState CR", "content", j)
if err := r.syncSriovNetworkNodeState(ctx, dc, npl, ns, &node, utils.HashConfigMap(found)); err != nil {
if err := r.syncSriovNetworkNodeState(ctx, dc, npl, ns, &node); err != nil {
logger.Error(err, "Fail to sync", "SriovNetworkNodeState", ns.Name)
return err
}
Expand Down Expand Up @@ -294,9 +293,13 @@ func (r *SriovNetworkNodePolicyReconciler) syncAllSriovNetworkNodeStates(ctx con
return nil
}

func (r *SriovNetworkNodePolicyReconciler) syncSriovNetworkNodeState(ctx context.Context, dc *sriovnetworkv1.SriovOperatorConfig, npl *sriovnetworkv1.SriovNetworkNodePolicyList, ns *sriovnetworkv1.SriovNetworkNodeState, node *corev1.Node, cksum string) error {
func (r *SriovNetworkNodePolicyReconciler) syncSriovNetworkNodeState(ctx context.Context,
dc *sriovnetworkv1.SriovOperatorConfig,
npl *sriovnetworkv1.SriovNetworkNodePolicyList,
ns *sriovnetworkv1.SriovNetworkNodeState,
node *corev1.Node) error {
logger := log.Log.WithName("syncSriovNetworkNodeState")
logger.V(1).Info("Start to sync SriovNetworkNodeState", "Name", ns.Name, "cksum", cksum)
logger.V(1).Info("Start to sync SriovNetworkNodeState", "Name", ns.Name)

if err := controllerutil.SetControllerReference(dc, ns, r.Scheme); err != nil {
return err
Expand All @@ -306,7 +309,6 @@ func (r *SriovNetworkNodePolicyReconciler) syncSriovNetworkNodeState(ctx context
if err != nil {
logger.Error(err, "Fail to get SriovNetworkNodeState", "namespace", ns.Namespace, "name", ns.Name)
if errors.IsNotFound(err) {
ns.Spec.DpConfigVersion = cksum
err = r.Create(ctx, ns)
if err != nil {
return fmt.Errorf("couldn't create SriovNetworkNodeState: %v", err)
Expand Down Expand Up @@ -351,7 +353,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncSriovNetworkNodeState(ctx context
ppp = p.Spec.Priority
}
}
newVersion.Spec.DpConfigVersion = cksum

// Note(adrianc): we check same ownerReferences since SriovNetworkNodeState
// was owned by a default SriovNetworkNodePolicy. if we encounter a descripancy
// we need to update.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ spec:
spec:
description: SriovNetworkNodeStateSpec defines the desired state of SriovNetworkNodeState
properties:
dpConfigVersion:
type: string
interfaces:
items:
properties:
Expand Down
3 changes: 0 additions & 3 deletions pkg/systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ func ReadConfFile() (spec *SriovConfig, err error) {

func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error) {
newFile := false
// remove the device plugin revision as we don't need it here
newState.Spec.DpConfigVersion = ""

sriovConfig := &SriovConfig{
newState.Spec,
vars.DevMode,
Expand Down
20 changes: 0 additions & 20 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package utils

import (
"bytes"
"encoding/hex"
"fmt"
"hash/fnv"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"sort"
"syscall"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
Expand Down Expand Up @@ -82,22 +78,6 @@ func GenerateRandomGUID() net.HardwareAddr {
return guid
}

func HashConfigMap(cm *corev1.ConfigMap) string {
var keys []string
for k := range cm.Data {
keys = append(keys, k)
}
sort.Strings(keys)

hash := fnv.New128()
for _, k := range keys {
hash.Write([]byte(k))
hash.Write([]byte(cm.Data[k]))
}
hashed := hash.Sum(nil)
return hex.EncodeToString(hashed)
}

func IsCommandNotFound(err error) bool {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok && status.ExitStatus() == 127 {
Expand Down
71 changes: 0 additions & 71 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,8 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

utils "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
)

var _ = Describe("HashConfigMap", func() {
It("should hash the ConfigMap correctly", func() {
data := make(map[string]string)
data["key1"] = "value1"
data["key2"] = "value2"
cm := &corev1.ConfigMap{
Data: data,
}

expectedHash := "7cb7a94f45100d7dc8aadffbcd409f25"

actualHash := utils.HashConfigMap(cm)

Expect(actualHash).To(Equal(expectedHash))
})

It("Should not change hash for different resource versions", func() {
data := make(map[string]string)
data["key1"] = "value1"
data["key2"] = "value2"

cm1 := &corev1.ConfigMap{
Data: data,
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "68790",
},
}

cm2 := &corev1.ConfigMap{
Data: data,
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "69889",
},
}

hash1 := utils.HashConfigMap(cm1)
hash2 := utils.HashConfigMap(cm2)

Expect(hash1).To(Equal(hash2))
})

It("should not change hash for different key orderings", func() {
data1 := map[string]string{}
data1["key1"] = "value1"
data1["key2"] = "value2"
data2 := map[string]string{}
data2["key1"] = "value1"
data2["key2"] = "value2"
// Collisions in the hashmap _can_ change the order of keys
data2["key2"] = "value2"

cm1 := &corev1.ConfigMap{
Data: data1,
}

cm2 := &corev1.ConfigMap{
Data: data2,
}

hash1 := utils.HashConfigMap(cm1)
hash2 := utils.HashConfigMap(cm2)

Expect(hash1).To(Equal(hash2))
})
})

func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils Suite")
Expand Down

0 comments on commit 46062c3

Please sign in to comment.