Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Feb 5, 2024
1 parent 2cbfb68 commit d393aca
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 266 deletions.
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
paths=./test/infrastructure/vcsim/controllers/... \
output:rbac:dir=$(VCSIM_RBAC_ROOT) \
rbac:roleName=manager-role
# rbac for capv-janitor
$(CONTROLLER_GEN) \
paths=$(JANITOR_DIR) \
output:rbac:dir=$(JANITOR_DIR)/config/rbac \
rbac:roleName=janitor-role

.PHONY: generate-go-deepcopy
generate-go-deepcopy: $(CONTROLLER_GEN) ## Generate deepcopy go code for core
Expand Down Expand Up @@ -762,6 +757,19 @@ clean-bin: ## Remove all generated binaries
rm -rf $(BIN_DIR)
rm -rf $(TOOLS_BIN_DIR)

.PHONY: clean-ci
clean-ci: ## Cleanup orphaned objects in CI
@if [ -z "${GOVC_USERNAME}" ]; then echo "GOVC_USERNAME is not set"; exit 1; fi
@if [ -z "${GOVC_PASSWORD}" ]; then echo "GOVC_PASSWORD is not set"; exit 1; fi
@if [ -z "${GOVC_URL}" ]; then echo "GOVC_URL is not set"; exit 1; fi
go run $(JANITOR_DIR) \
--dry-run=false \
--max-age=12h \
--ipam-namespace=default \
--folder=/SDDC-Datacenter/vm/Workloads/cluster-api-provider-vsphere \
--folder=/SDDC-Datacenter/vm/Workloads/cloud-provider-vsphere \
--folder=/SDDC-Datacenter/vm/Workloads/image-builder

.PHONY: clean-temporary
clean-temporary: ## Remove all temporary files and folders
rm -f minikube.kubeconfig
Expand Down
56 changes: 0 additions & 56 deletions hack/tools/janitor/Dockerfile

This file was deleted.

29 changes: 0 additions & 29 deletions hack/tools/janitor/Makefile

This file was deleted.

24 changes: 1 addition & 23 deletions hack/tools/janitor/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
# janitor

The janitor is a tool for CI to cleanup objects leftover from failed or killed prowjobs.
It can be run regularily as cronjob.
It can be run regularly as prowjob.

It tries to delete:

* vSphere: virtual machines in the configured folders which exist longer than the configured `--max-age` flag.
* vSphere: cluster modules which do not refer any virtual machine
* IPAM: IPAddressClaims which exist longer than the configured `--max-age` flag

## Deployment

1. (Optional:) Build and push image:

```sh
cd hack/tools/janitor
make build push
```

2. Deploy using kustomize and envsubst:

```sh
export
export VSPHERE_SERVER=""
export VSPHERE_USERNAME=""
export VSPHERE_PASSWORD=""
export VSPHERE_TLS_THUMBPRINT=""
cd hack/tools/janitor
kustomize build config/default | envsubst | kubectl apply -f -
```
11 changes: 0 additions & 11 deletions hack/tools/janitor/config/cronjob/credentials.yaml

This file was deleted.

47 changes: 0 additions & 47 deletions hack/tools/janitor/config/cronjob/cronjob.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions hack/tools/janitor/config/cronjob/kustomization.yaml

This file was deleted.

4 changes: 0 additions & 4 deletions hack/tools/janitor/config/cronjob/namespace.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions hack/tools/janitor/config/cronjob/service_account.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions hack/tools/janitor/config/default/kustomization.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions hack/tools/janitor/config/rbac/kustomization.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions hack/tools/janitor/config/rbac/role.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions hack/tools/janitor/config/rbac/rolebinding.yaml

This file was deleted.

18 changes: 9 additions & 9 deletions hack/tools/janitor/janitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func newJanitor(vSphereClients *vSphereClients, ipamClient client.Client, delta time.Duration, ipamNamespace string, dryRun bool) *janitor {
func newJanitor(vSphereClients *vSphereClients, ipamClient client.Client, maxAge time.Duration, ipamNamespace string, dryRun bool) *janitor {
return &janitor{
dryRun: dryRun,
ipamClient: ipamClient,
ipamNamespace: ipamNamespace,
maxCreationDate: time.Now().Add(-delta),
maxCreationDate: time.Now().Add(-maxAge),
vSphereClients: vSphereClients,
}
}
Expand All @@ -57,9 +57,9 @@ type virtualMachine struct {
object *object.VirtualMachine
}

// vSphereVMs deletes all vSphereVMs in a given folder in vSphere if their creation
// deleteVSphereVMs deletes all deleteVSphereVMs in a given folder in vSphere if their creation
// timestamp is before the janitor's configured maxCreationDate.
func (s *janitor) vSphereVMs(ctx context.Context, folder string) error {
func (s *janitor) deleteVSphereVMs(ctx context.Context, folder string) error {
log := ctrl.LoggerFrom(ctx).WithName("vSphereVMs").WithValues("folder", folder)
ctx = ctrl.LoggerInto(ctx, log)

Expand Down Expand Up @@ -124,6 +124,7 @@ func (s *janitor) vSphereVMs(ctx context.Context, folder string) error {
if err != nil {
return err
}
log.Info("Created PowerOff task for VM", "vm", vm.managedObject.Config.Name, "task", task.Name())
poweroffTasks = append(poweroffTasks, task)
}
// Wait for all PowerOff tasks to be finished. We intentionally ignore errors here
Expand All @@ -144,11 +145,12 @@ func (s *janitor) vSphereVMs(ctx context.Context, folder string) error {
if err != nil {
return err
}
log.Info("Created Destroy task for VM", "vm", vm.managedObject.Config.Name, "task", task.Name())
destroyTasks = append(destroyTasks, task)
}
// Wait for all destroy tasks to succeed.
if err := waitForTasksFinished(ctx, destroyTasks, false); err != nil {
return errors.Wrap(err, "failed to wait for vm poweroff task to finish")
return errors.Wrap(err, "failed to wait for vm destroy task to finish")
}

return nil
Expand All @@ -163,9 +165,7 @@ func waitForTasksFinished(ctx context.Context, tasks []*object.Task, ignoreError
return nil
}

// +kubebuilder:rbac:groups=ipam.cluster.x-k8s.io,resources=ipaddressclaims,verbs=get;list;watch;delete

func (s *janitor) ipAddressClaims(ctx context.Context) error {
func (s *janitor) deleteIPAddressClaims(ctx context.Context) error {
log := ctrl.LoggerFrom(ctx).WithName("IPAddressClaims")
ctrl.LoggerInto(ctx, log)
log.Info("Deleting IPAddressClaims")
Expand Down Expand Up @@ -202,7 +202,7 @@ func (s *janitor) ipAddressClaims(ctx context.Context) error {
return kerrors.NewAggregate(errList)
}

func (s *janitor) vSphereClusterModules(ctx context.Context) error {
func (s *janitor) deleteVSphereClusterModules(ctx context.Context) error {
log := ctrl.LoggerFrom(ctx).WithName("vSphere cluster modules")
ctrl.LoggerInto(ctx, log)
log.Info("Deleting vSphere cluster modules")
Expand Down
Loading

0 comments on commit d393aca

Please sign in to comment.