Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dig with plain maps #5403

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/go-playground/validator/v10 v10.23.0
github.com/google/go-cmp v0.6.0
github.com/k0sproject/bootloose v0.9.0
github.com/k0sproject/dig v0.3.1
github.com/k0sproject/version v0.6.0
github.com/kardianos/service v1.2.2
github.com/logrusorgru/aurora/v3 v3.0.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0sproject/bootloose v0.9.0 h1:Ae45gF0jfS7ND6z1EotWs4qWAwqTRahvET91Y2+GMwo=
github.com/k0sproject/bootloose v0.9.0/go.mod h1:4NkBMQoJ5ZZCGNWjiiEBOxn4ciEo1mBQVOmCYYglGmM=
github.com/k0sproject/dig v0.3.1 h1:/QK40lXQ/HEE3LMT3r/kST1ANhMVZiajNDXI+spbL9o=
github.com/k0sproject/dig v0.3.1/go.mod h1:rlZ7N7ZEcB4Fi96TPXkZ4dqyAiDWOGLapyL9YpZ7Qz4=
github.com/k0sproject/version v0.6.0 h1:Wi8wu9j+H36+okIQA47o/YHbzNpKeIYj8IjGdJOdqsI=
github.com/k0sproject/version v0.6.0/go.mod h1:5/7Js62gDCLBP6mEs0mUcYEEkYneM5qXDKN/hyFlQTM=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
Expand Down
38 changes: 17 additions & 21 deletions pkg/component/controller/kuberouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (
"fmt"
"testing"

"github.com/k0sproject/dig"
"github.com/k0sproject/k0s/internal/testutil"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
"github.com/k0sproject/k0s/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -73,9 +72,9 @@ func TestKubeRouterConfig(t *testing.T) {

p, err := getKubeRouterPlugin(cm, "bridge")
require.NoError(t, err)
require.InEpsilon(t, 1450, p.Dig("mtu"), 0)
require.Equal(t, true, p.Dig("hairpinMode"))
require.Equal(t, true, p.Dig("ipMasq"))
assert.InEpsilon(t, 1450, p["mtu"], 0)
assert.Equal(t, true, p["hairpinMode"])
assert.Equal(t, true, p["ipMasq"])
}

type hairpinTest struct {
Expand Down Expand Up @@ -155,9 +154,9 @@ func TestKubeRouterDefaultManifests(t *testing.T) {

p, err := getKubeRouterPlugin(cm, "bridge")
require.NoError(t, err)
require.Nil(t, p.Dig("mtu"))
require.Equal(t, true, p.Dig("hairpinMode"))
require.Equal(t, false, p.Dig("ipMasq"))
assert.NotContains(t, p, "mtu")
assert.Equal(t, true, p["hairpinMode"])
assert.Equal(t, false, p["ipMasq"])
}

func TestKubeRouterManualMTUManifests(t *testing.T) {
Expand Down Expand Up @@ -191,7 +190,7 @@ func TestKubeRouterManualMTUManifests(t *testing.T) {

p, err := getKubeRouterPlugin(cm, "bridge")
require.NoError(t, err)
require.InEpsilon(t, 1234, p.Dig("mtu"), 0)
assert.InEpsilon(t, 1234, p["mtu"], 0)
}

func TestExtraArgs(t *testing.T) {
Expand Down Expand Up @@ -242,28 +241,25 @@ func findConfig(resources []*unstructured.Unstructured) (corev1.ConfigMap, error
return cm, errors.New("kube-router cm not found in manifests")
}

func getKubeRouterPlugin(cm corev1.ConfigMap, pluginType string) (dig.Mapping, error) {
data := dig.Mapping{}
func getKubeRouterPlugin(cm corev1.ConfigMap, pluginType string) (map[string]any, error) {
var data map[string]any
err := json.Unmarshal([]byte(cm.Data["cni-conf.json"]), &data)
if err != nil {
return data, err
}
plugins, ok := data.Dig("plugins").([]interface{})
if !ok {
return data, errors.New("failed to dig plugins")
}
for _, p := range plugins {
plugin, ok := p.(dig.Mapping)
if ok && plugin.DigString("type") == pluginType {
return plugin, nil
if plugins, ok := data["plugins"].([]any); ok {
for _, plugin := range plugins {
if p, ok := plugin.(map[string]any); ok && p["type"] == pluginType {
return p, nil
}
}
}

return data, fmt.Errorf("failed to find plugin of type %s", pluginType)
}

func findDaemonset(resources []*unstructured.Unstructured) (v1.DaemonSet, error) {
var ds v1.DaemonSet
func findDaemonset(resources []*unstructured.Unstructured) (appsv1.DaemonSet, error) {
var ds appsv1.DaemonSet
for _, r := range resources {
if r.GetKind() == "DaemonSet" {
err := runtime.DefaultUnstructuredConverter.FromUnstructured(r.Object, &ds)
Expand Down
Loading