func()

in appconfigmgrv2/api/webhooks/builtins/pod_webhook.go [542:592]


func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admission.Response {

	pod := &corev1.Pod{}

	log.Info("HandleUpdate:Start", req.Name, req.Namespace)

	err := a.decoder.Decode(req, pod)
	if err != nil {
		return admission.Errored(http.StatusBadRequest, err)
	}

	app := &appconfig.AppEnvConfigTemplateV2{}

	applicationName, err := getApplicationName(pod)
	if err != nil {
		log.Error(err, "Application annotation not found")

	}

	log.Info("HandleUpdate:applicationName", "applicationName", applicationName,
		"req.Namespace", req.Namespace, "req.Operation", req.Operation)

	err = localMgr.GetClient().Get(ctx, types.NamespacedName{Name: applicationName, Namespace: req.Namespace}, app)
	if err != nil {
		log.Error(err, "Application Does not Exist - working to see why it is not in scheme, hardcoded app to pubsub")
		//return admission.Errored(http.StatusBadRequest, err)
	}

	if req.Operation == "CREATE" {
		if err := a.handleGCPSecretIfNeeded(ctx, pod, app); err != nil {
			log.Error(err, "Application GCP Secret could not be handled see error")
			return admission.Errored(http.StatusBadRequest, err)
		}
		if err := a.handleServiceAccount(ctx, pod, app); err != nil {
			log.Error(err, "Handling service account")
			return admission.Errored(http.StatusBadRequest, err)
		}
	}

	if pod.Annotations == nil {
		pod.Annotations = map[string]string{}
	}
	pod.Annotations["example-mutating-admission-webhook"] = "foo"

	marshaledPod, err := json.Marshal(pod)
	if err != nil {
		return admission.Errored(http.StatusInternalServerError, err)
	}

	return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
}