@ -2,7 +2,6 @@ package agent
import (
import (
"context"
"context"
"fmt"
"net"
"net"
"os"
"os"
"path/filepath"
"path/filepath"
@ -30,14 +29,18 @@ import (
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/util"
"github.com/pkg/errors"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
core v1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd"
toolswatch "k8s.io/client-go/tools/watch"
app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
utilsnet "k8s.io/utils/net"
utilsnet "k8s.io/utils/net"
@ -271,18 +274,25 @@ func createProxyAndValidateToken(ctx context.Context, cfg *cmds.Agent) (proxy.Pr
return proxy , nil
return proxy , nil
}
}
// configureNode waits for the node object to be created, and if/when it does,
// ensures that the labels and annotations are up to date.
func configureNode ( ctx context . Context , agentConfig * daemonconfig . Agent , nodes typedcorev1 . NodeInterface ) error {
func configureNode ( ctx context . Context , agentConfig * daemonconfig . Agent , nodes typedcorev1 . NodeInterface ) error {
fieldSelector := fields . Set { metav1 . ObjectNameField : agentConfig . NodeName } . String ( )
fieldSelector := fields . Set { metav1 . ObjectNameField : agentConfig . NodeName } . String ( )
watch , err := nodes . Watch ( ctx , metav1 . ListOptions { FieldSelector : fieldSelector } )
lw := & cache . ListWatch {
if err != nil {
ListFunc : func ( options metav1 . ListOptions ) ( object runtime . Object , e error ) {
return err
options . FieldSelector = fieldSelector
}
return nodes . List ( ctx , options )
defer watch . Stop ( )
} ,
WatchFunc : func ( options metav1 . ListOptions ) ( i watch . Interface , e error ) {
for ev := range watch . ResultChan ( ) {
options . FieldSelector = fieldSelector
node , ok := ev . Object . ( * corev1 . Node )
return nodes . Watch ( ctx , options )
} ,
}
condition := func ( ev watch . Event ) ( bool , error ) {
node , ok := ev . Object . ( * v1 . Node )
if ! ok {
if ! ok {
return fmt . Errorf ( "could not convert event object to node: %v" , ev )
return false , errors . New ( "event object not of type v1.Node" )
}
}
updateNode := false
updateNode := false
@ -304,7 +314,7 @@ func configureNode(ctx context.Context, agentConfig *daemonconfig.Agent, nodes t
// inject node config
// inject node config
if changed , err := nodeconfig . SetNodeConfigAnnotations ( node ) ; err != nil {
if changed , err := nodeconfig . SetNodeConfigAnnotations ( node ) ; err != nil {
return err
return false , err
} else if changed {
} else if changed {
updateNode = true
updateNode = true
}
}
@ -312,16 +322,18 @@ func configureNode(ctx context.Context, agentConfig *daemonconfig.Agent, nodes t
if updateNode {
if updateNode {
if _ , err := nodes . Update ( ctx , node , metav1 . UpdateOptions { } ) ; err != nil {
if _ , err := nodes . Update ( ctx , node , metav1 . UpdateOptions { } ) ; err != nil {
logrus . Infof ( "Failed to update node %s: %v" , agentConfig . NodeName , err )
logrus . Infof ( "Failed to update node %s: %v" , agentConfig . NodeName , err )
continue
return false , nil
}
}
logrus . Infof ( "labels have been set successfully on node: %s" , agentConfig . NodeName )
logrus . Infof ( "labels have been set successfully on node: %s" , agentConfig . NodeName )
} else {
return true , nil
logrus . Infof ( "labels have already set on node: %s" , agentConfig . NodeName )
}
}
logrus . Infof ( "labels have already set on node: %s" , agentConfig . NodeName )
break
return true , nil
}
}
if _ , err := toolswatch . UntilWithSync ( ctx , lw , & v1 . Node { } , nil , condition ) ; err != nil {
return errors . Wrap ( err , "failed to configure node" )
}
return nil
return nil
}
}