From e8f9bbbf9d2651a515c775656b743a7271f43ed7 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Thu, 1 Jun 2023 12:05:53 +0800 Subject: [PATCH] feat: update repo Spec var type skipTLSVerify to bool (#877) * feat: update repo Spec var type skipTLSVerify to bool Signed-off-by: yxxhero --- pkg/app/app_test.go | 2 +- pkg/exectest/helm.go | 4 ++-- pkg/helmexec/exec.go | 4 ++-- pkg/helmexec/exec_test.go | 24 ++++++++++++------------ pkg/helmexec/helmexec.go | 2 +- pkg/state/state.go | 4 ++-- pkg/state/state_test.go | 25 +++++++++---------------- pkg/testutil/mocks.go | 2 +- 8 files changed, 30 insertions(+), 37 deletions(-) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index ad442dcc..bd2f9137 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -2469,7 +2469,7 @@ func (helm *mockHelmExec) SetEnableLiveOutput(enableLiveOutput bool) { func (helm *mockHelmExec) SetDisableForceUpdate(forceUpdate bool) { } -func (helm *mockHelmExec) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error { +func (helm *mockHelmExec) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error { helm.repos = append(helm.repos, mockRepo{Name: name}) return nil } diff --git a/pkg/exectest/helm.go b/pkg/exectest/helm.go index e8a4801c..6019a429 100644 --- a/pkg/exectest/helm.go +++ b/pkg/exectest/helm.go @@ -94,8 +94,8 @@ func (helm *Helm) SetEnableLiveOutput(enableLiveOutput bool) { } func (helm *Helm) SetDisableForceUpdate(forceUpdate bool) { } -func (helm *Helm) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error { - helm.Repo = []string{name, repository, cafile, certfile, keyfile, username, password, managed, passCredentials, skipTLSVerify} +func (helm *Helm) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error { + helm.Repo = []string{name, repository, cafile, certfile, keyfile, username, password, managed, passCredentials, fmt.Sprintf("%v", skipTLSVerify)} return nil } func (helm *Helm) UpdateRepo() error { diff --git a/pkg/helmexec/exec.go b/pkg/helmexec/exec.go index 119d69a7..1215d1db 100644 --- a/pkg/helmexec/exec.go +++ b/pkg/helmexec/exec.go @@ -148,7 +148,7 @@ func (helm *execer) SetDisableForceUpdate(forceUpdate bool) { helm.options.DisableForceUpdate = forceUpdate } -func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error { +func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error { var args []string var out []byte var err error @@ -184,7 +184,7 @@ func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, usernam if passCredentials == "true" { args = append(args, "--pass-credentials") } - if skipTLSVerify == "true" { + if skipTLSVerify { args = append(args, "--insecure-skip-tls-verify") } helm.logger.Infof("Adding repo %v %v", name, repository) diff --git a/pkg/helmexec/exec_test.go b/pkg/helmexec/exec_test.go index 7d66f1a4..b52f5506 100644 --- a/pkg/helmexec/exec_test.go +++ b/pkg/helmexec/exec_test.go @@ -114,7 +114,7 @@ func Test_AddRepo_Helm_3_3_2(t *testing.T) { kubeContext: "dev", runner: &mockRunner{}, } - err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", "") + err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", false) expected := `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --force-update --cert-file cert.pem --key-file key.pem ` @@ -138,7 +138,7 @@ func Test_AddRepo_Helm_3_3_2_NoForceUpdate(t *testing.T) { kubeContext: "dev", runner: &mockRunner{}, } - err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", "") + err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", false) expected := `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --cert-file cert.pem --key-file key.pem ` @@ -155,7 +155,7 @@ func Test_AddRepo(t *testing.T) { var buffer bytes.Buffer logger := NewLogger(&buffer, "debug") helm := MockExecer(logger, "dev") - err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", "") + err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", "", false) expected := `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --cert-file cert.pem --key-file key.pem ` @@ -168,7 +168,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --cert-f } buffer.Reset() - err = helm.AddRepo("myRepo", "https://repo.example.com/", "ca.crt", "", "", "", "", "", "", "") + err = helm.AddRepo("myRepo", "https://repo.example.com/", "ca.crt", "", "", "", "", "", "", false) expected = `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --ca-file ca.crt ` @@ -181,7 +181,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --ca-fil } buffer.Reset() - err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", "", "") + err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", "", false) expected = `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ ` @@ -194,7 +194,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ } buffer.Reset() - err = helm.AddRepo("acrRepo", "", "", "", "", "", "", "acr", "", "") + err = helm.AddRepo("acrRepo", "", "", "", "", "", "", "acr", "", false) expected = `Adding repo acrRepo (acr) exec: az acr helm repo add --name acrRepo exec: az acr helm repo add --name acrRepo: @@ -208,7 +208,7 @@ exec: az acr helm repo add --name acrRepo: } buffer.Reset() - err = helm.AddRepo("otherRepo", "", "", "", "", "", "", "unknown", "", "") + err = helm.AddRepo("otherRepo", "", "", "", "", "", "", "unknown", "", false) expected = `ERROR: unknown type 'unknown' for repository otherRepo ` if err != nil { @@ -219,7 +219,7 @@ exec: az acr helm repo add --name acrRepo: } buffer.Reset() - err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "", "") + err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "", false) expected = `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --username example_user --password example_password ` @@ -231,7 +231,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --userna } buffer.Reset() - err = helm.AddRepo("", "https://repo.example.com/", "", "", "", "", "", "", "", "") + err = helm.AddRepo("", "https://repo.example.com/", "", "", "", "", "", "", "", false) expected = `empty field name ` @@ -243,7 +243,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --userna } buffer.Reset() - err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "true", "") + err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "true", false) expected = `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --username example_user --password example_password --pass-credentials ` @@ -255,7 +255,7 @@ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --userna } buffer.Reset() - err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", "", "true") + err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", "", true) expected = `Adding repo myRepo https://repo.example.com/ exec: helm --kube-context dev repo add myRepo https://repo.example.com/ --insecure-skip-tls-verify ` @@ -856,7 +856,7 @@ func Test_LogLevels(t *testing.T) { buffer.Reset() logger := NewLogger(&buffer, logLevel) helm := MockExecer(logger, "") - err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "", "") + err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", "", false) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/pkg/helmexec/helmexec.go b/pkg/helmexec/helmexec.go index 0f8b807f..d2f096b2 100644 --- a/pkg/helmexec/helmexec.go +++ b/pkg/helmexec/helmexec.go @@ -16,7 +16,7 @@ type Interface interface { SetEnableLiveOutput(enableLiveOutput bool) SetDisableForceUpdate(forceUpdate bool) - AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error + AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error UpdateRepo() error RegistryLogin(name string, username string, password string) error BuildDeps(name, chart string, flags ...string) error diff --git a/pkg/state/state.go b/pkg/state/state.go index 93a24a75..8eb812ed 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -210,7 +210,7 @@ type RepositorySpec struct { Managed string `yaml:"managed,omitempty"` OCI bool `yaml:"oci,omitempty"` PassCredentials string `yaml:"passCredentials,omitempty"` - SkipTLSVerify string `yaml:"skipTLSVerify,omitempty"` + SkipTLSVerify bool `yaml:"skipTLSVerify,omitempty"` } type Inherit struct { @@ -499,7 +499,7 @@ func (st *HelmState) ApplyOverrides(spec *ReleaseSpec) { type RepoUpdater interface { IsHelm3() bool - AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error + AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error UpdateRepo() error RegistryLogin(name string, username string, password string) error } diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index 0d389fa5..7202306c 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -1093,11 +1093,10 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "", Password: "", PassCredentials: "", - SkipTLSVerify: "", }, }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "", "", "", "", "", "", ""}, + want: []string{"name", "http://example.com/", "", "", "", "", "", "", "", "false"}, }, { name: "ACR hosted repository", @@ -1108,7 +1107,7 @@ func TestHelmState_SyncRepos(t *testing.T) { }, }, helm: &exectest.Helm{}, - want: []string{"name", "", "", "", "", "", "", "acr", "", ""}, + want: []string{"name", "", "", "", "", "", "", "acr", "", "false"}, }, { name: "repository with cert and key", @@ -1121,11 +1120,10 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "", Password: "", PassCredentials: "", - SkipTLSVerify: "", }, }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "certfile", "keyfile", "", "", "", "", ""}, + want: []string{"name", "http://example.com/", "", "certfile", "keyfile", "", "", "", "", "false"}, }, { name: "repository with ca file", @@ -1137,11 +1135,10 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "", Password: "", PassCredentials: "", - SkipTLSVerify: "", }, }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "cafile", "", "", "", "", "", "", ""}, + want: []string{"name", "http://example.com/", "cafile", "", "", "", "", "", "", "false"}, }, { name: "repository with username and password", @@ -1154,11 +1151,10 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "example_user", Password: "example_password", PassCredentials: "", - SkipTLSVerify: "", }, }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "", ""}, + want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "", "false"}, }, { name: "repository with username and password and pass-credentials", @@ -1171,11 +1167,10 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "example_user", Password: "example_password", PassCredentials: "true", - SkipTLSVerify: "", }, }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "true", ""}, + want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "true", "false"}, }, { name: "repository without username and password and environment with username and password", @@ -1188,7 +1183,6 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "", Password: "", PassCredentials: "", - SkipTLSVerify: "", }, }, envs: map[string]string{ @@ -1196,7 +1190,7 @@ func TestHelmState_SyncRepos(t *testing.T) { "NAME_PASSWORD": "example_password", }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "", ""}, + want: []string{"name", "http://example.com/", "", "", "", "example_user", "example_password", "", "", "false"}, }, { name: "repository with username and password and environment with username and password", @@ -1209,7 +1203,6 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "example_user1", Password: "example_password1", PassCredentials: "", - SkipTLSVerify: "", }, }, envs: map[string]string{ @@ -1217,7 +1210,7 @@ func TestHelmState_SyncRepos(t *testing.T) { "NAME_PASSWORD": "example_password2", }, helm: &exectest.Helm{}, - want: []string{"name", "http://example.com/", "", "", "", "example_user1", "example_password1", "", "", ""}, + want: []string{"name", "http://example.com/", "", "", "", "example_user1", "example_password1", "", "", "false"}, }, { name: "repository with skip-tls-verify", @@ -1230,7 +1223,7 @@ func TestHelmState_SyncRepos(t *testing.T) { Username: "", Password: "", PassCredentials: "", - SkipTLSVerify: "true", + SkipTLSVerify: true, }, }, helm: &exectest.Helm{}, diff --git a/pkg/testutil/mocks.go b/pkg/testutil/mocks.go index d29cf251..9c0fd74e 100644 --- a/pkg/testutil/mocks.go +++ b/pkg/testutil/mocks.go @@ -77,7 +77,7 @@ func (helm *noCallHelmExec) SetDisableForceUpdate(forceUpdate bool) { helm.doPanic() } -func (helm *noCallHelmExec) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify string) error { +func (helm *noCallHelmExec) AddRepo(name, repository, cafile, certfile, keyfile, username, password string, managed string, passCredentials string, skipTLSVerify bool) error { helm.doPanic() return nil }