Skip to content

Commit 2a86d34

Browse files
authored
chore: move proxy and network specs to runtime config (#2296)
* chore: move proxy and network specs to runtime config * f * f * f * f * f
1 parent b98ff98 commit 2a86d34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1261
-621
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ jobs:
405405
with:
406406
fetch-depth: 0
407407

408+
- name: Free up runner disk space
409+
uses: ./.github/actions/free-disk-space
410+
408411
- name: Cache embedded bins
409412
uses: actions/cache@v4
410413
with:

api/controllers/install/controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/replicatedhq/embedded-cluster/api/internal/managers/installation"
99
"github.com/replicatedhq/embedded-cluster/api/internal/managers/preflight"
1010
"github.com/replicatedhq/embedded-cluster/api/pkg/logger"
11+
"github.com/replicatedhq/embedded-cluster/api/pkg/utils"
1112
"github.com/replicatedhq/embedded-cluster/api/types"
1213
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
1314
"github.com/replicatedhq/embedded-cluster/pkg-new/hostutils"
@@ -45,6 +46,7 @@ type InstallController struct {
4546
rc runtimeconfig.RuntimeConfig
4647
logger logrus.FieldLogger
4748
hostUtils hostutils.HostUtilsInterface
49+
netUtils utils.NetUtils
4850
metricsReporter metrics.ReporterInterface
4951
releaseData *release.ReleaseData
5052
password string
@@ -76,6 +78,12 @@ func WithHostUtils(hostUtils hostutils.HostUtilsInterface) InstallControllerOpti
7678
}
7779
}
7880

81+
func WithNetUtils(netUtils utils.NetUtils) InstallControllerOption {
82+
return func(c *InstallController) {
83+
c.netUtils = netUtils
84+
}
85+
}
86+
7987
func WithMetricsReporter(metricsReporter metrics.ReporterInterface) InstallControllerOption {
8088
return func(c *InstallController) {
8189
c.metricsReporter = metricsReporter
@@ -159,6 +167,10 @@ func NewInstallController(opts ...InstallControllerOption) (*InstallController,
159167
)
160168
}
161169

170+
if controller.netUtils == nil {
171+
controller.netUtils = utils.NewNetUtils()
172+
}
173+
162174
if controller.installationManager == nil {
163175
controller.installationManager = installation.NewInstallationManager(
164176
installation.WithRuntimeConfig(controller.rc),
@@ -167,6 +179,7 @@ func NewInstallController(opts ...InstallControllerOption) (*InstallController,
167179
installation.WithLicenseFile(controller.licenseFile),
168180
installation.WithAirgapBundle(controller.airgapBundle),
169181
installation.WithHostUtils(controller.hostUtils),
182+
installation.WithNetUtils(controller.netUtils),
170183
)
171184
}
172185

@@ -176,6 +189,7 @@ func NewInstallController(opts ...InstallControllerOption) (*InstallController,
176189
preflight.WithLogger(controller.logger),
177190
preflight.WithMetricsReporter(controller.metricsReporter),
178191
preflight.WithHostPreflightStore(preflight.NewMemoryStore(controller.install.Steps.HostPreflight)),
192+
preflight.WithNetUtils(controller.netUtils),
179193
)
180194
}
181195

@@ -193,5 +207,6 @@ func NewInstallController(opts ...InstallControllerOption) (*InstallController,
193207
infra.WithEndUserConfig(controller.endUserConfig),
194208
)
195209
}
210+
196211
return controller, nil
197212
}

api/controllers/install/controller_test.go

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
1616
"github.com/replicatedhq/embedded-cluster/pkg/metrics"
1717
"github.com/replicatedhq/embedded-cluster/pkg/release"
18+
"github.com/replicatedhq/embedded-cluster/pkg/runtimeconfig"
1819
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1920
)
2021

