Skip to content

Commit

Permalink
Merge pull request #2271 from jstrachan/fixes5
Browse files Browse the repository at this point in the history
fix for devpods
  • Loading branch information
jenkins-x-bot committed Nov 16, 2018
2 parents 4076c28 + 88a6b4c commit c61ca0e
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 34 deletions.
38 changes: 22 additions & 16 deletions pkg/jx/cmd/create_devpod.go
Expand Up @@ -2,31 +2,30 @@ package cmd

import (
"fmt"
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
"github.com/jenkins-x/jx/pkg/kube/services"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/kube/serviceaccount"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"io"
"io/ioutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -79,6 +78,7 @@ type CreateDevPodOptions struct {
Username string
DockerRegistry string
TillerNamespace string
ServiceAccount string

GitCredentials StepGitCredentialsOptions

Expand Down Expand Up @@ -137,6 +137,7 @@ func NewCmdCreateDevPod(f Factory, in terminal.FileReader, out terminal.FileWrit
cmd.Flags().StringVarP(&options.Username, "username", "", "", "The username to create the DevPod. If not specified defaults to the current operating system user or $USER'")
cmd.Flags().StringVarP(&options.DockerRegistry, "docker-registry", "", "", "The Docker registry to use within the DevPod. If not specified, default to the built-in registry or $DOCKER_REGISTRY")
cmd.Flags().StringVarP(&options.TillerNamespace, "tiller-namespace", "", "", "The optional tiller namespace to use within the DevPod.")
cmd.Flags().StringVarP(&options.ServiceAccount, "service-account", "", "jenkins", "The ServiceAccount name used for the DevPod")

options.addCommonFlags(cmd)
return cmd
Expand Down Expand Up @@ -294,6 +295,10 @@ func (o *CreateDevPodOptions) Run() error {
}
}

if pod.Spec.ServiceAccountName == "" {
pod.Spec.ServiceAccountName = o.ServiceAccount
}

if !o.Sync {
pod.Spec.Volumes = append(pod.Spec.Volumes, workspaceVolume)
container1.VolumeMounts = append(container1.VolumeMounts, workspaceVolumeMount)
Expand Down Expand Up @@ -490,8 +495,13 @@ func (o *CreateDevPodOptions) Run() error {

log.Infof("Created pod %s - waiting for it to be ready...\n", util.ColorInfo(name))

err = kube.WaitForPodNameToBeReady(client, ns, name, time.Hour)
if err != nil {
return err
}

// Get the pod UID
pod, err = client.CoreV1().Pods(curNs).Get(name, metav1.GetOptions{})
pod, err = client.CoreV1().Pods(ns).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -603,10 +613,6 @@ func (o *CreateDevPodOptions) Run() error {
}
}

err = kube.WaitForPodNameToBeReady(client, ns, name, time.Hour)
if err != nil {
return err
}

}

Expand Down
1 change: 1 addition & 0 deletions pkg/jx/cmd/delete.go
Expand Up @@ -62,6 +62,7 @@ func NewCmdDelete(f Factory, in terminal.FileReader, out terminal.FileWriter, er
cmd.AddCommand(NewCmdDeleteEnv(f, in, out, errOut))
cmd.AddCommand(NewCmdDeleteGit(f, in, out, errOut))
cmd.AddCommand(NewCmdDeleteJenkins(f, in, out, errOut))
cmd.AddCommand(NewCmdDeleteNamespace(f, in, out, errOut))
cmd.AddCommand(NewCmdDeletePostPreviewJob(f, in, out, errOut))
cmd.AddCommand(NewCmdDeletePreview(f, in, out, errOut))
cmd.AddCommand(NewCmdDeleteQuickstartLocation(f, in, out, errOut))
Expand Down
134 changes: 134 additions & 0 deletions pkg/jx/cmd/delete_namespace.go
@@ -0,0 +1,134 @@
package cmd

import (
"fmt"
"io"
"sort"
"strings"

"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// DeleteNamespaceOptions are the flags for delete commands
type DeleteNamespaceOptions struct {
CommonOptions

SelectAll bool
SelectFilter string
Confirm bool
}

var (
deleteNamespaceLong = templates.LongDesc(`
Deletes one or more namespaces
`)

deleteNamespaceExample = templates.Examples(`
# Delete the named namespace
jx delete namespace cheese
# Delete the namespaces matching the given filter
jx delete namespace -f foo -a
`)
)

// NewCmdDeleteNamespace creates a command object
// retrieves one or more resources from a server.
func NewCmdDeleteNamespace(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteNamespaceOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,

Out: out,
Err: errOut,
},
}

cmd := &cobra.Command{
Use: "namespace",
Short: "Deletes one or more namespaces and their associated resources (Environments, Jenkins etc)",
Long: deleteNamespaceLong,
Example: deleteNamespaceExample,
Aliases: []string{"namespaces", "ns"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}

options.addCommonFlags(cmd)
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Should we default to selecting all the matched namespaces for deletion")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filters the list of namespaces you can pick from")
cmd.Flags().BoolVarP(&options.Confirm, "yes", "y", false, "Confirms we should uninstall this installation")
return cmd
}

// Run implements this command
func (o *DeleteNamespaceOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
namespaceInterface := kubeClient.CoreV1().Namespaces()
nsList, err := namespaceInterface.List(metav1.ListOptions{})
if err != nil {
return err
}
namespaceNames := []string{}
for _, namespace := range nsList.Items {
namespaceNames = append(namespaceNames, namespace.Name)
}
sort.Strings(namespaceNames)

names := o.Args
if len(names) == 0 {
if o.BatchMode {
return fmt.Errorf("Missing namespace name argument")
}
names, err = util.SelectNamesWithFilter(namespaceNames, "Which namespaces do you want to delete: ", o.SelectAll, o.SelectFilter, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
}

if o.BatchMode {
if !o.Confirm {
return fmt.Errorf("In batch mode you must specify the '-y' flag to confirm")
}
} else {
log.Warnf("You are about to delete the following namespaces '%s'. This operation CANNOT be undone!",
strings.Join(names, ","))

flag := false
prompt := &survey.Confirm{
Message: "Are you sure you want to delete all these namespaces?",
Default: false,
}
err = survey.AskOne(prompt, &flag, nil, surveyOpts)
if err != nil {
return err
}
if !flag {
return nil
}
}

for _, name := range names {
log.Infof("Deleting namespace: %s\n", util.ColorInfo(name))
err = namespaceInterface.Delete(name, nil)
if err != nil {
log.Warnf("Failed to delete namespace %s: %s\n", name, err)
}
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/jx/cmd/delete_team.go
Expand Up @@ -106,7 +106,7 @@ func (o *DeleteTeamOptions) Run() error {
return fmt.Errorf("In batch mode you must specify the '-y' flag to confirm")
}
} else {
log.Warnf("You are about to delete the following teams '%s' on the Git provider. This operation CANNOT be undone!",
log.Warnf("You are about to delete the following teams '%s'. This operation CANNOT be undone!",
strings.Join(names, ","))

flag := false
Expand Down
22 changes: 11 additions & 11 deletions pkg/jx/cmd/uninstall.go
Expand Up @@ -120,23 +120,22 @@ func (o *UninstallOptions) Run() error {
}
}
}
errs := []error{}
o.Helm().DeleteRelease(namespace, "jx-prow", true)
err = o.Helm().DeleteRelease(namespace, "jenkins-x", true)
if err != nil {
errc := o.cleanupNamespaces(namespace, envNames, envMap)
if errc != nil {
errc = errors.Wrap(errc, "failed to cleanup the jenkins-x platform")
return errc
}
return errors.Wrap(err, "failed to purge the jenkins-x chart")
errs = append(errs, fmt.Errorf("failed to uninstall the jenkins-x helm chart in namespace %s: %s", namespace ,err))
}
err = jxClient.JenkinsV1().Environments(namespace).DeleteCollection(&meta_v1.DeleteOptions{}, meta_v1.ListOptions{})
if err != nil {
return err
errs = append(errs, fmt.Errorf("failed to delete the environments in namespace %s: %s", namespace, err))
}
err = o.cleanupNamespaces(namespace, envNames, envMap)
if err != nil {
return err
errs = append(errs, fmt.Errorf("failed to cleanup namespaces in namespace %s: %s", namespace, err))
}
if len(errs) > 0 {
return util.CombineErrors(errs...)
}
log.Successf("Jenkins X has been successfully uninstalled from team namespace %s", namespace)
return nil
Expand All @@ -147,9 +146,10 @@ func (o *UninstallOptions) cleanupNamespaces(namespace string, envNames []string
if err != nil {
return errors.Wrap(err, "failed to get the kube client")
}
errs := []error{}
err = o.deleteNamespace(namespace)
if err != nil {
return errors.Wrap(err, "failed to delete team namespace namespace")
errs = append(errs, fmt.Errorf("failed to delete namespace %s: %s", namespace, err))
}
if !o.KeepEnvironments {
for _, env := range envNames {
Expand All @@ -170,12 +170,12 @@ func (o *UninstallOptions) cleanupNamespaces(namespace string, envNames []string
}
err = o.deleteNamespace(envNamespace)
if err != nil {
return errors.Wrap(err, "failed to delete environment namespace")
errs = append(errs, fmt.Errorf("failed to delete namespace %s: %s", envNamespace, err))
}
}
}
}
return nil
return util.CombineErrors(errs...)
}

func (o *UninstallOptions) deleteNamespace(namespace string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/downloads.go
Expand Up @@ -30,7 +30,7 @@ func DownloadFile(filepath string, url string) (err error) {
defer out.Close()

// Get the data
resp, err := GetClientWithTimeout(int(time.Hour) * 2).Get(url)
resp, err := GetClientWithTimeout(time.Duration(time.Hour * 2)).Get(url)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/http_utils.go
Expand Up @@ -29,11 +29,11 @@ func GetClient() *http.Client {
return &defaultClient
}

// GetClientWithTimeout returns a client with JX default transport and user specified timeout (in seconds)
func GetClientWithTimeout(timeout int) *http.Client {
// GetClientWithTimeout returns a client with JX default transport and user specified timeout
func GetClientWithTimeout(duration time.Duration) (*http.Client) {
client := http.Client{}
client.Transport = jxDefaultTransport
client.Timeout = time.Duration(timeout) * time.Second
client.Timeout = duration
return &client
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/util/http_utils_test.go
Expand Up @@ -30,8 +30,8 @@ func TestGetClient(t *testing.T) {
assert.Equal(t, time.Duration(getIntFromEnv("DEFAULT_HTTP_REQUEST_TIMEOUT", 30))*time.Second, myClient.Timeout)

// verify that it times out properly
timeoutClient := GetClientWithTimeout(3)
assert.Equal(t, time.Duration(3)*time.Second, timeoutClient.Timeout)
timeoutClient := GetClientWithTimeout(time.Duration(3 * time.Second))
assert.Equal(t, time.Duration(3 * time.Second), timeoutClient.Timeout)

handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Second)
Expand Down

0 comments on commit c61ca0e

Please sign in to comment.