Skip to content

Commit

Permalink
deps: Adapt code
Browse files Browse the repository at this point in the history
Signed-off-by: Enrique Llorente <[email protected]>
  • Loading branch information
qinqon committed Feb 13, 2025
1 parent c507105 commit ca88f7e
Show file tree
Hide file tree
Showing 28 changed files with 207 additions and 82 deletions.
16 changes: 8 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linters-settings:
lines: 100
statements: 50
gci:
local-prefixes: github.com/nmstate/nmpolicy
prefix: github.com/nmstate/nmpolicy
goconst:
min-len: 2
min-occurrences: 3
Expand All @@ -32,16 +32,16 @@ linters-settings:
goheader:
template-path: hack/header.tpl
goimports:
local-prefixes: github.com/nmstate/nmpolicy
gomnd:
prefix: github.com/nmstate/nmpolicy
mnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
govet:
check-shadowing: true
shadow: true
lll:
line-length: 140
maligned:
Expand All @@ -61,13 +61,13 @@ issues:
# Exclude some linters from running on tests files.
- path: test/
linters:
- gomnd
- mnd
- dupl
- gocritic
- path: _test.go
linters:
- unparam
- gomnd
- mnd
- dupl
- gocritic

Expand All @@ -78,7 +78,7 @@ linters:
- dogsled
- dupl
#- errcheck
- exportloopref
- copyloopvar
- exhaustive
- funlen
#- gochecknoinits
Expand All @@ -88,7 +88,7 @@ linters:
- gofmt
- goheader
- goimports
- gomnd
- mnd
- goprintffuncname
- gosec
- gosimple
Expand Down
17 changes: 17 additions & 0 deletions api/shared/nodenetworkstate_types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shared

import (
Expand Down
21 changes: 19 additions & 2 deletions api/shared/nodenetworkstate_util.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shared

// custom marshaling/unmarshaling that will allow to populate nmstate state
// as play yaml without the need to generate a golang struct following [1]
//
Expand All @@ -6,8 +25,6 @@
//
// [1] https://github.com/nmstate/nmstate/blob/base/libnmstate/schemas/operational-state.yaml

package shared

import (
yaml "sigs.k8s.io/yaml"
)
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha1/nodenetworkstate_types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
Expand Down
1 change: 1 addition & 0 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 1 addition & 0 deletions api/v1beta1/nmstate_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
17 changes: 17 additions & 0 deletions api/v1beta1/nodenetworkconfigurationpolicy_types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
Expand Down
17 changes: 17 additions & 0 deletions api/v1beta1/nodenetworkstate_types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
Copyright The Kubernetes NMState Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
Expand Down
8 changes: 5 additions & 3 deletions automation/check-patch.setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ mkdir -p $tmp_dir

if gimme --help > /dev/null 2>&1; then

go_mod_version=$(grep '^go' go.mod |sed 's/go //')
full_version=$(curl -s https://go.dev/dl/?mode=json |jq -r .[].version |grep "$go_mod_version")
export GIMME_GO_VERSION=$(echo $full_version |sed 's/go//')
version=$(grep '^go' go.mod |sed 's/go //')
if [ "$(echo $version | grep "[0-9]*\.[0-9]*\.[0-9]*$")" == "" ] ; then
version=$(curl -s https://go.dev/dl/?mode=json |jq -r .[].version |grep "$go_mod_version")
fi
export GIMME_GO_VERSION=$(echo $version |sed 's/go//')
echo "Installing go $GIMME_GO_VERSION with gimme"
eval "$(gimme)"
else
Expand Down
3 changes: 1 addition & 2 deletions build/install-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ os=linux
go_mod_version=$1

dnf install -y jq
full_version=$(curl -s https://go.dev/dl/?mode=json |jq -r .[].version |grep "$go_mod_version")

case $(uname -m) in
x86_64) arch="amd64";;
Expand All @@ -18,6 +17,6 @@ if [ "$arch" == "" ]; then
exit 1
fi

tarball_url="https://go.dev/dl/${full_version}.${os}-${arch}.tar.gz"
tarball_url="https://go.dev/dl/go${version}.${os}-${arch}.tar.gz"

curl --retry 10 -L $tarball_url | tar -C /usr/local -zxf -
13 changes: 8 additions & 5 deletions cmd/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

// +kubebuilder:scaffold:imports

Expand Down Expand Up @@ -128,8 +129,10 @@ func mainHandler() int {
setupLog.Info("Successfully took nmstate exclusive lock")
}
ctrlOptions := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: ":8089", // Explicitly enable metrics
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: ":8089", // Explicitly enable metrics
},
}

if environment.IsHandler() {
Expand Down Expand Up @@ -187,8 +190,8 @@ func cacheResourcesOnNodes(ctrlOptions *ctrl.Options) {
nodeName := environment.NodeName()
metadataNameMatchingNodeNameSelector := fields.Set{"metadata.name": nodeName}.AsSelector()
nodeLabelMatchingNodeNameSelector := labels.Set{nmstateapi.EnactmentNodeLabel: nodeName}.AsSelector()
ctrlOptions.NewCache = cache.BuilderWithOptions(cache.Options{
SelectorsByObject: cache.SelectorsByObject{
ctrlOptions.Cache = cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Node{}: {
Field: metadataNameMatchingNodeNameSelector,
},
Expand All @@ -199,7 +202,7 @@ func cacheResourcesOnNodes(ctrlOptions *ctrl.Options) {
Label: nodeLabelMatchingNodeNameSelector,
},
},
})
}
}

func setupHandlerControllers(mgr manager.Manager) error {
Expand Down
7 changes: 5 additions & 2 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

// +kubebuilder:scaffold:imports

Expand Down Expand Up @@ -85,8 +86,10 @@ func main() {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opt)))

