Skip to content

Commit

Permalink
Merge branch 'master' into fix-jenkins-token-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Sep 25, 2018
2 parents f67cf28 + cfbf97a commit f93a0ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 28 deletions.
16 changes: 16 additions & 0 deletions pkg/cloud/amazon/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package amazon

import "os"

const DefaultRegion = "us-west-2"

func ResolveRegion() string {
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
if region == "" {
region = DefaultRegion
}
}
return region
}
28 changes: 28 additions & 0 deletions pkg/cloud/amazon/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package amazon_test

import (
"os"
"testing"

"github.com/jenkins-x/jx/pkg/cloud/amazon"
"github.com/stretchr/testify/assert"
)

func TestResolvingDefaultRegion(t *testing.T) {
os.Setenv("AWS_REGION", "")
os.Setenv("AWS_DEFAULT_REGION", "")
region := amazon.ResolveRegion()
assert.Equal(t, amazon.DefaultRegion, region)
}

func TestResolvingRegionFromAwsRegionEnv(t *testing.T) {
os.Setenv("AWS_REGION", "us-east-1")
region := amazon.ResolveRegion()
assert.Equal(t, "us-east-1", region)
}

func TestResolvingRegionFromAwsDefaultRegionEnv(t *testing.T) {
os.Setenv("DEFAULT_AWS_REGION", "us-east-1")
region := amazon.ResolveRegion()
assert.Equal(t, "us-east-1", region)
}
15 changes: 4 additions & 11 deletions pkg/cloud/amazon/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package amazon

import (
"fmt"
"os"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -35,17 +34,11 @@ func GetAccountIDAndRegion() (string, string, error) {
}

func NewAwsSession() (*session.Session, string, error) {
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
if region == "" {
region = "us-west-2"
}
config := aws.Config{
Region: aws.String(ResolveRegion()),
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
return sess, region, err
sess, err := session.NewSession(&config)
return sess, *config.Region, err
}

// GetContainerRegistryHost
Expand Down
29 changes: 22 additions & 7 deletions pkg/jx/cmd/common_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,49 @@ func (o *CommonOptions) addHelmBinaryRepoIfMissing(helmUrl string, repoName stri

// installChart installs the given chart
func (o *CommonOptions) installChart(releaseName string, chart string, version string, ns string, helmUpdate bool, setValues []string) error {
return o.installChartAt("", releaseName, chart, version, ns, helmUpdate, setValues)
return o.installChartOptions(InstallChartOptions{ReleaseName: releaseName, Chart: chart, Version: version, Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues})
}

// installChartAt installs the given chart
func (o *CommonOptions) installChartAt(dir string, releaseName string, chart string, version string, ns string, helmUpdate bool, setValues []string) error {
if helmUpdate {
return o.installChartOptions(InstallChartOptions{ Dir: dir, ReleaseName: releaseName, Chart: chart, Version: version, Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues})
}

type InstallChartOptions struct {
Dir string
ReleaseName string
Chart string
Version string
Ns string
HelmUpdate bool
SetValues []string
ValueFiles []string
}

func (o *CommonOptions) installChartOptions(options InstallChartOptions) error {
if options.HelmUpdate {
log.Infoln("Updating Helm repository...")
err := o.Helm().UpdateRepo()
if err != nil {
return errors.Wrap(err, "failed to update repository")
}
log.Infoln("Helm repository update done.")
}
if ns != "" {
if options.Ns != "" {
kubeClient, _, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "failed to create the kube client")
}
annotations := map[string]string{"jenkins-x.io/created-by": "Jenkins X"}
kube.EnsureNamespaceCreated(kubeClient, ns, nil, annotations)
kube.EnsureNamespaceCreated(kubeClient, options.Ns, nil, annotations)
}
timeout, err := strconv.Atoi(defaultInstallTimeout)
if err != nil {
return errors.Wrap(err, "failed to convert the timeout to an int")
}
o.Helm().SetCWD(dir)
return o.Helm().UpgradeChart(chart, releaseName, ns, &version, true,
&timeout, true, false, setValues, nil)
o.Helm().SetCWD(options.Dir)
return o.Helm().UpgradeChart(options.Chart, options.ReleaseName, options.Ns, &options.Version, true,
&timeout, true, false, options.SetValues, options.ValueFiles)
}

// deleteChart deletes the given chart
Expand Down
13 changes: 3 additions & 10 deletions pkg/jx/cmd/delete_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package cmd

import (
"fmt"
"io"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/jenkins-x/jx/pkg/cloud/amazon"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"io"

"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,13 +50,7 @@ func (o *DeleteAwsOptions) Run() error {

region := o.Region
if region == "" {
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
if region == "" {
region = "us-west-2"
}
}
region = amazon.ResolveRegion()
}
svc := ec2.New(session.New(&aws.Config{Region: aws.String(region)}))

Expand Down

0 comments on commit f93a0ae

Please sign in to comment.