Skip to content
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

feat: Implemented rename enclave method in container engine lib #755

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,48 @@ func (backend *KubernetesKurtosisBackend) GetEnclaves(
return matchingEnclaves, nil
}

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

// we need to call the K8s client in order to get the current annotations
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)
}

currentAnnotations := kubernetesResources.namespace.Annotations
if _, found := currentAnnotations[kubernetes_annotation_key_consts.EnclaveNameAnnotationKey.GetString()]; !found {
logrus.Debugf(
"Expected to find annotation with key '%s' for namespace with UUID '%s' while "+
"renaming it, but it was not found. It should not happens, it could be a "+
"bug in Kurtosis",
kubernetes_annotation_key_consts.EnclaveNameAnnotationKey.GetString(),
enclaveUuid,
)
}

// we need the copy the current annotations in order to preserve the values of the other keys
updatedAnnotations := currentAnnotations
updatedAnnotations[kubernetes_annotation_key_consts.EnclaveNameAnnotationKey.GetString()] = newName

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

if _, err := backend.kubernetesManager.UpdateNamespace(ctx, enclave.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
Original file line number Diff line number Diff line change
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: false,
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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