Skip to content

Commit

Permalink
feat: Implemented rename enclave method in container engine lib (#755)
Browse files Browse the repository at this point in the history
## Description:
Implemented rename enclave method in container engine lib

## Is this change user facing?
NO

## References (if applicable):
This is part of the enclave pool project, more context here:
https://www.notion.so/kurtosistech/Enclave-Pool-Project-7273552e53e44b0f931a57568cd170a0
  • Loading branch information
leoporoli committed Jun 22, 2023
1 parent a7e58b9 commit f1570f7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
Expand Up @@ -357,6 +357,14 @@ func (backend *DockerKurtosisBackend) DestroyEnclaves(
return successfulNetworkRemovalEnclaveUuids, erroredEnclaveUuids, nil
}

func (backend *DockerKurtosisBackend) RenameEnclave(
ctx context.Context,
enclaveUuid enclave.EnclaveUUID,
newName string,
) error {
return stacktrace.NewError("RenameEnclave isn't implemented for Docker yet")
}

// ====================================================================================================
//
// Private helper methods
Expand Down
Expand Up @@ -157,6 +157,36 @@ func (backend *KubernetesKurtosisBackend) GetEnclaves(
return matchingEnclaves, nil
}

func (backend *KubernetesKurtosisBackend) RenameEnclave(
ctx context.Context,
enclaveUuid enclave.EnclaveUUID,
newName string,
) error {

enclave, kubernetesResources, err := backend.getSingleEnclaveAndKubernetesResources(ctx, enclaveUuid)
if err != nil {
return stacktrace.Propagate(err, "An error occurred getting enclave object and Kubernetes resources for enclave ID '%v'", enclaveUuid)
}
namespace := kubernetesResources.namespace
if namespace == nil {
return stacktrace.NewError("Cannot rename enclave '%v' because no Kubernetes namespace exists for it", enclaveUuid)
}

updatedAnnotations := map[string]string{
kubernetes_annotation_key_consts.EnclaveNameAnnotationKey.GetString(): newName,
}

namespaceApplyConfigurator := func(namespaceApplyConfig *applyconfigurationsv1.NamespaceApplyConfiguration) {
namespaceApplyConfig.WithAnnotations(updatedAnnotations)
}

if _, err := backend.kubernetesManager.UpdateNamespace(ctx, namespace.GetName(), namespaceApplyConfigurator); err != nil {
return stacktrace.Propagate(err, "An error occurred renaming enclave with UUID '%v', renaming from '%s' to '%s'", enclaveUuid, enclave.GetName(), newName)
}

return nil
}

func (backend *KubernetesKurtosisBackend) StopEnclaves(
ctx context.Context,
filters *enclave.EnclaveFilters,
Expand Down
Expand Up @@ -432,6 +432,35 @@ func (manager *KubernetesManager) CreateNamespace(
return namespaceResult, nil
}

func (manager *KubernetesManager) UpdateNamespace(
ctx context.Context,
namespaceName string,
// We use a configurator, rather than letting the user pass in their own NamespaceApplyConfiguration, so that we ensure
// they use the constructor (and don't do struct instantiation and forget to add the object name, etc. which
// would result in removing the object name)
updateConfigurator func(configuration *applyconfigurationsv1.NamespaceApplyConfiguration),
) (*apiv1.Namespace, error) {
updatesToApply := applyconfigurationsv1.Namespace(namespaceName)
updateConfigurator(updatesToApply)

namespaceClient := manager.kubernetesClientSet.CoreV1().Namespaces()

applyOpts := metav1.ApplyOptions{
TypeMeta: metav1.TypeMeta{
Kind: "",
APIVersion: "",
},
DryRun: nil,
Force: true, //We need to use force to avoid conflict errors
FieldManager: fieldManager,
}
result, err := namespaceClient.Apply(ctx, updatesToApply, applyOpts)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to update namespace '%v' ", namespaceName)
}
return result, nil
}

func (manager *KubernetesManager) RemoveNamespace(ctx context.Context, namespace *apiv1.Namespace) error {
name := namespace.Name
namespaceClient := manager.kubernetesClientSet.CoreV1().Namespaces()
Expand Down
Expand Up @@ -120,6 +120,19 @@ func (backend *MetricsReportingKurtosisBackend) GetEnclaves(
return results, nil
}

func (backend *MetricsReportingKurtosisBackend) RenameEnclave(
ctx context.Context,
enclaveUuid enclave.EnclaveUUID,
newName string,
) error {

if err := backend.underlying.RenameEnclave(ctx, enclaveUuid, newName); err != nil {
return stacktrace.Propagate(err, "An error occurred renaming enclave with UUID '%v' to '%s'", enclaveUuid, newName)
}

return nil
}

func (backend *MetricsReportingKurtosisBackend) StopEnclaves(
ctx context.Context,
filters *enclave.EnclaveFilters,
Expand Down
Expand Up @@ -91,6 +91,10 @@ func (backend *RemoteContextKurtosisBackend) GetEnclaves(ctx context.Context, fi
return backend.remoteKurtosisBackend.GetEnclaves(ctx, filters)
}

func (backend *RemoteContextKurtosisBackend) RenameEnclave(ctx context.Context, enclaveUuid enclave.EnclaveUUID, newName string) error {
return backend.remoteKurtosisBackend.RenameEnclave(ctx, enclaveUuid, newName)
}

func (backend *RemoteContextKurtosisBackend) StopEnclaves(ctx context.Context, filters *enclave.EnclaveFilters) (successfulEnclaveIds map[enclave.EnclaveUUID]bool, erroredEnclaveIds map[enclave.EnclaveUUID]error, resultErr error) {
return backend.remoteKurtosisBackend.StopEnclaves(ctx, filters)
}
Expand Down
Expand Up @@ -67,9 +67,16 @@ type KurtosisBackend interface {
// Dumps all of Kurtosis (engines + all enclaves)
DumpKurtosis(ctx context.Context, outputDirpath string) error

// Creates an enclave with the given enclave ID
// Creates an enclave with the given enclave UUID
CreateEnclave(ctx context.Context, enclaveUuid enclave.EnclaveUUID, enclaveName string, isPartitioningEnabled bool) (*enclave.Enclave, error)

// Rename an enclave with a new name by UUID
RenameEnclave(
ctx context.Context,
enclaveUuid enclave.EnclaveUUID,
newName string,
) error

// Gets enclaves matching the given filters
GetEnclaves(
ctx context.Context,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1570f7

Please sign in to comment.