@@ -82,10 +83,16 @@ func TestGetInstallationConfig(t *testing.T) {
8283

8384
for _, tt := range tests {
8485
t.Run(tt.name, func(t *testing.T) {
86+
rc := runtimeconfig.New(nil, runtimeconfig.WithEnvSetter(&testEnvSetter{}))
87+
rc.SetDataDir(t.TempDir())
88+
8589
mockManager := &installation.MockInstallationManager{}
8690
tt.setupMock(mockManager)
8791

88-
controller, err := NewInstallController(WithInstallationManager(mockManager))
92+
controller, err := NewInstallController(
93+
WithRuntimeConfig(rc),
94+
WithInstallationManager(mockManager),
95+
)
8996
require.NoError(t, err)
9097

9198
result, err := controller.GetInstallationConfig(t.Context())
@@ -120,7 +127,7 @@ func TestConfigureInstallation(t *testing.T) {
120127
mock.InOrder(
121128
m.On("ValidateConfig", config).Return(nil),
122129
m.On("SetConfig", *config).Return(nil),
123-
m.On("ConfigureHost", t.Context(), config).Return(nil),
130+
m.On("ConfigureHost", t.Context()).Return(nil),
124131
)
125132
},
126133
expectedErr: false,
@@ -159,7 +166,7 @@ func TestConfigureInstallation(t *testing.T) {
159166
mock.InOrder(
160167
m.On("ValidateConfig", config).Return(nil),
161168
m.On("SetConfig", configWithCIDRs).Return(nil),
162-
m.On("ConfigureHost", t.Context(), &configWithCIDRs).Return(nil),
169+
m.On("ConfigureHost", t.Context()).Return(nil),
163170
)
164171
},
165172
expectedErr: false,
@@ -168,14 +175,20 @@ func TestConfigureInstallation(t *testing.T) {
168175

169176
for _, tt := range tests {
170177
t.Run(tt.name, func(t *testing.T) {
178+
rc := runtimeconfig.New(nil, runtimeconfig.WithEnvSetter(&testEnvSetter{}))
179+
rc.SetDataDir(t.TempDir())
180+
171181
mockManager := &installation.MockInstallationManager{}
172182

173183
// Create a copy of the config to avoid modifying the original
174184
configCopy := *tt.config
175185

176186
tt.setupMock(mockManager, &configCopy)
177187

178-
controller, err := NewInstallController(WithInstallationManager(mockManager))
188+
controller, err := NewInstallController(
189+
WithRuntimeConfig(rc),
190+
WithInstallationManager(mockManager),
191+
)
179192
require.NoError(t, err)
180193

181194
err = controller.ConfigureInstallation(t.Context(), tt.config)
@@ -258,49 +271,39 @@ func TestRunHostPreflights(t *testing.T) {
258271
},
259272
}
260273

261-
expectedProxy := &ecv1beta1.ProxySpec{
262-
HTTPProxy: "http://proxy.example.com",
263-
HTTPSProxy: "https://proxy.example.com",
264-
ProvidedNoProxy: "provided-proxy.com",
265-
NoProxy: "no-proxy.com",
266-
}
267-
268274
tests := []struct {
269275
name string
270-
setupMocks func(*installation.MockInstallationManager, *preflight.MockHostPreflightManager)
276+
setupMocks func(*preflight.MockHostPreflightManager)
271277
expectedErr bool
272278
}{
273279
{
274280
name: "successful run preflights",
275-
setupMocks: func(im *installation.MockInstallationManager, pm *preflight.MockHostPreflightManager) {
281+
setupMocks: func(pm *preflight.MockHostPreflightManager) {
276282
mock.InOrder(
277-
im.On("GetConfig").Return(&types.InstallationConfig{}, nil),
278-
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(expectedHPF, expectedProxy, nil),
283+
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(expectedHPF, nil),
279284
pm.On("RunHostPreflights", t.Context(), mock.MatchedBy(func(opts preflight.RunHostPreflightOptions) bool {
280-
return expectedHPF == opts.HostPreflightSpec && expectedProxy == opts.Proxy
285+
return expectedHPF == opts.HostPreflightSpec
281286
})).Return(nil),
282287
)
283288
},
284289
expectedErr: false,
285290
},
286291
{
287292
name: "prepare preflights error",
288-
setupMocks: func(im *installation.MockInstallationManager, pm *preflight.MockHostPreflightManager) {
293+
setupMocks: func(pm *preflight.MockHostPreflightManager) {
289294
mock.InOrder(
290-
im.On("GetConfig").Return(&types.InstallationConfig{}, nil),
291-
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(nil, nil, errors.New("prepare error")),
295+
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(nil, errors.New("prepare error")),
292296
)
293297
},
294298
expectedErr: true,
295299
},
296300
{
297301
name: "run preflights error",
298-
setupMocks: func(im *installation.MockInstallationManager, pm *preflight.MockHostPreflightManager) {
302+
setupMocks: func(pm *preflight.MockHostPreflightManager) {
299303
mock.InOrder(
300-
im.On("GetConfig").Return(&types.InstallationConfig{}, nil),
301-
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(expectedHPF, expectedProxy, nil),
304+
pm.On("PrepareHostPreflights", t.Context(), mock.Anything).Return(expectedHPF, nil),
302305
pm.On("RunHostPreflights", t.Context(), mock.MatchedBy(func(opts preflight.RunHostPreflightOptions) bool {
303-
return expectedHPF == opts.HostPreflightSpec && expectedProxy == opts.Proxy
306+
return expectedHPF == opts.HostPreflightSpec
304307
})).Return(errors.New("run preflights error")),
305308
)
306309
},
@@ -310,12 +313,20 @@ func TestRunHostPreflights(t *testing.T) {
310313

311314
for _, tt := range tests {
312315
t.Run(tt.name, func(t *testing.T) {
313-
mockInstallationManager := &installation.MockInstallationManager{}
314316
mockPreflightManager := &preflight.MockHostPreflightManager{}
315-
tt.setupMocks(mockInstallationManager, mockPreflightManager)
317+
tt.setupMocks(mockPreflightManager)
318+
319+
rc := runtimeconfig.New(nil)
320+
rc.SetDataDir(t.TempDir())
321+
rc.SetProxySpec(&ecv1beta1.ProxySpec{
322+
HTTPProxy: "http://proxy.example.com",
323+
HTTPSProxy: "https://proxy.example.com",
324+
ProvidedNoProxy: "provided-proxy.com",
325+
NoProxy: "no-proxy.com",
326+
})
316327

317328
controller, err := NewInstallController(
318-
WithInstallationManager(mockInstallationManager),
329+
WithRuntimeConfig(rc),
319330
WithHostPreflightManager(mockPreflightManager),
320331
WithReleaseData(getTestReleaseData()),
321332
)
@@ -324,12 +335,11 @@ func TestRunHostPreflights(t *testing.T) {
324335
err = controller.RunHostPreflights(t.Context(), RunHostPreflightsOptions{})
325336

326337
if tt.expectedErr {
327-
assert.Error(t, err)
338+
require.Error(t, err)
328339
} else {
329-
assert.NoError(t, err)
340+
require.NoError(t, err)
330341
}
331342

332-
mockInstallationManager.AssertExpectations(t)
333343
mockPreflightManager.AssertExpectations(t)
334344
})
335345
}
@@ -857,3 +867,15 @@ func WithInfraManager(infraManager infra.InfraManager) InstallControllerOption {
857867
c.infraManager = infraManager
858868
}
859869
}
870+
871+
type testEnvSetter struct {
872+
env map[string]string
873+
}
874+
875+
func (e *testEnvSetter) Setenv(key string, val string) error {
876+
if e.env == nil {
877+
e.env = make(map[string]string)
878+
}
879+
e.env[key] = val
880+
return nil
881+
}

api/controllers/install/hostpreflight.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ import (
1111
)
1212

1313
func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostPreflightsOptions) error {
14-
// Get current installation config and add it to options
15-
config, err := c.installationManager.GetConfig()
16-
if err != nil {
17-
return fmt.Errorf("failed to read installation config: %w", err)
18-
}
19-
2014
// Get the configured custom domains
2115
ecDomains := utils.GetDomains(c.releaseData)
2216

2317
// Prepare host preflights
24-
hpf, proxy, err := c.hostPreflightManager.PrepareHostPreflights(ctx, preflight.PrepareHostPreflightOptions{
25-
InstallationConfig: config,
18+
hpf, err := c.hostPreflightManager.PrepareHostPreflights(ctx, preflight.PrepareHostPreflightOptions{
2619
ReplicatedAppURL: netutils.MaybeAddHTTPS(ecDomains.ReplicatedAppDomain),
2720
ProxyRegistryURL: netutils.MaybeAddHTTPS(ecDomains.ProxyRegistryDomain),
2821
HostPreflightSpec: c.releaseData.HostPreflights,
@@ -37,7 +30,6 @@ func (c *InstallController) RunHostPreflights(ctx context.Context, opts RunHostP
3730
// Run host preflights
3831
return c.hostPreflightManager.RunHostPreflights(ctx, preflight.RunHostPreflightOptions{
3932
HostPreflightSpec: hpf,
40-
Proxy: proxy,
4133
})
4234
}
4335

api/controllers/install/installation.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package install
33
import (
44
"context"
55
"fmt"
6-
"os"
76

87
"github.com/replicatedhq/embedded-cluster/api/types"
8+
ecv1beta1 "github.com/replicatedhq/embedded-cluster/kinds/apis/v1beta1"
9+
newconfig "github.com/replicatedhq/embedded-cluster/pkg-new/config"
910
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
1011
)
1112

@@ -43,17 +44,33 @@ func (c *InstallController) ConfigureInstallation(ctx context.Context, config *t
4344
return fmt.Errorf("write: %w", err)
4445
}
4546

47+
proxy, err := newconfig.GetProxySpec(config.HTTPProxy, config.HTTPSProxy, config.NoProxy, config.PodCIDR, config.ServiceCIDR, config.NetworkInterface, c.netUtils)
48+
if err != nil {
49+
return fmt.Errorf("get proxy spec: %w", err)
50+
}
51+
52+
networkSpec := ecv1beta1.NetworkSpec{
53+
NetworkInterface: config.NetworkInterface,
54+
GlobalCIDR: config.GlobalCIDR,
55+
PodCIDR: config.PodCIDR,
56+
ServiceCIDR: config.ServiceCIDR,
57+
NodePortRange: c.rc.NodePortRange(),
58+
}
59+
4660
// TODO (@team): discuss the distinction between the runtime config and the installation config
4761
// update the runtime config
4862
c.rc.SetDataDir(config.DataDirectory)
4963
c.rc.SetLocalArtifactMirrorPort(config.LocalArtifactMirrorPort)
5064
c.rc.SetAdminConsolePort(config.AdminConsolePort)
65+
c.rc.SetProxySpec(proxy)
66+
c.rc.SetNetworkSpec(networkSpec)
5167

5268
// update process env vars from the runtime config
53-
os.Setenv("KUBECONFIG", c.rc.PathToKubeConfig())
54-
os.Setenv("TMPDIR", c.rc.EmbeddedClusterTmpSubDir())
69+
if err := c.rc.SetEnv(); err != nil {
70+
return fmt.Errorf("set env vars: %w", err)
71+
}
5572

56-
if err := c.installationManager.ConfigureHost(ctx, config); err != nil {
73+
if err := c.installationManager.ConfigureHost(ctx); err != nil {
5774
return fmt.Errorf("configure: %w", err)
5875
}
5976

0 commit comments

Comments
 (0)