Closed matthyx closed 3 months ago
Summary:
Summary:
Summary:
Summary:
Summary:
Summary:
PR Description updated to latest commit (https://github.com/kubescape/storage/commit/82a0123c51633f658efedd4758c69fb3c98f5fa4)
⏱️ Estimated effort to review [1-5] | 4, because the PR introduces a significant new feature with multiple files across the codebase, including API changes, storage implementations, and tests. The complexity of the changes, especially around the new `NetworkNeighborhood` type and its integration, requires a thorough review to ensure compatibility, performance, and maintainability. Additionally, the deprecation of `NetworkNeighbors` in favor of `NetworkNeighborhood` necessitates careful consideration of backward compatibility and migration paths. |
🧪 Relevant tests | Yes |
🔍 Possible issues | Possible Bug: The transition logic in `PrepareForUpdate` within `pkg/registry/softwarecomposition/networkneighborhood/strategy.go` might not correctly handle all edge cases related to annotation updates. Specifically, the logic for handling changes in completion and status annotations could lead to unexpected behavior if not thoroughly tested. |
Performance Concern: The implementation of `deflateNetworkNeighbors` in `pkg/registry/file/networkneighborhood_processor.go` could potentially lead to performance issues with a large number of network neighbors due to the use of nested loops and set operations. Consider optimizing this function to handle large datasets more efficiently. | |
Deprecation Handling: The PR deprecates `NetworkNeighbors` in favor of `NetworkNeighborhood`. It's crucial to ensure that existing clients relying on `NetworkNeighbors` are not adversely affected and that there's a clear migration path to `NetworkNeighborhood`. | |
🔒 Security concerns | No |
Category | Suggestions |
Possible issue |
Remove misleading default value for array field.___ **Consider removing the default value for "dnsNames" as it is an array type. Providing adefault value for an array field, especially an empty string, can be misleading because the field expects an array, not a string.** [pkg/generated/openapi/zz_generated.openapi.go [3668]](https://github.com/kubescape/storage/pull/107/files#diff-9f4c1466e676f9e733cae72d369ffd5ff37f446116c98562807cf3bfb872ae95R3668-R3668) ```diff -Default: "", +Default: nil, ``` |
Remove misleading default maps for complex object references.___ **For themetadata and spec properties in schema_pkg_apis_softwarecomposition_v1beta1_NetworkNeighborhood , consider not setting a default map since these are references to other objects. Setting a default map could be misleading as it suggests that the object can be instantiated with empty defaults, which is not the case for complex types.** [pkg/generated/openapi/zz_generated.openapi.go [3744-3751]](https://github.com/kubescape/storage/pull/107/files#diff-9f4c1466e676f9e733cae72d369ffd5ff37f446116c98562807cf3bfb872ae95R3744-R3751) ```diff -Default: map[string]interface{}{}, +Default: nil, ``` | |
Remove default value for required string field.___ **For theNetworkNeighborhoodContainer schema, consider removing the default value for the "name" property. A default empty string might not be meaningful and could lead to validation issues or confusion if the name is expected to be provided explicitly.** [pkg/generated/openapi/zz_generated.openapi.go [3771]](https://github.com/kubescape/storage/pull/107/files#diff-9f4c1466e676f9e733cae72d369ffd5ff37f446116c98562807cf3bfb872ae95R3771-R3771) ```diff -Default: "", +Default: nil, ``` | |
Add nil pointer checks before dereferencing in conversion functions.___ **Consider checking for nil pointers before dereferencing them in the conversion functionsto prevent potential nil pointer dereferences. This is particularly important when converting between types where the source or destination could potentially be nil.** [pkg/apis/softwarecomposition/v1beta1/zz_generated.conversion.go [782-818]](https://github.com/kubescape/storage/pull/107/files#diff-2bc1d43253045f615835b4dd60a6a23470291fd031ef91c6d79e1b489922eaebR782-R818) ```diff -return Convert_v1beta1_NetworkNeighborhood_To_softwarecomposition_NetworkNeighborhood(a.(*NetworkNeighborhood), b.(*softwarecomposition.NetworkNeighborhood), scope) +aTyped, aOK := a.(*NetworkNeighborhood) +bTyped, bOK := b.(*softwarecomposition.NetworkNeighborhood) +if !aOK || !bOK { + return errors.New("type assertion failed") +} +return Convert_v1beta1_NetworkNeighborhood_To_softwarecomposition_NetworkNeighborhood(aTyped, bTyped, scope) ``` | |
Add nil check before copying
___
**To avoid potential nil pointer dereferences, add a nil check for | |
Enhancement |
Update required fields to exclude deprecated items.___ **Update the "Required" field forschema_pkg_apis_softwarecomposition_v1beta1_NetworkNeighbor to remove "dnsNames" if it is truly deprecated and not required. This ensures the schema accurately reflects required fields.** [pkg/generated/openapi/zz_generated.openapi.go [3713]](https://github.com/kubescape/storage/pull/107/files#diff-9f4c1466e676f9e733cae72d369ffd5ff37f446116c98562807cf3bfb872ae95R3713-R3713) ```diff -Required: []string{"identifier", "type", "dns", "dnsNames", "ports", "podSelector", "namespaceSelector", "ipAddress"}, +Required: []string{"identifier", "type", "dns", "ports", "podSelector", "namespaceSelector", "ipAddress"}, ``` |
Rename properties for clarity and consistency.___ **For consistency and clarity, consider renaming the "matchLabels" and "matchExpressions"properties in schema_pkg_apis_softwarecomposition_v1beta1_NetworkNeighborhoodSpec to follow a naming convention that clearly indicates their purpose and relation to label selection.** [pkg/generated/openapi/zz_generated.openapi.go [3866-3884]](https://github.com/kubescape/storage/pull/107/files#diff-9f4c1466e676f9e733cae72d369ffd5ff37f446116c98562807cf3bfb872ae95R3866-R3884) ```diff -"matchLabels": { -"matchExpressions": { +"selectorMatchLabels": { +"selectorMatchExpressions": { ``` | |
Wrap errors in conversion functions for better context.___ **For better error handling, consider wrapping the error returned by the conversionfunctions to provide more context about the failure.** [pkg/apis/softwarecomposition/v1beta1/zz_generated.conversion.go [812-818]](https://github.com/kubescape/storage/pull/107/files#diff-2bc1d43253045f615835b4dd60a6a23470291fd031ef91c6d79e1b489922eaebR812-R818) ```diff -return Convert_v1beta1_NetworkNeighborhoodSpec_To_softwarecomposition_NetworkNeighborhoodSpec(a.(*NetworkNeighborhoodSpec), b.(*softwarecomposition.NetworkNeighborhoodSpec), scope) +err := Convert_v1beta1_NetworkNeighborhoodSpec_To_softwarecomposition_NetworkNeighborhoodSpec(a.(*NetworkNeighborhoodSpec), b.(*softwarecomposition.NetworkNeighborhoodSpec), scope) +if err != nil { + return fmt.Errorf("error converting NetworkNeighborhoodSpec: %w", err) +} +return nil ``` | |
Deduplicate ports in test data to match expected output.___ **Consider deduplicating the ports inInitContainers and EphemeralContainers to match the expected want structure. This will ensure that the test accurately reflects the behavior of deduplicating ports for the same identifier, as seen in the want section for EphemeralContainers and InitContainers .**
[pkg/registry/file/networkneighborhood_processor_test.go [42-43]](https://github.com/kubescape/storage/pull/107/files#diff-1e8fc905fc32f32a00864c8487d79dc5af450bec8898b40df605e0fd33ff094dR42-R43)
```diff
-{Identifier: "a", Ports: []softwarecomposition.NetworkPort{{Name: "80"}}},
{Identifier: "a", Ports: []softwarecomposition.NetworkPort{{Name: "80"}}},
```
| |
Ensure object is modified as expected after PreSave.___ **It's recommended to verify not just the absence of errors withassert.NoError , but also to ensure that the object has been modified as expected after PreSave is called. This can be done by comparing the object against the want struct to ensure all fields are correctly updated.** [pkg/registry/file/networkneighborhood_processor_test.go [115]](https://github.com/kubescape/storage/pull/107/files#diff-1e8fc905fc32f32a00864c8487d79dc5af450bec8898b40df605e0fd33ff094dR115-R115) ```diff tt.wantErr(t, a.PreSave(tt.object), fmt.Sprintf("PreSave(%v)", tt.object)) +assert.Equal(t, tt.want, tt.object, "Expected object to match want after PreSave") ``` | |
Add test cases for expected errors to improve coverage.___ **For better test coverage, consider adding more test cases to cover scenarios where errorsare expected. This can help ensure that PreSave handles error conditions gracefully and as expected.** [pkg/registry/file/networkneighborhood_processor_test.go [109]](https://github.com/kubescape/storage/pull/107/files#diff-1e8fc905fc32f32a00864c8487d79dc5af450bec8898b40df605e0fd33ff094dR109-R109) ```diff -wantErr: assert.NoError, +wantErr: assert.Error, ``` | |
Add tests for edge cases to ensure robustness.___ **To ensure the test suite remains robust and future-proof, consider adding tests that coveredge cases, such as empty NetworkNeighborhood objects, objects with missing fields, or objects with invalid data. This can help identify potential issues in the PreSave logic that may not be caught by the current test scenarios.** [pkg/registry/file/networkneighborhood_processor_test.go [15]](https://github.com/kubescape/storage/pull/107/files#diff-1e8fc905fc32f32a00864c8487d79dc5af450bec8898b40df605e0fd33ff094dR15-R15) ```diff tests := []struct { + name: "Empty NetworkNeighborhood object", + object: &softwarecomposition.NetworkNeighborhood{}, + want: &softwarecomposition.NetworkNeighborhood{}, + wantErr: assert.NoError, ``` | |
Maintainability |
Refactor repeated conversion function additions into a loop or method.___ **To improve code readability and maintainability, consider refactoring the repeated patternof adding conversion functions into a loop or a separate method that accepts parameters for the varying parts.** [pkg/apis/softwarecomposition/v1beta1/zz_generated.conversion.go [782-820]](https://github.com/kubescape/storage/pull/107/files#diff-2bc1d43253045f615835b4dd60a6a23470291fd031ef91c6d79e1b489922eaebR782-R820) ```diff -if err := s.AddGeneratedConversionFunc((*NetworkNeighborhood)(nil), (*softwarecomposition.NetworkNeighborhood)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_NetworkNeighborhood_To_softwarecomposition_NetworkNeighborhood(a.(*NetworkNeighborhood), b.(*softwarecomposition.NetworkNeighborhood), scope) -}); err != nil { - return err +conversionFuncs := []struct{ + srcType interface{} + dstType interface{} + convertFunc func(interface{}, interface{}, conversion.Scope) error +}{ + {(*NetworkNeighborhood)(nil), (*softwarecomposition.NetworkNeighborhood)(nil), Convert_v1beta1_NetworkNeighborhood_To_softwarecomposition_NetworkNeighborhood}, + // Add other conversion functions here +} +for _, cf := range conversionFuncs { + if err := s.AddGeneratedConversionFunc(cf.srcType, cf.dstType, func(a, b interface{}, scope conversion.Scope) error { + return cf.convertFunc(a, b, scope) + }); err != nil { + return err + } } ``` |
Extract label matching logic into a separate method for clarity.___ **For better maintainability and readability, consider extracting the logic for matchinglabels into a separate method. This can make the List method cleaner and easier to understand, especially when dealing with complex label matching logic.** [pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/fake/fake_networkneighborhood.go [62-70]](https://github.com/kubescape/storage/pull/107/files#diff-202eb473fc760c3765d6f2b31c79e1beab43251925d6914dd5c049bc98f9a0d7R62-R70) ```diff -label, _, _ := testing.ExtractFromListOptions(opts) -if label == nil { - label = labels.Everything() -} -list := &v1beta1.NetworkNeighborhoodList{ListMeta: obj.(*v1beta1.NetworkNeighborhoodList).ListMeta} -for _, item := range obj.(*v1beta1.NetworkNeighborhoodList).Items { - if label.Matches(labels.Set(item.Labels)) { - list.Items = append(list.Items, item) - } +list, err := c.filterNetworkNeighborhoodsByLabel(obj.(*v1beta1.NetworkNeighborhoodList), opts) +if err != nil { + return nil, err } ``` | |
Enhance test readability with more descriptive names.___ **To improve test readability and maintainability, consider using table-driven tests withsubtests for different scenarios. This approach is already used, but enhancing it with more descriptive test names and possibly breaking down complex test cases into simpler subtests could make it easier to understand and maintain.** [pkg/registry/file/networkneighborhood_processor_test.go [22]](https://github.com/kubescape/storage/pull/107/files#diff-1e8fc905fc32f32a00864c8487d79dc5af450bec8898b40df605e0fd33ff094dR22-R22) ```diff -name: "NetworkNeighborhood with initContainers and ephemeralContainers", +name: "Test NetworkNeighborhood with various container types", ``` | |
Best practice |
Replace unsafe.Pointer conversions with type-safe methods.___ **Use type-safe conversions instead of unsafe.Pointer to convert slices of complex types.This enhances type safety and maintainability of the code.** [pkg/apis/softwarecomposition/v1beta1/zz_generated.conversion.go [3699]](https://github.com/kubescape/storage/pull/107/files#diff-2bc1d43253045f615835b4dd60a6a23470291fd031ef91c6d79e1b489922eaebR3699-R3699) ```diff -out.DNSNames = *(*[]string)(unsafe.Pointer(&in.DNSNames)) +out.DNSNames = make([]string, len(in.DNSNames)) +copy(out.DNSNames, in.DNSNames) ``` |
Use context.WithTimeout to manage request timeouts.___ **To avoid potential resource leaks, consider using thecontext.WithTimeout for operations that should be bound by a specific duration, instead of manually setting the timeout on the request.** [pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/networkneighborhood.go [81-84]](https://github.com/kubescape/storage/pull/107/files#diff-0818802f45648795691fe7e3ac98070de1cd1de4b86d963899e82e584400a4f0R81-R84) ```diff -var timeout time.Duration -if opts.TimeoutSeconds != nil { - timeout = time.Duration(*opts.TimeoutSeconds) * time.Second -} +ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Duration(*opts.TimeoutSeconds)*time.Second) +defer cancel() ``` | |
Handle errors returned by the
___
**Consider handling the error returned by |
Summary:
Type
enhancement
Description
NetworkNeighborhood
as a new collection type for network communications, deprecatingNetworkNeighbors
.NetworkNeighborhood
to various parts of the codebase, including APIs, clientsets, informers, listers, and storage.ApplicationProfile
andNetworkNeighborhood
data before saving.Changes walkthrough
19 files
network_types.go
Introduce NetworkNeighborhood and Deprecate NetworkNeighbors
pkg/apis/softwarecomposition/network_types.go
NetworkNeighborsList
andNetworkNeighbors
, useNetworkNeighborhoodList
andNetworkNeighborhood
instead.NetworkNeighborhood
,NetworkNeighborhoodList
,NetworkNeighborhoodSpec
, andNetworkNeighborhoodContainer
to representnetwork communications for a specific workload.
DNS
inNetworkNeighbor
, useDNSNames
instead.String()
method toNetworkPort
for better string representation.network_types.go
Introduce NetworkNeighborhood Types in v1beta1
pkg/apis/softwarecomposition/v1beta1/network_types.go
NetworkNeighborsList
andNetworkNeighbors
, useNetworkNeighborhoodList
andNetworkNeighborhood
instead.NetworkNeighborhood
,NetworkNeighborhoodList
,NetworkNeighborhoodSpec
, andNetworkNeighborhoodContainer
to representnetwork communications for a specific workload.
DNS
inNetworkNeighbor
, useDNSNames
instead.zz_generated.conversion.go
Generate Conversion Functions for NetworkNeighborhood
pkg/apis/softwarecomposition/v1beta1/zz_generated.conversion.go
NetworkNeighborhood
,NetworkNeighborhoodList
,NetworkNeighborhoodSpec
, andNetworkNeighborhoodContainer
.zz_generated.deepcopy.go
Generate DeepCopy Functions for NetworkNeighborhood
pkg/apis/softwarecomposition/v1beta1/zz_generated.deepcopy.go
NetworkNeighborhood
,NetworkNeighborhoodList
, andNetworkNeighborhoodSpec
.zz_generated.deepcopy.go
Generate DeepCopy Functions for NetworkNeighborhood
pkg/apis/softwarecomposition/zz_generated.deepcopy.go
NetworkNeighborhood
,NetworkNeighborhoodList
, andNetworkNeighborhoodSpec
.cleanup.go
Add Cleanup Handler for NetworkNeighborhoods
pkg/cleanup/cleanup.go - Added cleanup handler for `networkneighborhoods`.
generated_expansion.go
Add NetworkNeighborhoodExpansion Interface
pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/generated_expansion.go - Added `NetworkNeighborhoodExpansion` interface.
networkneighborhood.go
Implement ClientSet for NetworkNeighborhood
pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/networkneighborhood.go - Implemented clientset for `NetworkNeighborhood`.
softwarecomposition_client.go
Add NetworkNeighborhoods to SoftwareComposition Client
pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/softwarecomposition_client.go - Added `NetworkNeighborhoods` to the `SoftwareComposition` client.
generic.go
Add Informer for NetworkNeighborhood
pkg/generated/informers/externalversions/generic.go - Added informer for `NetworkNeighborhood`.
interface.go
Add NetworkNeighborhoods Informer Interface
pkg/generated/informers/externalversions/softwarecomposition/v1beta1/interface.go - Added `NetworkNeighborhoods` informer interface.
networkneighborhood.go
Implement NetworkNeighborhood Informer
pkg/generated/informers/externalversions/softwarecomposition/v1beta1/networkneighborhood.go - Implemented `NetworkNeighborhood` informer.
expansion_generated.go
Add NetworkNeighborhood Lister Expansion Interfaces
pkg/generated/listers/softwarecomposition/v1beta1/expansion_generated.go
NetworkNeighborhoodListerExpansion
andNetworkNeighborhoodNamespaceListerExpansion
interfaces.networkneighborhood.go
Implement Lister for NetworkNeighborhood
pkg/generated/listers/softwarecomposition/v1beta1/networkneighborhood.go - Implemented lister for `NetworkNeighborhood`.
applicationprofile_processor.go
Implement PreSave Processor for ApplicationProfile
pkg/registry/file/applicationprofile_processor.go
PreSave
processor forApplicationProfile
to deflate data.networkneighborhood_processor.go
Implement PreSave Processor for NetworkNeighborhood
pkg/registry/file/networkneighborhood_processor.go
PreSave
processor forNetworkNeighborhood
to deflate data.processor.go
Refactor Processors to Dedicated Files
pkg/registry/file/processor.go - Removed specific processors, now using dedicated files.
etcd.go
Add REST Storage for NetworkNeighborhood
pkg/registry/softwarecomposition/networkneighborhood/etcd.go - Added REST storage for `NetworkNeighborhood`.
strategy.go
Implement Strategy for NetworkNeighborhood
pkg/registry/softwarecomposition/networkneighborhood/strategy.go
NetworkNeighborhood
including validation andupdate preparation.
3 files
networkpolicy.go
Add FIXME for NetworkNeighborhood Transition
pkg/apis/softwarecomposition/networkpolicy/networkpolicy.go - Added FIXME comments to switch to `NetworkNeighborhood`.
types.go
Add FIXME Comments for Sorting
pkg/apis/softwarecomposition/types.go
flags.
networkpolicy.go
Add FIXME for NetworkNeighborhood Transition in v1beta1
pkg/apis/softwarecomposition/v1beta1/networkpolicy/networkpolicy.go - Added FIXME comments to switch to `NetworkNeighborhood`.
4 files
register.go
Register NetworkNeighborhood Types
pkg/apis/softwarecomposition/register.go
NetworkNeighborhood
andNetworkNeighborhoodList
with thescheme.
register.go
Register NetworkNeighborhood Types in v1beta1
pkg/apis/softwarecomposition/v1beta1/register.go
NetworkNeighborhood
andNetworkNeighborhoodList
with thescheme in v1beta1.
apiserver.go
Register NetworkNeighborhood Storage with API Server
pkg/apiserver/apiserver.go
networkNeighborhoodStorageImpl
and registerednetworkneighborhood
storage with the API server..dockerignore
Add .dockerignore File
.dockerignore
.dockerignore
file to exclude directories from Docker context.5 files
fake_networkneighborhood.go
Implement Fake ClientSet for NetworkNeighborhood
pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/fake/fake_networkneighborhood.go - Added fake clientset implementation for `NetworkNeighborhood`.
fake_softwarecomposition_client.go
Add NetworkNeighborhoods to Fake SoftwareComposition Client
pkg/generated/clientset/versioned/typed/softwarecomposition/v1beta1/fake/fake_softwarecomposition_client.go
NetworkNeighborhoods
to the fakeSoftwareComposition
client.applicationprofile_processor_test.go
Add Tests for ApplicationProfileProcessor
pkg/registry/file/applicationprofile_processor_test.go - Added tests for `ApplicationProfileProcessor`.
networkneighborhood_processor_test.go
Add Tests for NetworkNeighborhoodProcessor
pkg/registry/file/networkneighborhood_processor_test.go - Added tests for `NetworkNeighborhoodProcessor`.
strategy_test.go
Add Tests for NetworkNeighborhoodStrategy
pkg/registry/softwarecomposition/networkneighborhood/strategy_test.go - Added tests for `NetworkNeighborhoodStrategy`.
2 files
zz_generated.openapi.go
Generate OpenAPI Definitions for NetworkNeighborhood
pkg/generated/openapi/zz_generated.openapi.go
NetworkNeighborhood
,NetworkNeighborhoodList
,NetworkNeighborhoodSpec
, andNetworkNeighborhoodContainer
.01-example.yaml
Add Example YAML for NetworkNeighborhood
artifacts/networkneighborhood/01-example.yaml - Added example YAML for `NetworkNeighborhood`.