Skip to content

Commit

Permalink
Add NetworkManager and nmstate versions to NodeNetworkState Status (#834
Browse files Browse the repository at this point in the history
)

* Bump networkmanager-go to 0.3.0

Bump is needed to expose host NetworkManager version for NodeNetworkState

Signed-off-by: Tomas Psota <[email protected]>

* Add nm and nmstate version to nns Status

In NodeNetworkState Status, following fields are added:
- host NetworkManager version
- container NetworkManager version
- container nmstate version

Signed-off-by: Tomas Psota <[email protected]>
  • Loading branch information
tompsota authored Nov 22, 2021
1 parent 37d6a12 commit 06577e1
Show file tree
Hide file tree
Showing 64 changed files with 2,049 additions and 1,062 deletions.
7 changes: 5 additions & 2 deletions api/shared/nodenetworkstate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
// NodeNetworkStateStatus is the status of the NodeNetworkState of a specific node
type NodeNetworkStateStatus struct {
// +kubebuilder:validation:XPreserveUnknownFields
CurrentState State `json:"currentState,omitempty"`
LastSuccessfulUpdateTime metav1.Time `json:"lastSuccessfulUpdateTime,omitempty"`
CurrentState State `json:"currentState,omitempty"`
LastSuccessfulUpdateTime metav1.Time `json:"lastSuccessfulUpdateTime,omitempty"`
HostNetworkManagerVersion string `json:"hostNetworkManagerVersion,omitempty"`
HandlerNetworkManagerVersion string `json:"handlerNetworkManagerVersion,omitempty"`
HandlerNmstateVersion string `json:"handlerNmstateVersion,omitempty"`

Conditions ConditionList `json:"conditions,omitempty" optional:"true"`
}
Expand Down
43 changes: 41 additions & 2 deletions controllers/handler/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ import (
"github.com/nmstate/kubernetes-nmstate/pkg/nmstatectl"
"github.com/nmstate/kubernetes-nmstate/pkg/node"
"github.com/nmstate/kubernetes-nmstate/pkg/state"
networkmanager "github.com/phoracek/networkmanager-go/src"
corev1 "k8s.io/api/core/v1"
)

// Added for test purposes
type NmstateUpdater func(client client.Client, node *corev1.Node, observedState shared.State, nns *nmstatev1beta1.NodeNetworkState) error
type NmstateUpdater func(client client.Client, node *corev1.Node, observedState shared.State, nns *nmstatev1beta1.NodeNetworkState, versions *nmstate.DependencyVersions) error
type NmstatectlShow func() (string, error)

// NodeReconciler reconciles a Node object
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, request ctrl.Request) (c
// Error reading the object - requeue the request.
return ctrl.Result{}, err
}
err = r.nmstateUpdater(r.Client, nodeInstance, currentState, nnsInstance)
err = r.nmstateUpdater(r.Client, nodeInstance, currentState, nnsInstance, r.getDependencyVersions())
if err != nil {
err = errors.Wrap(err, "error at node reconcile creating NodeNetworkState")
return ctrl.Result{}, err
Expand All @@ -115,6 +116,44 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, request ctrl.Request) (c
return ctrl.Result{RequeueAfter: node.NetworkStateRefreshWithJitter()}, nil
}

func (r *NodeReconciler) getDependencyVersions() *nmstate.DependencyVersions {
handlerNetworkManagerVersion, err := nmstate.ExecuteCommand("NetworkManager", "--version")
if err != nil {
r.Log.Info("error retrieving handler NetworkManager version: %s", err.Error())
}

handlerNmstateVersion, err := nmstate.ExecuteCommand("nmstatectl", "--version")
if err != nil {
r.Log.Info("error retrieving handler nmstate version: %s", err.Error())
}

hostNmstateVersion := ""
nmClient, err := networkmanager.NewClientPrivate()

if err != nil {
r.Log.Info("error retrieving new client: %s", err.Error())

return &nmstate.DependencyVersions{
HandlerNetworkManagerVersion: handlerNetworkManagerVersion,
HandlerNmstateVersion: handlerNmstateVersion,
HostNmstateVersion: hostNmstateVersion,
}
}

defer nmClient.Close()

hostNmstateVersion, err = nmClient.GetVersion()
if err != nil {
r.Log.Error(err, "error retrieving host nmstate version")
}

return &nmstate.DependencyVersions{
HandlerNetworkManagerVersion: handlerNetworkManagerVersion,
HandlerNmstateVersion: handlerNmstateVersion,
HostNmstateVersion: hostNmstateVersion,
}
}

