diff --git a/LICENSES/third_party/forked/vishhstress/LICENSE b/LICENSES/third_party/forked/vishhstress/LICENSE new file mode 100644 index 00000000000..471730c7743 --- /dev/null +++ b/LICENSES/third_party/forked/vishhstress/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Vish Kannan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/test/images/agnhost/agnhost.go b/test/images/agnhost/agnhost.go index 7f32499a14e..dfd10c7a0fc 100644 --- a/test/images/agnhost/agnhost.go +++ b/test/images/agnhost/agnhost.go @@ -48,6 +48,7 @@ import ( tcpreset "k8s.io/kubernetes/test/images/agnhost/tcp-reset" testwebserver "k8s.io/kubernetes/test/images/agnhost/test-webserver" "k8s.io/kubernetes/test/images/agnhost/webhook" + "k8s.io/kubernetes/third_party/forked/vishhstress" // MIT License ) var Version = "development" @@ -86,6 +87,7 @@ func main() { rootCmd.AddCommand(webhook.CmdWebhook) rootCmd.AddCommand(openidmetadata.CmdTestServiceAccountIssuerDiscovery) rootCmd.AddCommand(grpchealthchecking.CmdGrpcHealthChecking) + rootCmd.AddCommand(vishhstress.CmdStress) // NOTE(claudiub): Some tests are passing logging related flags, so we need to be able to // accept them. This will also include them in the printed help. diff --git a/third_party/forked/vishhstress/LICENSE b/third_party/forked/vishhstress/LICENSE new file mode 100644 index 00000000000..471730c7743 --- /dev/null +++ b/third_party/forked/vishhstress/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Vish Kannan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/forked/vishhstress/stress.go b/third_party/forked/vishhstress/stress.go new file mode 100644 index 00000000000..b81818e0c26 --- /dev/null +++ b/third_party/forked/vishhstress/stress.go @@ -0,0 +1,99 @@ +/* +MIT License + +Copyright (c) 2024 Vish Kannan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +// Package vishhstress was forked from +// so that it can be consumed from agnhost. +package vishhstress + +import ( + "io" + "io/ioutil" + "os" + "time" + + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/klog/v2" +) + +var ( + argMemTotal string + argMemStepSize string + argMemSleepDuration time.Duration + argCpus int + buffer [][]byte +) + +// CmdStress is used by agnhost Cobra. +var CmdStress = &cobra.Command{ + Use: "stress", + Short: "Lightweight compute resource stress utlity", + Args: cobra.NoArgs, + Run: main, +} + +func init() { + flags := CmdStress.Flags() + flags.StringVar(&argMemTotal, "mem-total", "0", "total memory to be consumed. Memory will be consumed via multiple allocations.") + flags.StringVar(&argMemStepSize, "mem-alloc-size", "4Ki", "amount of memory to be consumed in each allocation") + flags.DurationVar(&argMemSleepDuration, "mem-alloc-sleep", time.Millisecond, "duration to sleep between allocations") + flags.IntVar(&argCpus, "cpus", 0, "total number of CPUs to utilize") +} + +func main(cmd *cobra.Command, _ []string) { + total := resource.MustParse(argMemTotal) + stepSize := resource.MustParse(argMemStepSize) + klog.Infof("Allocating %q memory, in %q chunks, with a %v sleep between allocations", total.String(), stepSize.String(), argMemSleepDuration) + burnCPU() + allocateMemory(total, stepSize) + klog.Infof("Allocated %q memory", total.String()) + select {} +} + +func burnCPU() { + src, err := os.Open("/dev/zero") + if err != nil { + klog.Fatalf("failed to open /dev/zero") + } + for i := 0; i < argCpus; i++ { + klog.Infof("Spawning a thread to consume CPU") + go func() { + _, err := io.Copy(ioutil.Discard, src) + if err != nil { + klog.Fatalf("failed to copy from /dev/zero to /dev/null: %v", err) + } + }() + } +} + +func allocateMemory(total, stepSize resource.Quantity) { + for i := int64(1); i*stepSize.Value() <= total.Value(); i++ { + newBuffer := make([]byte, stepSize.Value()) + for i := range newBuffer { + newBuffer[i] = 0 + } + buffer = append(buffer, newBuffer) + time.Sleep(argMemSleepDuration) + } +}