Skip to content

Commit 9b09061

Browse files
authored
fix(up): fix dry-run for plural up across all providers (#729)
1 parent 982c26a commit 9b09061

11 files changed

Lines changed: 190 additions & 160 deletions

File tree

cmd/command/up/up.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ func (p *Plural) handleUp(c *cli.Context) error {
122122
return err
123123
}
124124

125-
ctx, err := up.Build(c.Bool("cloud"))
126-
ctx.IgnorePreflights(c.Bool("ignore-preflights") || c.Bool("dry-run"))
127-
125+
ctx, err := up.Build(cloud)
128126
if err != nil {
129127
return err
130128
}
129+
ctx.IgnorePreflights(c.Bool("ignore-preflights") || dryRun)
131130

132131
byok := ctx.Provider.Name() == api.BYOK
133132

pkg/client/plural.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ import (
88
"strings"
99

1010
"github.com/pluralsh/console/go/polly/algorithms"
11-
gitutils "github.com/pluralsh/plural-cli/pkg/utils/git"
1211
"github.com/samber/lo"
1312
apierrors "k8s.io/apimachinery/pkg/api/errors"
1413

14+
gitutils "github.com/pluralsh/plural-cli/pkg/utils/git"
15+
1516
"github.com/urfave/cli"
1617

1718
"github.com/pluralsh/plural-cli/pkg/common"
1819
"github.com/pluralsh/plural-cli/pkg/scm"
1920
"github.com/pluralsh/plural-cli/pkg/wkspace"
2021

22+
"sigs.k8s.io/yaml"
23+
2124
"github.com/pluralsh/plural-cli/pkg/api"
2225
"github.com/pluralsh/plural-cli/pkg/config"
2326
"github.com/pluralsh/plural-cli/pkg/console"
2427
"github.com/pluralsh/plural-cli/pkg/kubernetes"
2528
"github.com/pluralsh/plural-cli/pkg/manifest"
2629
"github.com/pluralsh/plural-cli/pkg/utils"
27-
"sigs.k8s.io/yaml"
2830
)
2931

3032
type Plural struct {
@@ -106,13 +108,14 @@ func (p *Plural) HandleInit(c *cli.Context) error {
106108
return err
107109
}
108110

109-
func (p *Plural) HandleInitWithProject(c *cli.Context) (*manifest.ProjectManifest, error) {
111+
func (p *Plural) HandleInitWithProject(c *cli.Context) (project *manifest.ProjectManifest, err error) {
110112
gitCreated := false
111113
repo := ""
114+
dryRun := c.Bool("dry-run")
112115
p.InitPluralClient()
113116

114-
git, err := wkspace.Preflight(c.Bool("dry-run"), c.Bool("ignore-preflights"))
115-
if err != nil && (git || c.Bool("dry-run")) {
117+
git, err := wkspace.Preflight(dryRun, c.Bool("ignore-preflights"))
118+
if err != nil && (git || dryRun) {
116119
return nil, err
117120
}
118121

@@ -156,7 +159,7 @@ func (p *Plural) HandleInitWithProject(c *cli.Context) (*manifest.ProjectManifes
156159
return nil, err
157160
}
158161

159-
project, err := manifest.FetchProject()
162+
project, err = manifest.FetchProject()
160163
if err != nil {
161164
return nil, err
162165
}

pkg/provider/aws.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ func mkAWS(conf config.Config, dryRun bool) (provider *AWSProvider, err error) {
7676
ctx := context.Background()
7777
provider = &AWSProvider{}
7878
if dryRun {
79+
projectManifest := manifest.ProjectManifest{
80+
Provider: api.ProviderAWS,
81+
Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint},
82+
}
83+
84+
provider.writer = func() error { return projectManifest.Write(manifest.ProjectManifestPath()) }
7985
return provider, nil
8086
}
8187
iamSession, callerIdentity, err := GetAWSCallerIdentity(ctx)

pkg/provider/azure.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ type AzureProvider struct {
8989
func mkAzure(conf config.Config, dryRun bool) (prov *AzureProvider, err error) {
9090
prov = &AzureProvider{}
9191
if dryRun {
92+
projectManifest := manifest.ProjectManifest{
93+
Provider: api.ProviderAzure,
94+
Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint},
95+
}
96+
prov.writer = func() error { return projectManifest.Write(manifest.ProjectManifestPath()) }
9297
return prov, nil
9398
}
9499
subId, tenID, subName, err := GetAzureAccount()
@@ -155,7 +160,7 @@ func mkAzure(conf config.Config, dryRun bool) (prov *AzureProvider, err error) {
155160
func AzureFromManifest(man *manifest.ProjectManifest, clientSet *ClientSet) (*AzureProvider, error) {
156161
var err error
157162
clients := clientSet
158-
if clientSet == nil {
163+
if clientSet == nil && utils.ToString(man.Context["SubscriptionId"]) != "" {
159164
clients, err = GetClientSet(utils.ToString(man.Context["SubscriptionId"]))
160165
if err != nil {
161166
return nil, err

pkg/provider/byok.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import (
88
"strings"
99

1010
"github.com/AlecAivazis/survey/v2"
11+
"k8s.io/client-go/tools/clientcmd"
12+
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
13+
1114
"github.com/pluralsh/plural-cli/pkg/api"
1215
"github.com/pluralsh/plural-cli/pkg/config"
1316
"github.com/pluralsh/plural-cli/pkg/kubernetes"
1417
"github.com/pluralsh/plural-cli/pkg/manifest"
1518
"github.com/pluralsh/plural-cli/pkg/provider/permissions"
1619
"github.com/pluralsh/plural-cli/pkg/provider/preflights"
1720
"github.com/pluralsh/plural-cli/pkg/utils"
18-
"k8s.io/client-go/tools/clientcmd"
19-
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2021
)
2122

2223
const defaultValue = "default"
@@ -42,6 +43,12 @@ func mkBYOK(conf config.Config, name string, dryRun, cloud bool) (prov *ByokProv
4243
ctx: map[string]interface{}{},
4344
}
4445
if dryRun {
46+
projectManifest := manifest.ProjectManifest{
47+
Provider: api.BYOK,
48+
Owner: &manifest.Owner{Email: conf.Email, Endpoint: conf.Endpoint},
49+
}
50+
51+
prov.writer = func() error { return projectManifest.Write(manifest.ProjectManifestPath()) }
4552
return prov, nil
4653
}
4754

pkg/provider/gcp/option.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ type Option func(*Provider) error
1414
func WithConfig(c config.Config, defaultCluster string, cloudEnabled, dryRun bool) Option {
1515
return func(gcp *Provider) error {
1616
if dryRun {
17+
projectManifest := manifest.ProjectManifest{
18+
Bucket: "dry-run",
19+
Provider: api.ProviderGCP,
20+
Owner: &manifest.Owner{Email: c.Email, Endpoint: c.Endpoint},
21+
}
22+
gcp.InputProvider = NewReadonlyInputProvider(defaultCluster, "", "")
23+
gcp.bucket = projectManifest.Bucket
24+
gcp.writer = func() error { return projectManifest.Write(manifest.ProjectManifestPath()) }
1725
return nil
1826
}
1927
err := printUserInfo()

pkg/up/context.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,70 +42,70 @@ type delims struct {
4242
right string
4343
}
4444

45-
func (ctx *Context) identifier() string {
46-
if ctx.RepoUrl == "" {
45+
func (c *Context) identifier() string {
46+
if c.RepoUrl == "" {
4747
return ""
4848
}
4949

50-
if strings.HasPrefix(ctx.RepoUrl, "http") {
51-
parsed, err := giturls.Parse(ctx.RepoUrl)
50+
if strings.HasPrefix(c.RepoUrl, "http") {
51+
parsed, err := giturls.Parse(c.RepoUrl)
5252
if err == nil {
5353
return strings.TrimSuffix(strings.TrimPrefix(parsed.Path, "/"), ".git")
5454
}
5555
}
5656

57-
split := strings.Split(ctx.RepoUrl, ":")
57+
split := strings.Split(c.RepoUrl, ":")
5858
return strings.TrimSuffix(split[len(split)-1], ".git")
5959
}
6060

61-
func (ctx *Context) changeDelims() {
62-
ctx.Delims = &delims{"[[", "]]"}
61+
func (c *Context) changeDelims() {
62+
c.Delims = &delims{"[[", "]]"}
6363
}
6464

65-
func (ctx *Context) IgnorePreflights(ignore bool) {
66-
ctx.ignorePreflights = ignore
65+
func (c *Context) IgnorePreflights(ignore bool) {
66+
c.ignorePreflights = ignore
6767
}
6868

69-
func (ctx *Context) SetImportCluster(id string) {
70-
ctx.ImportCluster = lo.ToPtr(id)
69+
func (c *Context) SetImportCluster(id string) {
70+
c.ImportCluster = lo.ToPtr(id)
7171
}
7272

73-
func (ctx *Context) Backfill() error {
73+
func (c *Context) Backfill() error {
7474
context, err := manifest.FetchContext()
7575
if err != nil {
76-
return ctx.backfillConsoleContext(ctx.Manifest)
76+
return c.backfillConsoleContext(c.Manifest)
7777
}
7878

7979
console, ok := context.Configuration["console"]
8080
if !ok {
81-
return ctx.backfillConsoleContext(ctx.Manifest)
81+
return c.backfillConsoleContext(c.Manifest)
8282
}
8383

8484
_, hasSSH := console["private_key"]
8585
_, hasHTTPS := console["git_password"]
8686
if !hasSSH && !hasHTTPS {
87-
return ctx.backfillConsoleContext(ctx.Manifest)
87+
return c.backfillConsoleContext(c.Manifest)
8888
}
8989

9090
if v, ok := console["repo_url"]; ok {
9191
if r, ok := v.(string); ok {
92-
ctx.RepoUrl = r
92+
c.RepoUrl = r
9393
}
9494
}
9595

9696
if v, ok := console["git_username"]; ok {
9797
if s, ok := v.(string); ok {
98-
ctx.GitUsername = s
98+
c.GitUsername = s
9999
}
100100
}
101101

102102
if v, ok := console["git_password"]; ok {
103103
if s, ok := v.(string); ok {
104-
ctx.GitPassword = s
104+
c.GitPassword = s
105105
}
106106
}
107107

108-
if ctx.RepoUrl == "" {
108+
if c.RepoUrl == "" {
109109
return fmt.Errorf("you never configured a repoUrl for your workspace, check `context.yaml`")
110110
}
111111

@@ -133,7 +133,7 @@ func Build(cloud bool) (*Context, error) {
133133
}, nil
134134
}
135135

136-
func (context *Context) backfillConsoleContext(_ *manifest.ProjectManifest) error {
136+
func (c *Context) backfillConsoleContext(_ *manifest.ProjectManifest) error {
137137
path := manifest.ContextPath()
138138
ctx, err := manifest.FetchContext()
139139
if err != nil {
@@ -153,13 +153,13 @@ func (context *Context) backfillConsoleContext(_ *manifest.ProjectManifest) erro
153153
}
154154

155155
if strings.HasPrefix(url, "http") {
156-
return context.backfillHTTPS(url, console, ctx, path)
156+
return c.backfillHTTPS(url, console, ctx, path)
157157
}
158158

159-
return context.backfillSSH(url, console, ctx, path)
159+
return c.backfillSSH(url, console, ctx, path)
160160
}
161161

162-
func (context *Context) backfillSSH(url string, console map[string]interface{}, ctx *manifest.Context, path string) error {
162+
func (c *Context) backfillSSH(url string, console map[string]interface{}, ctx *manifest.Context, path string) error {
163163
utils.Highlight("If you want, you can use `plural crypto ssh-keygen` to generate a keypair to use as a deploy key as well\n\n")
164164

165165
files, err := filepath.Glob(filepath.Join(os.Getenv("HOME"), ".ssh", "*"))
@@ -188,7 +188,7 @@ func (context *Context) backfillSSH(url string, console map[string]interface{},
188188
return err
189189
}
190190

191-
if !context.ignorePreflights {
191+
if !c.ignorePreflights {
192192
if err := verifySSHKey(contents, url); err != nil {
193193
return fmt.Errorf("ssh key not valid for url %s, error: %w. If you want to bypass this check, you can use the --ignore-preflights flag", url, err)
194194
}
@@ -197,11 +197,11 @@ func (context *Context) backfillSSH(url string, console map[string]interface{},
197197
console["repo_url"] = url
198198
console["private_key"] = contents
199199
ctx.Configuration["console"] = console
200-
context.RepoUrl = url
200+
c.RepoUrl = url
201201
return ctx.Write(path)
202202
}
203203

204-
func (context *Context) backfillHTTPS(url string, console map[string]interface{}, ctx *manifest.Context, path string) error {
204+
func (c *Context) backfillHTTPS(url string, console map[string]interface{}, ctx *manifest.Context, path string) error {
205205
utils.Highlight("If you want, you can also reclone with an SSH URL and re-run to use deploy-key authentication instead\n\n")
206206

207207
var username, token string
@@ -219,7 +219,7 @@ func (context *Context) backfillHTTPS(url string, console map[string]interface{}
219219
return err
220220
}
221221

222-
if !context.ignorePreflights {
222+
if !c.ignorePreflights {
223223
if err := verifyHTTPS(username, token, url); err != nil {
224224
return fmt.Errorf("PAT not valid for url %s, error: %w. If you want to bypass this check, you can use the --ignore-preflights flag", url, err)
225225
}
@@ -229,9 +229,9 @@ func (context *Context) backfillHTTPS(url string, console map[string]interface{}
229229
console["git_username"] = username
230230
console["git_password"] = token
231231
ctx.Configuration["console"] = console
232-
context.RepoUrl = url
233-
context.GitUsername = username
234-
context.GitPassword = token
232+
c.RepoUrl = url
233+
c.GitUsername = username
234+
c.GitPassword = token
235235
return ctx.Write(path)
236236
}
237237

0 commit comments

Comments
 (0)