Skip to content

Commit

Permalink
Merge pull request #90 from jwcesign/fix-flannel
Browse files Browse the repository at this point in the history
fix: fix the overhead calculation
  • Loading branch information
jwcesign authored Nov 6, 2024
2 parents 199ca21 + 62b0d2e commit 59df58e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
3 changes: 2 additions & 1 deletion pkg/operator/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type Options struct {
func (o *Options) AddFlags(fs *coreoptions.FlagSet) {
fs.StringVar(&o.ClusterID, "cluster-id", env.WithDefaultString("CLUSTER_ID", ""), "The external kubernetes cluster id for new nodes to connect with.")
fs.StringVar(&o.ClusterCNI, "cluster-cni", env.WithDefaultString("CLUSTER_CNI", "terway-eniip"), "The network cni used by the cluster.")
fs.Float64Var(&o.VMMemoryOverheadPercent, "vm-memory-overhead-percent", utils.WithDefaultFloat64("VM_MEMORY_OVERHEAD_PERCENT", 0.075), "The VM memory overhead as a percent that will be subtracted from the total memory for all instance types.")
// TODO: for different OS, the overhead is different, find a way to fix this.
fs.Float64Var(&o.VMMemoryOverheadPercent, "vm-memory-overhead-percent", utils.WithDefaultFloat64("VM_MEMORY_OVERHEAD_PERCENT", 0.065), "The VM memory overhead as a percent that will be subtracted from the total memory for all instance types.")
fs.BoolVar(&o.Interruption, "interruption", env.WithDefaultBool("INTERRUPTION", true), "Enable interruption handling.")
}

Expand Down
50 changes: 19 additions & 31 deletions pkg/providers/instancetype/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewInstanceType(ctx context.Context,
Offerings: offerings,
Capacity: computeCapacity(ctx, info, kc.MaxPods, kc.PodsPerCore, systemDisk, clusterCNI),
Overhead: &cloudprovider.InstanceTypeOverhead{
KubeReserved: kubeReservedResources(cpu(info), kc.KubeReserved),
KubeReserved: kubeReservedResources(kc.KubeReserved),
SystemReserved: systemReservedResources(kc.SystemReserved),
EvictionThreshold: evictionThreshold(memory(ctx, info), ephemeralStorage(systemDisk), kc.EvictionHard, kc.EvictionSoft),
},
Expand Down Expand Up @@ -179,49 +179,37 @@ func computeCapacity(ctx context.Context,
return resourceList
}

func kubeReservedResources(cpus *resource.Quantity, kubeReserved map[string]string) corev1.ResourceList {
func kubeReservedResources(kubeReserved map[string]string) corev1.ResourceList {
resources := corev1.ResourceList{
// TODO: I am not sure whether these values are correct, let's figure it out latter
corev1.ResourceMemory: resource.MustParse("255Mi"),
corev1.ResourceEphemeralStorage: resource.MustParse("1Gi"), // default kube-reserved ephemeral-storage
}
// kube-reserved Computed from
// https://github.com/bottlerocket-os/bottlerocket/pull/1388/files#diff-bba9e4e3e46203be2b12f22e0d654ebd270f0b478dd34f40c31d7aa695620f2fR611
for _, cpuRange := range []struct {
start int64
end int64
percentage float64
}{
{start: 0, end: 1000, percentage: 0.06},
{start: 1000, end: 2000, percentage: 0.01},
{start: 2000, end: 4000, percentage: 0.005},
{start: 4000, end: 1 << 31, percentage: 0.0025},
} {
if cpu := cpus.MilliValue(); cpu >= cpuRange.start {
r := float64(cpuRange.end - cpuRange.start)
if cpu < cpuRange.end {
r = float64(cpu - cpuRange.start)
}
cpuOverhead := resources.Cpu()
cpuOverhead.Add(*resource.NewMilliQuantity(int64(r*cpuRange.percentage), resource.DecimalSI))
resources[corev1.ResourceCPU] = *cpuOverhead
}
// TODO: Following data is extract from real env
// Please check it more
corev1.ResourceMemory: resource.MustParse("447Mi"),
corev1.ResourceCPU: resource.MustParse("35m"),
}

return lo.Assign(resources, lo.MapEntries(kubeReserved, func(k string, v string) (corev1.ResourceName, resource.Quantity) {
return corev1.ResourceName(k), resource.MustParse(v)
}))
}

func systemReservedResources(systemReserved map[string]string) corev1.ResourceList {
return lo.MapEntries(systemReserved, func(k string, v string) (corev1.ResourceName, resource.Quantity) {
resources := corev1.ResourceList{
// TODO: Following data is extract from real env
// Please check it more
corev1.ResourceMemory: resource.MustParse("447Mi"),
corev1.ResourceCPU: resource.MustParse("35m"),
}

return lo.Assign(resources, lo.MapEntries(systemReserved, func(k string, v string) (corev1.ResourceName, resource.Quantity) {
return corev1.ResourceName(k), resource.MustParse(v)
})
}))
}

func evictionThreshold(memory *resource.Quantity, storage *resource.Quantity, evictionHard map[string]string, evictionSoft map[string]string) corev1.ResourceList {
overhead := corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("100Mi"),
corev1.ResourceEphemeralStorage: resource.MustParse(fmt.Sprint(math.Ceil(float64(storage.Value()) / 100 * 10))),
// TODO: Following data is extract from real env
// Please check it more
corev1.ResourceMemory: resource.MustParse("300Mi"),
}

override := corev1.ResourceList{}
Expand Down

0 comments on commit 59df58e

Please sign in to comment.