Skip to content

Commit

Permalink
[SAAS-1407] Update PacketCapture with a status subresource (projectca…
Browse files Browse the repository at this point in the history
…lico#486)

* [SAAS-1407] Update PacketCapture with a status subresource

* [CODE REVIEW] Use named values instead of string as key

* [CODE REVIEW] Add directory and more comments

Co-authored-by: Alina Militaru <[email protected]>
  • Loading branch information
asincu and asincu authored Jun 1, 2021
1 parent 8ed805f commit 75401bf
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 3 deletions.
32 changes: 32 additions & 0 deletions config/crd/crd.projectcalico.org_packetcaptures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ spec:
\"dev\" \t! has(label_name)"
type: string
type: object
status:
description: PacketCaptureStatus describes the files that have been captured,
for a given PacketCapture, on each node that generates packet capture
files
properties:
files:
items:
description: PacketCaptureFile describes files generated by a PacketCapture.
It describes the location of the packet capture files that is
identified via a node, its directory and the file names generated.
properties:
directory:
description: Directory represents the path inside the calico-node
container for the the generated files
type: string
fileNames:
description: 'FileNames represents the name of the generated
file for a PacketCapture ordered alphanumerically. The active
packet capture file will be identified using the following
schema: "{workload endpoint name}_{host network interface}.pcap"
. Rotated capture files name will contain an index matching
the rotation timestamp.'
items:
type: string
type: array
node:
description: Node identifies with a a physical node from the
cluster via its hostname
type: string
type: object
type: array
type: object
type: object
served: true
storage: true
Expand Down
3 changes: 2 additions & 1 deletion lib/apis/crd.projectcalico.org/v1/packetcapture_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ import (
type PacketCapture struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec v3.PacketCaptureSpec `json:"spec,omitempty"`
Spec v3.PacketCaptureSpec `json:"spec,omitempty"`
Status v3.PacketCaptureStatus `json:"status,omitempty"`
}
81 changes: 80 additions & 1 deletion lib/apis/v3/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions lib/apis/v3/packetcapture.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:subresource:status

// PacketCapture contains the configuration for any packet capture.
type PacketCapture struct {
Expand All @@ -21,6 +22,8 @@ type PacketCapture struct {
metav1.ObjectMeta `json:"metadata,omitempty"`
// Specification of the PacketCapture.
Spec PacketCaptureSpec `json:"spec,omitempty"`
// Status of the PacketCapture
Status PacketCaptureStatus `json:"status,omitempty"`
}

// PacketCaptureSpec contains the values of the packet capture.
Expand Down Expand Up @@ -54,6 +57,26 @@ type PacketCaptureSpec struct {
Selector string `json:"selector,omitempty" validate:"selector"`
}

// PacketCaptureStatus describes the files that have been captured, for a given PacketCapture, on each node
// that generates packet capture files
type PacketCaptureStatus struct {
Files []PacketCaptureFile `json:"files,omitempty"`
}

// PacketCaptureFile describes files generated by a PacketCapture. It describes the location of the packet capture files
// that is identified via a node, its directory and the file names generated.
type PacketCaptureFile struct {
// Node identifies with a a physical node from the cluster via its hostname
Node string `json:"node,omitempty"`
// Directory represents the path inside the calico-node container for the the generated files
Directory string `json:"directory,omitempty"`
// FileNames represents the name of the generated file for a PacketCapture ordered alphanumerically.
// The active packet capture file will be identified using the following schema:
// "{workload endpoint name}_{host network interface}.pcap" .
// Rotated capture files name will contain an index matching the rotation timestamp.
FileNames []string `json:"fileNames,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// PacketCaptureList contains a list of PacketCapture resources.
Expand Down
45 changes: 45 additions & 0 deletions lib/apis/v3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion lib/clientv3/packetcapture_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"time"

log "github.com/sirupsen/logrus"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -189,6 +191,33 @@ var _ = testutils.E2eDatastoreDescribe("PacketCapture tests", testutils.Datastor
testutils.Resource(apiv3.KindPacketCapture, namespace2, name2, spec2),
))

By("Setting status1 on resource")
status1 := apiv3.PacketCaptureStatus{
Files: []apiv3.PacketCaptureFile{
{
Node: "node1",
FileNames: []string{"file1", "file2"},
},
},
}
res.Status = status1
log.Infof("Before update %#v", res)
res, outError = c.PacketCaptures().Update(ctx, res, options.SetOptions{})
Expect(outError).ToNot(HaveOccurred())
log.Infof("After update %#v", res)
Expect(res.Status.Files).To(HaveLen(1))
Expect(res).To(MatchResourceWithStatus(apiv3.KindPacketCapture, namespace1, name1, spec2, status1))

By("Getting resource and verifying status1 is present")
res, outError = c.PacketCaptures().Get(ctx, namespace1, name1, options.GetOptions{})
log.Infof("After get %#v", res)
Expect(outError).ToNot(HaveOccurred())
Expect(res).To(MatchResourceWithStatus(apiv3.KindPacketCapture, namespace1, name1, spec2, status1))
Expect(res.Status.Files).To(HaveLen(1))

// Track the version of the updated name1 data.
rv1_3 := res.ResourceVersion

if config.Spec.DatastoreType != apiconfig.Kubernetes {
By("Deleting PacketCapture (namespace1/name1) with the old resource version")
_, outError = c.PacketCaptures().Delete(ctx, namespace1, name1, options.DeleteOptions{ResourceVersion: rv1_1})
Expand All @@ -197,7 +226,7 @@ var _ = testutils.E2eDatastoreDescribe("PacketCapture tests", testutils.Datastor
}

By("Deleting PacketCapture (namespace1/name1) with the new resource version")
dres, outError := c.PacketCaptures().Delete(ctx, namespace1, name1, options.DeleteOptions{ResourceVersion: rv1_2})
dres, outError := c.PacketCaptures().Delete(ctx, namespace1, name1, options.DeleteOptions{ResourceVersion: rv1_3})
Expect(outError).NotTo(HaveOccurred())
Expect(dres).To(MatchResource(apiv3.KindPacketCapture, namespace1, name1, spec2))

Expand Down

0 comments on commit 75401bf

Please sign in to comment.