2019-01-09 11:54:15 -05:00
package cmds
import (
"os"
"path/filepath"
2022-03-02 18:47:27 -05:00
"github.com/k3s-io/k3s/pkg/version"
2025-03-12 12:02:45 -04:00
"github.com/urfave/cli/v2"
2019-01-09 11:54:15 -05:00
)
type Agent struct {
2019-03-04 01:29:06 -05:00
Token string
2019-03-04 12:10:01 -05:00
TokenFile string
2019-11-14 14:42:42 -05:00
ClusterSecret string
2019-03-24 15:19:05 -04:00
ServerURL string
2022-02-16 17:19:58 -05:00
APIAddressCh chan [ ] string
2019-07-24 03:22:31 -04:00
DisableLoadBalancer bool
2021-11-09 10:44:34 -05:00
DisableServiceLB bool
2021-02-12 10:35:57 -05:00
ETCDAgent bool
LBServerPort int
2019-03-26 18:15:16 -04:00
ResolvConf string
2019-03-04 01:29:06 -05:00
DataDir string
2024-04-24 21:02:05 -04:00
BindAddress string
2021-04-21 18:56:20 -04:00
NodeIP cli . StringSlice
NodeExternalIP cli . StringSlice
2024-09-06 17:15:19 -04:00
NodeInternalDNS cli . StringSlice
NodeExternalDNS cli . StringSlice
2019-03-04 01:29:06 -05:00
NodeName string
2019-05-03 13:36:12 -04:00
PauseImage string
2020-07-17 19:16:23 -04:00
Snapshotter string
2019-03-04 01:29:06 -05:00
Docker bool
2023-11-29 21:14:01 -05:00
ContainerdNoDefault bool
2024-10-30 16:55:40 -04:00
ContainerdNonrootDevices bool
2019-03-04 01:29:06 -05:00
ContainerRuntimeEndpoint string
2023-11-21 15:38:56 -05:00
DefaultRuntime string
2023-09-27 16:20:50 -04:00
ImageServiceEndpoint string
2019-03-19 19:28:43 -04:00
FlannelIface string
2019-08-08 01:56:09 -04:00
FlannelConf string
2022-06-08 04:38:07 -04:00
FlannelCniConfFile string
2022-09-01 13:20:32 -04:00
VPNAuth string
VPNAuthFile string
2019-03-04 01:29:06 -05:00
Debug bool
2024-04-24 21:02:05 -04:00
EnablePProf bool
2019-03-08 17:47:44 -05:00
Rootless bool
2019-10-19 06:18:51 -04:00
RootlessAlreadyUnshared bool
2019-11-05 04:45:07 -05:00
WithNodeID bool
2020-08-11 19:17:32 -04:00
EnableSELinux bool
2020-07-14 18:46:10 -04:00
ProtectKernelDefaults bool
2021-05-10 18:58:41 -04:00
ClusterReset bool
2021-02-12 02:37:58 -05:00
PrivateRegistry string
2021-05-10 18:58:41 -04:00
SystemDefaultRegistry string
2021-02-12 02:37:58 -05:00
AirgapExtraRegistry cli . StringSlice
ExtraKubeletArgs cli . StringSlice
ExtraKubeProxyArgs cli . StringSlice
Labels cli . StringSlice
Taints cli . StringSlice
2021-05-10 18:58:41 -04:00
ImageCredProvBinDir string
ImageCredProvConfig string
2019-01-09 11:54:15 -05:00
AgentShared
}
type AgentShared struct {
NodeIP string
}
var (
2021-12-07 17:31:32 -05:00
appName = filepath . Base ( os . Args [ 0 ] )
AgentConfig Agent
2023-01-31 15:57:48 -05:00
AgentTokenFlag = & cli . StringFlag {
2025-03-12 12:02:45 -04:00
Name : "token" ,
Aliases : [ ] string { "t" } ,
2021-12-07 17:31:32 -05:00
Usage : "(cluster) Token to use for authentication" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_TOKEN" } ,
2021-12-07 17:31:32 -05:00
Destination : & AgentConfig . Token ,
}
2023-01-31 15:57:48 -05:00
NodeIPFlag = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-ip" ,
Aliases : [ ] string { "i" } ,
Usage : "(agent/networking) IPv4/IPv6 addresses to advertise for node" ,
Destination : & AgentConfig . NodeIP ,
2019-01-09 11:54:15 -05:00
}
2023-01-31 15:57:48 -05:00
NodeExternalIPFlag = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-external-ip" ,
Usage : "(agent/networking) IPv4/IPv6 external IP addresses to advertise for node" ,
Destination : & AgentConfig . NodeExternalIP ,
2019-10-15 17:17:26 -04:00
}
2024-09-06 17:15:19 -04:00
NodeInternalDNSFlag = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-internal-dns" ,
Usage : "(agent/networking) internal DNS addresses to advertise for node" ,
Destination : & AgentConfig . NodeInternalDNS ,
2024-09-06 17:15:19 -04:00
}
NodeExternalDNSFlag = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-external-dns" ,
Usage : "(agent/networking) external DNS addresses to advertise for node" ,
Destination : & AgentConfig . NodeExternalDNS ,
2024-09-06 17:15:19 -04:00
}
2023-01-31 15:57:48 -05:00
NodeNameFlag = & cli . StringFlag {
2019-01-09 11:54:15 -05:00
Name : "node-name" ,
2019-10-27 01:53:25 -04:00
Usage : "(agent/node) Node name" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_NODE_NAME" } ,
2019-01-09 11:54:15 -05:00
Destination : & AgentConfig . NodeName ,
}
2023-01-31 15:57:48 -05:00
WithNodeIDFlag = & cli . BoolFlag {
2019-11-05 04:45:07 -05:00
Name : "with-node-id" ,
Usage : "(agent/node) Append id to node name" ,
Destination : & AgentConfig . WithNodeID ,
}
2023-01-31 15:57:48 -05:00
ProtectKernelDefaultsFlag = & cli . BoolFlag {
2022-08-02 16:51:16 -04:00
Name : "protect-kernel-defaults" ,
Usage : "(agent/node) Kernel tuning behavior. If set, error if kernel tunables are different than kubelet defaults." ,
Destination : & AgentConfig . ProtectKernelDefaults ,
}
2023-01-31 15:57:48 -05:00
SELinuxFlag = & cli . BoolFlag {
2022-08-02 16:51:16 -04:00
Name : "selinux" ,
Usage : "(agent/node) Enable SELinux in containerd" ,
Destination : & AgentConfig . EnableSELinux ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_SELINUX" } ,
2022-08-02 16:51:16 -04:00
}
2023-01-31 15:57:48 -05:00
LBServerPortFlag = & cli . IntFlag {
2022-08-02 16:51:16 -04:00
Name : "lb-server-port" ,
Usage : "(agent/node) Local port for supervisor client load-balancer. If the supervisor and apiserver are not colocated an additional port 1 less than this port will also be used for the apiserver client load-balancer." ,
Destination : & AgentConfig . LBServerPort ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_LB_SERVER_PORT" } ,
2022-08-02 16:51:16 -04:00
Value : 6444 ,
2019-03-01 19:10:18 -05:00
}
2023-01-31 15:57:48 -05:00
DockerFlag = & cli . BoolFlag {
2021-12-16 15:00:40 -05:00
Name : "docker" ,
Usage : "(agent/runtime) (experimental) Use cri-dockerd instead of containerd" ,
Destination : & AgentConfig . Docker ,
}
2023-01-31 15:57:48 -05:00
CRIEndpointFlag = & cli . StringFlag {
2019-10-27 01:53:25 -04:00
Name : "container-runtime-endpoint" ,
2021-12-16 15:00:40 -05:00
Usage : "(agent/runtime) Disable embedded containerd and use the CRI socket at the given path; when used with --docker this sets the docker socket path" ,
2019-10-27 01:53:25 -04:00
Destination : & AgentConfig . ContainerRuntimeEndpoint ,
}
2023-11-21 15:38:56 -05:00
DefaultRuntimeFlag = & cli . StringFlag {
Name : "default-runtime" ,
Usage : "(agent/runtime) Set the default runtime in containerd" ,
Destination : & AgentConfig . DefaultRuntime ,
}
2023-09-27 16:20:50 -04:00
ImageServiceEndpointFlag = & cli . StringFlag {
Name : "image-service-endpoint" ,
Usage : "(agent/runtime) Disable embedded containerd image service and use remote image service socket at the given path. If not specified, defaults to --container-runtime-endpoint." ,
Destination : & AgentConfig . ImageServiceEndpoint ,
}
2023-01-31 15:57:48 -05:00
PrivateRegistryFlag = & cli . StringFlag {
2019-10-27 01:53:25 -04:00
Name : "private-registry" ,
Usage : "(agent/runtime) Private registry configuration file" ,
Destination : & AgentConfig . PrivateRegistry ,
2020-05-05 18:09:04 -04:00
Value : "/etc/rancher/" + version . Program + "/registries.yaml" ,
2019-10-27 01:53:25 -04:00
}
2023-01-31 15:57:48 -05:00
AirgapExtraRegistryFlag = & cli . StringSliceFlag {
2021-02-26 14:07:15 -05:00
Name : "airgap-extra-registry" ,
Usage : "(agent/runtime) Additional registry to tag airgap images as being sourced from" ,
Value : & AgentConfig . AirgapExtraRegistry ,
Hidden : true ,
2021-02-12 02:37:58 -05:00
}
2023-01-31 15:57:48 -05:00
PauseImageFlag = & cli . StringFlag {
2019-10-27 01:53:25 -04:00
Name : "pause-image" ,
2022-07-28 15:32:15 -04:00
Usage : "(agent/runtime) Customized pause image for containerd or docker sandbox" ,
2019-10-27 01:53:25 -04:00
Destination : & AgentConfig . PauseImage ,
2025-02-06 15:35:27 -05:00
Value : "rancher/mirrored-pause:3.6" ,
2019-10-27 01:53:25 -04:00
}
2023-01-31 15:57:48 -05:00
SnapshotterFlag = & cli . StringFlag {
2020-07-17 19:16:23 -04:00
Name : "snapshotter" ,
Usage : "(agent/runtime) Override default containerd snapshotter" ,
Destination : & AgentConfig . Snapshotter ,
2021-06-10 15:27:00 -04:00
Value : DefaultSnapshotter ,
2020-07-17 19:16:23 -04:00
}
2023-01-31 15:57:48 -05:00
FlannelIfaceFlag = & cli . StringFlag {
2019-03-19 19:28:43 -04:00
Name : "flannel-iface" ,
2019-10-27 01:53:25 -04:00
Usage : "(agent/networking) Override default flannel interface" ,
2019-03-19 19:28:43 -04:00
Destination : & AgentConfig . FlannelIface ,
}
2023-01-31 15:57:48 -05:00
FlannelConfFlag = & cli . StringFlag {
2019-08-08 01:56:09 -04:00
Name : "flannel-conf" ,
2019-10-27 01:53:25 -04:00
Usage : "(agent/networking) Override default flannel config file" ,
2019-08-08 01:56:09 -04:00
Destination : & AgentConfig . FlannelConf ,
}
2023-01-31 15:57:48 -05:00
FlannelCniConfFileFlag = & cli . StringFlag {
2022-06-08 04:38:07 -04:00
Name : "flannel-cni-conf" ,
Usage : "(agent/networking) Override default flannel cni config file" ,
Destination : & AgentConfig . FlannelCniConfFile ,
}
2022-09-01 13:20:32 -04:00
VPNAuth = & cli . StringFlag {
Name : "vpn-auth" ,
2023-09-14 12:24:19 -04:00
Usage : "(agent/networking) (experimental) Credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>[,controlServerURL=<url>][,extraArgs=<args>]" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_VPN_AUTH" } ,
2022-09-01 13:20:32 -04:00
Destination : & AgentConfig . VPNAuth ,
}
VPNAuthFile = & cli . StringFlag {
Name : "vpn-auth-file" ,
2023-09-14 12:24:19 -04:00
Usage : "(agent/networking) (experimental) File containing credentials for the VPN provider. It must include the provider name and join key in the format name=<vpn-provider>,joinKey=<key>[,controlServerURL=<url>][,extraArgs=<args>]" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_VPN_AUTH_FILE" } ,
2022-09-01 13:20:32 -04:00
Destination : & AgentConfig . VPNAuthFile ,
}
2023-01-31 15:57:48 -05:00
ResolvConfFlag = & cli . StringFlag {
2019-03-26 18:15:16 -04:00
Name : "resolv-conf" ,
2019-10-27 01:53:25 -04:00
Usage : "(agent/networking) Kubelet resolv.conf file" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_RESOLV_CONF" } ,
2019-03-26 18:15:16 -04:00
Destination : & AgentConfig . ResolvConf ,
}
2023-01-31 15:57:48 -05:00
ExtraKubeletArgs = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "kubelet-arg" ,
Usage : "(agent/flags) Customized flag for kubelet process" ,
Destination : & AgentConfig . ExtraKubeletArgs ,
2019-04-04 20:43:00 -04:00
}
2023-01-31 15:57:48 -05:00
ExtraKubeProxyArgs = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "kube-proxy-arg" ,
Usage : "(agent/flags) Customized flag for kube-proxy process" ,
Destination : & AgentConfig . ExtraKubeProxyArgs ,
2019-04-04 20:43:00 -04:00
}
2023-01-31 15:57:48 -05:00
NodeTaints = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-taint" ,
Usage : "(agent/node) Registering kubelet with set of taints" ,
Destination : & AgentConfig . Taints ,
2019-05-07 19:47:07 -04:00
}
2023-01-31 15:57:48 -05:00
NodeLabels = & cli . StringSliceFlag {
2025-03-12 12:02:45 -04:00
Name : "node-label" ,
Usage : "(agent/node) Registering and starting kubelet with set of labels" ,
Destination : & AgentConfig . Labels ,
2019-05-07 19:47:07 -04:00
}
2023-01-31 15:57:48 -05:00
ImageCredProvBinDirFlag = & cli . StringFlag {
2021-05-10 18:58:41 -04:00
Name : "image-credential-provider-bin-dir" ,
Usage : "(agent/node) The path to the directory where credential provider plugin binaries are located" ,
Destination : & AgentConfig . ImageCredProvBinDir ,
Value : "/var/lib/rancher/credentialprovider/bin" ,
}
2023-01-31 15:57:48 -05:00
ImageCredProvConfigFlag = & cli . StringFlag {
2021-05-10 18:58:41 -04:00
Name : "image-credential-provider-config" ,
Usage : "(agent/node) The path to the credential provider plugin config file" ,
Destination : & AgentConfig . ImageCredProvConfig ,
Value : "/var/lib/rancher/credentialprovider/config.yaml" ,
}
2023-11-14 18:54:32 -05:00
DisableAgentLBFlag = & cli . BoolFlag {
Name : "disable-apiserver-lb" ,
Usage : "(agent/networking) (experimental) Disable the agent's client-side load-balancer and connect directly to the configured server address" ,
Destination : & AgentConfig . DisableLoadBalancer ,
}
2023-11-29 21:14:01 -05:00
DisableDefaultRegistryEndpointFlag = & cli . BoolFlag {
Name : "disable-default-registry-endpoint" ,
Usage : "(agent/containerd) Disables containerd's fallback default registry endpoint when a mirror is configured for that registry" ,
Destination : & AgentConfig . ContainerdNoDefault ,
}
2024-10-30 16:55:40 -04:00
NonrootDevicesFlag = & cli . BoolFlag {
Name : "nonroot-devices" ,
Usage : "(agent/containerd) Allows non-root pods to access devices by setting device_ownership_from_security_context=true in the containerd CRI config" ,
Destination : & AgentConfig . ContainerdNonrootDevices ,
}
2024-04-24 21:02:05 -04:00
EnablePProfFlag = & cli . BoolFlag {
Name : "enable-pprof" ,
Usage : "(experimental) Enable pprof endpoint on supervisor port" ,
Destination : & AgentConfig . EnablePProf ,
}
BindAddressFlag = & cli . StringFlag {
Name : "bind-address" ,
Usage : "(listener) " + version . Program + " bind address (default: 0.0.0.0)" ,
Destination : & AgentConfig . BindAddress ,
}
2019-01-09 11:54:15 -05:00
)
2025-03-12 12:02:45 -04:00
func NewAgentCommand ( action func ( ctx * cli . Context ) error ) * cli . Command {
return & cli . Command {
2019-01-09 11:54:15 -05:00
Name : "agent" ,
Usage : "Run node agent" ,
UsageText : appName + " agent [OPTIONS]" ,
2020-08-29 15:46:55 -04:00
Action : action ,
2019-01-09 11:54:15 -05:00
Flags : [ ] cli . Flag {
2020-08-29 23:30:07 -04:00
ConfigFlag ,
DebugFlag ,
2020-08-29 15:46:55 -04:00
VLevel ,
VModule ,
LogFile ,
AlsoLogToStderr ,
2021-12-07 17:31:32 -05:00
AgentTokenFlag ,
2023-01-31 15:57:48 -05:00
& cli . StringFlag {
2019-03-01 19:07:55 -05:00
Name : "token-file" ,
2019-10-27 01:53:25 -04:00
Usage : "(cluster) Token file to use for authentication" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_TOKEN_FILE" } ,
2019-03-01 19:07:55 -05:00
Destination : & AgentConfig . TokenFile ,
} ,
2023-01-31 15:57:48 -05:00
& cli . StringFlag {
2025-03-12 12:02:45 -04:00
Name : "server" ,
Aliases : [ ] string { "s" } ,
2019-10-27 01:53:25 -04:00
Usage : "(cluster) Server to connect to" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_URL" } ,
2019-01-09 11:54:15 -05:00
Destination : & AgentConfig . ServerURL ,
} ,
2024-07-29 15:22:50 -04:00
// Note that this is different from DataDirFlag used elswhere in the CLI,
// as this is bound to AgentConfig instead of ServerConfig.
2023-01-31 15:57:48 -05:00
& cli . StringFlag {
2025-03-12 12:02:45 -04:00
Name : "data-dir" ,
Aliases : [ ] string { "d" } ,
2019-10-27 01:53:25 -04:00
Usage : "(agent/data) Folder to hold state" ,
2019-01-09 11:54:15 -05:00
Destination : & AgentConfig . DataDir ,
2020-05-05 18:09:04 -04:00
Value : "/var/lib/rancher/" + version . Program + "" ,
2025-03-12 12:02:45 -04:00
EnvVars : [ ] string { version . ProgramUpper + "_DATA_DIR" } ,
2019-01-09 11:54:15 -05:00
} ,
2020-08-29 15:46:55 -04:00
NodeNameFlag ,
WithNodeIDFlag ,
NodeLabels ,
NodeTaints ,
2021-05-10 18:58:41 -04:00
ImageCredProvBinDirFlag ,
ImageCredProvConfigFlag ,
2023-01-31 15:57:48 -05:00
SELinuxFlag ,
2022-08-02 16:51:16 -04:00
LBServerPortFlag ,
ProtectKernelDefaultsFlag ,
2020-08-29 15:46:55 -04:00
CRIEndpointFlag ,
2023-11-21 15:38:56 -05:00
DefaultRuntimeFlag ,
2023-09-27 16:20:50 -04:00
ImageServiceEndpointFlag ,
2020-08-29 15:46:55 -04:00
PauseImageFlag ,
SnapshotterFlag ,
PrivateRegistryFlag ,
2023-11-29 21:14:01 -05:00
DisableDefaultRegistryEndpointFlag ,
2024-10-30 16:55:40 -04:00
NonrootDevicesFlag ,
2021-02-12 02:37:58 -05:00
AirgapExtraRegistryFlag ,
2020-08-29 15:46:55 -04:00
NodeIPFlag ,
2024-04-24 21:02:05 -04:00
BindAddressFlag ,
2020-08-29 15:46:55 -04:00
NodeExternalIPFlag ,
2024-09-06 17:15:19 -04:00
NodeInternalDNSFlag ,
NodeExternalDNSFlag ,
2020-08-29 15:46:55 -04:00
ResolvConfFlag ,
FlannelIfaceFlag ,
FlannelConfFlag ,
2022-06-08 04:38:07 -04:00
FlannelCniConfFileFlag ,
2020-08-29 15:46:55 -04:00
ExtraKubeletArgs ,
ExtraKubeProxyArgs ,
2022-11-22 16:43:16 -05:00
// Experimental flags
2024-04-24 21:02:05 -04:00
EnablePProfFlag ,
2023-01-31 15:57:48 -05:00
& cli . BoolFlag {
2019-10-27 01:53:25 -04:00
Name : "rootless" ,
Usage : "(experimental) Run rootless" ,
Destination : & AgentConfig . Rootless ,
} ,
2022-11-22 16:43:16 -05:00
PreferBundledBin ,
2019-10-27 01:53:25 -04:00
// Deprecated/hidden below
2022-08-02 16:51:16 -04:00
DockerFlag ,
2022-09-01 13:20:32 -04:00
VPNAuth ,
VPNAuthFile ,
2023-11-14 18:54:32 -05:00
DisableAgentLBFlag ,
2019-01-09 11:54:15 -05:00
} ,
}
}