Skip to content

Commit

Permalink
CR-4773 - implemented runtime delete (#17)
Browse files Browse the repository at this point in the history
* implemented `runtime delete`
* name each trigger differently
* fix crbs subject.namespace
* set events and appset kustomization to specific hash
  • Loading branch information
ATGardner authored Jul 6, 2021
1 parent 0448926 commit 9e7eaf6
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.20
VERSION=v0.0.21
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
94 changes: 83 additions & 11 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ type (
RuntimeCreateOptions struct {
RuntimeName string
KubeContext string
KubeFactory kube.Factory
insCloneOpts *git.CloneOptions
gsCloneOpts *git.CloneOptions
insCloneOpts *git.CloneOptions
KubeFactory kube.Factory
}

RuntimeDeleteOptions struct {
RuntimeName string
KubeContext string
Timeout time.Duration
CloneOpts *git.CloneOptions
KubeFactory kube.Factory
}
)

Expand All @@ -66,6 +74,7 @@ func NewRuntimeCommand() *cobra.Command {
}

cmd.AddCommand(NewRuntimeCreateCommand())
cmd.AddCommand(NewRuntimeDeleteCommand())

return cmd
}
Expand All @@ -92,7 +101,7 @@ func NewRuntimeCreateCommand() *cobra.Command {
# Adds a new runtime
<BIN> runtime create runtime-name --install-owner owner --install-name gitops_repo
<BIN> runtime create runtime-name --install-repo gitops_repo
`),
PreRun: func(_ *cobra.Command, _ []string) {
if gsCloneOpts.Auth.Password == "" {
Expand All @@ -108,16 +117,17 @@ func NewRuntimeCreateCommand() *cobra.Command {
gsCloneOpts.Parse()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) < 1 {
log.G().Fatal("must enter runtime name")
log.G(ctx).Fatal("must enter runtime name")
}

return RunRuntimeCreate(cmd.Context(), &RuntimeCreateOptions{
return RunRuntimeCreate(ctx, &RuntimeCreateOptions{
RuntimeName: args[0],
KubeContext: "",
KubeFactory: f,
insCloneOpts: insCloneOpts,
KubeContext: cmd.Flag("context").Value.String(),
gsCloneOpts: gsCloneOpts,
insCloneOpts: insCloneOpts,
KubeFactory: f,
})
},
}
Expand Down Expand Up @@ -185,6 +195,66 @@ func RunRuntimeCreate(ctx context.Context, opts *RuntimeCreateOptions) error {
return nil
}

func NewRuntimeDeleteCommand() *cobra.Command {
var (
f kube.Factory
cloneOpts *git.CloneOptions
)

cmd := &cobra.Command{
Use: "delete [runtime_name]",
Short: "Deletes a Codefresh runtime",
Example: util.Doc(`
# To run this command you need to create a personal access token for your git provider
# and provide it using:
export GIT_TOKEN=<token>
# or with the flag:
--git-token <token>
# Adds a new runtime
<BIN> runtime delete runtime-name --repo gitops_repo
`),
PreRun: func(_ *cobra.Command, _ []string) {
cloneOpts.Parse()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) < 1 {
log.G(ctx).Fatal("must enter runtime name")
}

return RunRuntimeDelete(ctx, &RuntimeDeleteOptions{
RuntimeName: args[0],
KubeContext: cmd.Flag("context").Value.String(),
Timeout: aputil.MustParseDuration(cmd.Flag("request-timeout").Value.String()),
CloneOpts: cloneOpts,
KubeFactory: f,
})
},
}

cloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{
FS: memfs.New(),
})
f = kube.AddFlags(cmd.Flags())

return cmd
}

func RunRuntimeDelete(ctx context.Context, opts *RuntimeDeleteOptions) error {
return apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
Namespace: opts.RuntimeName,
KubeContext: opts.KubeContext,
Timeout: opts.Timeout,
CloneOptions: opts.CloneOpts,
KubeFactory: opts.KubeFactory,
})
}

func createApp(ctx context.Context, f kube.Factory, cloneOpts *git.CloneOptions, projectName, appName, appURL, appType, namespace string, wait bool) error {
timeout := time.Duration(0)
if wait {
Expand Down Expand Up @@ -242,9 +312,10 @@ func createComponentsReporter(ctx context.Context, cloneOpts *git.CloneOptions,
return err
}

return r.Persist(ctx, &git.PushOptions{
_, err = r.Persist(ctx, &git.PushOptions{
CommitMsg: "Created Codefresh Resources",
})
return err
}

func updateProject(repofs fs.FS, runtimeName string) error {
Expand Down Expand Up @@ -441,9 +512,10 @@ func createDemoWorkflowTemplate(ctx context.Context, gsCloneOpts *git.CloneOptio
return err
}

return gsRepo.Persist(ctx, &git.PushOptions{
_, err = gsRepo.Persist(ctx, &git.PushOptions{
CommitMsg: fmt.Sprintf("Created %s Directory", gsPath),
})
return err
}

func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsCloneOpts *git.CloneOptions, gsName, runtimeName string) error {
Expand Down Expand Up @@ -583,7 +655,7 @@ func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsClon
return err
}

err = insRepo.Persist(ctx, &git.PushOptions{
_, err = insRepo.Persist(ctx, &git.PushOptions{
CommitMsg: fmt.Sprintf("Created %s Resources", gsName),
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/commands/cli-v2_runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ cli-v2 runtime [flags]

* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops
* [cli-v2 runtime create](cli-v2_runtime_create.md) - Create a new Codefresh runtime
* [cli-v2 runtime delete](cli-v2_runtime_delete.md) - Deletes a Codefresh runtime

2 changes: 1 addition & 1 deletion docs/commands/cli-v2_runtime_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cli-v2 runtime create [runtime_name] [flags]
# Adds a new runtime
cli-v2 runtime create runtime-name --install-owner owner --install-name gitops_repo
cli-v2 runtime create runtime-name --install-repo gitops_repo
```

Expand Down
63 changes: 63 additions & 0 deletions docs/commands/cli-v2_runtime_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## cli-v2 runtime delete

Deletes a Codefresh runtime

```
cli-v2 runtime delete [runtime_name] [flags]
```

### Examples

```
# To run this command you need to create a personal access token for your git provider
# and provide it using:
export GIT_TOKEN=<token>
# or with the flag:
--git-token <token>
# Adds a new runtime
cli-v2 runtime delete runtime-name --repo gitops_repo
```

### Options

```
--as string Username to impersonate for the operation
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--cache-dir string Default cache directory (default "/home/user/.kube/cache")
--certificate-authority string Path to a cert file for the certificate authority
--client-certificate string Path to a client certificate file for TLS
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
--context string The name of the kubeconfig context to use
-t, --git-token string Your git provider api token [GIT_TOKEN]
-h, --help help for delete
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
-n, --namespace string If present, the namespace scope for this CLI request
--repo string Repository URL [GIT_REPO]
-s, --server string The address and port of the Kubernetes API server
--tls-server-name string Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
```

### Options inherited from parent commands

```
--auth-context string Run the next command using a specific authentication context
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
--request-timeout duration Request timeout (default 30s)
```

### SEE ALSO

* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes

10 changes: 2 additions & 8 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

### Linux
```bash
# get the latest version or change to a specific version
VERSION=$(curl --silent "https://api.github.com/repos/codefresh-io/cli-v2/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')

# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/$VERSION/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.21/cf-linux-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-* /usr/local/bin/cf
Expand All @@ -22,11 +19,8 @@ cf version

### Mac
```bash
# get the latest version or change to a specific version
VERSION=$(curl --silent "https://api.github.com/repos/codefresh-io/cli-v2/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')

# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/$VERSION/cf-darwin-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.21/cf-darwin-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-* /usr/local/bin/cf
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/argoproj-labs/applicationset v0.1.0
github.com/argoproj-labs/argocd-autopilot v0.2.8
github.com/argoproj-labs/argocd-autopilot v0.2.9
github.com/argoproj/argo-cd/v2 v2.0.3
github.com/argoproj/argo-events v1.3.1
github.com/argoproj/argo-workflows/v3 v3.1.0
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ github.com/ardielle/ardielle-go v1.5.2/go.mod h1:I4hy1n795cUhaVt/ojz83SNVCYIGsAF
github.com/ardielle/ardielle-tools v1.5.4/go.mod h1:oZN+JRMnqGiIhrzkRN9l26Cej9dEx4jeNG6A+AdkShk=
github.com/argoproj-labs/applicationset v0.0.0-20210614145856-2c62537a8e5a h1:8Nm2KtOu/G7NtoAgucj4TkX8rghzwgFJGXNrAmuz7gc=
github.com/argoproj-labs/applicationset v0.0.0-20210614145856-2c62537a8e5a/go.mod h1:5rxggh8ymYXedQDIYylNzIHe6jdshDNasIBCVJuR1iU=
github.com/argoproj-labs/argocd-autopilot v0.2.8 h1:whsV51FygB5OI2qGgLui0aHTVt/V+9M0VgOfz1U+m3I=
github.com/argoproj-labs/argocd-autopilot v0.2.8/go.mod h1:mFkBpj09ofv0oe4K7LcN3Ds1pAXUEOLum/Lk8KUJXH0=
github.com/argoproj-labs/argocd-autopilot v0.2.9 h1:zYStiQq1cCT1EkfwEpdMEVfSkj+7ZEe1CwilmXGoldQ=
github.com/argoproj-labs/argocd-autopilot v0.2.9/go.mod h1:o3HQ3wBzSFlLILnFDFWMkHetHTSjwwC30runsLvwVp8=
github.com/argoproj/argo-cd v1.8.1/go.mod h1:Vfl7OGgBC83dVWgq58wU6UR3kG864h0dtHEIQ8xqw4s=
github.com/argoproj/argo-cd v1.8.7 h1:CkIu8p/gcTY/fOZWM2tHuSCIAV2HggXjJftrT1IIT3k=
github.com/argoproj/argo-cd v1.8.7/go.mod h1:tqFZW5Lr9KBCDsvOaE5Fh8M1eJ1ThvR58pyyLv8Zqvs=
Expand All @@ -177,8 +177,9 @@ github.com/argoproj/argo-workflows/v3 v3.1.0 h1:YrTppFW7VYZsFnDB7/9A0nI3V+VVqozv
github.com/argoproj/argo-workflows/v3 v3.1.0/go.mod h1:Z8Wc7uDOGw8TRdhqqREHLFE5SAgS0ENqqwaLakv56MU=
github.com/argoproj/gitops-engine v0.2.1/go.mod h1:OxXp8YaT73rw9gEBnGBWg55af80nkV/uIjWCbJu1Nw0=
github.com/argoproj/gitops-engine v0.2.2/go.mod h1:OxXp8YaT73rw9gEBnGBWg55af80nkV/uIjWCbJu1Nw0=
github.com/argoproj/gitops-engine v0.3.2 h1:m5bjOk/bWwMsFBGFpurdK31/hC5UuLMQn0hAd51TlEk=
github.com/argoproj/gitops-engine v0.3.2/go.mod h1:IBHhAkqlC+3r/wBWUitWSidQhPzlLoSTWp2htq3dyQk=
github.com/argoproj/gitops-engine v0.3.3 h1:zRNwKRj3h+EBpciy/+Eyo4vW2GTG3UG4HXAdWn0mQRI=
github.com/argoproj/gitops-engine v0.3.3/go.mod h1:IBHhAkqlC+3r/wBWUitWSidQhPzlLoSTWp2htq3dyQk=
github.com/argoproj/pkg v0.2.0/go.mod h1:F4TZgInLUEjzsWFB/BTJBsewoEy0ucnKSq6vmQiD/yc=
github.com/argoproj/pkg v0.8.1/go.mod h1:ra+bQPmbVAoEL+gYSKesuigt4m49i3Qa3mE/xQcjCiA=
github.com/argoproj/pkg v0.9.0 h1:PfWWYykfcEQdN0g41XLbVh/aonTjD+dPkvDp3hwpLYM=
Expand Down
51 changes: 31 additions & 20 deletions manifests/argo-cd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- https://raw.githubusercontent.com/argoproj/argo-cd/v2.0.4/manifests/install.yaml
- https://raw.githubusercontent.com/argoproj-labs/applicationset/master/manifests/install.yaml # TODO: switch to the next release when available
- https://raw.githubusercontent.com/argoproj-labs/applicationset/master/manifests/install.yaml?ref=2c62537a8e5a # TODO: switch to the next release when available

# will be effective on argo-cd 2.1
configMapGenerator:
- name: argocd-cm
behavior: merge
literals:
- "timeout.reconciliation=5s"
# currently in use since we are on 2.0.4

patches:
- patch: |-
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: argocd-application-controller
spec:
template:
spec:
containers:
- name: argocd-application-controller
command:
- argocd-application-controller
- --status-processors
- "20"
- --operation-processors
- "10"
- --app-resync
- "5"
# reset the crbs to `subject.namespace: default`, so that argo-cd will later change them to the actual ns
- target:
group: rbac.authorization.k8s.io
version: v1
kind: ClusterRoleBinding
patch: |-
- op: replace
path: /subjects/0/namespace
value: default
# currently in use since we are on 2.0.4
- patch: |-
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: argocd-application-controller
spec:
template:
spec:
containers:
- name: argocd-application-controller
command:
- argocd-application-controller
- --status-processors
- "20"
- --operation-processors
- "10"
- --app-resync
- "5"
2 changes: 1 addition & 1 deletion manifests/argo-events/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
# events from master to get the secureHeaders for now, should move to next release when available
- https://raw.githubusercontent.com/argoproj/argo-events/master/manifests/install.yaml
- https://raw.githubusercontent.com/argoproj/argo-events/master/manifests/install.yaml?ref=d403c441bc1d
- eventbus.yaml
2 changes: 1 addition & 1 deletion pkg/eventUtils/eventUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func CreateTrigger(opts *CreateTriggerOptions) *sensorsv1alpha1.Trigger {
return &sensorsv1alpha1.Trigger{
Template: &sensorsv1alpha1.TriggerTemplate{
Conditions: opts.Conditions,
Name: "http-trigger",
Name: opts.DependencyName,
HTTP: &sensorsv1alpha1.HTTPTrigger{
URL: opts.URL,
Method: "POST",
Expand Down

0 comments on commit 9e7eaf6

Please sign in to comment.