Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Aug 15, 2023
1 parent 13f5e32 commit 86378c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
14 changes: 5 additions & 9 deletions internal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ func (s *Service) Run(stop <-chan os.Signal) error {
}

func (s *Service) Mutate(ar admission.AdmissionReview) *admission.AdmissionResponse {
// Info.Printf("mutating\n")
// pt := admission.PatchTypeJSONPatch
// return &admission.AdmissionResponse{Allowed: true, PatchType: &pt, Patch: []byte(fmt.Sprintf(`[{"op":"replace","path":"","value":%s}]`, strings.ReplaceAll(string(ar.Request.Object.Raw), "${VAR}", "var-value")))}

Info.Printf("mutating pod")
podResource := metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
if ar.Request.Resource != podResource {
Error.Printf("expect resource to be %s\n", podResource)
Expand All @@ -103,11 +98,13 @@ func (s *Service) Mutate(ar admission.AdmissionReview) *admission.AdmissionRespo
}
}

Info.Printf("mutating pod")

raw := ar.Request.Object.Raw
pod := corev1.Pod{}

if _, _, err := deserializer.Decode(raw, nil, &pod); err != nil {
Error.Printf("error: %v\n", err)
Error.Printf("deserialization failed: %v\n", err)
return &admission.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
Expand All @@ -124,7 +121,6 @@ func (s *Service) Mutate(ar admission.AdmissionReview) *admission.AdmissionRespo
patchOps = append(patchOps, fmt.Sprintf(`{"op":"remove","path":"/spec/initContainers/%d/resources"}`, i))
}
patch := fmt.Sprintf("[%s]", strings.Join(patchOps, ","))
Info.Printf("%+v\n", patch)
return &admission.AdmissionResponse{Allowed: true, PatchType: &patchType, Patch: []byte(patch)}
}

Expand All @@ -144,7 +140,7 @@ func (s *Service) ServeAdmitHandler(w http.ResponseWriter, r *http.Request, admi
return
}

Info.Printf("handling request: %s", body)
Debug.Printf("handling request: %s", body)
var responseObj runtime.Object
if obj, gvk, err := deserializer.Decode(body, nil, nil); err != nil {
Error.Printf("decoding request failed: %v", err)
Expand All @@ -164,7 +160,7 @@ func (s *Service) ServeAdmitHandler(w http.ResponseWriter, r *http.Request, admi
responseObj = responseAdmissionReview

}
Info.Printf("sending response: %v\n", responseObj)
Debug.Printf("sending response: %v\n", responseObj)
respBytes, err := json.Marshal(responseObj)
if err != nil {
Error.Printf("marshalling response failed: %v\n", err)
Expand Down
11 changes: 11 additions & 0 deletions test/deploy/kubernetes/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ resources:
images:
- name: ghcr.io/airfocusio/kube-resourceless
newTag: 0.0.0-dev-amd64
patches:
- target:
version: v1
group: apps
kind: Deployment
name: kube-resourceless
patch: |
- op: add
path: /spec/template/spec/containers/0/args
value:
- --verbose
8 changes: 4 additions & 4 deletions test/examples/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ spec:
- infinite
resources:
requests:
cpu: 2000m
memory: 4096Mi
cpu: 100m
memory: 128Mi
limits:
cpu: 4000m
memory: 4096Mi
cpu: 100m
memory: 128Mi
- name: container-2
image: nginx:alpine
command:
Expand Down

0 comments on commit 86378c9

Please sign in to comment.