Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cns/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,3 +1100,36 @@

return &response, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the Lint erros in the PR

func (c *Client) DeleteEndpointState(ctx context.Context, endpointId string) (*cns.Response, error) {

Check failure on line 1104 in cns/client/client.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

var-naming: method parameter endpointId should be endpointID (revive)

Check failure on line 1104 in cns/client/client.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

var-naming: method parameter endpointId should be endpointID (revive)

Check failure on line 1104 in cns/client/client.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

var-naming: method parameter endpointId should be endpointID (revive)

Check failure on line 1104 in cns/client/client.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

var-naming: method parameter endpointId should be endpointID (revive)
// build the request
u := c.routes[cns.EndpointAPI]
uString := u.String() + endpointId
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uString, http.NoBody)
if err != nil {
return nil, errors.Wrap(err, "failed to build request")
}
req.Header.Set(headerContentType, contentTypeJSON)
res, err := c.client.Do(req)
if err != nil {
return nil, &ConnectionFailureErr{cause: err}
}

defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("http response %d", res.StatusCode)
}

var response cns.Response
err = json.NewDecoder(res.Body).Decode(&response)

if err != nil {
return nil, errors.Wrap(err, "failed to decode CNS Response")
}

if response.ReturnCode != 0 {
return nil, errors.New(response.Message)
}

return &response, nil
}
68 changes: 68 additions & 0 deletions cns/restserver/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@
service.GetEndpointHandler(w, r)
case http.MethodPatch:
service.UpdateEndpointHandler(w, r)
case http.MethodDelete:
service.DeleteEndpointStateHandler(w, r)
default:
logger.Errorf("[EndpointHandlerAPI] EndpointHandler API expect http Get or Patch method")
}
Expand Down Expand Up @@ -1327,3 +1329,69 @@
}
return nil
}

func (service *HTTPRestService) DeleteEndpointStateHandler(w http.ResponseWriter, r *http.Request) {
opName := "DeleteEndpointStateHandler"
logger.Printf("[DeleteEndpointStateHandler] DeleteEndpointState for %s", r.URL.Path)

Check failure on line 1335 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1335 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1335 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1335 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
endpointID := strings.TrimPrefix(r.URL.Path, cns.EndpointPath)

if service.EndpointStateStore == nil {
response := cns.Response{
ReturnCode: types.UnexpectedError,
Message: fmt.Sprintf("[DeleteEndpointStateHandler] EndpointStateStore is not initialized"),

Check failure on line 1341 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 1341 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 1341 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 1341 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)
}
err := common.Encode(w, &response)
logger.Response(opName, response, response.ReturnCode, err)

Check failure on line 1344 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1344 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1344 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1344 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
return
}

// Decode the request body to get ipInfo if needed
var req map[string]*IPInfo
err := common.Decode(w, r, &req)
if err != nil {
logger.Printf("[DeleteEndpointStateHandler] Failed to decode request body: %v", err)

Check failure on line 1352 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1352 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1352 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1352 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
// Continue with deletion even if decode fails, as ipInfo might not be needed
}

// Delete the endpoint from state
err = service.DeleteEndpointStateHelper(endpointID)
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request body decoding appears unnecessary for a DELETE operation. Consider removing this unused variable and the associated decode logic (lines 1349-1354) since DELETE requests typically don't require request body parsing for endpoint deletion.

Suggested change
err = service.DeleteEndpointStateHelper(endpointID)
// Delete the endpoint from state
err := service.DeleteEndpointStateHelper(endpointID)

Copilot uses AI. Check for mistakes.
if err != nil {
response := cns.Response{
ReturnCode: types.UnexpectedError,
Message: fmt.Sprintf("[DeleteEndpointStateHandler] Failed to delete endpoint state for %s with error: %s", endpointID, err.Error()),
}
err = common.Encode(w, &response)
logger.Response(opName, response, response.ReturnCode, err)

Check failure on line 1364 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1364 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1364 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1364 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
return
}

response := cns.Response{
ReturnCode: types.Success,
Message: "[DeleteEndpointStateHandler] Endpoint state deleted successfully",
}
err = common.Encode(w, &response)
logger.Response(opName, response, response.ReturnCode, err)

Check failure on line 1373 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1373 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1373 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1373 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Response is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
}

func (service *HTTPRestService) DeleteEndpointStateHelper(endpointID string) error {
if service.EndpointStateStore == nil {
return ErrStoreEmpty
}
logger.Printf("[deleteEndpointState] Deleting Endpoint state from state file %s", endpointID)

Check failure on line 1380 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1380 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1380 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1380 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
_, endpointExist := service.EndpointState[endpointID]
if !endpointExist {
logger.Printf("[deleteEndpointState] endpoint could not be found in the statefile %s", endpointID)

Check failure on line 1383 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1383 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1383 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)

Check failure on line 1383 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

SA1019: logger.Printf is deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead. (staticcheck)
return fmt.Errorf("[deleteEndpointState] endpoint %s does not exist in the statefile", endpointID)

Check failure on line 1384 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"[deleteEndpointState] endpoint %s does not exist in the statefile\", endpointID)" (err113)

Check failure on line 1384 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"[deleteEndpointState] endpoint %s does not exist in the statefile\", endpointID)" (err113)

Check failure on line 1384 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"[deleteEndpointState] endpoint %s does not exist in the statefile\", endpointID)" (err113)

Check failure on line 1384 in cns/restserver/ipam.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"[deleteEndpointState] endpoint %s does not exist in the statefile\", endpointID)" (err113)
}

// Delete the endpoint from the state
delete(service.EndpointState, endpointID)

// Write the updated state back to the store
err := service.EndpointStateStore.Write(EndpointStoreKey, service.EndpointState)
if err != nil {
return fmt.Errorf("[deleteEndpointState] failed to write endpoint state to store: %w", err)
}
logger.Printf("[deleteEndpointState] successfully deleted endpoint %s from state file", endpointID)
return nil
}
11 changes: 10 additions & 1 deletion network/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ func (nm *networkManager) SaveState(eps []*endpoint) error {
return nm.save()
}

func (nm *networkManager) DeleteState(_ []*EndpointInfo) error {
func (nm *networkManager) DeleteState(epInfos []*EndpointInfo) error {
nm.Lock()
defer nm.Unlock()

Expand All @@ -771,6 +771,15 @@ func (nm *networkManager) DeleteState(_ []*EndpointInfo) error {
// For stateless cni, plugin.ipamInvoker.Delete takes care of removing the state in the main Delete function

if nm.IsStatelessCNIMode() {
for _, epInfo := range epInfos {
if epInfo.NICType == cns.NodeNetworkInterfaceFrontendNIC {
response, err := nm.CnsClient.DeleteEndpointState(context.TODO(), epInfo.ContainerID)
if err != nil {
return errors.Wrapf(err, "Delete endpoint API returned with error for endpoint %s", epInfo.ContainerID)
}
logger.Info("Delete endpoint API returned", zap.String("endpointID", epInfo.ContainerID), zap.String("returnCode", response.ReturnCode.String()))
}
}
return nil
}

Expand Down
Loading