doujiyun0041 2019-07-13 20:23
浏览 84

Kubernetes变异Webhook补丁问题

I am trying to get my head around Kubernetes Admission Controllers and trying to mutate a pod to include an init container and a volume in which all containers will mount.

The volume gets created and the init container mounts this volume but the original containers I can not get to mount the new volume. I am using Golang to write the webhook and using Json patch to mutate the pod. If anyone could point me in the right direction that would be great!



func serve(w http.ResponseWriter, r *http.Request) {
    body, err := ioutil.ReadAll(r.Body)

    if err != nil {
        glog.Error(err.Error())
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    if len(body) == 0 {
        glog.Error("the request body is empty")
        http.Error(w, "the request body is empty", http.StatusBadRequest)
        return
    }

    if r.Header.Get("Content-Type") != "application/json" {
        glog.Errorf("the content-type is %s, but expect application/json", r.Header.Get("Content-Type"))
        http.Error(w, "invalid content-type, expect `application/json`", http.StatusUnsupportedMediaType)
        return
    }

    request := v1beta1.AdmissionReview{}

    if _, _, err := universalDeserializer.Decode(body, nil, &request); err != nil {
        glog.Errorf("could not deserialize request: %s", err)
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    if request.Request == nil {
        glog.Error("could not find admission review in the request body")
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    response := v1beta1.AdmissionReview{Response: &v1beta1.AdmissionResponse{UID: request.Request.UID}}
    response.Response.Allowed = true

    var pod corev1.Pod
    if err := json.Unmarshal(request.Request.Object.Raw, &pod); err != nil {
        glog.Errorf("Could not unmarshal raw object to a pod object: %v", err)
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    required, err := mutationRequired(&pod.ObjectMeta)

    if err != nil {
        glog.Error(err)
    }

    if required {
        pbytes, _ := createPatch(&pod)
        response.Response.Patch = pbytes
    }

    reviewBytes, err := json.Marshal(response)

    if err != nil {
        glog.Error(err)
        http.Error(w, fmt.Sprintf("problem marshaling json: %v", err), http.StatusInternalServerError)
    }

    if _, err := w.Write(reviewBytes); err != nil {
        glog.Errorf("error writting response back to kubernetes: %v", err)
        http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
    }
}

func mutationRequired(m *metav1.ObjectMeta) (bool, error) {
    for _, n := range config.AllowedNamespaces {
        if m.Namespace != n {
            glog.Infof("request from pod %s is from namespace %s which is allowed", m.Name, m.Namespace)
            a := m.GetAnnotations()

            if len(a) < 1 {
                glog.Infof("pod %s has no annotations so mutation is not required", m.Name)
                return false, nil
            }

            if _, ok := a[enableKey]; ok {
                glog.Infof("annotation: %s found with value %s", enableKey, a[enableKey])
                b, err := strconv.ParseBool(a[enableKey])

                if err != nil {
                    glog.Errorf("was unable to parse the annotation value: %s", err)
                    return false, err
                }
                return b, nil
            }
            glog.Infof("pod %s has does not have the enable annotation so mutation not required", m.Name)
        }
    }
    glog.Infof("request from %s is coming from namespace %s which is not allowed", m.Name, m.Namespace)
    return false, nil
}

func addCredentialVolume() (patch patchOperation) {
    volume := corev1.Volume{
        Name: "vault-creds",
        VolumeSource: corev1.VolumeSource{
            EmptyDir: &corev1.EmptyDirVolumeSource{
                Medium: "Memory",
            },
        },
    }

    value := []corev1.Volume{volume}

    return patchOperation{
        Op:    "add",
        Path:  "/spec/volumes",
        Value: value,
    }
}
// this is the part not working 
func updateVolumeMounts(p *corev1.Pod) (patch patchOperation) {
    vmount := corev1.VolumeMount{
        Name:      "vault-creds",
        MountPath: "/creds",
        ReadOnly:  true,
    }
    for _, c := range p.Spec.Containers {
        c.VolumeMounts = append(c.VolumeMounts, vmount)
    }
    return patchOperation{
        Op:    "replace",
        Path:  "/spec/containers",
        Value: p.Spec.Containers,
    }
}

func addInitContainer(p *corev1.Pod) (patch patchOperation) {

    req := corev1.ResourceList{
        "cpu":    resource.MustParse("10m"),
        "memory": resource.MustParse("20Mi"),
    }

    lim := corev1.ResourceList{
        "cpu":    resource.MustParse("30m"),
        "memory": resource.MustParse("50Mi"),
    }

    vault := corev1.Container{
        Name:            "vault-init",
        Image:           config.Image,
        ImagePullPolicy: "Always",

        Resources: corev1.ResourceRequirements{
            Requests: req,
            Limits:   lim,
        },
        VolumeMounts: []corev1.VolumeMount{
            corev1.VolumeMount{
                Name:      "vault-creds",
                MountPath: "/creds",
            },
        },
        Command: []string{"echo", "'Hello' > /creds/cred.txt"},
    }
    p.Spec.InitContainers = append(p.Spec.InitContainers, vault)

    return patchOperation{
        Op:    "add",
        Path:  "/spec/initContainers",
        Value: p.Spec.InitContainers,
    }
}

func createPatch(p *corev1.Pod) ([]byte, error) {
    var patch []patchOperation
    patch = append(patch, addInitContainer(p))
    patch = append(patch, addCredentialVolume())
    patch = append(patch, updateVolumeMounts(p))
    return json.Marshal(patch)
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)