Skip to content

Commit

Permalink
Fix access to hostNetwork port on NodeIP when egress-selector-mode=agent
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Donohue <git@PaulSD.com>
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
PaulSD authored and brandond committed Feb 10, 2023
1 parent 058c6e2 commit 89cba26
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/agent/run.go
Expand Up @@ -344,13 +344,13 @@ func configureNode(ctx context.Context, nodeConfig *daemonconfig.Node, nodes typ
}

// inject node config
if changed, err := nodeconfig.SetNodeConfigAnnotations(node); err != nil {
if changed, err := nodeconfig.SetNodeConfigAnnotations(nodeConfig, node); err != nil {
return false, err
} else if changed {
updateNode = true
}

if changed, err := nodeconfig.SetNodeConfigLabels(node); err != nil {
if changed, err := nodeconfig.SetNodeConfigLabels(nodeConfig, node); err != nil {
return false, err
} else if changed {
updateNode = true
Expand Down
20 changes: 15 additions & 5 deletions pkg/nodeconfig/nodeconfig.go
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/k3s-io/k3s/pkg/configfilearg"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/version"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,7 +74,7 @@ func getNodeEnv() (string, error) {
// environment variables as annotations on the node object. It also stores a
// hash of the combined args + variables. These are used by other components
// to determine if the node configuration has been changed.
func SetNodeConfigAnnotations(node *corev1.Node) (bool, error) {
func SetNodeConfigAnnotations(nodeConfig *config.Node, node *corev1.Node) (bool, error) {
nodeArgs, err := getNodeArgs()
if err != nil {
return false, err
Expand Down Expand Up @@ -106,13 +107,22 @@ func SetNodeConfigAnnotations(node *corev1.Node) (bool, error) {
// that may not be present on down-level or up-level nodes.
// These labels are used by other components to determine whether
// or not a node supports particular functionality.
func SetNodeConfigLabels(node *corev1.Node) (bool, error) {
func SetNodeConfigLabels(nodeConfig *config.Node, node *corev1.Node) (bool, error) {
if node.Labels == nil {
node.Labels = make(map[string]string)
}
if _, ok := node.Labels[ClusterEgressLabel]; !ok {
node.Labels[ClusterEgressLabel] = "true"
return true, nil
_, hasLabel := node.Labels[ClusterEgressLabel]
switch nodeConfig.EgressSelectorMode {
case config.EgressSelectorModeCluster, config.EgressSelectorModePod:
if !hasLabel {
node.Labels[ClusterEgressLabel] = "true"
return true, nil
}
default:
if hasLabel {
delete(node.Labels, ClusterEgressLabel)
return true, nil
}
}
return false, nil
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/nodeconfig/nodeconfig_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/version"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -20,6 +21,7 @@ var FakeNodeWithNoAnnotation = &corev1.Node{
}

var TestEnvName = version.ProgramUpper + "_NODE_NAME"
var FakeNodeConfig = &config.Node{}
var FakeNodeWithAnnotation = &corev1.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Expand All @@ -39,7 +41,7 @@ func Test_UnitSetExistingNodeConfigAnnotations(t *testing.T) {
// adding same config
os.Args = []string{version.Program, "server", "--no-flannel"}
os.Setenv(version.ProgramUpper+"_NODE_NAME", "fakeNode-with-annotation")
nodeUpdated, err := SetNodeConfigAnnotations(FakeNodeWithAnnotation)
nodeUpdated, err := SetNodeConfigAnnotations(FakeNodeConfig, FakeNodeWithAnnotation)
if err != nil {
t.Fatalf("Failed to set node config annotation: %v", err)
}
Expand All @@ -50,6 +52,7 @@ func Test_UnitSetExistingNodeConfigAnnotations(t *testing.T) {

func Test_UnitSetNodeConfigAnnotations(t *testing.T) {
type args struct {
config *config.Node
node *corev1.Node
osArgs []string
}
Expand All @@ -72,6 +75,7 @@ func Test_UnitSetNodeConfigAnnotations(t *testing.T) {
{
name: "Set empty NodeConfigAnnotations",
args: args{
config: FakeNodeConfig,
node: FakeNodeWithAnnotation,
osArgs: []string{version.Program, "server", "--no-flannel"},
},
Expand All @@ -83,6 +87,7 @@ func Test_UnitSetNodeConfigAnnotations(t *testing.T) {
{
name: "Set args with equal",
args: args{
config: FakeNodeConfig,
node: FakeNodeWithNoAnnotation,
osArgs: []string{version.Program, "server", "--no-flannel", "--write-kubeconfig-mode=777"},
},
Expand All @@ -98,7 +103,7 @@ func Test_UnitSetNodeConfigAnnotations(t *testing.T) {
t.Errorf("Setup for SetNodeConfigAnnotations() failed = %v", err)
return
}
got, err := SetNodeConfigAnnotations(tt.args.node)
got, err := SetNodeConfigAnnotations(tt.args.config, tt.args.node)
if (err != nil) != tt.wantErr {
t.Errorf("SetNodeConfigAnnotations() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 89cba26

Please sign in to comment.