Skip to content

Commit 07c701f

Browse files
committed
agnhost: merge registry.k8s.io/stress:v1 (github.com/vishh/stress)
Merge vishh/stress@eab4e33 into agnhost. Old usage: `stress -mem-alloc-size 12Mi -mem-alloc-sleep 10s -mem-total 4Gi` New usage: `agnhost stress --mem-alloc-size 12Mi --mem-alloc-sleep 10s --mem-total 4Gi` This is a part of the steps to migrate from legacy Schema 1 images (issue 123146) Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 5bf2312 commit 07c701f

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Vish Kannan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

test/images/agnhost/agnhost.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
tcpreset "k8s.io/kubernetes/test/images/agnhost/tcp-reset"
4949
testwebserver "k8s.io/kubernetes/test/images/agnhost/test-webserver"
5050
"k8s.io/kubernetes/test/images/agnhost/webhook"
51+
"k8s.io/kubernetes/third_party/forked/vishhstress" // MIT License
5152
)
5253

5354
var Version = "development"
@@ -86,6 +87,7 @@ func main() {
8687
rootCmd.AddCommand(webhook.CmdWebhook)
8788
rootCmd.AddCommand(openidmetadata.CmdTestServiceAccountIssuerDiscovery)
8889
rootCmd.AddCommand(grpchealthchecking.CmdGrpcHealthChecking)
90+
rootCmd.AddCommand(vishhstress.CmdStress)
8991

9092
// NOTE(claudiub): Some tests are passing logging related flags, so we need to be able to
9193
// accept them. This will also include them in the printed help.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Vish Kannan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2024 Vish Kannan
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
// Package vishhstress was forked from <https://github.com/vishh/stress/tree/eab4e3384bcad9899b8b801b4a1917a758e97d96>
26+
// so that it can be consumed from agnhost.
27+
package vishhstress
28+
29+
import (
30+
"io"
31+
"io/ioutil"
32+
"os"
33+
"time"
34+
35+
"github.com/spf13/cobra"
36+
"k8s.io/apimachinery/pkg/api/resource"
37+
"k8s.io/klog/v2"
38+
)
39+
40+
var (
41+
argMemTotal string
42+
argMemStepSize string
43+
argMemSleepDuration time.Duration
44+
argCpus int
45+
buffer [][]byte
46+
)
47+
48+
// CmdStress is used by agnhost Cobra.
49+
var CmdStress = &cobra.Command{
50+
Use: "stress",
51+
Short: "Lightweight compute resource stress utlity",
52+
Args: cobra.NoArgs,
53+
Run: main,
54+
}
55+
56+
func init() {
57+
flags := CmdStress.Flags()
58+
flags.StringVar(&argMemTotal, "mem-total", "0", "total memory to be consumed. Memory will be consumed via multiple allocations.")
59+
flags.StringVar(&argMemStepSize, "mem-alloc-size", "4Ki", "amount of memory to be consumed in each allocation")
60+
flags.DurationVar(&argMemSleepDuration, "mem-alloc-sleep", time.Millisecond, "duration to sleep between allocations")
61+
flags.IntVar(&argCpus, "cpus", 0, "total number of CPUs to utilize")
62+
}
63+
64+
func main(cmd *cobra.Command, _ []string) {
65+
total := resource.MustParse(argMemTotal)
66+
stepSize := resource.MustParse(argMemStepSize)
67+
klog.Infof("Allocating %q memory, in %q chunks, with a %v sleep between allocations", total.String(), stepSize.String(), argMemSleepDuration)
68+
burnCPU()
69+
allocateMemory(total, stepSize)
70+
klog.Infof("Allocated %q memory", total.String())
71+
select {}
72+
}
73+
74+
func burnCPU() {
75+
src, err := os.Open("/dev/zero")
76+
if err != nil {
77+
klog.Fatalf("failed to open /dev/zero")
78+
}
79+
for i := 0; i < argCpus; i++ {
80+
klog.Infof("Spawning a thread to consume CPU")
81+
go func() {
82+
_, err := io.Copy(ioutil.Discard, src)
83+
if err != nil {
84+
klog.Fatalf("failed to copy from /dev/zero to /dev/null: %v", err)
85+
}
86+
}()
87+
}
88+
}
89+
90+
func allocateMemory(total, stepSize resource.Quantity) {
91+
for i := int64(1); i*stepSize.Value() <= total.Value(); i++ {
92+
newBuffer := make([]byte, stepSize.Value())
93+
for i := range newBuffer {
94+
newBuffer[i] = 0
95+
}
96+
buffer = append(buffer, newBuffer)
97+
time.Sleep(argMemSleepDuration)
98+
}
99+
}

0 commit comments

Comments
 (0)