2018-06-11 15:13:19 +02:00
package registries
import (
2023-10-24 09:24:09 +13:00
"fmt"
2018-06-11 15:13:19 +02:00
"net/http"
2021-02-23 05:21:39 +02:00
portainer "github.com/portainer/portainer/api"
2021-07-14 11:15:21 +02:00
httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/http/security"
2024-05-09 08:10:10 +12:00
"github.com/portainer/portainer/api/pendingactions/handlers"
2023-09-01 19:27:02 -03:00
httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response"
2023-10-24 09:24:09 +13:00
"github.com/rs/zerolog/log"
2018-06-11 15:13:19 +02:00
)
2021-02-23 05:21:39 +02:00
// @id RegistryDelete
// @summary Remove a registry
// @description Remove a registry
2021-10-12 12:12:08 +13:00
// @description **Access policy**: restricted
2021-02-23 05:21:39 +02:00
// @tags registries
2021-11-30 15:31:16 +13:00
// @security ApiKeyAuth
2021-02-23 05:21:39 +02:00
// @security jwt
// @param id path int true "Registry identifier"
// @success 204 "Success"
// @failure 400 "Invalid request"
// @failure 404 "Registry not found"
// @failure 500 "Server error"
// @router /registries/{id} [delete]
2018-06-11 15:13:19 +02:00
func ( handler * Handler ) registryDelete ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2021-07-14 11:15:21 +02:00
securityContext , err := security . RetrieveRestrictedRequestContext ( r )
if err != nil {
2022-09-14 20:42:39 -03:00
return httperror . InternalServerError ( "Unable to retrieve info from request context" , err )
2024-06-18 15:59:12 -03:00
} else if ! securityContext . IsAdmin {
2022-09-14 20:42:39 -03:00
return httperror . Forbidden ( "Permission denied to delete registry" , httperrors . ErrResourceAccessDenied )
2021-07-14 11:15:21 +02:00
}
2018-06-11 15:13:19 +02:00
registryID , err := request . RetrieveNumericRouteVariableValue ( r , "id" )
if err != nil {
2022-09-14 20:42:39 -03:00
return httperror . BadRequest ( "Invalid registry identifier route variable" , err )
2018-06-11 15:13:19 +02:00
}
2023-10-24 09:24:09 +13:00
registry , err := handler . DataStore . Registry ( ) . Read ( portainer . RegistryID ( registryID ) )
if err != nil {
return httperror . InternalServerError ( fmt . Sprintf ( "Unable to load registry %q from the database" , registry . Name ) , err )
2018-06-11 15:13:19 +02:00
}
2024-06-18 15:59:12 -03:00
if err := handler . DataStore . Registry ( ) . Delete ( portainer . RegistryID ( registryID ) ) ; err != nil {
2022-09-14 20:42:39 -03:00
return httperror . InternalServerError ( "Unable to remove the registry from the database" , err )
2018-06-11 15:13:19 +02:00
}
2024-06-18 15:59:12 -03:00
handler . deleteKubernetesSecrets ( registry )
2023-10-24 09:24:09 +13:00
2018-06-11 15:13:19 +02:00
return response . Empty ( w )
}
2023-10-24 09:24:09 +13:00
2024-06-18 15:59:12 -03:00
func ( handler * Handler ) deleteKubernetesSecrets ( registry * portainer . Registry ) {
2023-10-24 09:24:09 +13:00
for endpointId , access := range registry . RegistryAccesses {
if access . Namespaces != nil {
// Obtain a kubeclient for the endpoint
endpoint , err := handler . DataStore . Endpoint ( ) . Endpoint ( endpointId )
if err != nil {
// Skip environments that can't be loaded from the DB
log . Warn ( ) . Err ( err ) . Msgf ( "Unable to load the environment with id %d from the database" , endpointId )
2024-06-18 15:59:12 -03:00
2023-10-24 09:24:09 +13:00
continue
}
2024-10-01 14:15:51 +13:00
cli , err := handler . K8sClientFactory . GetPrivilegedKubeClient ( endpoint )
2023-10-24 09:24:09 +13:00
if err != nil {
// Skip environments that can't get a kubeclient from
log . Warn ( ) . Err ( err ) . Msgf ( "Unable to get kubernetes client for environment %d" , endpointId )
2024-06-18 15:59:12 -03:00
2023-10-24 09:24:09 +13:00
continue
}
failedNamespaces := make ( [ ] string , 0 )
2024-06-18 15:59:12 -03:00
2023-10-24 09:24:09 +13:00
for _ , ns := range access . Namespaces {
2024-06-18 15:59:12 -03:00
if err := cli . DeleteRegistrySecret ( registry . ID , ns ) ; err != nil {
2023-10-24 09:24:09 +13:00
failedNamespaces = append ( failedNamespaces , ns )
log . Warn ( ) . Err ( err ) . Msgf ( "Unable to delete registry secret %q from namespace %q for environment %d. Retrying offline" , cli . RegistrySecretName ( registry . ID ) , ns , endpointId )
}
}
if len ( failedNamespaces ) > 0 {
2024-05-09 08:10:10 +12:00
handler . PendingActionsService . Create (
2024-06-10 09:32:52 -03:00
handlers . NewDeleteK8sRegistrySecrets ( endpointId , registry . ID , failedNamespaces ) ,
2024-05-09 08:10:10 +12:00
)
2023-10-24 09:24:09 +13:00
}
}
}
}