From 8ccb3b88c0169e521d109ad6c4c2e9c5b1e8e369 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Wed, 12 Jan 2022 12:16:11 +0100 Subject: [PATCH 1/9] Revert "Fixes `gitops upgrade` with gitlab (#1149)" This reverts commit 49613650ef31da628c32e4a2c4a090200b9940b4. --- pkg/upgrade/upgrade.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 9284ec65b3..05e429a481 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -97,6 +97,7 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi // Create pull request path := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, WegoEnterpriseName) + wegoAppPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "wego-app.yaml") capiKeepPath := filepath.Join(git.WegoRoot, git.WegoAppDir, "capi", "templates", ".keep") capiKeepContents := string(strconv.AppendQuote(nil, "# keep")) @@ -115,6 +116,10 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi Path: &capiKeepPath, Content: &capiKeepContents, }, + { + Path: &wegoAppPath, + Content: nil, + }, }, } From 333c86870a14f471fdc0d7a61325f81fb7f873b6 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Wed, 12 Jan 2022 14:34:21 +0100 Subject: [PATCH 2/9] wip --- cmd/gitops/upgrade/cmd.go | 8 ++++++-- go.mod | 26 ++++++++++++++------------ go.sum | 33 ++++++++++++--------------------- pkg/server/profiles.go | 2 +- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/cmd/gitops/upgrade/cmd.go b/cmd/gitops/upgrade/cmd.go index 3e55523adb..d8428e2e93 100644 --- a/cmd/gitops/upgrade/cmd.go +++ b/cmd/gitops/upgrade/cmd.go @@ -19,7 +19,11 @@ import ( var upgradeCmdFlags upgrade.UpgradeValues var example = fmt.Sprintf(` # Upgrade Weave GitOps in the %s namespace - gitops upgrade --version 0.0.15 --config-repo https://github.com/my-org/my-management-cluster.git`, + gitops upgrade --version 0.0.17 --config-repo https://github.com/my-org/my-management-cluster.git + + # Upgrade Weave GitOps and set the natsURL + gitops upgrade --version 0.0.17 --set "agentTemplate.natsURL=my-cluster.acme.org:4222" \ + --config-repo https://github.com/my-org/my-management-cluster.git`, wego.DefaultNamespace) var Cmd = &cobra.Command{ @@ -37,7 +41,7 @@ func init() { Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.BaseBranch, "base", "main", "The base branch to open the pull request against") Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.HeadBranch, "branch", "tier-upgrade-enterprise", "The branch to create the pull request from") Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.CommitMessage, "commit-message", "Upgrade to WGE", "The commit message") - Cmd.PersistentFlags().StringArrayVar(&upgradeCmdFlags.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") + Cmd.PersistentFlags().StringArrayVar(&upgradeCmdFlags.Values, "set", []string{}, "set profile values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") Cmd.PersistentFlags().BoolVar(&upgradeCmdFlags.DryRun, "dry-run", false, "Output the generated profile without creating a pull request") cobra.CheckErr(Cmd.MarkPersistentFlagRequired("config-repo")) diff --git a/go.mod b/go.mod index b6dd8c8f12..8591fb9e8c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/benbjohnson/clock v1.1.0 github.com/coreos/go-oidc/v3 v3.1.0 github.com/deepmap/oapi-codegen v1.8.1 - github.com/fluxcd/go-git-providers v0.4.0 + github.com/fluxcd/go-git-providers v0.5.2-0.20220111143741-04eef9791373 github.com/fluxcd/helm-controller/api v0.14.1 github.com/fluxcd/kustomize-controller/api v0.18.2 github.com/fluxcd/pkg/apis/meta v0.10.1 @@ -17,8 +17,8 @@ require ( github.com/go-errors/errors v1.4.0 // indirect github.com/go-git/go-billy/v5 v5.3.1 github.com/go-git/go-git/v5 v5.4.2 - github.com/go-logr/logr v1.1.0 - github.com/go-logr/zapr v1.1.0 + github.com/go-logr/logr v1.2.2 + github.com/go-logr/zapr v1.2.2 github.com/go-resty/resty/v2 v2.6.0 github.com/gobuffalo/flect v0.2.2 // indirect github.com/golang-jwt/jwt/v4 v4.0.0 @@ -49,7 +49,7 @@ require ( github.com/spf13/viper v1.9.0 github.com/stretchr/testify v1.7.0 github.com/weaveworks/go-checkpoint v0.0.0-20170503165305-ebbb8b0518ab - github.com/xanzy/go-gitlab v0.51.1 + github.com/xanzy/go-gitlab v0.54.3 go.uber.org/zap v1.19.0 golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect @@ -70,7 +70,10 @@ require ( sigs.k8s.io/yaml v1.3.0 ) -require github.com/oauth2-proxy/mockoidc v0.0.0-20210703044157-382d3faf2671 +require ( + github.com/oauth2-proxy/mockoidc v0.0.0-20210703044157-382d3faf2671 + gopkg.in/square/go-jose.v2 v2.5.1 +) require ( cloud.google.com/go v0.93.3 // indirect @@ -133,8 +136,8 @@ require ( github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/btree v1.0.1 // indirect - github.com/google/go-github/v35 v35.3.0 // indirect - github.com/google/go-querystring v1.0.0 // indirect + github.com/google/go-github/v41 v41.0.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/googleapis/gnostic v0.5.5 // indirect github.com/gorilla/mux v1.8.0 // indirect @@ -207,9 +210,9 @@ require ( go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect - golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect + golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/mod v0.4.2 // indirect - golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect + golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect golang.org/x/text v0.3.7 // indirect @@ -221,7 +224,6 @@ require ( gopkg.in/gorp.v1 v1.7.2 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.63.2 // indirect - gopkg.in/square/go-jose.v2 v2.5.1 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect @@ -246,9 +248,9 @@ replace github.com/opencontainers/runc => github.com/opencontainers/runc v1.0.2 // https://github.com/gorilla/websocket/security/advisories/GHSA-jf24-p9p9-4rjh replace github.com/gorilla/websocket v0.0.0 => github.com/gorilla/websocket v1.4.1 -replace github.com/go-logr/logr v1.1.0 => github.com/go-logr/logr v0.4.0 +replace github.com/go-logr/logr v1.2.2 => github.com/go-logr/logr v0.4.0 -replace github.com/go-logr/zapr v1.1.0 => github.com/go-logr/zapr v0.4.0 +replace github.com/go-logr/zapr v1.2.2 => github.com/go-logr/zapr v0.4.0 // https://security.snyk.io/vuln/SNYK-GOLANG-GITHUBCOMOPENCONTAINERSIMAGESPECSPECSGOV1-1922832 replace github.com/opencontainers/image-spec v1.0.1 => github.com/opencontainers/image-spec v1.0.2 diff --git a/go.sum b/go.sum index a72a399339..032102e8e7 100644 --- a/go.sum +++ b/go.sum @@ -368,8 +368,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= -github.com/fluxcd/go-git-providers v0.4.0 h1:hdGGRDCNphf9FRrk297lorhwHWcST74R7cGAOZTRtSU= -github.com/fluxcd/go-git-providers v0.4.0/go.mod h1:enIPrXnSOBxahS6rngohpG3d/QZ3yjjy/w+agbp97ZI= +github.com/fluxcd/go-git-providers v0.5.2-0.20220111143741-04eef9791373 h1:d2RHYaFZrWQy3KKobyqkwToLxH8kgujnqf8/BGo8cj8= +github.com/fluxcd/go-git-providers v0.5.2-0.20220111143741-04eef9791373/go.mod h1:4jTHTmSx3rFGnG78KUVgFYeG6vWFnKwUSr2mi31tvp8= github.com/fluxcd/helm-controller/api v0.14.1 h1:aAWaYZxTI68SD1R2SpNJh8+hm9oBeIOa9nW4YX5qYjM= github.com/fluxcd/helm-controller/api v0.14.1/go.mod h1:NkfZ5ugs9EUUPSGHfAnNs295mf8sVKG0842aL6cFzMM= github.com/fluxcd/kustomize-controller/api v0.18.2 h1:rGu9R6PMFw3x0S6tVj/ZS54sJWW6/cdWe0Gga09e1AY= @@ -562,7 +562,6 @@ github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= -github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -607,10 +606,11 @@ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II= github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI= -github.com/google/go-github/v35 v35.3.0 h1:fU+WBzuukn0VssbayTT+Zo3/ESKX9JYWjbZTLOTEyho= -github.com/google/go-github/v35 v35.3.0/go.mod h1:yWB7uCcVWaUbUP74Aq3whuMySRMatyRmq5U9FTNlbio= -github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= +github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg= +github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= @@ -695,7 +695,6 @@ github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjh github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= -github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-retryablehttp v0.6.8/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4= github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= @@ -769,8 +768,6 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= -github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= -github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/karrick/godirwalk v1.15.8 h1:7+rWAZPn9zuRxaIqqT8Ohs2Q2Ac0msBqwRdxNCr2VVs= @@ -801,7 +798,6 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/ktrysmt/go-bitbucket v0.9.28/go.mod h1:FWxy2UK7GlK5b0NSJGc5hPqnssVlkNnsChvyuOf/Xno= github.com/labstack/echo/v4 v4.2.1 h1:LF5Iq7t/jrtUuSutNuiEWtB5eiHfZ5gSe2pcu5exjQw= github.com/labstack/echo/v4 v4.2.1/go.mod h1:AA49e0DZ8kk5jTOOCKNuPR6oTnBS0dYiM4FW1e6jwpg= github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= @@ -899,7 +895,6 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= @@ -1215,8 +1210,8 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17 github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/weaveworks/go-checkpoint v0.0.0-20170503165305-ebbb8b0518ab h1:mW+hgchD9qUUBqnuaDBj7BkcpFPk/FxeFcUFI5lvvUw= github.com/weaveworks/go-checkpoint v0.0.0-20170503165305-ebbb8b0518ab/go.mod h1:qkbvw5GPibQ/Nf7IZJL0UoLwmJ6858b4S/hUWRd+cH4= -github.com/xanzy/go-gitlab v0.51.1 h1:wWKLalwx4omxFoHh3PLs9zDgAD4GXDP/uoxwMRCSiWM= -github.com/xanzy/go-gitlab v0.51.1/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE= +github.com/xanzy/go-gitlab v0.54.3 h1:fPfZ3Jcu5dPc3xyIYtAALZsEgoyKNFNuULD+TdJ7Zvk= +github.com/xanzy/go-gitlab v0.54.3/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= @@ -1337,8 +1332,9 @@ golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= -golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -1377,7 +1373,6 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1440,11 +1435,10 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20210825183410-e898025ed96a h1:bRuuGXV8wwSdGTB+CtJf+FjgO1APK1CoO39T4BN/XBw= golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/oauth2 v0.0.0-20180227000427-d7d64896b5ff/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1475,7 +1469,6 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180224232135-f6cff0780e54/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1718,10 +1711,8 @@ google.golang.org/api v0.50.0/go.mod h1:4bNT5pAuq5ji4SRZm+5QIkjny9JAyVD/3gaSihNe google.golang.org/api v0.51.0/go.mod h1:t4HdrdoNgyN5cbEfm7Lum0lcLDLiise1F8qDKX00sOU= google.golang.org/api v0.54.0/go.mod h1:7C4bFFOvVDGXjfDTAsgGwDgAxRDeQ4X8NvUedIt6z3k= google.golang.org/api v0.56.0/go.mod h1:38yMfeP1kfjsl8isn0tliTjIb1rJXcQi4UXlbqivdVE= -google.golang.org/appengine v1.0.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= diff --git a/pkg/server/profiles.go b/pkg/server/profiles.go index e11eaae419..6eaf05efd5 100644 --- a/pkg/server/profiles.go +++ b/pkg/server/profiles.go @@ -109,7 +109,7 @@ func (s *ProfilesServer) GetProfiles(ctx context.Context, msg *pb.GetProfilesReq } } - return nil, fmt.Errorf("failed to get HelmRepository %q/%q", s.HelmRepoNamespace, s.HelmRepoName) + return nil, fmt.Errorf("failed to get HelmRepository %q/%q: %s", s.HelmRepoNamespace, s.HelmRepoName, err) } ps, err := s.HelmChartManager.GetCharts(ctx, helmRepo, helm.Profiles) From 9f24ea40a83fecc6680c9f48b799ab1851ec1608 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Thu, 13 Jan 2022 16:24:52 +0100 Subject: [PATCH 3/9] Fixes upgrade command --- cmd/gitops/upgrade/cmd.go | 2 +- pkg/git/git.go | 1 + pkg/git/gitfakes/fake_git.go | 79 +++++++++++++++++++++++++++++++++ pkg/git/gogit.go | 23 ++++++++++ pkg/upgrade/upgrade.go | 84 +++++++++++++++++++++++++----------- 5 files changed, 163 insertions(+), 26 deletions(-) diff --git a/cmd/gitops/upgrade/cmd.go b/cmd/gitops/upgrade/cmd.go index d8428e2e93..56540de578 100644 --- a/cmd/gitops/upgrade/cmd.go +++ b/cmd/gitops/upgrade/cmd.go @@ -38,7 +38,7 @@ var Cmd = &cobra.Command{ func init() { Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.ConfigRepo, "config-repo", "", "URL of external repository that will hold automation manifests") Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.Version, "version", "", "Version of Weave GitOps Enterprise to be installed") - Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.BaseBranch, "base", "main", "The base branch to open the pull request against") + Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.BaseBranch, "base", "", "The base branch to open the pull request against") Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.HeadBranch, "branch", "tier-upgrade-enterprise", "The branch to create the pull request from") Cmd.PersistentFlags().StringVar(&upgradeCmdFlags.CommitMessage, "commit-message", "Upgrade to WGE", "The commit message") Cmd.PersistentFlags().StringArrayVar(&upgradeCmdFlags.Values, "set", []string{}, "set profile values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)") diff --git a/pkg/git/git.go b/pkg/git/git.go index 6dad17866b..96ce1d53b9 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -82,6 +82,7 @@ type Git interface { Init(path, url, branch string) (bool, error) Clone(ctx context.Context, path, url, branch string) (bool, error) Checkout(newBranch string) error + Read(path string) ([]byte, error) Write(path string, content []byte) error Remove(path string) error Commit(message Commit, filters ...func(string) bool) (string, error) diff --git a/pkg/git/gitfakes/fake_git.go b/pkg/git/gitfakes/fake_git.go index 193497dd52..36f790d91b 100644 --- a/pkg/git/gitfakes/fake_git.go +++ b/pkg/git/gitfakes/fake_git.go @@ -116,6 +116,19 @@ type FakeGit struct { pushReturnsOnCall map[int]struct { result1 error } + ReadStub func(string) ([]byte, error) + readMutex sync.RWMutex + readArgsForCall []struct { + arg1 string + } + readReturns struct { + result1 []byte + result2 error + } + readReturnsOnCall map[int]struct { + result1 []byte + result2 error + } RemoveStub func(string) error removeMutex sync.RWMutex removeArgsForCall []struct { @@ -673,6 +686,70 @@ func (fake *FakeGit) PushReturnsOnCall(i int, result1 error) { }{result1} } +func (fake *FakeGit) Read(arg1 string) ([]byte, error) { + fake.readMutex.Lock() + ret, specificReturn := fake.readReturnsOnCall[len(fake.readArgsForCall)] + fake.readArgsForCall = append(fake.readArgsForCall, struct { + arg1 string + }{arg1}) + stub := fake.ReadStub + fakeReturns := fake.readReturns + fake.recordInvocation("Read", []interface{}{arg1}) + fake.readMutex.Unlock() + if stub != nil { + return stub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *FakeGit) ReadCallCount() int { + fake.readMutex.RLock() + defer fake.readMutex.RUnlock() + return len(fake.readArgsForCall) +} + +func (fake *FakeGit) ReadCalls(stub func(string) ([]byte, error)) { + fake.readMutex.Lock() + defer fake.readMutex.Unlock() + fake.ReadStub = stub +} + +func (fake *FakeGit) ReadArgsForCall(i int) string { + fake.readMutex.RLock() + defer fake.readMutex.RUnlock() + argsForCall := fake.readArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *FakeGit) ReadReturns(result1 []byte, result2 error) { + fake.readMutex.Lock() + defer fake.readMutex.Unlock() + fake.ReadStub = nil + fake.readReturns = struct { + result1 []byte + result2 error + }{result1, result2} +} + +func (fake *FakeGit) ReadReturnsOnCall(i int, result1 []byte, result2 error) { + fake.readMutex.Lock() + defer fake.readMutex.Unlock() + fake.ReadStub = nil + if fake.readReturnsOnCall == nil { + fake.readReturnsOnCall = make(map[int]struct { + result1 []byte + result2 error + }) + } + fake.readReturnsOnCall[i] = struct { + result1 []byte + result2 error + }{result1, result2} +} + func (fake *FakeGit) Remove(arg1 string) error { fake.removeMutex.Lock() ret, specificReturn := fake.removeReturnsOnCall[len(fake.removeArgsForCall)] @@ -939,6 +1016,8 @@ func (fake *FakeGit) Invocations() map[string][][]interface{} { defer fake.openMutex.RUnlock() fake.pushMutex.RLock() defer fake.pushMutex.RUnlock() + fake.readMutex.RLock() + defer fake.readMutex.RUnlock() fake.removeMutex.RLock() defer fake.removeMutex.RUnlock() fake.statusMutex.RLock() diff --git a/pkg/git/gogit.go b/pkg/git/gogit.go index 6156471d7a..fb40048b87 100644 --- a/pkg/git/gogit.go +++ b/pkg/git/gogit.go @@ -153,6 +153,29 @@ func (g *GoGit) clone(ctx context.Context, path, url, branch string, depth int) return r, nil } +// Read reads the content from the path +func (g *GoGit) Read(path string) ([]byte, error) { + if g.repository == nil { + return nil, ErrNoGitRepository + } + + wt, err := g.repository.Worktree() + if err != nil { + return nil, fmt.Errorf("failed to open the worktree: %w", err) + } + + f, err := wt.Filesystem.Open(path) + if err != nil { + return nil, fmt.Errorf("failed to open file %s: %w", path, err) + } + defer f.Close() + + buf := bytes.NewBuffer(nil) + buf.ReadFrom(f) + + return buf.Bytes(), nil +} + // Write writes the provided content to the path, if the file exists, it will be // truncated. func (g *GoGit) Write(path string, content []byte) error { diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 05e429a481..08a11f451a 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -11,7 +11,6 @@ import ( "strconv" "time" - "github.com/fluxcd/go-git-providers/gitprovider" helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" kustomizev2 "github.com/fluxcd/kustomize-controller/api/v1beta2" "github.com/fluxcd/pkg/apis/meta" @@ -22,6 +21,7 @@ import ( "github.com/weaveworks/weave-gitops/pkg/kube" "github.com/weaveworks/weave-gitops/pkg/logger" "github.com/weaveworks/weave-gitops/pkg/services/automation" + "github.com/weaveworks/weave-gitops/pkg/services/gitrepo" corev1 "k8s.io/api/core/v1" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -95,32 +95,32 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi return fmt.Errorf("failed to normalize URL %q: %w", uv.ConfigRepo, err) } - // Create pull request - path := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, WegoEnterpriseName) - wegoAppPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "wego-app.yaml") - capiKeepPath := filepath.Join(git.WegoRoot, git.WegoAppDir, "capi", "templates", ".keep") - capiKeepContents := string(strconv.AppendQuote(nil, "# keep")) + configBranch := uv.BaseBranch + if configBranch == "" { + configBranch, err = gitProvider.GetDefaultBranch(ctx, normalizedURL) + if err != nil { + return fmt.Errorf("could not determine default branch for config repository: %q %w", uv.ConfigRepo, err) + } + } + + remover, _, err := gitrepo.CloneRepo(ctx, gitClient, normalizedURL, configBranch) + if err != nil { + return fmt.Errorf("failed to clone configuration repo: %w", err) + } + + defer remover() + + err = upgradeGitManfiests(gitClient, cname, stringOut, w) + if err != nil { + return fmt.Errorf("failed to write update manifest in clone repo: %w", err) + } pri := gitproviders.PullRequestInfo{ - Title: "Gitops upgrade", - Description: "Pull request to upgrade to Weave GitOps Enterprise", - CommitMessage: uv.CommitMessage, - TargetBranch: uv.BaseBranch, - NewBranch: uv.HeadBranch, - Files: []gitprovider.CommitFile{ - { - Path: &path, - Content: &stringOut, - }, - { - Path: &capiKeepPath, - Content: &capiKeepContents, - }, - { - Path: &wegoAppPath, - Content: nil, - }, - }, + Title: "Gitops upgrade", + Description: "Pull request to upgrade to Weave GitOps Enterprise", + SkipAddingFilesOnCreation: true, + TargetBranch: configBranch, + NewBranch: uv.HeadBranch, } pr, err := gitProvider.CreatePullRequest(ctx, normalizedURL, pri) @@ -251,6 +251,40 @@ func makeHelmResources(namespace, version, clusterName, repoURL string, values [ return []runtime.Object{helmRepository, helmRelease}, nil } +func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests string, w io.Writer) error { + wegoEnterprisePath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, WegoEnterpriseName) + wegoAppPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "wego-app.yaml") + capiKeepPath := filepath.Join(git.WegoRoot, git.WegoAppDir, "capi", "templates", ".keep") + capiKeepContents := string(strconv.AppendQuote(nil, "# keep")) + manifests := map[string]string{ + wegoEnterprisePath: wegoEnterpriseManifests, + capiKeepPath: capiKeepContents, + } + + for path, content := range manifests { + if err := gitClient.Write(path, []byte(content)); err != nil { + return fmt.Errorf("failed to write out manifest to %s: %s", path, err) + } + } + + // FIXME: replace w/ constant + kustomizationPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "kustomization.yaml") + + kustomizationBytes, err := gitClient.Read(kustomizationPath) + if err != nil { + w.Write([]byte(fmt.Sprintf("Failed to remote existing %s deployment, skipping.\n", wegoAppPath))) + } + + w.Write(kustomizationBytes) + + err = gitClient.Remove(wegoAppPath) + if err != nil { + w.Write([]byte(fmt.Sprintf("Failed to remote existing %s deployment, skipping.\n", wegoAppPath))) + } + + return nil +} + func getBasicAuth(ctx context.Context, kubeClient client.Client, ns string) error { deployKeySecret := &corev1.Secret{} From a3ee1b0ca2ad515356947838df8ec48939f2abbe Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Thu, 13 Jan 2022 17:19:03 +0100 Subject: [PATCH 4/9] wip --- pkg/upgrade/upgrade.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 08a11f451a..d6c8e947ed 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -25,6 +25,7 @@ import ( corev1 "k8s.io/api/core/v1" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/yaml" "k8s.io/apimachinery/pkg/runtime" @@ -110,11 +111,21 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi defer remover() + err = gitClient.Checkout(uv.HeadBranch) + if err != nil { + return fmt.Errorf("failed to create new branch %s: %w", uv.HeadBranch, err) + } + err = upgradeGitManfiests(gitClient, cname, stringOut, w) if err != nil { return fmt.Errorf("failed to write update manifest in clone repo: %w", err) } + err = gitrepo.CommitAndPush(ctx, gitClient, "Upgrade to Weave Gitops Enterprise", logger) + if err != nil { + return fmt.Errorf("failed to commit and push: %w", err) + } + pri := gitproviders.PullRequestInfo{ Title: "Gitops upgrade", Description: "Pull request to upgrade to Weave GitOps Enterprise", @@ -270,12 +281,25 @@ func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests strin // FIXME: replace w/ constant kustomizationPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "kustomization.yaml") + var k types.Kustomization + kustomizationBytes, err := gitClient.Read(kustomizationPath) if err != nil { w.Write([]byte(fmt.Sprintf("Failed to remote existing %s deployment, skipping.\n", wegoAppPath))) } - w.Write(kustomizationBytes) + if err := yaml.Unmarshal(kustomizationBytes, &k); err != nil { + return fmt.Errorf("failed to read existing kustomize file %s: %w", kustomizationPath, err) + } + + k.Resources = append(k.Resources, filepath.Join(".", WegoEnterpriseName)) + + newKustomizationBytes, err := yaml.Marshal(&k) + if err != nil { + return fmt.Errorf("failed to marshal kustomize %v : %w", k, err) + } + + w.Write(newKustomizationBytes) err = gitClient.Remove(wegoAppPath) if err != nil { From 2bc2bee4ca0d3b81280c7e22b4dc5946fae4c1b8 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Thu, 13 Jan 2022 18:02:26 +0100 Subject: [PATCH 5/9] cleancleanclean --- pkg/services/automation/automation_test.go | 2 +- pkg/services/automation/cluster_generator.go | 4 +- pkg/upgrade/upgrade.go | 59 +++++++++++--------- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/pkg/services/automation/automation_test.go b/pkg/services/automation/automation_test.go index e814e6b649..e1517de456 100644 --- a/pkg/services/automation/automation_test.go +++ b/pkg/services/automation/automation_test.go @@ -424,7 +424,7 @@ var _ = Describe("Generate cluster manifests", func() { configBranch, err := gitProviders.GetDefaultBranch(ctx, url) Expect(err).ShouldNot(HaveOccurred()) - sourceName := createClusterSourceName(url) + sourceName := CreateClusterSourceName(url) sourceManifest, err := realFlux.CreateSourceGit(sourceName, url, configBranch, secretStr, namespace) Expect(err).ShouldNot(HaveOccurred()) Expect(clusterAutomation.SourceManifest.Content).To(Equal(sourceManifest)) diff --git a/pkg/services/automation/cluster_generator.go b/pkg/services/automation/cluster_generator.go index dfba656ac0..c6e731348c 100644 --- a/pkg/services/automation/cluster_generator.go +++ b/pkg/services/automation/cluster_generator.go @@ -46,7 +46,7 @@ const ( WegoConfigMapName = "weave-gitops-config" ) -func createClusterSourceName(gitSourceURL gitproviders.RepoURL) string { +func CreateClusterSourceName(gitSourceURL gitproviders.RepoURL) string { provider := string(gitSourceURL.Provider()) cleanRepoName := replaceUnderscores(gitSourceURL.RepositoryName()) qualifiedName := fmt.Sprintf("wego-auto-%s-%s", provider, cleanRepoName) @@ -87,7 +87,7 @@ func (a *AutomationGen) GenerateClusterAutomation(ctx context.Context, cluster m wegoAppManifest := bytes.Join(m, []byte("---\n")) - sourceName := createClusterSourceName(configURL) + sourceName := CreateClusterSourceName(configURL) sourceManifest, err := a.Flux.CreateSourceGit(sourceName, configURL, configBranch, secretStr, namespace) if err != nil { diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index d6c8e947ed..447e225c99 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -116,18 +116,18 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi return fmt.Errorf("failed to create new branch %s: %w", uv.HeadBranch, err) } - err = upgradeGitManfiests(gitClient, cname, stringOut, w) + err = upgradeGitManfiests(gitClient, cname, stringOut, logger) if err != nil { return fmt.Errorf("failed to write update manifest in clone repo: %w", err) } - err = gitrepo.CommitAndPush(ctx, gitClient, "Upgrade to Weave Gitops Enterprise", logger) + err = gitrepo.CommitAndPush(ctx, gitClient, uv.CommitMessage, logger) if err != nil { return fmt.Errorf("failed to commit and push: %w", err) } pri := gitproviders.PullRequestInfo{ - Title: "Gitops upgrade", + Title: uv.CommitMessage, Description: "Pull request to upgrade to Weave GitOps Enterprise", SkipAddingFilesOnCreation: true, TargetBranch: configBranch, @@ -139,9 +139,9 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi return err } - _, err = w.Write([]byte(fmt.Sprintf("Pull Request created: %s\n", pr.Get().WebURL))) + logger.Successf("Pull Request created: %s", pr.Get().WebURL) - return err + return nil } func marshalToYamlStream(objects []runtime.Object) ([]byte, error) { @@ -165,7 +165,7 @@ func makeAppsCapiKustomization(namespace, repoURL string) ([]runtime.Object, err return nil, fmt.Errorf("failed to normalize URL %q: %w", repoURL, err) } - gitRepositoryName := automation.CreateRepoSecretName(normalizedURL).String() + gitRepositoryName := automation.CreateClusterSourceName(normalizedURL) appsCapiKustomization := &kustomizev2.Kustomization{ ObjectMeta: metav1.ObjectMeta{ @@ -262,14 +262,22 @@ func makeHelmResources(namespace, version, clusterName, repoURL string, values [ return []runtime.Object{helmRepository, helmRelease}, nil } -func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests string, w io.Writer) error { - wegoEnterprisePath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, WegoEnterpriseName) - wegoAppPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "wego-app.yaml") +func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests string, logger logger.Logger) error { capiKeepPath := filepath.Join(git.WegoRoot, git.WegoAppDir, "capi", "templates", ".keep") capiKeepContents := string(strconv.AppendQuote(nil, "# keep")) + kustomizationPath := git.GetSystemQualifiedPath(cname, automation.SystemKustomizationPath) + wegoAppPath := git.GetSystemQualifiedPath(cname, automation.WegoAppPath) + wegoEnterprisePath := git.GetSystemQualifiedPath(cname, WegoEnterpriseName) + + newKustomizationBytes, err := updateKustomization(gitClient, kustomizationPath, wegoAppPath, wegoEnterprisePath) + if err != nil { + return fmt.Errorf("Failed to update kustomization: %s", err) + } + manifests := map[string]string{ wegoEnterprisePath: wegoEnterpriseManifests, capiKeepPath: capiKeepContents, + kustomizationPath: string(newKustomizationBytes), } for path, content := range manifests { @@ -278,35 +286,36 @@ func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests strin } } - // FIXME: replace w/ constant - kustomizationPath := filepath.Join(git.WegoRoot, git.WegoClusterDir, cname, git.WegoClusterOSWorkloadDir, "kustomization.yaml") + err = gitClient.Remove(wegoAppPath) + if err != nil { + logger.Warningf("Failed to remove existing %s deployment, skipping.\n", wegoAppPath) + } - var k types.Kustomization + return nil +} +func updateKustomization(gitClient git.Git, kustomizationPath, wegoAppPath, wegoEnterprisePath string) ([]byte, error) { kustomizationBytes, err := gitClient.Read(kustomizationPath) if err != nil { - w.Write([]byte(fmt.Sprintf("Failed to remote existing %s deployment, skipping.\n", wegoAppPath))) + return nil, fmt.Errorf("Failed to read existing kustomization %s: %w", kustomizationPath, err) } + var k types.Kustomization if err := yaml.Unmarshal(kustomizationBytes, &k); err != nil { - return fmt.Errorf("failed to read existing kustomize file %s: %w", kustomizationPath, err) + return nil, fmt.Errorf("failed to unmarshal kustomization file %s: %w", kustomizationPath, err) } - k.Resources = append(k.Resources, filepath.Join(".", WegoEnterpriseName)) + newResources := []string{WegoEnterpriseName} - newKustomizationBytes, err := yaml.Marshal(&k) - if err != nil { - return fmt.Errorf("failed to marshal kustomize %v : %w", k, err) + for _, resource := range k.Resources { + if resource != automation.WegoAppPath { + newResources = append(newResources, resource) + } } - w.Write(newKustomizationBytes) + k.Resources = newResources - err = gitClient.Remove(wegoAppPath) - if err != nil { - w.Write([]byte(fmt.Sprintf("Failed to remote existing %s deployment, skipping.\n", wegoAppPath))) - } - - return nil + return yaml.Marshal(&k) } func getBasicAuth(ctx context.Context, kubeClient client.Client, ns string) error { From 07c702798cfea3f4d0910f26b73cafc347b639c3 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Thu, 13 Jan 2022 18:53:36 +0100 Subject: [PATCH 6/9] adds tests for Read() --- pkg/git/gogit_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pkg/git/gogit_test.go b/pkg/git/gogit_test.go index 607ede9e82..7262d237c7 100644 --- a/pkg/git/gogit_test.go +++ b/pkg/git/gogit_test.go @@ -142,6 +142,63 @@ var _ = Describe("ValidateAccess", func() { }) }) +var _ = Describe("Read", func() { + It("Reads a file from a repo", func() { + _, err = gitClient.Init(dir, "https://github.com/github/gitignore", "master") + + filePath := "/test.txt" + content := []byte("testing") + Expect(gitClient.Write(filePath, content)).To(Succeed()) + + _, err = gitClient.Commit(git.Commit{ + Author: git.Author{Name: "test", Email: "test@example.com"}, + Message: "test commit", + }) + Expect(err).ShouldNot(HaveOccurred()) + + fileContent, err := gitClient.Read(filePath) + Expect(err).ShouldNot(HaveOccurred()) + Expect(content).To(Equal(fileContent)) + }) + + It("Reads a file from a repo even if not commited", func() { + _, err = gitClient.Init(dir, "https://github.com/github/gitignore", "master") + filePath := "/test.txt" + content := []byte("testing") + err := ioutil.WriteFile(dir+filePath, content, 0644) + Expect(err).ShouldNot(HaveOccurred()) + fileContent, err := gitClient.Read(filePath) + Expect(err).ShouldNot(HaveOccurred()) + Expect(content).To(Equal(fileContent)) + }) + + It("gives a nice error when file not present", func() { + _, err = gitClient.Init(dir, "https://github.com/github/gitignore", "master") + filePath := "/test.txt" + _, err := gitClient.Read(filePath) + Expect(fmt.Sprint(err)).To(MatchRegexp("failed to open file /test.txt")) + }) + + It("fails when the repository has not been initialized", func() { + _, err := gitClient.Init(dir, "https://github.com/github/gitignore", "master") + Expect(err).ShouldNot(HaveOccurred()) + + gc := git.New(nil, wrapper.NewGoGit()) + _, err = gc.Read("testing.txt") + Expect(err).To(MatchError("no git repository")) + }) + + It("returns an error if the repository is bare", func() { + _, err = gogit.PlainInit(dir, true) + Expect(err).ShouldNot(HaveOccurred()) + _, err = gitClient.Open(dir) + Expect(err).ShouldNot(HaveOccurred()) + + _, err := gitClient.Read("testing.txt") + Expect(err).Should(MatchError("failed to open the worktree: worktree not available in a bare repository")) + }) +}) + var _ = Describe("Write", func() { It("writes a file into a given repository", func() { _, err = gitClient.Init(dir, "https://github.com/github/gitignore", "master") From ce2715613ed9762f3d976aedaa0c3276e6e4197a Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Thu, 13 Jan 2022 19:16:51 +0100 Subject: [PATCH 7/9] check that buffer --- pkg/git/gogit.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/git/gogit.go b/pkg/git/gogit.go index fb40048b87..22889de9c4 100644 --- a/pkg/git/gogit.go +++ b/pkg/git/gogit.go @@ -171,7 +171,11 @@ func (g *GoGit) Read(path string) ([]byte, error) { defer f.Close() buf := bytes.NewBuffer(nil) - buf.ReadFrom(f) + + _, err = buf.ReadFrom(f) + if err != nil { + return nil, fmt.Errorf("error reading bytes from buffer %s: %w", path, err) + } return buf.Bytes(), nil } From 9330818fbf999a63871bf69336763fd9b08917f4 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 13 Jan 2022 20:38:53 +0100 Subject: [PATCH 8/9] Apply suggestions from code review --- pkg/server/profiles.go | 2 +- pkg/upgrade/upgrade.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/server/profiles.go b/pkg/server/profiles.go index 6eaf05efd5..81c3154baf 100644 --- a/pkg/server/profiles.go +++ b/pkg/server/profiles.go @@ -109,7 +109,7 @@ func (s *ProfilesServer) GetProfiles(ctx context.Context, msg *pb.GetProfilesReq } } - return nil, fmt.Errorf("failed to get HelmRepository %q/%q: %s", s.HelmRepoNamespace, s.HelmRepoName, err) + return nil, fmt.Errorf("failed to get HelmRepository %q/%q: %w", s.HelmRepoNamespace, s.HelmRepoName, err) } ps, err := s.HelmChartManager.GetCharts(ctx, helmRepo, helm.Profiles) diff --git a/pkg/upgrade/upgrade.go b/pkg/upgrade/upgrade.go index 447e225c99..18542be471 100644 --- a/pkg/upgrade/upgrade.go +++ b/pkg/upgrade/upgrade.go @@ -116,7 +116,7 @@ func upgrade(ctx context.Context, uv UpgradeValues, kube kube.Kube, gitClient gi return fmt.Errorf("failed to create new branch %s: %w", uv.HeadBranch, err) } - err = upgradeGitManfiests(gitClient, cname, stringOut, logger) + err = upgradeGitManifests(gitClient, cname, stringOut, logger) if err != nil { return fmt.Errorf("failed to write update manifest in clone repo: %w", err) } @@ -262,7 +262,7 @@ func makeHelmResources(namespace, version, clusterName, repoURL string, values [ return []runtime.Object{helmRepository, helmRelease}, nil } -func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests string, logger logger.Logger) error { +func upgradeGitManifests(gitClient git.Git, cname, wegoEnterpriseManifests string, logger logger.Logger) error { capiKeepPath := filepath.Join(git.WegoRoot, git.WegoAppDir, "capi", "templates", ".keep") capiKeepContents := string(strconv.AppendQuote(nil, "# keep")) kustomizationPath := git.GetSystemQualifiedPath(cname, automation.SystemKustomizationPath) @@ -271,7 +271,7 @@ func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests strin newKustomizationBytes, err := updateKustomization(gitClient, kustomizationPath, wegoAppPath, wegoEnterprisePath) if err != nil { - return fmt.Errorf("Failed to update kustomization: %s", err) + return fmt.Errorf("Failed to update kustomization: %w", err) } manifests := map[string]string{ @@ -282,7 +282,7 @@ func upgradeGitManfiests(gitClient git.Git, cname, wegoEnterpriseManifests strin for path, content := range manifests { if err := gitClient.Write(path, []byte(content)); err != nil { - return fmt.Errorf("failed to write out manifest to %s: %s", path, err) + return fmt.Errorf("failed to write out manifest to %s: %w", path, err) } } From 589b96fe99c07be41cc32ffc53bb21d24c6a3545 Mon Sep 17 00:00:00 2001 From: Simon Howe Date: Mon, 17 Jan 2022 11:34:23 +0100 Subject: [PATCH 9/9] poke --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0a0926a25b..cf9bb44d4f 100644 --- a/README.md +++ b/README.md @@ -176,3 +176,4 @@ Need help or want to contribute? Please see the links below. ## License scan details [![FOSSA Status](https://app.fossa.com/api/projects/custom%2B19155%2Fgithub.com%2Fweaveworks%2Fweave-gitops.svg?type=large)](https://app.fossa.com/reports/005da7c4-1f10-4889-9432-8b97c2084e41) +