Skip to content

Conversation

alichaddad
Copy link
Contributor

No description provided.

@alichaddad alichaddad added the enhancement New feature or request label Sep 6, 2022
@alichaddad alichaddad requested review from bigkevmcd, sarataha and jmickey and removed request for bigkevmcd and sarataha September 6, 2022 11:54
Copy link
Contributor

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

I think we need to handle the removal of tenant from the file (with --prune).

@@ -104,27 +103,99 @@ func (t Tenant) Validate() error {
}

// CreateTenants creates resources for tenants given a file for definition.
func CreateTenants(ctx context.Context, config *Config, c client.Client, out io.Writer) error {
resources, err := GenerateTenantResources(config)
func CreateTenants(ctx context.Context, config *Config, c client.Client, prune bool, out io.Writer) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can update this to reflect what it does now?

It's now applying the Tenant resources.

It also doesn't handle deletion of removed tenants?

We can use client.HasLabels ?

if prune {
err := kubeClient.Delete(ctx, existingResource)
if err != nil {
return fmt.Errorf("failed to clean up tenant %s resources: %w", tenant.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be fatal?

Given how far into the process it could be, would it be better to output a message to say that it failed to delete a resource, and continue?

What do existing tools do in this case?


// cleanResources cleanup resources that should be pruned when certain configuration is removed
func cleanResources(ctx context.Context, tenant Tenant, kubeClient client.Client, newResources, existingResources []client.Object, prune bool, out io.Writer) error {
newResourcesMap := make(map[string]client.Object)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can make this code easier to understand, and removing the existing resources for delete tenants is part of that.

If you have a set of resources to create and a set of existing resources, you can calculate the set of resources that you need to create, the set of resources you need to update, and the set of resources you need to delete.

Copy link
Collaborator

@foot foot left a comment

Choose a reason for hiding this comment

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

Wicked :), works great too.

// upsert applies runtime objects to the cluster, if they already exist,
// patching them with type specific elements.
func upsert(ctx context.Context, kubeClient client.Client, obj client.Object, out io.Writer) error {
existing := runtimeObjectFromObject(obj)
objectID := fmt.Sprintf("%s/%s", strings.ToLower(obj.GetObjectKind().GroupVersionKind().GroupKind().String()), obj.GetName())
objectID := getObjectID(obj)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice 👍

}

// getResourcesToDelete returns list of resources to be deleted from resources to be generated and existing resources
func getResourcesToDelete(newResources, existingResources []client.Object) []client.Object {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clean 👍 , I'm coming around to golang being all verbose and explicit. If this gets more complicated and we wanna add another layer, https://github.com/kubernetes/apimachinery/blob/master/pkg/util/sets/string.go has been popping up in the core codebase which can be slick for stuff like this.

resourcesToDelete = existingResources.difference(newResources)

@alichaddad alichaddad merged commit ba02897 into main Sep 14, 2022
@alichaddad alichaddad deleted the 1252_prune_resources branch September 14, 2022 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants