Skip to content

Commit fb8a736

Browse files
feat: Implement plural upgrade (#391)
* feat: Implement `plural upgrade` creates an entry on an upgrade queue, especially helpful for CONFIG upgrades in CD * Add require args validator
1 parent 7777f1f commit fb8a736

9 files changed

Lines changed: 133 additions & 23 deletions

File tree

cmd/plural/plural.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ func (p *Plural) getCommands() []cli.Command {
235235
},
236236
Action: tracked(latestVersion(owned(upstreamSynced(p.destroy))), "cli.destroy"),
237237
},
238+
{
239+
Name: "upgrade",
240+
Usage: "creates an upgrade in the upgrade queue QUEUE for application REPO",
241+
ArgsUsage: "QUEUE REPO",
242+
Flags: []cli.Flag{
243+
cli.StringFlag{
244+
Name: "f",
245+
Usage: "file containing upgrade contents, use - for stdin",
246+
},
247+
},
248+
Action: latestVersion(requireArgs(p.handleUpgrade, []string{"QUEUE", "REPO"})),
249+
},
238250
{
239251
Name: "init",
240252
Usage: "initializes plural within a git repo",

cmd/plural/upgrade.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/pluralsh/plural/pkg/api"
8+
"github.com/urfave/cli"
9+
)
10+
11+
func (p *Plural) handleUpgrade(c *cli.Context) (err error) {
12+
p.InitPluralClient()
13+
queue, repo := c.Args().Get(0), c.Args().Get(1)
14+
f := os.Stdin
15+
fname := c.String("f")
16+
if fname != "-" && fname != "" {
17+
f, err = os.Open(fname)
18+
if err != nil {
19+
return
20+
}
21+
defer f.Close()
22+
}
23+
24+
contents, err := io.ReadAll(f)
25+
if err != nil {
26+
return
27+
}
28+
29+
attrs, err := api.ConstructUpgradeAttributes(contents)
30+
if err != nil {
31+
return
32+
}
33+
34+
err = p.CreateUpgrade(queue, repo, attrs)
35+
return api.GetErrorResponse(err, "CreateUpgrade")
36+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
github.com/olekukonko/tablewriter v0.0.5
4545
github.com/packethost/packngo v0.29.0
4646
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
47-
github.com/pluralsh/gqlclient v1.3.12
47+
github.com/pluralsh/gqlclient v1.3.13
4848
github.com/pluralsh/plural-operator v0.5.3
4949
github.com/pluralsh/polly v0.1.1
5050
github.com/rodaine/hclencoder v0.0.1

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,12 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
899899
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
900900
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
901901
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
902-
github.com/pluralsh/gqlclient v1.3.12 h1:BM+7fILoO06kvVBrL4kEB5AuZMVc3TvqupNi/HNrZkA=
903-
github.com/pluralsh/gqlclient v1.3.12/go.mod h1:z1qHnvPeqIN/a+5OzFs40e6HI6tDxzh1+yJuEpvqGy4=
902+
github.com/pluralsh/gqlclient v1.3.13 h1:ihix+ruoIkwiVdPnvJecrsxrVYEKTG26JFHPiF36O3U=
903+
github.com/pluralsh/gqlclient v1.3.13/go.mod h1:z1qHnvPeqIN/a+5OzFs40e6HI6tDxzh1+yJuEpvqGy4=
904904
github.com/pluralsh/oauth v0.9.2 h1:tM9hBK4tCnJUeCOgX0ctxBBCS3hiCDPoxkJLODtedmQ=
905905
github.com/pluralsh/oauth v0.9.2/go.mod h1:aTUw/75rzcsbvW+/TLvWtHVDXFIdtFrDtUncOq9vHyM=
906906
github.com/pluralsh/plural-operator v0.5.3 h1:GaPL3LgimfzKZNHt7zXzqYZpb0hgyW9noHYnkA+rqNs=
907907
github.com/pluralsh/plural-operator v0.5.3/go.mod h1:WIXiz26/WDcUn0FA7Q1jPxmfsm98U1/JL8YpIdKVLX0=
908-
github.com/pluralsh/polly v0.1.0 h1:xr/+LWIORtA0CzedEQ8gM7YhHBjcql6Oekwwz1+SEfM=
909-
github.com/pluralsh/polly v0.1.0/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
910908
github.com/pluralsh/polly v0.1.1 h1:VtbS83re2YuDgscvaFgfzEDZs9uXRV84fCdvKCgIRE4=
911909
github.com/pluralsh/polly v0.1.1/go.mod h1:Yo1/jcW+4xwhWG+ZJikZy4J4HJkMNPZ7sq5auL2c/tY=
912910
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

pkg/api/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type Client interface {
8383
PromoteCluster() error
8484
Clusters() ([]*Cluster, error)
8585
Cluster(id string) (*Cluster, error)
86+
CreateUpgrade(queue, repository string, attrs gqlclient.UpgradeAttributes) error
8687
}
8788

8889
type client struct {

pkg/api/upgrades.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package api
2+
3+
import (
4+
"github.com/pluralsh/gqlclient"
5+
"sigs.k8s.io/yaml"
6+
)
7+
8+
func (client *client) CreateUpgrade(queue, repository string, attrs gqlclient.UpgradeAttributes) error {
9+
_, err := client.pluralClient.CreateUpgrade(client.ctx, queue, repository, attrs)
10+
return err
11+
}
12+
13+
func ConstructUpgradeAttributes(marshalled []byte) (gqlclient.UpgradeAttributes, error) {
14+
var attrs gqlclient.UpgradeAttributes
15+
err := yaml.Unmarshal(marshalled, &attrs)
16+
return attrs, err
17+
}

pkg/api/users.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
"github.com/samber/lo"
99
)
1010

11-
type UpgradeAttributes struct {
12-
Message string
13-
}
14-
1511
type UserEventAttributes struct {
1612
Event string
1713
Data string

pkg/test/mocks/Client.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/test/mocks/Kube.go

Lines changed: 49 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)