func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {

r.nmstateUpdater = nmstate.CreateOrUpdateNodeNetworkState
Expand Down
2 changes: 1 addition & 1 deletion controllers/handler/node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ routes:
reconciler.lastState = filteredOutObservedState

reconciler.nmstateUpdater = func(client.Client, *corev1.Node,
shared.State, *nmstatev1beta1.NodeNetworkState) error {
shared.State, *nmstatev1beta1.NodeNetworkState, *nmstate.DependencyVersions) error {
return fmt.Errorf("we are not suppose to catch this error")
}

Expand Down
12 changes: 12 additions & 0 deletions deploy/crds/nmstate.io_nodenetworkstates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ spec:
\n [1] https://github.com/nmstate/nmstate/blob/base/libnmstate/schemas/operational-state.yaml"
type: object
x-kubernetes-preserve-unknown-fields: true
handlerNetworkManagerVersion:
type: string
handlerNmstateVersion:
type: string
hostNetworkManagerVersion:
type: string
lastSuccessfulUpdateTime:
format: date-time
type: string
Expand Down Expand Up @@ -130,6 +136,12 @@ spec:
\n [1] https://github.com/nmstate/nmstate/blob/base/libnmstate/schemas/operational-state.yaml"
type: object
x-kubernetes-preserve-unknown-fields: true
handlerNetworkManagerVersion:
type: string
handlerNmstateVersion:
type: string
hostNetworkManagerVersion:
type: string
lastSuccessfulUpdateTime:
format: date-time
type: string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/opencontainers/runtime-spec v1.0.3-0.20200520003142-237cc4f519e2 // indirect
github.com/openshift/cluster-network-operator v0.0.0-20200922032245-f47200e8dbc0
github.com/operator-framework/operator-registry v1.17.0
github.com/phoracek/networkmanager-go v0.1.0
github.com/phoracek/networkmanager-go v0.3.0
github.com/pkg/errors v0.9.1
github.com/qinqon/kube-admission-webhook v0.17.0
github.com/spf13/pflag v1.0.5
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,13 @@ github.com/gobuffalo/packr/v2 v2.5.1/go.mod h1:8f9c96ITobJlPzI44jj+4tHnEKNt0xXWS
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/goccy/go-yaml v1.8.1/go.mod h1:wS4gNoLalDSJxo/SpngzPQ2BN4uuZVLCmbM4S3vd4+Y=
github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
github.com/godbus/dbus v4.1.0+incompatible/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.0.0-20190320160742-5135e617513b/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY=
Expand Down Expand Up @@ -1095,6 +1098,8 @@ github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoU
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
github.com/phoracek/networkmanager-go v0.1.0 h1:wEbl5Mt/8E82lfAzGokMcJSRFBugqdkR/wQGOeIv/0E=
github.com/phoracek/networkmanager-go v0.1.0/go.mod h1:VnyIk+hGRR+qg0dlnYrW6BqJuJWJ0vsoQJmANuwegDY=
github.com/phoracek/networkmanager-go v0.3.0 h1:B4vwwLwvepcyXhJAhQ62V4YT+mkDJt2G5+wTo18NHpQ=
github.com/phoracek/networkmanager-go v0.3.0/go.mod h1:7VSZu5Tgpg2ciGivl+RitmMTT3L2+fFth2WmOf9S3w0=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4/v4 v4.0.3/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
30 changes: 27 additions & 3 deletions pkg/helper/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package helper

import (
"bytes"
"context"
"fmt"
"os/exec"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -32,6 +34,12 @@ const (
DesiredStateConfigurationTimeout = (defaultGwProbeTimeout + apiServerProbeTimeout) * 2
)

type DependencyVersions struct {
HandlerNetworkManagerVersion string
HandlerNmstateVersion string
HostNmstateVersion string
}

func InitializeNodeNetworkState(client client.Client, node *corev1.Node) (*nmstatev1beta1.NodeNetworkState, error) {
ownerRefList := []metav1.OwnerReference{{Name: node.ObjectMeta.Name, Kind: "Node", APIVersion: "v1", UID: node.UID}}

Expand All @@ -52,24 +60,28 @@ func InitializeNodeNetworkState(client client.Client, node *corev1.Node) (*nmsta
return &nodeNetworkState, nil
}

func CreateOrUpdateNodeNetworkState(client client.Client, node *corev1.Node, observedState shared.State, nns *nmstatev1beta1.NodeNetworkState) error {
func CreateOrUpdateNodeNetworkState(client client.Client, node *corev1.Node, observedState shared.State, nns *nmstatev1beta1.NodeNetworkState, versions *DependencyVersions) error {
if nns == nil {
var err error
nns, err = InitializeNodeNetworkState(client, node)
if err != nil {
return err
}
}
return UpdateCurrentState(client, nns, observedState)
return UpdateCurrentState(client, nns, observedState, versions)
}

func UpdateCurrentState(client client.Client, nodeNetworkState *nmstatev1beta1.NodeNetworkState, observedState shared.State) error {
func UpdateCurrentState(client client.Client, nodeNetworkState *nmstatev1beta1.NodeNetworkState, observedState shared.State, versions *DependencyVersions) error {

if observedState.String() == nodeNetworkState.Status.CurrentState.String() {
log.Info("Skipping NodeNetworkState update, node network configuration not changed")
return nil
}

nodeNetworkState.Status.HandlerNetworkManagerVersion = versions.HandlerNetworkManagerVersion
nodeNetworkState.Status.HandlerNmstateVersion = versions.HandlerNmstateVersion
nodeNetworkState.Status.HostNetworkManagerVersion = versions.HostNmstateVersion

nodeNetworkState.Status.CurrentState = observedState
nodeNetworkState.Status.LastSuccessfulUpdateTime = metav1.Time{Time: time.Now()}

Expand All @@ -85,6 +97,18 @@ func UpdateCurrentState(client client.Client, nodeNetworkState *nmstatev1beta1.N
return nil
}

func ExecuteCommand(command string, arguments ...string) (string, error) {
cmd := exec.Command(command, arguments...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to execute %s: '%s', '%s', '%s'", command, err.Error(), stdout.String(), stderr.String())
}

return string(bytes.Trim(stdout.Bytes(), "\n")), nil
}

func rollback(client client.Client, probes []probe.Probe, cause error) error {
message := fmt.Sprintf("rolling back desired state configuration: %s", cause)
err := nmstatectl.Rollback()
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/handler/nns_dependencies_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package handler

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("[nns] NNS Dependencies", func() {
BeforeEach(func() {
// Make sure NNSes are present
for _, node := range nodes {
key := types.NamespacedName{Name: node}
_ = nodeNetworkState(key)
}
})

It("should include versions of NNS dependencies", func() {
for _, node := range nodes {
key := types.NamespacedName{Name: node}
status := nodeNetworkState(key).Status
Expect(status.HostNetworkManagerVersion).ToNot(BeEmpty())
Expect(status.HandlerNetworkManagerVersion).ToNot(BeEmpty())
Expect(status.HandlerNmstateVersion).ToNot(BeEmpty())
}
})
})
40 changes: 0 additions & 40 deletions vendor/github.com/godbus/dbus/.travis.yml

This file was deleted.

36 changes: 0 additions & 36 deletions vendor/github.com/godbus/dbus/call.go

This file was deleted.

Loading

0 comments on commit 06577e1

Please sign in to comment.