From 38d491552fbda113e88e9e740a922dfca43d8755 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Fri, 7 Apr 2023 16:03:51 +0100 Subject: [PATCH 1/7] created unit test for the k8 describe package Signed-off-by: Philip-21 --- utils/kubernetes/describe/describe_test.go | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 utils/kubernetes/describe/describe_test.go diff --git a/utils/kubernetes/describe/describe_test.go b/utils/kubernetes/describe/describe_test.go new file mode 100644 index 00000000..e3b3f058 --- /dev/null +++ b/utils/kubernetes/describe/describe_test.go @@ -0,0 +1,88 @@ +package describe + +import ( + "testing" + + meshkitkube "github.com/layer5io/meshkit/utils/kubernetes" +) + +/* +The MockDescriber is used in the implementation of the Describe function to create a mock output that is returned when the +function is called with specific options +it takes in a Client object and a DescriberOptions object, and returns a string and an error +*/ +type MockDescriber struct { + DescribeFunc func(*meshkitkube.Client, DescriberOptions) (string, error) +} + +// Describe method of the MockDescriber simply calls DescribeFunc and returns the result. +func (m *MockDescriber) Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) { + return m.DescribeFunc(client, options) +} + +func TestDescribe(t *testing.T) { + //meshkitkube.Client provides the ability to interact with the Kubernetes API server + + //set up mock client client to return expected responses + mockClient := meshkitkube.Client{} + + //create test cases + testCases := []struct { + Name string + Options DescriberOptions + DescribeFunc func(*meshkitkube.Client, DescriberOptions) (string, error) + ExpectedOutput string + ExpectedError error + }{ + { + Name: "describe pod", + Options: DescriberOptions{ + Name: "test-pod", + Namespace: "test-namespace", + Type: Pod, + }, + /* + DescribeFunc field is a function that takes in a Client + object and a DescriberOptions object, and returns a string and an error + */ + DescribeFunc: func(client *meshkitkube.Client, options DescriberOptions) (string, error) { + return "Name: test-pod\nNamespace: test-namespace\n", nil + }, + ExpectedOutput: "Name: test-pod\nNamespace: test-namespace\n", + ExpectedError: nil, + }, + { + Name: "describe deployment", + Options: DescriberOptions{ + Name: "test-deployment", + Namespace: "test-namespace", + Type: Deployment, + }, + DescribeFunc: func(client *meshkitkube.Client, options DescriberOptions) (string, error) { + return "Name: test-deployment\nNamespace: test-namespace\n", nil + }, + ExpectedOutput: "Name: test-deployment\nNamespace: test-namespace\n", + ExpectedError: nil, + }, + } + + //run test cases + for _, tc := range testCases { + t.Run(tc.Name, func(t *testing.T) { + //create a mockDescriber + mockDescriber := &MockDescriber{ + DescribeFunc: tc.DescribeFunc, + } + output, err := mockDescriber.Describe(&mockClient, tc.Options) + + //check if the output and error match the expected values8 + if output != tc.ExpectedOutput { + t.Errorf("Test case %s failed. Expected: %s, but got: %s", tc.Name, tc.ExpectedOutput, output) + } + if err != tc.ExpectedError { + t.Errorf("Test case %s failed. Expected error: %v, but got: %v", tc.Name, tc.ExpectedError, err) + } + }) + } + +} From 6e9756d06f0379c6966fd057e66dcefff03f935a Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Mon, 10 Apr 2023 08:01:44 +0100 Subject: [PATCH 2/7] Added signoff DCO Signed-off-by: Philip-21 --- utils/kubernetes/describe/describe_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/kubernetes/describe/describe_test.go b/utils/kubernetes/describe/describe_test.go index e3b3f058..8a0c523c 100644 --- a/utils/kubernetes/describe/describe_test.go +++ b/utils/kubernetes/describe/describe_test.go @@ -7,9 +7,9 @@ import ( ) /* -The MockDescriber is used in the implementation of the Describe function to create a mock output that is returned when the -function is called with specific options -it takes in a Client object and a DescriberOptions object, and returns a string and an error + The MockDescriber is used in the implementation of the Describe function to create a mock output that is returned when the + function is called with specific options + it takes in a Client object and a DescriberOptions object, and returns a string and an error */ type MockDescriber struct { DescribeFunc func(*meshkitkube.Client, DescriberOptions) (string, error) From 66f02731d8bf3b05ebe00d5a0fa1defdb6e2bcd8 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Mon, 10 Apr 2023 21:38:45 +0100 Subject: [PATCH 3/7] created comments for the k8 describe.go file Signed-off-by: Philip-21 --- utils/kubernetes/describe/describe.go | 35 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/utils/kubernetes/describe/describe.go b/utils/kubernetes/describe/describe.go index 1a6d2afb..6ff0b8b3 100644 --- a/utils/kubernetes/describe/describe.go +++ b/utils/kubernetes/describe/describe.go @@ -1,5 +1,7 @@ package describe +// describe package provides a way to describe Kubernetes objects for the kubernetes Api + import ( meshkitkube "github.com/layer5io/meshkit/utils/kubernetes" appsv1 "k8s.io/api/apps/v1" @@ -17,13 +19,18 @@ import ( // DescriberOptions give control of which kubernetes object to describe. type DescriberOptions struct { - Name string // Name of the kubernetes obj - Namespace string // Namespace of the kubernetes obj - ShowEvents bool - ChunkSize int64 - Type DescribeType + Name string // Name of the kubernetes obj + Namespace string // Namespace of the kubernetes obj + ShowEvents bool // A boolean flag indicating whether to show events associated with the Kubernetes object or not. + ChunkSize int64 //Size of the chunk in which the Kubernetes object's output is written. + Type DescribeType //an integer value that represents the Kubernetes source that needs to be described } +/* +DescribeType represents the Kubernetes Source that needs to be Described +The integer value of the DescribeType is used to get the corresponding GroupKind information of the resource +from the ResourceMap variable, which is then used to get the describer function for that resource type. +*/ type DescribeType int const ( @@ -57,6 +64,12 @@ const ( EndpointSlice ) +/* + The "ResourceMap" map associates each "DescribeType" with a corresponding + Kubernetes GroupKind object. + which are used to identify the Kubernetes API resources that need to be described +*/ + var ResourceMap = map[DescribeType]schema.GroupKind{ Pod: {Group: corev1.GroupName, Kind: "Pod"}, Deployment: {Group: appsv1.GroupName, Kind: "Deployment"}, @@ -88,6 +101,11 @@ var ResourceMap = map[DescribeType]schema.GroupKind{ EndpointSlice: {Group: discoveryv1.GroupName, Kind: "EndpointSlice"}, } +/* +The Describe() takes in a Kubernetes client and options for describing a particular Kubernetes resource. +It retrieves the GroupKind object associated with the specified "DescribeType" from the ResourceMap, +and then calls a corresponding "describer" function to retrieve the description of the specified Kubernetes resource +*/ func Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) { // getting schema.GroupKind from Resource map kind := ResourceMap[options.Type] @@ -95,15 +113,18 @@ func Describe(client *meshkitkube.Client, options DescriberOptions) (string, err if !ok { return "", ErrGetDescriberFunc() } - describerSetting := describe.DescriberSettings{ ShowEvents: options.ShowEvents, ChunkSize: options.ChunkSize, } + //calls a corresponding "describer" function to retrieve the description of the specified Kubernetes resource output, err := describer.Describe(options.Namespace, options.Name, describerSetting) if err != nil { return "", err } - + /* + The output returned includes information such as the resource's metadata (name, namespace.) + and other details such as the resource's specifications, configuration, and associated events if ShowEvents option is set to true. + */ return output, nil } From 3b612b3f3dffc79fcb7d21cf8acd40bd4766a033 Mon Sep 17 00:00:00 2001 From: Philip Obiora <73377830+Philip-21@users.noreply.github.com> Date: Wed, 12 Apr 2023 00:31:02 -0700 Subject: [PATCH 4/7] Delete describe_test.go Signed-off-by: Philip Obiora <73377830+Philip-21@users.noreply.github.com> --- utils/kubernetes/describe/describe_test.go | 88 ---------------------- 1 file changed, 88 deletions(-) delete mode 100644 utils/kubernetes/describe/describe_test.go diff --git a/utils/kubernetes/describe/describe_test.go b/utils/kubernetes/describe/describe_test.go deleted file mode 100644 index 8a0c523c..00000000 --- a/utils/kubernetes/describe/describe_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package describe - -import ( - "testing" - - meshkitkube "github.com/layer5io/meshkit/utils/kubernetes" -) - -/* - The MockDescriber is used in the implementation of the Describe function to create a mock output that is returned when the - function is called with specific options - it takes in a Client object and a DescriberOptions object, and returns a string and an error -*/ -type MockDescriber struct { - DescribeFunc func(*meshkitkube.Client, DescriberOptions) (string, error) -} - -// Describe method of the MockDescriber simply calls DescribeFunc and returns the result. -func (m *MockDescriber) Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) { - return m.DescribeFunc(client, options) -} - -func TestDescribe(t *testing.T) { - //meshkitkube.Client provides the ability to interact with the Kubernetes API server - - //set up mock client client to return expected responses - mockClient := meshkitkube.Client{} - - //create test cases - testCases := []struct { - Name string - Options DescriberOptions - DescribeFunc func(*meshkitkube.Client, DescriberOptions) (string, error) - ExpectedOutput string - ExpectedError error - }{ - { - Name: "describe pod", - Options: DescriberOptions{ - Name: "test-pod", - Namespace: "test-namespace", - Type: Pod, - }, - /* - DescribeFunc field is a function that takes in a Client - object and a DescriberOptions object, and returns a string and an error - */ - DescribeFunc: func(client *meshkitkube.Client, options DescriberOptions) (string, error) { - return "Name: test-pod\nNamespace: test-namespace\n", nil - }, - ExpectedOutput: "Name: test-pod\nNamespace: test-namespace\n", - ExpectedError: nil, - }, - { - Name: "describe deployment", - Options: DescriberOptions{ - Name: "test-deployment", - Namespace: "test-namespace", - Type: Deployment, - }, - DescribeFunc: func(client *meshkitkube.Client, options DescriberOptions) (string, error) { - return "Name: test-deployment\nNamespace: test-namespace\n", nil - }, - ExpectedOutput: "Name: test-deployment\nNamespace: test-namespace\n", - ExpectedError: nil, - }, - } - - //run test cases - for _, tc := range testCases { - t.Run(tc.Name, func(t *testing.T) { - //create a mockDescriber - mockDescriber := &MockDescriber{ - DescribeFunc: tc.DescribeFunc, - } - output, err := mockDescriber.Describe(&mockClient, tc.Options) - - //check if the output and error match the expected values8 - if output != tc.ExpectedOutput { - t.Errorf("Test case %s failed. Expected: %s, but got: %s", tc.Name, tc.ExpectedOutput, output) - } - if err != tc.ExpectedError { - t.Errorf("Test case %s failed. Expected error: %v, but got: %v", tc.Name, tc.ExpectedError, err) - } - }) - } - -} From 4a4a05504cbd65403d58686779448708591c6ddd Mon Sep 17 00:00:00 2001 From: Philip Obiora <73377830+Philip-21@users.noreply.github.com> Date: Wed, 12 Apr 2023 13:07:49 -0700 Subject: [PATCH 5/7] adjusting comments making adjustments to comments Signed-off-by: Philip Obiora <73377830+Philip-21@users.noreply.github.com> --- utils/kubernetes/describe/describe.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/utils/kubernetes/describe/describe.go b/utils/kubernetes/describe/describe.go index 6ff0b8b3..18ca3690 100644 --- a/utils/kubernetes/describe/describe.go +++ b/utils/kubernetes/describe/describe.go @@ -26,11 +26,7 @@ type DescriberOptions struct { Type DescribeType //an integer value that represents the Kubernetes source that needs to be described } -/* -DescribeType represents the Kubernetes Source that needs to be Described -The integer value of the DescribeType is used to get the corresponding GroupKind information of the resource -from the ResourceMap variable, which is then used to get the describer function for that resource type. -*/ + // DescribeType is an integer value that represents the Kubernetes resource that needs to be described. type DescribeType int const ( @@ -64,12 +60,7 @@ const ( EndpointSlice ) -/* - The "ResourceMap" map associates each "DescribeType" with a corresponding - Kubernetes GroupKind object. - which are used to identify the Kubernetes API resources that need to be described -*/ - + // The ResourceMap variable contains the GroupKind information of all the Kubernetes resources that can be described. var ResourceMap = map[DescribeType]schema.GroupKind{ Pod: {Group: corev1.GroupName, Kind: "Pod"}, Deployment: {Group: appsv1.GroupName, Kind: "Deployment"}, @@ -101,11 +92,8 @@ var ResourceMap = map[DescribeType]schema.GroupKind{ EndpointSlice: {Group: discoveryv1.GroupName, Kind: "EndpointSlice"}, } -/* -The Describe() takes in a Kubernetes client and options for describing a particular Kubernetes resource. -It retrieves the GroupKind object associated with the specified "DescribeType" from the ResourceMap, -and then calls a corresponding "describer" function to retrieve the description of the specified Kubernetes resource -*/ +// The Describe() function takes a meshkitkube.Client object and a DescriberOptions object as input. + // And it returns the description of the specified Kubernetes resource as a string. func Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) { // getting schema.GroupKind from Resource map kind := ResourceMap[options.Type] @@ -122,9 +110,5 @@ func Describe(client *meshkitkube.Client, options DescriberOptions) (string, err if err != nil { return "", err } - /* - The output returned includes information such as the resource's metadata (name, namespace.) - and other details such as the resource's specifications, configuration, and associated events if ShowEvents option is set to true. - */ return output, nil } From 3bdd18476020b20938bbc9d00b842078f6427c8c Mon Sep 17 00:00:00 2001 From: Philip Obiora <73377830+Philip-21@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:47:09 -0700 Subject: [PATCH 6/7] removing spaces Signed-off-by: Philip Obiora <73377830+Philip-21@users.noreply.github.com> --- utils/kubernetes/describe/describe.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/utils/kubernetes/describe/describe.go b/utils/kubernetes/describe/describe.go index 18ca3690..e5ddb8cb 100644 --- a/utils/kubernetes/describe/describe.go +++ b/utils/kubernetes/describe/describe.go @@ -25,7 +25,6 @@ type DescriberOptions struct { ChunkSize int64 //Size of the chunk in which the Kubernetes object's output is written. Type DescribeType //an integer value that represents the Kubernetes source that needs to be described } - // DescribeType is an integer value that represents the Kubernetes resource that needs to be described. type DescribeType int @@ -59,7 +58,6 @@ const ( CertificateSigningRequest EndpointSlice ) - // The ResourceMap variable contains the GroupKind information of all the Kubernetes resources that can be described. var ResourceMap = map[DescribeType]schema.GroupKind{ Pod: {Group: corev1.GroupName, Kind: "Pod"}, @@ -91,9 +89,8 @@ var ResourceMap = map[DescribeType]schema.GroupKind{ CertificateSigningRequest: {Group: certificatesv1beta1.GroupName, Kind: "CertificateSigningRequest"}, EndpointSlice: {Group: discoveryv1.GroupName, Kind: "EndpointSlice"}, } - // The Describe() function takes a meshkitkube.Client object and a DescriberOptions object as input. - // And it returns the description of the specified Kubernetes resource as a string. +// And it returns the description of the specified Kubernetes resource as a string. func Describe(client *meshkitkube.Client, options DescriberOptions) (string, error) { // getting schema.GroupKind from Resource map kind := ResourceMap[options.Type] From c59ad50b5b40382a13c9ef3ce44386afe181541c Mon Sep 17 00:00:00 2001 From: Philip Obiora <73377830+Philip-21@users.noreply.github.com> Date: Thu, 13 Apr 2023 01:22:51 -0700 Subject: [PATCH 7/7] adjusting spaces Signed-off-by: Philip Obiora <73377830+Philip-21@users.noreply.github.com> --- utils/kubernetes/describe/describe.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/kubernetes/describe/describe.go b/utils/kubernetes/describe/describe.go index e5ddb8cb..2a61f8cc 100644 --- a/utils/kubernetes/describe/describe.go +++ b/utils/kubernetes/describe/describe.go @@ -25,7 +25,7 @@ type DescriberOptions struct { ChunkSize int64 //Size of the chunk in which the Kubernetes object's output is written. Type DescribeType //an integer value that represents the Kubernetes source that needs to be described } - // DescribeType is an integer value that represents the Kubernetes resource that needs to be described. +// DescribeType is an integer value that represents the Kubernetes resource that needs to be described. type DescribeType int const (