Skip to content

Commit def2784

Browse files
committed
f
1 parent ae77d01 commit def2784

File tree

5 files changed

+93
-46
lines changed

5 files changed

+93
-46
lines changed

api/integration/app/upgrade/apppreflight_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/stretchr/testify/require"
3838
"github.com/stretchr/testify/suite"
3939
helmcli "helm.sh/helm/v3/pkg/cli"
40-
"k8s.io/cli-runtime/pkg/genericclioptions"
4140
)
4241

4342
type AppPreflightTestSuite struct {
@@ -501,8 +500,7 @@ func TestAppPreflightSuite(t *testing.T) {
501500
InstallTarget: types.InstallTargetKubernetes,
502501
Password: "password",
503502
KubernetesConfig: types.KubernetesConfig{
504-
RESTClientGetter: &genericclioptions.ConfigFlags{},
505-
Installation: mockInstallation,
503+
Installation: mockInstallation,
506504
},
507505
ReleaseData: rd,
508506
Mode: types.ModeUpgrade,

api/integration/kubernetes/install/apppreflight_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
"github.com/stretchr/testify/mock"
2929
"github.com/stretchr/testify/require"
30-
"k8s.io/cli-runtime/pkg/genericclioptions"
3130
)
3231

3332
// Test the getAppPreflightsStatus endpoint returns app preflights status correctly
@@ -348,8 +347,7 @@ func TestPostRunAppPreflights(t *testing.T) {
348347
InstallTarget: types.InstallTargetKubernetes,
349348
Password: "password",
350349
KubernetesConfig: types.KubernetesConfig{
351-
RESTClientGetter: &genericclioptions.ConfigFlags{},
352-
Installation: mockInstallation,
350+
Installation: mockInstallation,
353351
},
354352
ReleaseData: integration.DefaultReleaseData(),
355353
Mode: types.ModeInstall,
@@ -403,8 +401,7 @@ func TestPostRunAppPreflights(t *testing.T) {
403401
InstallTarget: types.InstallTargetKubernetes,
404402
Password: "password",
405403
KubernetesConfig: types.KubernetesConfig{
406-
RESTClientGetter: &genericclioptions.ConfigFlags{},
407-
Installation: mockInstallation,
404+
Installation: mockInstallation,
408405
},
409406
ReleaseData: integration.DefaultReleaseData(),
410407
Mode: types.ModeInstall,
@@ -452,8 +449,7 @@ func TestPostRunAppPreflights(t *testing.T) {
452449
InstallTarget: types.InstallTargetKubernetes,
453450
Password: "password",
454451
KubernetesConfig: types.KubernetesConfig{
455-
RESTClientGetter: &genericclioptions.ConfigFlags{},
456-
Installation: mockInstallation,
452+
Installation: mockInstallation,
457453
},
458454
ReleaseData: integration.DefaultReleaseData(),
459455
Mode: types.ModeInstall,

api/types/api.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/replicatedhq/embedded-cluster/pkg/airgap"
77
"github.com/replicatedhq/embedded-cluster/pkg/release"
88
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
9-
"k8s.io/cli-runtime/pkg/genericclioptions"
109
)
1110

1211
// APIConfig holds the configuration for the API server
@@ -51,6 +50,5 @@ type LinuxConfig struct {
5150
}
5251

5352
type KubernetesConfig struct {
54-
RESTClientGetter genericclioptions.RESTClientGetter
55-
Installation kubernetesinstallation.Installation
53+
Installation kubernetesinstallation.Installation
5654
}

cmd/installer/cli/api_test.go

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"encoding/json"
78
"fmt"
89
"io"
910
"net"
1011
"net/http"
12+
"net/http/httptest"
13+
"os"
14+
"path/filepath"
1115
"strconv"
1216
"testing"
1317
"testing/fstest"
1418
"time"
1519

1620
apilogger "github.com/replicatedhq/embedded-cluster/api/pkg/logger"
1721
apitypes "github.com/replicatedhq/embedded-cluster/api/types"
22+
"github.com/replicatedhq/embedded-cluster/pkg-new/kubernetesinstallation"
1823
"github.com/replicatedhq/embedded-cluster/pkg-new/tlsutils"
1924
"github.com/replicatedhq/embedded-cluster/pkg/release"
2025
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
@@ -24,6 +29,8 @@ import (
2429
"github.com/stretchr/testify/assert"
2530
"github.com/stretchr/testify/require"
2631
"golang.org/x/crypto/bcrypt"
32+
helmcli "helm.sh/helm/v3/pkg/cli"
33+
"k8s.io/apimachinery/pkg/version"
2734
)
2835

2936
func Test_serveAPI(t *testing.T) {
@@ -124,14 +131,14 @@ func Test_serveAPI(t *testing.T) {
124131
func Test_serveAPIHTMLInjection(t *testing.T) {
125132
tests := []struct {
126133
name string
127-
installTarget string
134+
installTarget apitypes.InstallTarget
128135
mode web.Mode
129136
title string
130137
}{
131-
{"linux install mode", "linux", web.ModeInstall, "Linux Install App"},
132-
{"linux upgrade mode", "linux", web.ModeUpgrade, "Linux Upgrade App"},
133-
{"kubernetes install mode", "kubernetes", web.ModeInstall, "K8s Install App"},
134-
{"kubernetes upgrade mode", "kubernetes", web.ModeUpgrade, "K8s Upgrade App"},
138+
{"linux install mode", apitypes.InstallTargetLinux, web.ModeInstall, "Linux Install App"},
139+
{"linux upgrade mode", apitypes.InstallTargetLinux, web.ModeUpgrade, "Linux Upgrade App"},
140+
{"kubernetes install mode", apitypes.InstallTargetKubernetes, web.ModeInstall, "K8s Install App"},
141+
{"kubernetes upgrade mode", apitypes.InstallTargetKubernetes, web.ModeUpgrade, "K8s Upgrade App"},
135142
}
136143

137144
for _, tt := range tests {
@@ -175,7 +182,7 @@ func Test_serveAPIHTMLInjection(t *testing.T) {
175182

176183
config := apiOptions{
177184
APIConfig: apitypes.APIConfig{
178-
InstallTarget: apitypes.InstallTargetLinux,
185+
InstallTarget: tt.installTarget,
179186
Password: password,
180187
PasswordHash: passwordHash,
181188
ReleaseData: &release.ReleaseData{
@@ -197,6 +204,15 @@ func Test_serveAPIHTMLInjection(t *testing.T) {
197204
WebAssetsFS: webAssetsFS,
198205
}
199206

207+
if tt.installTarget == apitypes.InstallTargetKubernetes {
208+
ki := setupMockKubernetesInstallation(t)
209+
config.Installation = ki
210+
} else {
211+
// Create a runtime config with temp directory
212+
rc := setupMockRuntimeConfig(t)
213+
config.RuntimeConfig = rc
214+
}
215+
200216
go func() {
201217
err := serveAPI(ctx, listener, cert, config)
202218
t.Logf("Install API exited with error: %v", err)
@@ -236,3 +252,65 @@ func Test_serveAPIHTMLInjection(t *testing.T) {
236252
})
237253
}
238254
}
255+
256+
func setupMockRuntimeConfig(t *testing.T) *runtimeconfig.MockRuntimeConfig {
257+
// Set up mock Kubernetes API server for helm client to use
258+
mockK8sServer := setupMockKubernetesAPI(t)
259+
t.Cleanup(func() {
260+
mockK8sServer.Close()
261+
})
262+
t.Setenv("HELM_KUBEAPISERVER", mockK8sServer.URL)
263+
fmt.Println("HELM_KUBEAPISERVER", mockK8sServer.URL)
264+
265+
// Write the helm binary to the temp directory for helm client to use
266+
helmPath := filepath.Join(t.TempDir(), "helm")
267+
err := os.WriteFile(helmPath, []byte(mockK8sServer.URL), 0644)
268+
require.NoError(t, err)
269+
270+
rc := &runtimeconfig.MockRuntimeConfig{}
271+
rc.On("GetKubernetesEnvSettings").Return(helmcli.New())
272+
rc.On("PathToEmbeddedClusterBinary", "helm").Return(helmPath, nil)
273+
return rc
274+
}
275+
276+
func setupMockKubernetesInstallation(t *testing.T) *kubernetesinstallation.MockInstallation {
277+
// Set up mock Kubernetes API server for helm client to use
278+
mockK8sServer := setupMockKubernetesAPI(t)
279+
t.Cleanup(func() {
280+
mockK8sServer.Close()
281+
})
282+
t.Setenv("HELM_KUBEAPISERVER", mockK8sServer.URL)
283+
fmt.Println("HELM_KUBEAPISERVER", mockK8sServer.URL)
284+
285+
// Write the helm binary to the temp directory for helm client to use
286+
helmPath := filepath.Join(t.TempDir(), "helm")
287+
err := os.WriteFile(helmPath, []byte(mockK8sServer.URL), 0644)
288+
require.NoError(t, err)
289+
290+
ki := &kubernetesinstallation.MockInstallation{}
291+
ki.On("GetKubernetesEnvSettings").Return(helmcli.New())
292+
ki.On("PathToEmbeddedBinary", "helm").Return(helmPath, nil)
293+
return ki
294+
}
295+
296+
// setupMockKubernetesAPI creates a mock Kubernetes API server for testing
297+
func setupMockKubernetesAPI(_ *testing.T) *httptest.Server {
298+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
299+
switch r.URL.Path {
300+
case "/version":
301+
// Return a mock Kubernetes version
302+
versionInfo := version.Info{
303+
Major: "1",
304+
Minor: "28",
305+
GitVersion: "v1.28.0",
306+
}
307+
w.Header().Set("Content-Type", "application/json")
308+
json.NewEncoder(w).Encode(versionInfo)
309+
default:
310+
// Return 404 for other endpoints
311+
w.WriteHeader(http.StatusNotFound)
312+
}
313+
}))
314+
315+
return server
316+
}

cmd/installer/cli/install.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import (
5454
"github.com/spf13/pflag"
5555
"golang.org/x/crypto/bcrypt"
5656
helmcli "helm.sh/helm/v3/pkg/cli"
57-
"k8s.io/cli-runtime/pkg/genericclioptions"
5857
"k8s.io/client-go/discovery"
5958
"k8s.io/client-go/metadata"
6059
nodeutil "k8s.io/component-helpers/node/util"
@@ -102,8 +101,6 @@ type installConfig struct {
102101
tlsCert tls.Certificate
103102
tlsCertBytes []byte
104103
tlsKeyBytes []byte
105-
106-
kubernetesRESTClientGetter genericclioptions.RESTClientGetter
107104
}
108105

109106
// webAssetsFS is the filesystem to be used by the web component. Defaults to nil allowing the web server to use the default assets embedded in the binary. Useful for testing.
@@ -322,27 +319,8 @@ func newKubernetesInstallFlags(flags *InstallCmdFlags, enableV3 bool) *pflag.Fla
322319
}
323320

324321
func addKubernetesCLIFlags(flagSet *pflag.FlagSet, flags *InstallCmdFlags) {
325-
// From helm
326-
// https://github.com/helm/helm/blob/v3.18.3/pkg/cli/environment.go#L145-L163
327-
328322
s := helmcli.New()
329-
330-
flagSet.StringVar(&s.KubeConfig, "kubeconfig", "", "Path to the kubeconfig file")
331-
flagSet.StringVar(&s.KubeContext, "kube-context", s.KubeContext, "Name of the kubeconfig context to use")
332-
flagSet.StringVar(&s.KubeToken, "kube-token", s.KubeToken, "Bearer token used for authentication")
333-
flagSet.StringVar(&s.KubeAsUser, "kube-as-user", s.KubeAsUser, "Username to impersonate for the operation")
334-
flagSet.StringArrayVar(&s.KubeAsGroups, "kube-as-group", s.KubeAsGroups, "Group to impersonate for the operation, this flag can be repeated to specify multiple groups.")
335-
flagSet.StringVar(&s.KubeAPIServer, "kube-apiserver", s.KubeAPIServer, "The address and the port for the Kubernetes API server")
336-
flagSet.StringVar(&s.KubeCaFile, "kube-ca-file", s.KubeCaFile, "The certificate authority file for the Kubernetes API server connection")
337-
flagSet.StringVar(&s.KubeTLSServerName, "kube-tls-server-name", s.KubeTLSServerName, "Server name to use for Kubernetes API server certificate validation. If it is not provided, the hostname used to contact the server is used")
338-
// flagSet.BoolVar(&s.Debug, "helm-debug", s.Debug, "enable verbose output")
339-
flagSet.BoolVar(&s.KubeInsecureSkipTLSVerify, "kube-insecure-skip-tls-verify", s.KubeInsecureSkipTLSVerify, "If true, the Kubernetes API server's certificate will not be checked for validity. This will make your HTTPS connections insecure")
340-
// flagSet.StringVar(&s.RegistryConfig, "helm-registry-config", s.RegistryConfig, "Path to the Helm registry config file")
341-
// flagSet.StringVar(&s.RepositoryConfig, "helm-repository-config", s.RepositoryConfig, "Path to the file containing Helm repository names and URLs")
342-
// flagSet.StringVar(&s.RepositoryCache, "helm-repository-cache", s.RepositoryCache, "Path to the directory containing cached Helm repository indexes")
343-
flagSet.IntVar(&s.BurstLimit, "burst-limit", s.BurstLimit, "Kubernetes API client-side default throttling limit")
344-
flagSet.Float32Var(&s.QPS, "qps", s.QPS, "Queries per second used when communicating with the Kubernetes API, not including bursting")
345-
323+
helm.AddKubernetesCLIFlags(flagSet, s)
346324
flags.kubernetesEnvSettings = s
347325
}
348326

@@ -580,7 +558,7 @@ func preRunInstallLinux(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimeco
580558
return nil
581559
}
582560

583-
func preRunInstallKubernetes(_ *cobra.Command, flags *InstallCmdFlags, _ kubernetesinstallation.Installation) error {
561+
func preRunInstallKubernetes(_ *cobra.Command, flags *InstallCmdFlags, ki kubernetesinstallation.Installation) error {
584562
// TODO: we only support amd64 clusters for target=kubernetes installs
585563
helpers.SetClusterArch("amd64")
586564

@@ -608,7 +586,7 @@ func preRunInstallKubernetes(_ *cobra.Command, flags *InstallCmdFlags, _ kuberne
608586
return fmt.Errorf("failed to connect to kubernetes api server: %w", err)
609587
}
610588

611-
flags.kubernetesRESTClientGetter = flags.kubernetesEnvSettings.RESTClientGetter()
589+
ki.SetKubernetesEnvSettings(flags.kubernetesEnvSettings)
612590

613591
return nil
614592
}
@@ -722,8 +700,7 @@ func runManagerExperienceInstall(
722700
AllowIgnoreHostPreflights: flags.ignoreHostPreflights,
723701
},
724702
KubernetesConfig: apitypes.KubernetesConfig{
725-
RESTClientGetter: flags.kubernetesRESTClientGetter,
726-
Installation: ki,
703+
Installation: ki,
727704
},
728705
},
729706

0 commit comments

Comments
 (0)