-
Notifications
You must be signed in to change notification settings - Fork 93
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
comments for K8 describe package #294
Changes from 4 commits
38d4915
6e9756d
66f0273
a085bf3
3b612b3
4a4a055
3bdd184
c59ad50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace to |
||
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,22 +101,30 @@ var ResourceMap = map[DescribeType]schema.GroupKind{ | |
EndpointSlice: {Group: discoveryv1.GroupName, Kind: "EndpointSlice"}, | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace to |
||
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] | ||
describer, ok := describe.DescriberFor(kind, &client.RestConfig) | ||
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 | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this code comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, i have made the changes |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace to
// DescribeType is an integer value that represents the Kubernetes resource that needs to be described.