Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show diff when upgrading #934

Merged
merged 80 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 77 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
d1caaf6
Made an initial try at the consul-k8s upgrade command. Running into i…
sadjamz Nov 15, 2021
6edce01
notes from sync with Saad on what's left
ndhanushkodi Nov 23, 2021
fa4d8e7
Made an initial try at the consul-k8s upgrade command. Running into i…
sadjamz Nov 15, 2021
ac0b8bd
First pass at upgrade was successful.
sadjamz Nov 18, 2021
71b53b5
notes from sync with Saad on what's left
ndhanushkodi Nov 23, 2021
c152498
Some basic cleanup
Dec 1, 2021
8e5395b
Add the namespace and install flags
Dec 2, 2021
8054a42
Add flag test and remove install option
Dec 3, 2021
add5ff9
Move presets into a config package
Dec 3, 2021
0f55c29
Add Changelog
Dec 3, 2021
b0a7761
Made an initial try at the consul-k8s upgrade command. Running into i…
sadjamz Nov 15, 2021
a653e04
Upgrade commit
sadjamz Nov 17, 2021
73646bc
notes from sync with Saad on what's left
ndhanushkodi Nov 23, 2021
9ba277b
This commit contains the MapDiff function to compare the YAML of the …
sadjamz Nov 24, 2021
43b0569
Remove double comment on name setting
Dec 3, 2021
0a93e86
Clean up some merge issues from rebase
Dec 3, 2021
41bc84e
Move MergeMaps to util
Dec 3, 2021
850eccf
Reduce indent on check for installation
Dec 3, 2021
7723427
Clean up Help fn
Dec 3, 2021
6b95c6e
Use Synopsis in Help
Dec 3, 2021
26a6667
Remove duplicated MapMerge from git rebase
Dec 3, 2021
67d611f
Add IsValidLabel to utils
Dec 3, 2021
6b194e6
Move chart loading code to chart file
Dec 3, 2021
cab221d
Use the IsValidLabel in common
Dec 3, 2021
93a3496
Use LoadChart
Dec 3, 2021
a636932
Move create UI logger closer to it's first call
Dec 3, 2021
2ac44bb
Rename vals to chartValues
Dec 4, 2021
0120330
Add godebug dep
Dec 7, 2021
b9e157d
Add WithDiff{Added,Removed}Style
Dec 7, 2021
15012f1
Run go mod tidy
Dec 13, 2021
d6bef98
Remove double k8serrors from git
Dec 13, 2021
49c8759
Run go mod tidy
Dec 14, 2021
b20799d
Remove errant changelog entry for upgrade
Dec 14, 2021
8f35932
Fix some issues from the Git rebase
Dec 14, 2021
1675f78
Show a diff using a simple YAML differ
Dec 14, 2021
198485b
Fix a val to chartValues misnaming from Git
Dec 14, 2021
698b3a9
Add the diffing dependency
Dec 14, 2021
475c43e
Clean up comments and move some non-validating behavior out of the va…
Dec 14, 2021
11502de
Start writing my own diff
Dec 15, 2021
855e9fd
`go mod tidy` 2k22
Jan 5, 2022
5e3253b
Add several diff testing scenarios
Jan 5, 2022
7327493
Handle diffs using YAML parsing
Jan 5, 2022
79b284e
Get diff working with more complex maps
Jan 6, 2022
29e08c4
Move FetchChartValues so that public functions are at the top of the …
Jan 6, 2022
e778ed3
Handle error on invalid timeout flag
Jan 6, 2022
037becd
Undo that weird indent change.
Jan 6, 2022
946a90e
Catch the diff error
Jan 6, 2022
e3cb3da
Modify chart fixtures to be loadable
Jan 6, 2022
14efacd
Test loading charts
Jan 6, 2022
fb838f4
Check the error returned from diffRecursively.
Jan 6, 2022
9518c82
Update changelog
Jan 6, 2022
ae51d3e
Update cli/cmd/common/chart.go
Jan 10, 2022
ef3c737
TestReadChartFiles checks contents of files
Jan 10, 2022
160bebb
Test uppercase and underscores
Jan 10, 2022
4db1424
Test removal scenarios
Jan 10, 2022
ea2975d
Fix formatting
Jan 10, 2022
487075e
Update cli/cmd/common/diff.go
Jan 14, 2022
a9136cb
Change aMapSlice to aMapWithKey
Jan 14, 2022
c31ddd1
`go mod tidy`
Jan 14, 2022
38bf3b1
Replace "reflection" with better code
Jan 14, 2022
10a94b5
Remove double printing config
Jan 18, 2022
bf02b37
Return string, err from diffRecursively
Jan 18, 2022
e7e485d
Move common package up to cli root
Jan 18, 2022
b09a473
Move chart to helm package
Jan 19, 2022
b954032
Fix error string on install
Jan 19, 2022
0ab09f9
Delete the binary I added accidentally
Jan 19, 2022
57808eb
Move diff to helm pkg
Jan 19, 2022
87e0515
Match the update to /x/sys
Jan 20, 2022
ca325b8
Move diffing code back to common
Jan 20, 2022
08080df
Fix indentation on the diff and add a header explaining
Jan 20, 2022
ed9cb46
Remove extra name/namespace print
Jan 20, 2022
5119108
Remove extra dry-run printing
Jan 20, 2022
ac268af
Fix comment on WithDiffUnchangedStyle
Jan 20, 2022
53b001f
Remove unused constants
Jan 20, 2022
7f8e4e0
Merge branch 'main' into cli-upgrade-diff
Jan 20, 2022
5fc2bbf
Merge branch 'main' into cli-upgrade-diff
Jan 21, 2022
492785e
Merge branch 'main' into cli-upgrade-diff
Jan 24, 2022
0332ad7
Add a test case for valid label with leading numbers
Jan 26, 2022
e26140d
Add test for MergeMaps
Jan 26, 2022
5353486
Reassure users with a dry run header
Jan 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ IMPROVEMENTS:
* Support `ui.dashboardURLTemplates.service` value for setting [dashboard URL templates](https://www.consul.io/docs/agent/options#ui_config_dashboard_url_templates_service). [[GH-937](https://github.com/hashicorp/consul-k8s/pull/937)]
* Allow using dash-separated names for config entries when using `kubectl`. [[GH-965](https://github.com/hashicorp/consul-k8s/pull/965)]
* Support Pod Security Policies with Vault integration. [[GH-985](https://github.com/hashicorp/consul-k8s/pull/985)]
* CLI
* Show a diff when upgrading a Consul installation on Kubernetes [[GH-934](https://github.com/hashicorp/consul-k8s/pull/934)]
* Control Plane
* Support the value `$POD_NAME` for the annotation `consul.hashicorp.com/service-meta-*` that will now be interpolated and set to the pod's name in the service's metadata. [[GH-982](https://github.com/hashicorp/consul-k8s/pull/982)]
* Allow managing Consul sidecar resources via annotations. [[GH-956](https://github.com/hashicorp/consul-k8s/pull/956)]
Expand Down
1 change: 0 additions & 1 deletion cli/cmd/common/fixtures/consul/Chart.yaml

This file was deleted.

1 change: 0 additions & 1 deletion cli/cmd/common/fixtures/consul/templates/foo.yaml

This file was deleted.

1 change: 0 additions & 1 deletion cli/cmd/common/fixtures/consul/values.yaml

This file was deleted.

136 changes: 0 additions & 136 deletions cli/cmd/common/utils.go

This file was deleted.

39 changes: 0 additions & 39 deletions cli/cmd/common/utils_test.go

This file was deleted.

59 changes: 12 additions & 47 deletions cli/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"time"

consulChart "github.com/hashicorp/consul-k8s/charts"
"github.com/hashicorp/consul-k8s/cli/cmd/common"
"github.com/hashicorp/consul-k8s/cli/cmd/common/flag"
"github.com/hashicorp/consul-k8s/cli/cmd/common/terminal"
"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/consul-k8s/cli/common/flag"
"github.com/hashicorp/consul-k8s/cli/common/terminal"
"github.com/hashicorp/consul-k8s/cli/config"
"github.com/hashicorp/consul-k8s/cli/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
helmCLI "helm.sh/helm/v3/pkg/cli"
Expand Down Expand Up @@ -313,7 +314,7 @@ func (c *Command) Run(args []string) int {
// Without informing the user, default global.name to consul if it hasn't been set already. We don't allow setting
// the release name, and since that is hardcoded to "consul", setting global.name to "consul" makes it so resources
// aren't double prefixed with "consul-consul-...".
vals = MergeMaps(config.Convert(config.GlobalNameConsul), vals)
vals = common.MergeMaps(config.Convert(config.GlobalNameConsul), vals)

// Dry Run should exit here, no need to actual locate/download the charts.
if c.flagDryRun {
Expand Down Expand Up @@ -342,7 +343,7 @@ func (c *Command) Run(args []string) int {

// Setup action configuration for Helm Go SDK function calls.
actionConfig := new(action.Configuration)
actionConfig, err = common.InitActionConfig(actionConfig, c.flagNamespace, settings, uiLogger)
actionConfig, err = helm.InitActionConfig(actionConfig, c.flagNamespace, settings, uiLogger)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
Expand All @@ -357,7 +358,7 @@ func (c *Command) Run(args []string) int {
install.Timeout = c.timeoutDuration

// Read the embedded chart files into []*loader.BufferedFile.
chartFiles, err := common.ReadChartFiles(consulChart.ConsulHelmChart, common.TopLevelChartDirName)
chartFiles, err := helm.ReadChartFiles(consulChart.ConsulHelmChart, common.TopLevelChartDirName)
if err != nil {
c.UI.Output(err.Error(), terminal.WithErrorStyle())
return 1
Expand Down Expand Up @@ -455,32 +456,11 @@ func (c *Command) mergeValuesFlagsWithPrecedence(settings *helmCLI.EnvSettings)
if c.flagPreset != defaultPreset {
// Note the ordering of the function call, presets have lower precedence than set vals.
presetMap := config.Presets[c.flagPreset].(map[string]interface{})
vals = MergeMaps(presetMap, vals)
vals = common.MergeMaps(presetMap, vals)
}
return vals, err
}

// MergeMaps is a helper function used in Run. Merges two maps giving b precedent.
// @source: https://github.com/helm/helm/blob/main/pkg/cli/values/options.go
func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a))
for k, v := range a {
out[k] = v
}
for k, v := range b {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = MergeMaps(bv, v)
continue
}
}
}
out[k] = v
}
return out
}

// validateFlags is a helper function that performs sanity checks on the user's provided flags.
func (c *Command) validateFlags(args []string) error {
if err := c.set.Parse(args); err != nil {
Expand All @@ -490,14 +470,14 @@ func (c *Command) validateFlags(args []string) error {
return errors.New("should have no non-flag arguments")
}
if len(c.flagValueFiles) != 0 && c.flagPreset != defaultPreset {
return fmt.Errorf("Cannot set both -%s and -%s", flagNameConfigFile, flagNamePreset)
return fmt.Errorf("cannot set both -%s and -%s", flagNameConfigFile, flagNamePreset)
}
if _, ok := config.Presets[c.flagPreset]; c.flagPreset != defaultPreset && !ok {
return fmt.Errorf("'%s' is not a valid preset", c.flagPreset)
}
if !validLabel(c.flagNamespace) {
if !common.IsValidLabel(c.flagNamespace) {
return fmt.Errorf("'%s' is an invalid namespace. Namespaces follow the RFC 1123 label convention and must "+
"consist of a lower case alphanumeric character or '-' and must start/end with an alphanumeric", c.flagNamespace)
"consist of a lower case alphanumeric character or '-' and must start/end with an alphanumeric character", c.flagNamespace)
}
duration, err := time.ParseDuration(c.flagTimeout)
if err != nil {
Expand All @@ -507,7 +487,7 @@ func (c *Command) validateFlags(args []string) error {
if len(c.flagValueFiles) != 0 {
for _, filename := range c.flagValueFiles {
if _, err := os.Stat(filename); err != nil && os.IsNotExist(err) {
return fmt.Errorf("File '%s' does not exist.", filename)
return fmt.Errorf("file '%s' does not exist", filename)
}
}
}
Expand All @@ -518,21 +498,6 @@ func (c *Command) validateFlags(args []string) error {
return nil
}

// validLabel is a helper function that checks if a string follows RFC 1123 labels.
func validLabel(s string) bool {
for i, c := range s {
alphanum := ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')
// If the character is not the last or first, it can be a dash.
if i != 0 && i != (len(s)-1) {
alphanum = alphanum || (c == '-')
}
if !alphanum {
return false
}
}
return true
}

// checkValidEnterprise checks and validates an enterprise installation.
// When an enterprise license secret is provided, check that the secret exists
// in the "consul" namespace.
Expand Down
50 changes: 1 addition & 49 deletions cli/cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/hashicorp/consul-k8s/cli/cmd/common"
"github.com/hashicorp/consul-k8s/cli/common"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -121,54 +121,6 @@ func TestValidateFlags(t *testing.T) {
}
}

// TestValidLabel calls validLabel() which checks strings match RFC 1123 label convention.
func TestValidLabel(t *testing.T) {
testCases := []struct {
description string
input string
expected bool
}{
{
"Standard name with leading numbers works.",
"1234-abc",
true,
},
{
"All lower case letters works.",
"peppertrout",
true,
},
{
"Test that dashes in the middle are allowed.",
"pepper-trout",
true,
},
{
"Capitals violate RFC 1123 lower case label.",
"Peppertrout",
false,
},
{
"Underscores are not permitted anywhere.",
"ab_cd",
false,
},
{
"The dash must be in the middle of the word, not on the start/end character.",
"peppertrout-",
false,
},
}

for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
if result := validLabel(testCase.input); result != testCase.expected {
t.Errorf("Incorrect output, got %v and expected %v", result, testCase.expected)
}
})
}
}

// getInitializedCommand sets up a command struct for tests.
func getInitializedCommand(t *testing.T) *Command {
t.Helper()
Expand Down
Loading