我为kubernetes编写了mutating webhook,将默认类添加到新的入口对象。
核心部分是this:
func patch( annotations map[string]string, name string) []byte {
var patches []Patch
if len(annotations) == 0 {
klog.V(0).Infof("New annotation to %v.\n", name)
patchNew := Patch{"add", "/metadata/annotations", map[string]string{}}
patches = append(patches, patchNew)
}
if _, ok := annotations[key]; !ok {
klog.V(0).Infof("Added class %v to %v.\n", Class, name)
patchAdd := Patch{"add", "/metadata/annotations/" + strings.Replace(key, "/", "~1", -1), Class}
patches = append(patches, patchAdd)
} else {
klog.V(0).Info("No need to add class.\n")
}
bytes, _ := json.Marshal(patches)
return bytes
}
它可以工作,但是感觉很笨拙。此解决方案有意义吗,还是有一种更简单的方法来达到相同的结果?
(我不会做太多编程工作,欢迎进行任何改进。)