Created
November 22, 2019 11:26
-
-
Save CallMeFoxie/ab84d2dbb228b43bccc3cfe5cfb478aa to your computer and use it in GitHub Desktop.
Allow setting extended resources from within kubelet flags [kubernetes 1.16.3]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/cmd/kubelet/app/options/options.go b/cmd/kubelet/app/options/options.go | |
index 1b60f2cea45..5b245b7faa1 100644 | |
--- a/cmd/kubelet/app/options/options.go | |
+++ b/cmd/kubelet/app/options/options.go | |
@@ -144,6 +144,8 @@ type KubeletFlags struct { | |
ExperimentalNodeAllocatableIgnoreEvictionThreshold bool | |
// Node Labels are the node labels to add when registering the node in the cluster | |
NodeLabels map[string]string | |
+ // Extended resources of a node to register/update with | |
+ ExtendedResources map[string]string | |
// volumePluginDir is the full path of the directory in which to search | |
// for additional third party volume plugins | |
VolumePluginDir string | |
@@ -214,6 +216,7 @@ func NewKubeletFlags() *KubeletFlags { | |
ExperimentalKernelMemcgNotification: false, | |
RemoteRuntimeEndpoint: remoteRuntimeEndpoint, | |
NodeLabels: make(map[string]string), | |
+ ExtendedResources: make(map[string]string), | |
VolumePluginDir: "/usr/libexec/kubernetes/kubelet-plugins/volume/exec/", | |
RegisterNode: true, | |
SeccompProfileRoot: filepath.Join(defaultRootDir, "seccomp"), | |
@@ -397,6 +400,8 @@ func (f *KubeletFlags) AddFlags(mainfs *pflag.FlagSet) { | |
fs.StringVar(&f.SeccompProfileRoot, "seccomp-profile-root", f.SeccompProfileRoot, "<Warning: Alpha feature> Directory path for seccomp profiles.") | |
fs.StringVar(&f.BootstrapCheckpointPath, "bootstrap-checkpoint-path", f.BootstrapCheckpointPath, "<Warning: Alpha feature> Path to the directory where the checkpoints are stored") | |
fs.Int32Var(&f.NodeStatusMaxImages, "node-status-max-images", f.NodeStatusMaxImages, "<Warning: Alpha feature> The maximum number of images to report in Node.Status.Images. If -1 is specified, no cap will be applied.") | |
+ bindableExtendedResources := cliflag.ConfigurationMap(f.ExtendedResources) | |
+ fs.Var(&bindableExtendedResources, "extended-resources", "Extended resources to add when registering the node in the cluster. Resources must be key=value pairs separated by ','.") | |
// DEPRECATED FLAGS | |
fs.StringVar(&f.BootstrapKubeconfig, "experimental-bootstrap-kubeconfig", f.BootstrapKubeconfig, "") | |
diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go | |
index fe6e5e7e510..c9655e66a2f 100644 | |
--- a/cmd/kubelet/app/server.go | |
+++ b/cmd/kubelet/app/server.go | |
@@ -1010,6 +1010,17 @@ func RunKubelet(kubeServer *options.KubeletServer, kubeDeps *kubelet.Dependencie | |
kubeDeps.OSInterface = kubecontainer.RealOS{} | |
} | |
+ extendedResources := make(v1.ResourceList) | |
+ for k, v := range kubeServer.ExtendedResources { | |
+ q, err := resource.ParseQuantity(v) | |
+ if err != nil { | |
+ return err | |
+ } | |
+ if q.Sign() == -1 { | |
+ return fmt.Errorf("resource quantity for %s cannot be negative %v", k, v) | |
+ } | |
+ extendedResources[v1.ResourceName(k)] = q | |
+ } | |
k, err := createAndInitKubelet(&kubeServer.KubeletConfiguration, | |
kubeDeps, | |
&kubeServer.ContainerRuntimeOptions, | |
@@ -1038,6 +1049,7 @@ func RunKubelet(kubeServer *options.KubeletServer, kubeDeps *kubelet.Dependencie | |
kubeServer.NonMasqueradeCIDR, | |
kubeServer.KeepTerminatedPodVolumes, | |
kubeServer.NodeLabels, | |
+ extendedResources, | |
kubeServer.SeccompProfileRoot, | |
kubeServer.BootstrapCheckpointPath, | |
kubeServer.NodeStatusMaxImages) | |
@@ -1114,6 +1126,7 @@ func createAndInitKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration, | |
nonMasqueradeCIDR string, | |
keepTerminatedPodVolumes bool, | |
nodeLabels map[string]string, | |
+ extendedResources v1.ResourceList, | |
seccompProfileRoot string, | |
bootstrapCheckpointPath string, | |
nodeStatusMaxImages int32) (k kubelet.Bootstrap, err error) { | |
@@ -1148,6 +1161,7 @@ func createAndInitKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration, | |
nonMasqueradeCIDR, | |
keepTerminatedPodVolumes, | |
nodeLabels, | |
+ extendedResources, | |
seccompProfileRoot, | |
bootstrapCheckpointPath, | |
nodeStatusMaxImages) | |
diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go | |
index c2acd358e59..458ed02fa6e 100644 | |
--- a/pkg/kubelet/kubelet.go | |
+++ b/pkg/kubelet/kubelet.go | |
@@ -360,6 +360,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration, | |
nonMasqueradeCIDR string, | |
keepTerminatedPodVolumes bool, | |
nodeLabels map[string]string, | |
+ extendedResources v1.ResourceList, | |
seccompProfileRoot string, | |
bootstrapCheckpointPath string, | |
nodeStatusMaxImages int32) (*Kubelet, error) { | |
@@ -517,6 +518,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration, | |
providerID: providerID, | |
nodeRef: nodeRef, | |
nodeLabels: nodeLabels, | |
+ extendedResources: extendedResources, | |
nodeStatusUpdateFrequency: kubeCfg.NodeStatusUpdateFrequency.Duration, | |
nodeStatusReportFrequency: kubeCfg.NodeStatusReportFrequency.Duration, | |
os: kubeDeps.OSInterface, | |
@@ -965,6 +967,7 @@ type Kubelet struct { | |
// a list of node labels to register | |
nodeLabels map[string]string | |
+ extendedResources v1.ResourceList | |
// Last timestamp when runtime responded on ping. | |
// Mutex is used to protect this value. | |
diff --git a/pkg/kubelet/kubelet_node_status.go b/pkg/kubelet/kubelet_node_status.go | |
index 7e6fa2f12bc..aeeb7fe5030 100644 | |
--- a/pkg/kubelet/kubelet_node_status.go | |
+++ b/pkg/kubelet/kubelet_node_status.go | |
@@ -143,6 +143,12 @@ func (kl *Kubelet) reconcileExtendedResource(initialNode, node *v1.Node) bool { | |
} | |
} | |
} | |
+ | |
+ for k, v := range initialNode.Status.Capacity { | |
+ node.Status.Capacity[k] = v | |
+ node.Status.Allocatable[k] = v | |
+ requiresUpdate = true | |
+ } | |
return requiresUpdate | |
} | |
@@ -305,6 +311,14 @@ func (kl *Kubelet) initialNode() (*v1.Node, error) { | |
node.ObjectMeta.Labels[k] = v | |
} | |
+ if node.Status.Capacity == nil { | |
+ node.Status.Capacity = v1.ResourceList{} | |
+ } | |
+ for k, v := range kl.extendedResources { | |
+ node.Status.Capacity[k] = v | |
+ klog.Infof("Adding ", k, " with value ", v) | |
+ } | |
+ | |
if kl.providerID != "" { | |
node.Spec.ProviderID = kl.providerID | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment