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

Internal restructure #161

Merged
merged 14 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/chart-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Determine Go version from go.mod
run: echo "GO_VERSION=$(grep "go 1." go.mod | cut -d " " -f 2)" >> $GITHUB_ENV

- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- uses: actions/cache@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
CC_TEST_REPORTER_ID: 273b145c3b441d2abc054951c66b99a6d55f9eb4a25ddacd0731e14fcc921dc2
with:
coverageLocations: cover.out:gocov
prefix: github.com/${{ github.event.repository.name }}
prefix: github.com/${{ github.repository }}
44 changes: 0 additions & 44 deletions base_command.go

This file was deleted.

1 change: 1 addition & 0 deletions charts/clustercode/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ else
sed := sed -i
endif

.PHONY: $(rbac_gen_tgt)
$(rbac_gen_tgt):
@cp $(rbac_gen_src) $@
@yq -i e '.metadata.name="{{ include \"clustercode.fullname\" . }}-manager", del(.metadata.creationTimestamp)' $@
Expand Down
34 changes: 22 additions & 12 deletions charts/clustercode/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ rules:
- apiGroups:
- clustercode.github.io
resources:
- clustercodeplans
- clustercodetasks
- blueprints
- tasks
verbs:
- get
- list
- watch
- apiGroups:
- clustercode.github.io
resources:
- clustercodeplans/finalizers
- clustercodeplans/status
- clustercodetasks/finalizers
- clustercodetasks/status
- blueprints/finalizers
- blueprints/status
- tasks/finalizers
- tasks/status
verbs:
- get
---
Expand All @@ -39,8 +39,8 @@ rules:
- apiGroups:
- clustercode.github.io
resources:
- clustercodeplans
- clustercodetasks
- blueprints
- tasks
verbs:
- create
- delete
Expand All @@ -52,12 +52,22 @@ rules:
- apiGroups:
- clustercode.github.io
resources:
- clustercodeplans/finalizers
- clustercodeplans/status
- clustercodetasks/finalizers
- clustercodetasks/status
- blueprints/finalizers
- blueprints/status
- tasks/finalizers
- tasks/status
verbs:
- get
- patch
- update
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
- create
- update
{{- end }}
4 changes: 4 additions & 0 deletions charts/clustercode/templates/operator-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ rules:
- apiGroups:
- clustercode.github.io
resources:
- blueprints/finalizers
- blueprints/status
verbs:
- get
Expand All @@ -73,6 +74,7 @@ rules:
- apiGroups:
- clustercode.github.io
resources:
- tasks/finalizers
- tasks/status
verbs:
- get
Expand Down Expand Up @@ -108,6 +110,7 @@ rules:
- delete
- get
- list
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand All @@ -118,3 +121,4 @@ rules:
- delete
- get
- list
- watch
78 changes: 11 additions & 67 deletions cleanup_command.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,26 @@
package main

import (
"os"
"path/filepath"

"github.com/ccremer/clustercode/api/v1alpha1"
"github.com/ccremer/clustercode/pkg/operator/controllers"
"github.com/ccremer/clustercode/pkg/cleanupcmd"
"github.com/urfave/cli/v2"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
controllerruntime "sigs.k8s.io/controller-runtime"
)

type cleanupCommand struct {
kubeconfig *rest.Config
kube client.Client

TaskName string
TaskNamespace string
SourceRootDir string
}

var cleanupCommandName = "cleanup"
var cleanupLog = ctrl.Log.WithName("cleanup")

func newCleanupCommand() *cli.Command {
command := &cleanupCommand{}
command := cleanupcmd.Command{}
return &cli.Command{
Name: cleanupCommandName,
Name: "cleanup",
Usage: "Remove intermediary files and finish the task",
Action: command.execute,
Before: LogMetadata,
Action: func(ctx *cli.Context) error {
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
controllerruntime.SetLogger(command.Log)
return command.Execute(controllerruntime.LoggerInto(ctx.Context, command.Log))
},
Flags: []cli.Flag{
newTaskNameFlag(&command.TaskName),
newNamespaceFlag(&command.TaskNamespace),
newNamespaceFlag(&command.Namespace),
newSourceRootDirFlag(&command.SourceRootDir),
},
}
}

func (c *cleanupCommand) execute(ctx *cli.Context) error {

registerScheme()
if err := createClientFn(&commandContext{}); err != nil {
return err
}

nsName := types.NamespacedName{Namespace: c.TaskNamespace, Name: c.TaskName}
task := &v1alpha1.Task{}
cleanupLog.Info("get task", "name", nsName.String())
if err := c.kube.Get(ctx.Context, nsName, task); err != nil {
return err
}

intermediaryFiles, err := filepath.Glob(filepath.Join(c.SourceRootDir, controllers.IntermediateSubMountPath, task.Spec.TaskId.String()+"*"))
if err != nil {
return err
}
cleanupLog.Info("deleting intermediary files", "files", intermediaryFiles)
deleteFiles(intermediaryFiles)

sourceFile := filepath.Join(c.SourceRootDir, controllers.SourceSubMountPath, task.Spec.SourceUrl.GetPath())
cleanupLog.Info("deleting source file", "file", sourceFile)
if err := os.Remove(sourceFile); err != nil {
return err
}
if err := c.kube.Delete(ctx.Context, task); err != nil {
return err
}
return nil
}

func deleteFiles(files []string) {
for _, file := range files {
if err := os.Remove(file); err != nil {
cleanupLog.Info("could not delete file", "file", file, "error", err.Error())
} else {
cleanupLog.V(1).Info("deleted file", "file", file)
}
}
}