我有一个 pod.yaml,我想检索一个新的注释整数值,以进行睡眠。所以我使用 Go 来获取名称后面的整数:new-anno。有了这个值,我想返回一个函数来在 main Part
上制作
time.Sleep(SleepFunct * timeSecond)
代码来自https://github.com/CyCognito/k8s-controller-sidecars/tree/master
我想添加新的纪元
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
annotations:
new-anno/sleep: "5"
我的处理程序。去
package main
import (
"strings"
"time"
"strconv"
"github.com/avast/retry-go"
log "github.com/sirupsen/logrus"
core_v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/clientcmd"
set "github.com/deckarep/golang-set"
)
type SidecarShutdownHandler struct{}
func (t *SidecarShutdownHandler) Init() error {
log.Info("SidecarShutdownHandler.Init")
return nil
}
func (t *SidecarShutdownHandler) SleepFunct(obj interface{}) int{
pod := obj.(*core_v1.Pod)
sleepString:= pod.Annotations["new-anno/sleep"]
i, err := strconv.Atoi(sleepString)
if err != nil {
panic(err)
}
return i
}
main.go
func main() {
....
time.Sleep(SleepFunct * timeSecond)
....
}
如何调用返回整数 time.Sleep(SleepFunct * timeSecond) 的函数
我不知道你为什么这样做,但
strings.Split(sleepString, ",")
返回[]string
,所以res
的类型是[]string
。
然后, strconv.Atoi()
函数会导致错误,因为它需要一个 string
参数,但 res
是 []string
(不过我不知道为什么它在你的代码中是 s
)。