Skip to content

Commit

Permalink
Merge pull request #379 from jenkins-x-plugins/cleanupdestroy
Browse files Browse the repository at this point in the history
chore: Cleanup and upgrade
  • Loading branch information
jenkins-x-bot authored Sep 1, 2022
2 parents 9293a3b + ec2ae4d commit c570d89
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 103 deletions.
10 changes: 2 additions & 8 deletions OWNERS
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
approvers:
- rawlingsj
- jstrachan
- rajdavies
- ankitm123
- maintainers
reviewers:
- rawlingsj
- jstrachan
- rajdavies
- ankitm123
- maintainers
9 changes: 3 additions & 6 deletions OWNERS_ALIASES
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
aliases:
- rawlingsj
best-approvers:
- rawlingsj
best-reviewers:
- rawlingsj
foreignAliases:
- name: jx-community
org: jenkins-x
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require (
github.com/cpuguy83/go-md2man v1.0.10
github.com/jenkins-x-plugins/jx-gitops v0.3.27
github.com/jenkins-x/go-scm v1.11.16
github.com/jenkins-x/jx-api/v4 v4.3.7
github.com/jenkins-x/jx-helpers/v3 v3.2.16
github.com/jenkins-x/jx-api/v4 v4.4.0
github.com/jenkins-x/jx-helpers/v3 v3.3.1
github.com/jenkins-x/jx-kube-client/v3 v3.0.3
github.com/jenkins-x/jx-logging/v3 v3.0.9
github.com/pkg/errors v0.9.1
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -849,13 +849,12 @@ github.com/jenkins-x/go-scm v1.11.5 h1:bWfyBlcDB/HHSRmJOSmBs2l4VRL2YF1Ka6Y1dn4In
github.com/jenkins-x/go-scm v1.11.5/go.mod h1:GB6XjszezsDOxKTsPoyk4MT/cKw30qkPdJ4tml+MImg=
github.com/jenkins-x/jx-api/v4 v4.1.5/go.mod h1:l11kHlFy40UGu9pdhCRxDiJcEgRubSVzybWB2jXjLcs=
github.com/jenkins-x/jx-api/v4 v4.3.0/go.mod h1:l11kHlFy40UGu9pdhCRxDiJcEgRubSVzybWB2jXjLcs=
github.com/jenkins-x/jx-api/v4 v4.3.6/go.mod h1:BjLd7lmBB0CpqA1U2IZ2AyEQ2A66U1lDUBz/lsvld3A=
github.com/jenkins-x/jx-api/v4 v4.3.7 h1:SHDH+Z7kN8YBS/LzQTa69u5qWaYxraXIYjPSTmNnad8=
github.com/jenkins-x/jx-api/v4 v4.3.7/go.mod h1:1ZPMGLTfWeDz4CoB+QBScVQM+AjS780osJ4W6mHJWlk=
github.com/jenkins-x/jx-api/v4 v4.4.0 h1:N7nd0ao7sxv8l12YIMYSjRnbmhFn6qLQM4wBbI/n1o0=
github.com/jenkins-x/jx-api/v4 v4.4.0/go.mod h1:1ZPMGLTfWeDz4CoB+QBScVQM+AjS780osJ4W6mHJWlk=
github.com/jenkins-x/jx-helpers/v3 v3.0.127/go.mod h1:0U5fcXnqSv5ugx+XMZ2rYT+VU3o+pyJeXJEiNMAVeSU=
github.com/jenkins-x/jx-helpers/v3 v3.1.0/go.mod h1:O1nLQJKgoTao86yXM7cqKpgVk/LdqjV+nsMqNnVBl9k=
github.com/jenkins-x/jx-helpers/v3 v3.2.16 h1:DwN0vxSJHz1L8yaZaJxqYOjDZ0evLpdRosWxW8JvQsI=
github.com/jenkins-x/jx-helpers/v3 v3.2.16/go.mod h1:qC+koAUZJwftXaovby2FU2nawpGGXQaDVb5Q/FIuwzs=
github.com/jenkins-x/jx-helpers/v3 v3.3.1 h1:CK2jUtbK2dH+POprkkeHmLEaNqiYy9SfR7kyD1MhL/Q=
github.com/jenkins-x/jx-helpers/v3 v3.3.1/go.mod h1:wHq/JlH9YwNRV89Wge2E9xGsx6qEuuQFpZgCxkkJfAw=
github.com/jenkins-x/jx-kube-client/v3 v3.0.2/go.mod h1:C/mKnCT5wvolX61eLKJVBNev9sqnkGNpi4skTQ1Gr3Q=
github.com/jenkins-x/jx-kube-client/v3 v3.0.3 h1:yEHZKfVe9THcDSchwWuGKvIDydVzLUdJDxVss9vWmnk=
github.com/jenkins-x/jx-kube-client/v3 v3.0.3/go.mod h1:qiVO4ufdpbSXBFx3EDhjk4VUpHVoE0ItvnPYQe5hpPc=
Expand Down
28 changes: 14 additions & 14 deletions pkg/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func (o *Options) Run() error {

destroyCmd := o.createDestroyCommand(envVars)

// lets get the git clone URL with user/password so we can clone it again in the destroy command/CronJob
// let's get the git clone URL with user/password so we can clone it again in the destroy command/CronJob
ctx := context.Background()

_, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, o.PreviewHelmfile, o.PreviewNamespace, o.GitUser, o.GitToken)
_, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, filepath.Dir(o.PreviewHelmfile), o.PreviewNamespace, o.GitUser, o.GitToken)
if err != nil {
return errors.Wrapf(err, "failed to create the jx-values.yaml file")
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (o *Options) Run() error {
if url != "" {
log.Logger().Infof("preview %s is now running at %s", info(preview.Name), info(url))

// lets modify the preview
// let's modify the preview
preview.Spec.Resources.Name = o.Repository
preview.Spec.Resources.URL = url
preview, err = o.PreviewClient.PreviewV1alpha1().Previews(o.Namespace).Update(ctx, preview, metav1.UpdateOptions{})
Expand Down Expand Up @@ -434,13 +434,13 @@ func (o *Options) createPreviewNamespace() (string, error) {
}

func findAllServiceNamesInNamespace(client kubernetes.Interface, namespace string) ([]string, error) {
services, err := client.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
serviceList, err := client.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
appNames := []string{}
for k := range services.Items {
appNames = append(appNames, services.Items[k].Name)
var appNames []string
for k := range serviceList.Items {
appNames = append(appNames, serviceList.Items[k].Name)
}
return appNames, nil
}
Expand All @@ -452,12 +452,12 @@ func (o *Options) findPreviewURL(envVars map[string]string) (string, error) {
return "", errors.Wrapf(err, "failed to read helmfile releases")
}

// lets try find the release name
// let's try to find the release name
if len(releases) == 0 {
return "", errors.Errorf("helmfile %s has no releases", o.PreviewHelmfile)
}

// lets assume first release is the preview
// let's assume first release is the preview
release := releases[0]
releaseName := release.Name
if releaseName == "" {
Expand Down Expand Up @@ -532,7 +532,7 @@ func (o *Options) updatePipelineActivity(applicationURL, pullRequestURL string)

jxClient := o.JXClient

// lets see if we can update the pipeline
// let's see if we can update the pipeline
acts := jxClient.JenkinsV1().PipelineActivities(ns)
key := &activities.PromoteStepActivityKey{
PipelineActivityKey: activities.PipelineActivityKey{
Expand Down Expand Up @@ -621,7 +621,7 @@ func (o *Options) DiscoverPreviewHelmfile() error {
return nil
}

// lets make the preview dir
// let's make the preview dir
previewDir := filepath.Dir(o.PreviewHelmfile)
parentDir := filepath.Dir(previewDir)
relDir, err := filepath.Rel(o.Dir, parentDir)
Expand All @@ -644,7 +644,7 @@ func (o *Options) DiscoverPreviewHelmfile() error {
return errors.Wrapf(err, "failed to get the preview helmfile: %s via kpt", o.PreviewHelmfile)
}

// lets add the files
// let's add the files
if o.GitClient == nil {
o.GitClient = cli.NewCLIClient("", o.CommandRunner)
}
Expand All @@ -657,7 +657,7 @@ func (o *Options) DiscoverPreviewHelmfile() error {
return errors.Wrapf(err, "failed to commit the preview helmfile files to git")
}

// lets push the changes to git
// let's push the changes to git
_, po := push.NewCmdPullRequestPush()
po.CommandRunner = o.CommandRunner
po.ScmClient = o.ScmClient
Expand Down Expand Up @@ -689,7 +689,7 @@ func (o *Options) writeOutputEnvVars() error {
}

buf := strings.Builder{}
buf.WriteString("# preview environment varables\n")
buf.WriteString("# preview environment variables\n")
for k, v := range o.OutputEnvVars {
buf.WriteString(fmt.Sprintf("export %s=%q\n", k, v))
}
Expand Down
44 changes: 17 additions & 27 deletions pkg/cmd/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package destroy

import (
"context"
"fmt"
"path/filepath"

"github.com/jenkins-x/jx-helpers/v3/pkg/input"
Expand All @@ -28,8 +29,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"fmt"
)

var (
Expand All @@ -48,20 +47,19 @@ var (
// Options the CLI options for
type Options struct {
options.BaseOptions
scmhelpers.Options
Names []string
Namespace string
PreviewHelmfile string
Filter string
GitUser string
FailOnHelmError bool
SelectAll bool
PreviewClient versioned.Interface
KubeClient kubernetes.Interface
JXClient jxc.Interface
GitClient gitclient.Interface
CommandRunner cmdrunner.CommandRunner
Input input.Interface
scmhelpers.Options // Only used for tests
Names []string
Namespace string
Filter string
GitUser string // Only used for tests
FailOnHelmError bool
SelectAll bool
PreviewClient versioned.Interface
KubeClient kubernetes.Interface
JXClient jxc.Interface
GitClient gitclient.Interface
CommandRunner cmdrunner.CommandRunner
Input input.Interface
}

// NewCmdPreviewDestroy creates a command object for the command
Expand All @@ -80,10 +78,8 @@ func NewCmdPreviewDestroy() (*cobra.Command, *Options) {
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.PreviewHelmfile, "file", "f", "", "Preview helmfile.yaml path to use. If not specified it is discovered in preview/helmfile.yaml and created from a template if needed")
cmd.Flags().StringVarP(&o.Filter, "filter", "", "", "The filter to use to find a preview to delete")
cmd.Flags().StringVarP(&o.GitUser, "git-user", "", "", "The user name to git clone the environment repository")
cmd.Flags().BoolVarP(&o.SelectAll, "all", "", false, "Select all the filters by default to remove")
cmd.Flags().StringVarP(&o.Filter, "filter", "", "", "The filter to use to find previews to delete")
cmd.Flags().BoolVarP(&o.SelectAll, "all", "", false, "Select all the previews that match filter by default")
cmd.Flags().BoolVarP(&o.FailOnHelmError, "fail-on-helm", "", false, "If enabled do not try to remove the namespace or Preview resource if we fail to destroy helmfile resources")
return cmd, o
}
Expand Down Expand Up @@ -153,13 +149,7 @@ func (o *Options) Destroy(name string) error {

previewNamespace := preview.Spec.Resources.Namespace

if o.PreviewHelmfile == "" {
o.PreviewHelmfile, err = previews.DiscoverHelmfile(dir)
if err != nil {
return errors.Wrapf(err, "failed to discover the helmfile in the dir")
}
}
_, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, o.PreviewHelmfile, previewNamespace, o.GitUser, o.GitToken)
_, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, dir, previewNamespace, o.GitUser, o.GitToken)
if err != nil {
return errors.Wrapf(err, "failed to create the jx-values.yaml file")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"os"
"path/filepath"
"strconv"

"github.com/jenkins-x-plugins/jx-gitops/pkg/plugins"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (o *Options) Run() error {
}
}

o.envCloneDir, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, o.PreviewHelmfile, o.PreviewNamespace, o.GitUser, o.GitToken)
o.envCloneDir, err = previews.CreateJXValuesFile(o.GitClient, o.JXClient, o.Namespace, filepath.Dir(o.PreviewHelmfile), o.PreviewNamespace, o.GitUser, o.GitToken)
if err != nil {
return errors.Wrapf(err, "failed to create the jx-values.yaml file")
}
Expand Down
38 changes: 0 additions & 38 deletions pkg/previews/discover_helmfile.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/previews/jx_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
var info = termcolor.ColorInfo

// CreateJXValuesFile creates the jx-values.yaml file from the dev environment
func CreateJXValuesFile(gitter gitclient.Interface, jxClient jxc.Interface, namespace, helmfile, previewNamespace, gitUser, gitToken string) (string, error) {
dir := filepath.Dir(helmfile)
func CreateJXValuesFile(gitter gitclient.Interface, jxClient jxc.Interface, namespace, dir, previewNamespace, gitUser, gitToken string) (string, error) {
fileName := filepath.Join(dir, "jx-values.yaml")

devEnv, err := jxenv.GetDevEnvironment(jxClient, namespace)
Expand Down

0 comments on commit c570d89

Please sign in to comment.