Skip to content

Commit

Permalink
Merge pull request hashicorp#53 from hashicorp/bug/20
Browse files Browse the repository at this point in the history
Create patches for the default annotations that are being added
  • Loading branch information
Rebecca Zanzig authored Jan 11, 2019
2 parents 4889ec4 + 4391bfa commit 18b704d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
29 changes: 24 additions & 5 deletions connect-inject/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ func (h *Handler) Mutate(req *v1beta1.AdmissionRequest) *v1beta1.AdmissionRespon
UID: req.UID,
}

// Accumulate any patches here
var patches []jsonpatch.JsonPatchOperation

// Setup the default annotation values that are used for the container.
// This MUST be done before shouldInject is called since k.
if err := h.defaultAnnotations(&pod); err != nil {
if err := h.defaultAnnotations(&pod, &patches); err != nil {
return &v1beta1.AdmissionResponse{
Result: &metav1.Status{
Message: err.Error(),
Expand All @@ -160,9 +163,6 @@ func (h *Handler) Mutate(req *v1beta1.AdmissionRequest) *v1beta1.AdmissionRespon
return resp
}

// Accumulate any patches here
var patches []jsonpatch.JsonPatchOperation

// Add our volume that will be shared by the init container and
// the sidecar for passing data in the pod.
patches = append(patches, addVolume(
Expand Down Expand Up @@ -263,14 +263,21 @@ func (h *Handler) shouldInject(pod *corev1.Pod) (bool, error) {
return !h.RequireAnnotation, nil
}

func (h *Handler) defaultAnnotations(pod *corev1.Pod) error {
func (h *Handler) defaultAnnotations(pod *corev1.Pod, patches *[]jsonpatch.JsonPatchOperation) error {
if pod.ObjectMeta.Annotations == nil {
pod.ObjectMeta.Annotations = make(map[string]string)
}

// Default service name is the name of the first container.
if _, ok := pod.ObjectMeta.Annotations[annotationService]; !ok {
if cs := pod.Spec.Containers; len(cs) > 0 {
// Create the patch for this first, so that the Annotation
// object will be created if necessary
*patches = append(*patches, updateAnnotation(
pod.Annotations,
map[string]string{annotationService: cs[0].Name})...)

// Set the annotation for checking in shouldInject
pod.ObjectMeta.Annotations[annotationService] = cs[0].Name
}
}
Expand All @@ -280,8 +287,20 @@ func (h *Handler) defaultAnnotations(pod *corev1.Pod) error {
if cs := pod.Spec.Containers; len(cs) > 0 {
if ps := cs[0].Ports; len(ps) > 0 {
if ps[0].Name != "" {
// Create the patch for this first, so that the Annotation
// object will be created if necessary
*patches = append(*patches, updateAnnotation(
pod.Annotations,
map[string]string{annotationPort: ps[0].Name})...)

pod.ObjectMeta.Annotations[annotationPort] = ps[0].Name
} else {
// Create the patch for this first, so that the Annotation
// object will be created if necessary
*patches = append(*patches, updateAnnotation(
pod.Annotations,
map[string]string{annotationPort: strconv.Itoa(int(ps[0].ContainerPort))})...)

pod.ObjectMeta.Annotations[annotationPort] = strconv.Itoa(int(ps[0].ContainerPort))
}
}
Expand Down
15 changes: 14 additions & 1 deletion connect-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func TestHandlerHandle(t *testing.T) {
},
"",
[]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/metadata/annotations",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -107,6 +111,10 @@ func TestHandlerHandle(t *testing.T) {
},
"",
[]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(annotationService),
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -176,6 +184,10 @@ func TestHandlerHandle(t *testing.T) {
},
"",
[]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/metadata/annotations/" + escapeJSONPointer(annotationService),
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -369,7 +381,8 @@ func TestHandlerDefaultAnnotations(t *testing.T) {
require := require.New(t)

var h Handler
err := h.defaultAnnotations(tt.Pod)
var patches []jsonpatch.JsonPatchOperation
err := h.defaultAnnotations(tt.Pod, &patches)
if (tt.Err != "") != (err != nil) {
t.Fatalf("actual: %v, expected err: %v", err, tt.Err)
}
Expand Down

0 comments on commit 18b704d

Please sign in to comment.