Skip to content

Commit

Permalink
controller: improve logging
Browse files Browse the repository at this point in the history
By making the `DynamicAttachmentRequest` implement the stringer
interface, we can have proper information whenever we log those
requests.

Signed-off-by: Miguel Duarte Barroso <[email protected]>
  • Loading branch information
maiqueb committed Sep 30, 2022
1 parent dde46c2 commit 184ee5a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ type DynamicAttachmentRequest struct {
PodNetNS string
}

func (dar *DynamicAttachmentRequest) String() string {
return fmt.Sprintf(
"{PodName: %s; PodNamespace: %s; AttachmentNames: %s; Type: %s; PodNetNS: %s}",
dar.PodName,
dar.PodNamespace,
fmt.Sprintf("[%s]", strings.Join(dar.derefAttachments(), ", ")),
dar.Type,
dar.PodNetNS,
)
}

func (dar *DynamicAttachmentRequest) derefAttachments() []string {
var attachmentNames []string
for _, attachment := range dar.AttachmentNames {
attachmentNames = append(attachmentNames, attachment.Name)
}
return attachmentNames
}

// PodNetworksController handles the cncf networks annotations update, and
// triggers adding / removing networks from a running pod.
type PodNetworksController struct {
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,28 @@ var _ = Describe("Dynamic Attachment controller", func() {
})
})

var _ = Describe("DynamicAttachmentRequest", func() {
It("an empty DynamicAttachmentRequest should present no information", func() {
dynamicAttachmentRequest := DynamicAttachmentRequest{}
Expect(dynamicAttachmentRequest.String()).To(
Equal("{PodName: ; PodNamespace: ; AttachmentNames: []; Type: ; PodNetNS: }"),
)
})
It("a full DynamicAttachmentRequest should present all information", func() {
dynamicAttachmentRequest := DynamicAttachmentRequest{
PodName: "tiny-pod",
PodNamespace: "ns1",
AttachmentNames: []*nad.NetworkSelectionElement{{Name: "attach1"}, {Name: "attach2"}},
Type: "add",
PodNetNS: "/path-to-ns",
}
Expect(dynamicAttachmentRequest.String()).To(
Equal(
"{PodName: tiny-pod; PodNamespace: ns1; AttachmentNames: [attach1, attach2]; Type: add; PodNetNS: /path-to-ns}"),
)
})
})

type dummyPodController struct {
*PodNetworksController
networkCache cache.Store
Expand Down

0 comments on commit 184ee5a

Please sign in to comment.