Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create patches for the default annotations that are being added #53

Merged
merged 1 commit into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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