从注释中检索整数

问题描述 投票:0回答:1

我有一个 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, exists := pod.Annotations["new-anno/sleep"]

    if exists {
            log.Debugf("    ResourceTrackable: true")
            log.Infof("    sleep: %s", sleepString)
    } else {
            return
    }

    res := strings.Split(sleepString, ",")
    i, err := strconv.Atoi(s)
    if err != nil {
        panic(err)
    }
    return i

}

但是我得到了一个错误:

 8.504 ./handler.go:92:17: not enough return values                                                                                                                                                                  8.504   have ()                                                                                                                                                                                                     
 8.504   want (int)
 8.504 ./handler.go:96:32: cannot use res (variable of type []string) as string value in argument to strconv.Atoi
go kubernetes annotations sleep
1个回答
0
投票

我不知道你为什么这样做,但

strings.Split(sleepString, ",")
返回
[]string
,所以
res
的类型是
[]string
。 然后,
strconv.Atoi()
函数会导致错误,因为它需要一个
string
参数,但
res
[]string
(不过我不知道为什么它在你的代码中是
s
)。

© www.soinside.com 2019 - 2024. All rights reserved.