ctrlOptions := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: "0", // disable metrics
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: "0", // disable metrics
},
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrlOptions)
Expand Down
37 changes: 20 additions & 17 deletions controllers/handler/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,35 +158,35 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {

// By default all this functors return true so controller watch all events,
// but we only want to watch create/delete for current node.
onCreationForThisNode := predicate.Funcs{
CreateFunc: func(createEvent event.CreateEvent) bool {
onCreationForThisNode := predicate.TypedFuncs[*corev1.Node]{
CreateFunc: func(createEvent event.TypedCreateEvent[*corev1.Node]) bool {
return node.EventIsForThisNode(createEvent.Object)
},
DeleteFunc: func(event.DeleteEvent) bool {
DeleteFunc: func(event.TypedDeleteEvent[*corev1.Node]) bool {
return false
},
UpdateFunc: func(event.UpdateEvent) bool {
UpdateFunc: func(event.TypedUpdateEvent[*corev1.Node]) bool {
return false
},
GenericFunc: func(event.GenericEvent) bool {
GenericFunc: func(event.TypedGenericEvent[*corev1.Node]) bool {
return false
},
}

// By default all this functors return true so controller watch all events,
// but we only want to watch delete/update for current node.
onDeleteOrForceUpdateForThisNode := predicate.Funcs{
CreateFunc: func(event.CreateEvent) bool {
onDeleteOrForceUpdateForThisNode := predicate.TypedFuncs[*nmstatev1beta1.NodeNetworkState]{
CreateFunc: func(createEvent event.TypedCreateEvent[*nmstatev1beta1.NodeNetworkState]) bool {
return false
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
DeleteFunc: func(deleteEvent event.TypedDeleteEvent[*nmstatev1beta1.NodeNetworkState]) bool {
return node.EventIsForThisNode(deleteEvent.Object)
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
UpdateFunc: func(updateEvent event.TypedUpdateEvent[*nmstatev1beta1.NodeNetworkState]) bool {
return node.EventIsForThisNode(updateEvent.ObjectNew) &&
shouldForceRefresh(updateEvent)
},
GenericFunc: func(event.GenericEvent) bool {
GenericFunc: func(event.TypedGenericEvent[*nmstatev1beta1.NodeNetworkState]) bool {
return false
},
}
Expand All @@ -198,19 +198,22 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {

// Add watch for Node
err = c.Watch(
&source.Kind{Type: &corev1.Node{}},
&handler.EnqueueRequestForObject{},
onCreationForThisNode,
source.Kind(mgr.GetCache(), &corev1.Node{},
&handler.TypedEnqueueRequestForObject[*corev1.Node]{},
onCreationForThisNode,
),
)
if err != nil {
return errors.Wrap(err, "failed to add watch for Nodes")
}

// Add watch for NNS
err = c.Watch(
&source.Kind{Type: &nmstatev1beta1.NodeNetworkState{}},
&handler.EnqueueRequestForOwner{OwnerType: &corev1.Node{}},
onDeleteOrForceUpdateForThisNode,
source.Kind(mgr.GetCache(),
&nmstatev1beta1.NodeNetworkState{},
handler.TypedEnqueueRequestForOwner[*nmstatev1beta1.NodeNetworkState](mgr.GetScheme(), mgr.GetRESTMapper(), &corev1.Node{}),
onDeleteOrForceUpdateForThisNode,
),
)
if err != nil {
return errors.Wrap(err, "failed to add watch for NNSes")
Expand All @@ -219,7 +222,7 @@ func (r *NodeReconciler) SetupWithManager(mgr ctrl.Manager) error {
return nil
}

func shouldForceRefresh(updateEvent event.UpdateEvent) bool {
func shouldForceRefresh(updateEvent event.TypedUpdateEvent[*nmstatev1beta1.NodeNetworkState]) bool {
newForceRefresh, hasForceRefreshNow := updateEvent.ObjectNew.GetLabels()[forceRefreshLabel]
if !hasForceRefreshNow {
return false
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 @@ -82,7 +82,7 @@ var _ = Describe("Node controller reconcile", func() {
objs := []runtime.Object{&node, &nodenetworkstate}

// Create a fake client to mock API calls.
cl = fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()
cl = fake.NewClientBuilder().WithScheme(s).WithStatusSubresource(&nodenetworkstate).WithRuntimeObjects(objs...).Build()

reconciler.Client = cl
reconciler.Log = ctrl.Log.WithName("controllers").WithName("Node")
Expand Down
Loading

0 comments on commit ca88f7e

Please sign in to comment.