From 5df09850c6996c0fb6df7c951ea163322d9472c4 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Fri, 11 Oct 2024 12:37:35 +0200 Subject: [PATCH 01/13] Improve UX around setting Proxy configurations in the spec KU-1801 --- bootstrap/api/v1beta2/ck8sconfig_types.go | 12 +++ ...ootstrap.cluster.x-k8s.io_ck8sconfigs.yaml | 9 +++ ....cluster.x-k8s.io_ck8sconfigtemplates.yaml | 9 +++ .../controllers/ck8sconfig_controller.go | 6 ++ ...ne.cluster.x-k8s.io_ck8scontrolplanes.yaml | 9 +++ ...er.x-k8s.io_ck8scontrolplanetemplates.yaml | 9 +++ pkg/cloudinit/common.go | 40 ++++++++++ pkg/cloudinit/controlplane_init_test.go | 76 +++++++++++++++++++ pkg/cloudinit/controlplane_join_test.go | 71 +++++++++++++++++ pkg/cloudinit/embed.go | 20 ++--- .../scripts/configure-containerd-proxy.sh | 43 +++++++++++ pkg/cloudinit/worker_join_test.go | 71 +++++++++++++++++ 12 files changed, 366 insertions(+), 9 deletions(-) create mode 100644 pkg/cloudinit/scripts/configure-containerd-proxy.sh diff --git a/bootstrap/api/v1beta2/ck8sconfig_types.go b/bootstrap/api/v1beta2/ck8sconfig_types.go index 3d936b7b..31acb1f8 100644 --- a/bootstrap/api/v1beta2/ck8sconfig_types.go +++ b/bootstrap/api/v1beta2/ck8sconfig_types.go @@ -69,6 +69,18 @@ type CK8sConfigSpec struct { // +optional SnapstoreProxyID string `json:"snapstoreProxyId,omitempty"` + // HTTPSProxy is optional https proxy configuration + // +optional + HTTPSProxy string `json:"httpsProxy,omitempty"` + + // HTTPProxy is optional http proxy configuration + // +optional + HTTPProxy string `json:"httpProxy,omitempty"` + + // NoProxy is optional no proxy configuration + // +optional + NoProxy string `json:"noProxy,omitempty"` + // Channel is the channel to use for the snap install. // +optional Channel string `json:"channel,omitempty"` diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml index b237b1aa..08e53c5a 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml @@ -201,6 +201,12 @@ spec: - path type: object type: array + httpProxy: + description: HTTPProxy is optional http proxy configuration + type: string + httpsProxy: + description: HTTPSProxy is optional https proxy configuration + type: string initConfig: description: CK8sInitConfig is configuration for the initializing the cluster features. @@ -233,6 +239,9 @@ spec: LocalPath is the path of a local snap file in the workload cluster to use for the snap install. If Channel or Revision are set, this will be ignored. type: string + noProxy: + description: NoProxy is optional no proxy configuration + type: string nodeName: description: |- NodeName is the name to use for the kubelet of this node. It is needed for clouds diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml index c868cfd5..899eca47 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml @@ -210,6 +210,12 @@ spec: - path type: object type: array + httpProxy: + description: HTTPProxy is optional http proxy configuration + type: string + httpsProxy: + description: HTTPSProxy is optional https proxy configuration + type: string initConfig: description: CK8sInitConfig is configuration for the initializing the cluster features. @@ -242,6 +248,9 @@ spec: LocalPath is the path of a local snap file in the workload cluster to use for the snap install. If Channel or Revision are set, this will be ignored. type: string + noProxy: + description: NoProxy is optional no proxy configuration + type: string nodeName: description: |- NodeName is the name to use for the kubelet of this node. It is needed for clouds diff --git a/bootstrap/controllers/ck8sconfig_controller.go b/bootstrap/controllers/ck8sconfig_controller.go index 82d8002e..c2dbf7e9 100644 --- a/bootstrap/controllers/ck8sconfig_controller.go +++ b/bootstrap/controllers/ck8sconfig_controller.go @@ -378,6 +378,9 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err ConfigFileContents: string(joinConfig), MicroclusterAddress: scope.Config.Spec.ControlPlaneConfig.MicroclusterAddress, MicroclusterPort: microclusterPort, + ContainerdHTTPProxy: scope.Config.Spec.HTTPProxy, + ContainerdHTTPSProxy: scope.Config.Spec.HTTPSProxy, + ContainerdNoProxy: scope.Config.Spec.NoProxy, AirGapped: scope.Config.Spec.AirGapped, SnapstoreProxyScheme: scope.Config.Spec.SnapstoreProxyScheme, SnapstoreProxyDomain: scope.Config.Spec.SnapstoreProxyDomain, @@ -693,6 +696,9 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, MicroclusterAddress: scope.Config.Spec.ControlPlaneConfig.MicroclusterAddress, MicroclusterPort: microclusterPort, NodeName: scope.Config.Spec.NodeName, + ContainerdHTTPProxy: scope.Config.Spec.HTTPProxy, + ContainerdHTTPSProxy: scope.Config.Spec.HTTPSProxy, + ContainerdNoProxy: scope.Config.Spec.NoProxy, AirGapped: scope.Config.Spec.AirGapped, SnapstoreProxyScheme: scope.Config.Spec.SnapstoreProxyScheme, SnapstoreProxyDomain: scope.Config.Spec.SnapstoreProxyDomain, diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml index 7dc56812..255c8dbc 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml @@ -398,6 +398,12 @@ spec: - path type: object type: array + httpProxy: + description: HTTPProxy is optional http proxy configuration + type: string + httpsProxy: + description: HTTPSProxy is optional https proxy configuration + type: string initConfig: description: CK8sInitConfig is configuration for the initializing the cluster features. @@ -425,6 +431,9 @@ spec: the default CNI. type: boolean type: object + noProxy: + description: NoProxy is optional no proxy configuration + type: string localPath: description: |- LocalPath is the path of a local snap file in the workload cluster to use for the snap install. diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml index ff6e0fcd..4e3e9e28 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml @@ -376,6 +376,12 @@ spec: - path type: object type: array + httpProxy: + description: HTTPProxy is optional http proxy configuration + type: string + httpsProxy: + description: HTTPSProxy is optional https proxy configuration + type: string initConfig: description: CK8sInitConfig is configuration for the initializing the cluster features. @@ -408,6 +414,9 @@ spec: LocalPath is the path of a local snap file in the workload cluster to use for the snap install. If Channel or Revision are set, this will be ignored. type: string + noProxy: + description: NoProxy is optional no proxy configuration + type: string nodeName: description: |- NodeName is the name to use for the kubelet of this node. It is needed for clouds diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index 7f820350..fa03ea22 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -49,6 +49,12 @@ type BaseUserData struct { SnapstoreProxyDomain string // The snap store proxy ID SnapstoreProxyID string + // ContainerdHTTPProxy is http_proxy configuration for containerd. + ContainerdHTTPProxy string + // ContainerdHTTPSProxy is https_proxy configuration for containerd. + ContainerdHTTPSProxy string + // ContainerdNoProxy is no_proxy configuration for containerd. + ContainerdNoProxy string // MicroclusterAddress is the address to use for microcluster. MicroclusterAddress string // MicroclusterPort is the port to use for microcluster. @@ -95,6 +101,12 @@ func NewBaseCloudConfig(data BaseUserData) (CloudConfig, error) { config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-snapstore-proxy.sh") } + // containerd proxy configuration + if containerdProxyConfigFiles := getContainerdProxyConfigFiles(data); containerdProxyConfigFiles != nil { + config.WriteFiles = append(config.WriteFiles, containerdProxyConfigFiles...) + config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-containerd-proxy.sh") + } + var configFileContents string if data.BootstrapConfig != "" { configFileContents = data.BootstrapConfig @@ -140,6 +152,8 @@ func NewBaseCloudConfig(data BaseUserData) (CloudConfig, error) { )..., ) + + // boot commands config.BootCommands = data.BootCommands @@ -190,3 +204,29 @@ func getSnapstoreProxyConfigFiles(data BaseUserData) []File { return []File{schemeFile, domainFile, storeIDFile} } + +func getContainerdProxyConfigFiles(data BaseUserData) []File { + if data.ContainerdHTTPSProxy == "" || data.ContainerdHTTPProxy == "" { + return nil + } + return []File{ + { + Path: "/capi/etc/containerd-http-proxy", + Content: data.ContainerdHTTPProxy, + Permissions: "0400", + Owner: "root:root", + }, + { + Path: "/capi/etc/containerd-https-proxy", + Content: data.ContainerdHTTPSProxy, + Permissions: "0400", + Owner: "root:root", + }, + { + Path: "/capi/etc/containerd-no-proxy", + Content: data.ContainerdNoProxy, + Permissions: "0400", + Owner: "root:root", + }, + } +} diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index d113658c..780f2dcf 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -88,6 +88,7 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), @@ -105,6 +106,81 @@ func TestNewInitControlPlane(t *testing.T) { ), "Some /capi/scripts files are missing") } +func TestNewInitControlPlaneWithProxy(t *testing.T) { + g := NewWithT(t) + format.MaxLength = 20000 + + config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + ExtraFiles: []cloudinit.File{{ + Path: "/tmp/file", + Content: "test file", + Permissions: "0400", + Owner: "root:root", + }}, + ContainerdHTTPProxy: "http://proxy.internal", + ContainerdHTTPSProxy: "https://proxy.internal", + ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + ConfigFileContents: "### config file ###", + MicroclusterAddress: "10.0.0.0/8", + }, + AuthToken: "test-token", + K8sdProxyDaemonSet: "test-daemonset", + }) + + g.Expect(err).ToNot(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. + g.Expect(config.RunCommands).To(Equal([]string{ + "set -x", + "/capi/scripts/configure-containerd-proxy.sh", + "prerun1", + "prerun2", + "/capi/scripts/install.sh", + "/capi/scripts/bootstrap.sh", + "/capi/scripts/load-images.sh", + "/capi/scripts/wait-apiserver-ready.sh", + "/capi/scripts/deploy-manifests.sh", + "/capi/scripts/configure-auth-token.sh", + "/capi/scripts/configure-node-token.sh", + "/capi/scripts/create-sentinel-bootstrap.sh", + "postrun1", + "postrun2", + })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_init.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-node-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/containerd-http-proxy"), + HaveField("Path", "/capi/etc/containerd-https-proxy"), + HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/node-name"), + HaveField("Path", "/capi/etc/node-token"), + HaveField("Path", "/capi/etc/token"), + HaveField("Path", "/capi/etc/snap-channel"), + HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + func TestUserSuppliedBootstrapConfig(t *testing.T) { g := NewWithT(t) diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index a9a009ae..67854395 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -66,6 +66,7 @@ func TestNewJoinControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), @@ -82,6 +83,76 @@ func TestNewJoinControlPlane(t *testing.T) { ), "Some /capi/scripts files are missing") } +func TestNewJoinControlPlaneWithProxy(t *testing.T) { + g := NewWithT(t) + + config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + ExtraFiles: []cloudinit.File{{ + Path: "/tmp/file", + Content: "test file", + Permissions: "0400", + Owner: "root:root", + }}, + ContainerdHTTPProxy: "http://proxy.internal", + ContainerdHTTPSProxy: "https://proxy.internal", + ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + ConfigFileContents: "### config file ###", + MicroclusterAddress: "10.0.0.11", + }, + JoinToken: "test-token", + }) + + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. + g.Expect(config.RunCommands).To(Equal([]string{ + "set -x", + "/capi/scripts/configure-containerd-proxy.sh", + "prerun1", + "prerun2", + "/capi/scripts/install.sh", + "/capi/scripts/load-images.sh", + "/capi/scripts/join-cluster.sh", + "/capi/scripts/wait-apiserver-ready.sh", + "/capi/scripts/configure-node-token.sh", + "/capi/scripts/create-sentinel-bootstrap.sh", + "postrun1", + "postrun2", + })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_join.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-node-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/containerd-http-proxy"), + HaveField("Path", "/capi/etc/containerd-https-proxy"), + HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/node-name"), + HaveField("Path", "/capi/etc/node-token"), + HaveField("Path", "/capi/etc/join-token"), + HaveField("Path", "/capi/etc/snap-channel"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + func TestNewJoinControlPlaneInvalidVersionError(t *testing.T) { g := NewWithT(t) diff --git a/pkg/cloudinit/embed.go b/pkg/cloudinit/embed.go index 1d00a962..87a15504 100644 --- a/pkg/cloudinit/embed.go +++ b/pkg/cloudinit/embed.go @@ -20,6 +20,7 @@ var ( scriptBootstrap script = "bootstrap.sh" scriptLoadImages script = "load-images.sh" scriptConfigureAuthToken script = "configure-auth-token.sh" // #nosec G101 + scriptConfigureContainerdProxy script = "configure-containerd-proxy.sh" scriptConfigureNodeToken script = "configure-node-token.sh" // #nosec G101 scriptJoinCluster script = "join-cluster.sh" scriptWaitAPIServerReady script = "wait-apiserver-ready.sh" @@ -39,16 +40,17 @@ func mustEmbed(s script) string { var ( // scripts is a map of all embedded bash scripts used in the cloud-init. scripts = map[script]string{ - scriptInstall: mustEmbed(scriptInstall), + scriptInstall: mustEmbed(scriptInstall), scriptDisableHostServices: mustEmbed(scriptDisableHostServices), - scriptBootstrap: mustEmbed(scriptBootstrap), - scriptLoadImages: mustEmbed(scriptLoadImages), - scriptConfigureAuthToken: mustEmbed(scriptConfigureAuthToken), - scriptConfigureNodeToken: mustEmbed(scriptConfigureNodeToken), - scriptJoinCluster: mustEmbed(scriptJoinCluster), - scriptWaitAPIServerReady: mustEmbed(scriptWaitAPIServerReady), - scriptDeployManifests: mustEmbed(scriptDeployManifests), - scriptCreateSentinelBootstrap: mustEmbed(scriptCreateSentinelBootstrap), + scriptBootstrap: mustEmbed(scriptBootstrap), + scriptLoadImages: mustEmbed(scriptLoadImages), + scriptConfigureAuthToken: mustEmbed(scriptConfigureAuthToken), + scriptConfigureContainerdProxy: mustEmbed(scriptConfigureContainerdProxy), + scriptConfigureNodeToken: mustEmbed(scriptConfigureNodeToken), + scriptJoinCluster: mustEmbed(scriptJoinCluster), + scriptWaitAPIServerReady: mustEmbed(scriptWaitAPIServerReady), + scriptDeployManifests: mustEmbed(scriptDeployManifests), + scriptCreateSentinelBootstrap: mustEmbed(scriptCreateSentinelBootstrap), scriptConfigureSnapstoreProxy: mustEmbed(scriptConfigureSnapstoreProxy), } ) diff --git a/pkg/cloudinit/scripts/configure-containerd-proxy.sh b/pkg/cloudinit/scripts/configure-containerd-proxy.sh new file mode 100644 index 00000000..b8905980 --- /dev/null +++ b/pkg/cloudinit/scripts/configure-containerd-proxy.sh @@ -0,0 +1,43 @@ +#!/bin/bash -xe + +# Assumptions: +# - k8s is installed + +# - /capi/etc/containerd-http-proxy contains containerd http proxy value +# - /capi/etc/containerd-https-proxy contains containerd https proxy value +# - /capi/etc/containerd-no-proxy contains containerd no proxy value + + +HTTP_PROXY=$(cat /capi/etc/containerd-http-proxy) +HTTPS_PROXY=$(cat /capi/etc/containerd-https-proxy) +NO_PROXY=$(cat /capi/etc/containerd-no-proxy) + +mkdir -p /etc/systemd/system/snap.k8s.containerd.service.d +CONTAINERD_HTTP_PROXY="/etc/systemd/system/snap.k8s.containerd.service.d/http-proxy.conf" + +echo "[Service]" >> "${CONTAINERD_HTTP_PROXY}" +need_restart=false + + + +if [[ "${HTTP_PROXY}" != "" ]]; then + echo "Environment=\"http_proxy=${HTTP_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + echo "Environment=\"HTTP_PROXY=${HTTP_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + need_restart=true +fi + +if [[ "${HTTPS_PROXY}" != "" ]]; then + echo "Environment=\"https_proxy=${HTTPS_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + echo "Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + need_restart=true +fi + +if [[ "${NO_PROXY}" != "" ]]; then + echo "Environment=\"no_proxy=${NO_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + echo "Environment=\"NO_PROXY=${NO_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" + need_restart=true +fi + +if [[ "$need_restart" = "true" ]]; then + snap restart k8s.containerd +fi diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index addadc38..3e3b3718 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -66,6 +66,7 @@ func TestNewJoinWorker(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), @@ -82,6 +83,76 @@ func TestNewJoinWorker(t *testing.T) { ), "Some /capi/scripts files are missing") } +func TestNewJoinWorkerWithProxy(t *testing.T) { + g := NewWithT(t) + + config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ + BaseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + ExtraFiles: []cloudinit.File{{ + Path: "/tmp/file", + Content: "test file", + Permissions: "0400", + Owner: "root:root", + }}, + ContainerdHTTPProxy: "http://proxy.internal", + ContainerdHTTPSProxy: "https://proxy.internal", + ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + ConfigFileContents: "### config file ###", + MicroclusterAddress: "10.0.0.10", + MicroclusterPort: 8080, + }, + JoinToken: "test-token", + }) + + g.Expect(err).NotTo(HaveOccurred()) + + // Verify the boot commands. + g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) + + // Verify the run commands. + g.Expect(config.RunCommands).To(Equal([]string{ + "set -x", + "/capi/scripts/configure-containerd-proxy.sh", + "prerun1", + "prerun2", + "/capi/scripts/install.sh", + "/capi/scripts/load-images.sh", + "/capi/scripts/join-cluster.sh", + "/capi/scripts/configure-node-token.sh", + "/capi/scripts/create-sentinel-bootstrap.sh", + "postrun1", + "postrun2", + })) + + // NOTE (mateoflorido): Keep this test in sync with the expected paths in the worker_join.go file. + g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/bootstrap.sh"), + HaveField("Path", "/capi/scripts/load-images.sh"), + HaveField("Path", "/capi/scripts/join-cluster.sh"), + HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), + HaveField("Path", "/capi/scripts/deploy-manifests.sh"), + HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-node-token.sh"), + HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/etc/config.yaml"), + HaveField("Path", "/capi/etc/containerd-http-proxy"), + HaveField("Path", "/capi/etc/containerd-https-proxy"), + HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/node-name"), + HaveField("Path", "/capi/etc/node-token"), + HaveField("Path", "/capi/etc/join-token"), + HaveField("Path", "/capi/etc/snap-channel"), + HaveField("Path", "/tmp/file"), + ), "Some /capi/scripts files are missing") +} + func TestNewJoinWorkerInvalidVersionError(t *testing.T) { g := NewWithT(t) From e61a2c5c7e46958d26a1b39c1ee5d6039e3bf61e Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Sat, 12 Oct 2024 15:44:58 +0200 Subject: [PATCH 02/13] test fix --- pkg/cloudinit/common.go | 3 --- pkg/cloudinit/controlplane_init_test.go | 27 +++++++++++++------------ pkg/cloudinit/controlplane_join_test.go | 10 ++++++++- pkg/cloudinit/embed.go | 22 ++++++++++---------- pkg/cloudinit/worker_join_test.go | 10 ++++++++- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index fa03ea22..6799b01a 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -151,9 +151,6 @@ func NewBaseCloudConfig(data BaseUserData) (CloudConfig, error) { }, )..., ) - - - // boot commands config.BootCommands = data.BootCommands diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 780f2dcf..cac80bc7 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -34,13 +34,10 @@ func TestNewInitControlPlane(t *testing.T) { config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - BootCommands: []string{"bootcmd"}, - PreRunCommands: []string{"prerun1", "prerun2"}, - PostRunCommands: []string{"postrun1", "postrun2"}, - SnapstoreProxyScheme: "http", - SnapstoreProxyDomain: "snapstore.io", - SnapstoreProxyID: "abcd-1234-xyz", + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, ExtraFiles: []cloudinit.File{{ Path: "/tmp/file", Content: "test file", @@ -62,7 +59,6 @@ func TestNewInitControlPlane(t *testing.T) { // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", - "/capi/scripts/configure-snapstore-proxy.sh", "prerun1", "prerun2", "/capi/scripts/install.sh", @@ -89,9 +85,9 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), - HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/etc/config.yaml"), HaveField("Path", "/capi/etc/microcluster-address"), HaveField("Path", "/capi/etc/node-name"), @@ -99,14 +95,11 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/etc/token"), HaveField("Path", "/capi/etc/snap-channel"), HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), - HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), - HaveField("Path", "/capi/etc/snapstore-proxy-domain"), - HaveField("Path", "/capi/etc/snapstore-proxy-id"), HaveField("Path", "/tmp/file"), ), "Some /capi/scripts files are missing") } -func TestNewInitControlPlaneWithProxy(t *testing.T) { +func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { g := NewWithT(t) format.MaxLength = 20000 @@ -122,6 +115,9 @@ func TestNewInitControlPlaneWithProxy(t *testing.T) { Permissions: "0400", Owner: "root:root", }}, + SnapstoreProxyScheme: "http", + SnapstoreProxyDomain: "snapstore.io", + SnapstoreProxyID: "abcd-1234-xyz", ContainerdHTTPProxy: "http://proxy.internal", ContainerdHTTPSProxy: "https://proxy.internal", ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", @@ -140,6 +136,7 @@ func TestNewInitControlPlaneWithProxy(t *testing.T) { // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", + "/capi/scripts/configure-snapstore-proxy.sh", "/capi/scripts/configure-containerd-proxy.sh", "prerun1", "prerun2", @@ -165,6 +162,7 @@ func TestNewInitControlPlaneWithProxy(t *testing.T) { HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), @@ -177,6 +175,9 @@ func TestNewInitControlPlaneWithProxy(t *testing.T) { HaveField("Path", "/capi/etc/token"), HaveField("Path", "/capi/etc/snap-channel"), HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), + HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), + HaveField("Path", "/capi/etc/snapstore-proxy-domain"), + HaveField("Path", "/capi/etc/snapstore-proxy-id"), HaveField("Path", "/tmp/file"), ), "Some /capi/scripts files are missing") } diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index 67854395..0b8f79ed 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -83,7 +83,7 @@ func TestNewJoinControlPlane(t *testing.T) { ), "Some /capi/scripts files are missing") } -func TestNewJoinControlPlaneWithProxy(t *testing.T) { +func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { g := NewWithT(t) config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ @@ -98,6 +98,9 @@ func TestNewJoinControlPlaneWithProxy(t *testing.T) { Permissions: "0400", Owner: "root:root", }}, + SnapstoreProxyScheme: "http", + SnapstoreProxyDomain: "snapstore.io", + SnapstoreProxyID: "abcd-1234-xyz", ContainerdHTTPProxy: "http://proxy.internal", ContainerdHTTPSProxy: "https://proxy.internal", ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", @@ -115,6 +118,7 @@ func TestNewJoinControlPlaneWithProxy(t *testing.T) { // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", + "/capi/scripts/configure-snapstore-proxy.sh", "/capi/scripts/configure-containerd-proxy.sh", "prerun1", "prerun2", @@ -138,6 +142,7 @@ func TestNewJoinControlPlaneWithProxy(t *testing.T) { HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), @@ -149,6 +154,9 @@ func TestNewJoinControlPlaneWithProxy(t *testing.T) { HaveField("Path", "/capi/etc/node-token"), HaveField("Path", "/capi/etc/join-token"), HaveField("Path", "/capi/etc/snap-channel"), + HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), + HaveField("Path", "/capi/etc/snapstore-proxy-domain"), + HaveField("Path", "/capi/etc/snapstore-proxy-id"), HaveField("Path", "/tmp/file"), ), "Some /capi/scripts files are missing") } diff --git a/pkg/cloudinit/embed.go b/pkg/cloudinit/embed.go index 87a15504..6ae9f827 100644 --- a/pkg/cloudinit/embed.go +++ b/pkg/cloudinit/embed.go @@ -15,18 +15,18 @@ type script string // NOTE(eac): If you want to use a script from pkg/cloudinit/scripts in your code (for example, you want to include a script in the user-data.txt), // you need to add it to the scripts map below. var ( - scriptInstall script = "install.sh" + scriptInstall script = "install.sh" scriptDisableHostServices script = "disable-host-services.sh" - scriptBootstrap script = "bootstrap.sh" - scriptLoadImages script = "load-images.sh" - scriptConfigureAuthToken script = "configure-auth-token.sh" // #nosec G101 + scriptBootstrap script = "bootstrap.sh" + scriptLoadImages script = "load-images.sh" + scriptConfigureAuthToken script = "configure-auth-token.sh" // #nosec G101 scriptConfigureContainerdProxy script = "configure-containerd-proxy.sh" - scriptConfigureNodeToken script = "configure-node-token.sh" // #nosec G101 - scriptJoinCluster script = "join-cluster.sh" - scriptWaitAPIServerReady script = "wait-apiserver-ready.sh" - scriptDeployManifests script = "deploy-manifests.sh" - scriptCreateSentinelBootstrap script = "create-sentinel-bootstrap.sh" - scriptConfigureSnapstoreProxy script = "configure-snapstore-proxy.sh" + scriptConfigureNodeToken script = "configure-node-token.sh" // #nosec G101 + scriptJoinCluster script = "join-cluster.sh" + scriptWaitAPIServerReady script = "wait-apiserver-ready.sh" + scriptDeployManifests script = "deploy-manifests.sh" + scriptCreateSentinelBootstrap script = "create-sentinel-bootstrap.sh" + scriptConfigureSnapstoreProxy script = "configure-snapstore-proxy.sh" ) func mustEmbed(s script) string { @@ -51,6 +51,6 @@ var ( scriptWaitAPIServerReady: mustEmbed(scriptWaitAPIServerReady), scriptDeployManifests: mustEmbed(scriptDeployManifests), scriptCreateSentinelBootstrap: mustEmbed(scriptCreateSentinelBootstrap), - scriptConfigureSnapstoreProxy: mustEmbed(scriptConfigureSnapstoreProxy), + scriptConfigureSnapstoreProxy: mustEmbed(scriptConfigureSnapstoreProxy), } ) diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index 3e3b3718..c52ff982 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -83,7 +83,7 @@ func TestNewJoinWorker(t *testing.T) { ), "Some /capi/scripts files are missing") } -func TestNewJoinWorkerWithProxy(t *testing.T) { +func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { g := NewWithT(t) config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ @@ -98,6 +98,9 @@ func TestNewJoinWorkerWithProxy(t *testing.T) { Permissions: "0400", Owner: "root:root", }}, + SnapstoreProxyScheme: "http", + SnapstoreProxyDomain: "snapstore.io", + SnapstoreProxyID: "abcd-1234-xyz", ContainerdHTTPProxy: "http://proxy.internal", ContainerdHTTPSProxy: "https://proxy.internal", ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", @@ -116,6 +119,7 @@ func TestNewJoinWorkerWithProxy(t *testing.T) { // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", + "/capi/scripts/configure-snapstore-proxy.sh", "/capi/scripts/configure-containerd-proxy.sh", "prerun1", "prerun2", @@ -138,6 +142,7 @@ func TestNewJoinWorkerWithProxy(t *testing.T) { HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), @@ -145,6 +150,9 @@ func TestNewJoinWorkerWithProxy(t *testing.T) { HaveField("Path", "/capi/etc/containerd-https-proxy"), HaveField("Path", "/capi/etc/containerd-no-proxy"), HaveField("Path", "/capi/etc/microcluster-address"), + HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), + HaveField("Path", "/capi/etc/snapstore-proxy-domain"), + HaveField("Path", "/capi/etc/snapstore-proxy-id"), HaveField("Path", "/capi/etc/node-name"), HaveField("Path", "/capi/etc/node-token"), HaveField("Path", "/capi/etc/join-token"), From a7db9a63f95992cde8c32fbbddc0201461853aee Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Wed, 16 Oct 2024 19:29:11 +0200 Subject: [PATCH 03/13] move proxy to /etc/environment --- ...ootstrap.cluster.x-k8s.io_ck8sconfigs.yaml | 6 +-- ....cluster.x-k8s.io_ck8sconfigtemplates.yaml | 6 +-- .../controllers/ck8sconfig_controller.go | 12 ++--- ...ne.cluster.x-k8s.io_ck8scontrolplanes.yaml | 12 ++--- ...er.x-k8s.io_ck8scontrolplanetemplates.yaml | 8 ++-- pkg/cloudinit/common.go | 37 +++++++++------- pkg/cloudinit/controlplane_init_test.go | 18 ++++---- pkg/cloudinit/controlplane_join_test.go | 18 ++++---- pkg/cloudinit/embed.go | 44 +++++++++---------- .../scripts/configure-containerd-proxy.sh | 43 ------------------ pkg/cloudinit/scripts/configure-proxy.sh | 30 +++++++++++++ pkg/cloudinit/worker_join_test.go | 18 ++++---- 12 files changed, 121 insertions(+), 131 deletions(-) delete mode 100644 pkg/cloudinit/scripts/configure-containerd-proxy.sh create mode 100644 pkg/cloudinit/scripts/configure-proxy.sh diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml index 08e53c5a..d0b175e2 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigs.yaml @@ -51,9 +51,6 @@ spec: items: type: string type: array - channel: - description: Channel is the channel to use for the snap install. - type: string bootstrapConfig: description: BootstrapConfig is the data to be passed to the bootstrap script. @@ -87,6 +84,9 @@ spec: - secret type: object type: object + channel: + description: Channel is the channel to use for the snap install. + type: string controlPlane: description: CK8sControlPlaneConfig is configuration for the control plane node. diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml index 899eca47..a9fd896b 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_ck8sconfigtemplates.yaml @@ -58,9 +58,6 @@ spec: items: type: string type: array - channel: - description: Channel is the channel to use for the snap install. - type: string bootstrapConfig: description: BootstrapConfig is the data to be passed to the bootstrap script. @@ -94,6 +91,9 @@ spec: - secret type: object type: object + channel: + description: Channel is the channel to use for the snap install. + type: string controlPlane: description: CK8sControlPlaneConfig is configuration for the control plane node. diff --git a/bootstrap/controllers/ck8sconfig_controller.go b/bootstrap/controllers/ck8sconfig_controller.go index c2dbf7e9..60489ef8 100644 --- a/bootstrap/controllers/ck8sconfig_controller.go +++ b/bootstrap/controllers/ck8sconfig_controller.go @@ -378,9 +378,9 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err ConfigFileContents: string(joinConfig), MicroclusterAddress: scope.Config.Spec.ControlPlaneConfig.MicroclusterAddress, MicroclusterPort: microclusterPort, - ContainerdHTTPProxy: scope.Config.Spec.HTTPProxy, - ContainerdHTTPSProxy: scope.Config.Spec.HTTPSProxy, - ContainerdNoProxy: scope.Config.Spec.NoProxy, + HTTPProxy: scope.Config.Spec.HTTPProxy, + HTTPSProxy: scope.Config.Spec.HTTPSProxy, + NoProxy: scope.Config.Spec.NoProxy, AirGapped: scope.Config.Spec.AirGapped, SnapstoreProxyScheme: scope.Config.Spec.SnapstoreProxyScheme, SnapstoreProxyDomain: scope.Config.Spec.SnapstoreProxyDomain, @@ -696,9 +696,9 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, MicroclusterAddress: scope.Config.Spec.ControlPlaneConfig.MicroclusterAddress, MicroclusterPort: microclusterPort, NodeName: scope.Config.Spec.NodeName, - ContainerdHTTPProxy: scope.Config.Spec.HTTPProxy, - ContainerdHTTPSProxy: scope.Config.Spec.HTTPSProxy, - ContainerdNoProxy: scope.Config.Spec.NoProxy, + HTTPProxy: scope.Config.Spec.HTTPProxy, + HTTPSProxy: scope.Config.Spec.HTTPSProxy, + NoProxy: scope.Config.Spec.NoProxy, AirGapped: scope.Config.Spec.AirGapped, SnapstoreProxyScheme: scope.Config.Spec.SnapstoreProxyScheme, SnapstoreProxyDomain: scope.Config.Spec.SnapstoreProxyDomain, diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml index 255c8dbc..bc1ad0bb 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanes.yaml @@ -246,9 +246,6 @@ spec: items: type: string type: array - channel: - description: Channel is the channel to use for the snap install. - type: string bootstrapConfig: description: BootstrapConfig is the data to be passed to the bootstrap script. @@ -282,6 +279,9 @@ spec: - secret type: object type: object + channel: + description: Channel is the channel to use for the snap install. + type: string controlPlane: description: CK8sControlPlaneConfig is configuration for the control plane node. @@ -431,14 +431,14 @@ spec: the default CNI. type: boolean type: object - noProxy: - description: NoProxy is optional no proxy configuration - type: string localPath: description: |- LocalPath is the path of a local snap file in the workload cluster to use for the snap install. If Channel or Revision are set, this will be ignored. type: string + noProxy: + description: NoProxy is optional no proxy configuration + type: string nodeName: description: |- NodeName is the name to use for the kubelet of this node. It is needed for clouds diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml index 4e3e9e28..715a65e3 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_ck8scontrolplanetemplates.yaml @@ -221,10 +221,6 @@ spec: items: type: string type: array - channel: - description: Channel is the channel to use for the snap - install. - type: string bootstrapConfig: description: BootstrapConfig is the data to be passed to the bootstrap script. @@ -258,6 +254,10 @@ spec: - secret type: object type: object + channel: + description: Channel is the channel to use for the snap + install. + type: string controlPlane: description: CK8sControlPlaneConfig is configuration for the control plane node. diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index 6799b01a..81818b53 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -49,12 +49,12 @@ type BaseUserData struct { SnapstoreProxyDomain string // The snap store proxy ID SnapstoreProxyID string - // ContainerdHTTPProxy is http_proxy configuration for containerd. - ContainerdHTTPProxy string - // ContainerdHTTPSProxy is https_proxy configuration for containerd. - ContainerdHTTPSProxy string - // ContainerdNoProxy is no_proxy configuration for containerd. - ContainerdNoProxy string + // HTTPProxy is http_proxy configuration. + HTTPProxy string + // HTTPSProxy is https_proxy configuration. + HTTPSProxy string + // NoProxy is no_proxy configuration. + NoProxy string // MicroclusterAddress is the address to use for microcluster. MicroclusterAddress string // MicroclusterPort is the port to use for microcluster. @@ -101,10 +101,10 @@ func NewBaseCloudConfig(data BaseUserData) (CloudConfig, error) { config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-snapstore-proxy.sh") } - // containerd proxy configuration - if containerdProxyConfigFiles := getContainerdProxyConfigFiles(data); containerdProxyConfigFiles != nil { + // proxy configuration + if containerdProxyConfigFiles := getProxyConfigFiles(data); containerdProxyConfigFiles != nil { config.WriteFiles = append(config.WriteFiles, containerdProxyConfigFiles...) - config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-containerd-proxy.sh") + config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-proxy.sh") } var configFileContents string @@ -202,26 +202,29 @@ func getSnapstoreProxyConfigFiles(data BaseUserData) []File { return []File{schemeFile, domainFile, storeIDFile} } -func getContainerdProxyConfigFiles(data BaseUserData) []File { - if data.ContainerdHTTPSProxy == "" || data.ContainerdHTTPProxy == "" { +// getProxyConfigFiles returns the node proxy config files. +// If the HTTPProxy or HTTPPSProxy is not set, it returns nil. +// Nil indicates that no files are returned. +func getProxyConfigFiles(data BaseUserData) []File { + if data.HTTPProxy == "" || data.HTTPSProxy == "" { return nil } return []File{ { - Path: "/capi/etc/containerd-http-proxy", - Content: data.ContainerdHTTPProxy, + Path: "/capi/etc/http-proxy", + Content: data.HTTPProxy, Permissions: "0400", Owner: "root:root", }, { - Path: "/capi/etc/containerd-https-proxy", - Content: data.ContainerdHTTPSProxy, + Path: "/capi/etc/https-proxy", + Content: data.HTTPSProxy, Permissions: "0400", Owner: "root:root", }, { - Path: "/capi/etc/containerd-no-proxy", - Content: data.ContainerdNoProxy, + Path: "/capi/etc/no-proxy", + Content: data.NoProxy, Permissions: "0400", Owner: "root:root", }, diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index cac80bc7..43c6cf3f 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -84,7 +84,7 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), @@ -118,9 +118,9 @@ func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { SnapstoreProxyScheme: "http", SnapstoreProxyDomain: "snapstore.io", SnapstoreProxyID: "abcd-1234-xyz", - ContainerdHTTPProxy: "http://proxy.internal", - ContainerdHTTPSProxy: "https://proxy.internal", - ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", ConfigFileContents: "### config file ###", MicroclusterAddress: "10.0.0.0/8", }, @@ -137,7 +137,7 @@ func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-containerd-proxy.sh", + "/capi/scripts/configure-proxy.sh", "prerun1", "prerun2", "/capi/scripts/install.sh", @@ -161,14 +161,14 @@ func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), - HaveField("Path", "/capi/etc/containerd-http-proxy"), - HaveField("Path", "/capi/etc/containerd-https-proxy"), - HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), HaveField("Path", "/capi/etc/microcluster-address"), HaveField("Path", "/capi/etc/node-name"), HaveField("Path", "/capi/etc/node-token"), diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index 0b8f79ed..2c1f79e6 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -66,7 +66,7 @@ func TestNewJoinControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), @@ -101,9 +101,9 @@ func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { SnapstoreProxyScheme: "http", SnapstoreProxyDomain: "snapstore.io", SnapstoreProxyID: "abcd-1234-xyz", - ContainerdHTTPProxy: "http://proxy.internal", - ContainerdHTTPSProxy: "https://proxy.internal", - ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", ConfigFileContents: "### config file ###", MicroclusterAddress: "10.0.0.11", }, @@ -119,7 +119,7 @@ func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-containerd-proxy.sh", + "/capi/scripts/configure-proxy.sh", "prerun1", "prerun2", "/capi/scripts/install.sh", @@ -141,14 +141,14 @@ func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), - HaveField("Path", "/capi/etc/containerd-http-proxy"), - HaveField("Path", "/capi/etc/containerd-https-proxy"), - HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), HaveField("Path", "/capi/etc/microcluster-address"), HaveField("Path", "/capi/etc/node-name"), HaveField("Path", "/capi/etc/node-token"), diff --git a/pkg/cloudinit/embed.go b/pkg/cloudinit/embed.go index 6ae9f827..dafaa308 100644 --- a/pkg/cloudinit/embed.go +++ b/pkg/cloudinit/embed.go @@ -15,18 +15,18 @@ type script string // NOTE(eac): If you want to use a script from pkg/cloudinit/scripts in your code (for example, you want to include a script in the user-data.txt), // you need to add it to the scripts map below. var ( - scriptInstall script = "install.sh" + scriptInstall script = "install.sh" scriptDisableHostServices script = "disable-host-services.sh" - scriptBootstrap script = "bootstrap.sh" - scriptLoadImages script = "load-images.sh" - scriptConfigureAuthToken script = "configure-auth-token.sh" // #nosec G101 - scriptConfigureContainerdProxy script = "configure-containerd-proxy.sh" - scriptConfigureNodeToken script = "configure-node-token.sh" // #nosec G101 - scriptJoinCluster script = "join-cluster.sh" - scriptWaitAPIServerReady script = "wait-apiserver-ready.sh" - scriptDeployManifests script = "deploy-manifests.sh" - scriptCreateSentinelBootstrap script = "create-sentinel-bootstrap.sh" - scriptConfigureSnapstoreProxy script = "configure-snapstore-proxy.sh" + scriptBootstrap script = "bootstrap.sh" + scriptLoadImages script = "load-images.sh" + scriptConfigureAuthToken script = "configure-auth-token.sh" // #nosec G101 + scriptConfigureProxy script = "configure-proxy.sh" + scriptConfigureNodeToken script = "configure-node-token.sh" // #nosec G101 + scriptJoinCluster script = "join-cluster.sh" + scriptWaitAPIServerReady script = "wait-apiserver-ready.sh" + scriptDeployManifests script = "deploy-manifests.sh" + scriptCreateSentinelBootstrap script = "create-sentinel-bootstrap.sh" + scriptConfigureSnapstoreProxy script = "configure-snapstore-proxy.sh" ) func mustEmbed(s script) string { @@ -40,17 +40,17 @@ func mustEmbed(s script) string { var ( // scripts is a map of all embedded bash scripts used in the cloud-init. scripts = map[script]string{ - scriptInstall: mustEmbed(scriptInstall), + scriptInstall: mustEmbed(scriptInstall), scriptDisableHostServices: mustEmbed(scriptDisableHostServices), - scriptBootstrap: mustEmbed(scriptBootstrap), - scriptLoadImages: mustEmbed(scriptLoadImages), - scriptConfigureAuthToken: mustEmbed(scriptConfigureAuthToken), - scriptConfigureContainerdProxy: mustEmbed(scriptConfigureContainerdProxy), - scriptConfigureNodeToken: mustEmbed(scriptConfigureNodeToken), - scriptJoinCluster: mustEmbed(scriptJoinCluster), - scriptWaitAPIServerReady: mustEmbed(scriptWaitAPIServerReady), - scriptDeployManifests: mustEmbed(scriptDeployManifests), - scriptCreateSentinelBootstrap: mustEmbed(scriptCreateSentinelBootstrap), - scriptConfigureSnapstoreProxy: mustEmbed(scriptConfigureSnapstoreProxy), + scriptBootstrap: mustEmbed(scriptBootstrap), + scriptLoadImages: mustEmbed(scriptLoadImages), + scriptConfigureAuthToken: mustEmbed(scriptConfigureAuthToken), + scriptConfigureProxy: mustEmbed(scriptConfigureProxy), + scriptConfigureNodeToken: mustEmbed(scriptConfigureNodeToken), + scriptJoinCluster: mustEmbed(scriptJoinCluster), + scriptWaitAPIServerReady: mustEmbed(scriptWaitAPIServerReady), + scriptDeployManifests: mustEmbed(scriptDeployManifests), + scriptCreateSentinelBootstrap: mustEmbed(scriptCreateSentinelBootstrap), + scriptConfigureSnapstoreProxy: mustEmbed(scriptConfigureSnapstoreProxy), } ) diff --git a/pkg/cloudinit/scripts/configure-containerd-proxy.sh b/pkg/cloudinit/scripts/configure-containerd-proxy.sh deleted file mode 100644 index b8905980..00000000 --- a/pkg/cloudinit/scripts/configure-containerd-proxy.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash -xe - -# Assumptions: -# - k8s is installed - -# - /capi/etc/containerd-http-proxy contains containerd http proxy value -# - /capi/etc/containerd-https-proxy contains containerd https proxy value -# - /capi/etc/containerd-no-proxy contains containerd no proxy value - - -HTTP_PROXY=$(cat /capi/etc/containerd-http-proxy) -HTTPS_PROXY=$(cat /capi/etc/containerd-https-proxy) -NO_PROXY=$(cat /capi/etc/containerd-no-proxy) - -mkdir -p /etc/systemd/system/snap.k8s.containerd.service.d -CONTAINERD_HTTP_PROXY="/etc/systemd/system/snap.k8s.containerd.service.d/http-proxy.conf" - -echo "[Service]" >> "${CONTAINERD_HTTP_PROXY}" -need_restart=false - - - -if [[ "${HTTP_PROXY}" != "" ]]; then - echo "Environment=\"http_proxy=${HTTP_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - echo "Environment=\"HTTP_PROXY=${HTTP_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - need_restart=true -fi - -if [[ "${HTTPS_PROXY}" != "" ]]; then - echo "Environment=\"https_proxy=${HTTPS_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - echo "Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - need_restart=true -fi - -if [[ "${NO_PROXY}" != "" ]]; then - echo "Environment=\"no_proxy=${NO_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - echo "Environment=\"NO_PROXY=${NO_PROXY}\"" >> "${CONTAINERD_HTTP_PROXY}" - need_restart=true -fi - -if [[ "$need_restart" = "true" ]]; then - snap restart k8s.containerd -fi diff --git a/pkg/cloudinit/scripts/configure-proxy.sh b/pkg/cloudinit/scripts/configure-proxy.sh new file mode 100644 index 00000000..dfe31703 --- /dev/null +++ b/pkg/cloudinit/scripts/configure-proxy.sh @@ -0,0 +1,30 @@ +#!/bin/bash -xe + +# Assumptions: +# - runs before install k8s + +# - /capi/etc/http-proxy contains http proxy value +# - /capi/etc/https-proxy contains https proxy value +# - /capi/etc/no-proxy contains no proxy value + + +HTTP_PROXY=$(cat /capi/etc/http-proxy) +HTTPS_PROXY=$(cat /capi/etc/https-proxy) +NO_PROXY=$(cat /capi/etc/no-proxy) + +ENVIRONMENT_FILE="/etc/environment" + +if [[ "${HTTP_PROXY}" != "" ]]; then + echo "http_proxy=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" + echo "HTTP_PROXY=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" +fi + +if [[ "${HTTPS_PROXY}" != "" ]]; then + echo "https_proxy=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" + echo "HTTPS_PROXY=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" +fi + +if [[ "${NO_PROXY}" != "" ]]; then + echo "no_proxy=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" + echo "NO_PROXY=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" +fi diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index c52ff982..aee29e4f 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -66,7 +66,7 @@ func TestNewJoinWorker(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), @@ -101,9 +101,9 @@ func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { SnapstoreProxyScheme: "http", SnapstoreProxyDomain: "snapstore.io", SnapstoreProxyID: "abcd-1234-xyz", - ContainerdHTTPProxy: "http://proxy.internal", - ContainerdHTTPSProxy: "https://proxy.internal", - ContainerdNoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", ConfigFileContents: "### config file ###", MicroclusterAddress: "10.0.0.10", MicroclusterPort: 8080, @@ -120,7 +120,7 @@ func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { g.Expect(config.RunCommands).To(Equal([]string{ "set -x", "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-containerd-proxy.sh", + "/capi/scripts/configure-proxy.sh", "prerun1", "prerun2", "/capi/scripts/install.sh", @@ -141,14 +141,14 @@ func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), - HaveField("Path", "/capi/scripts/configure-containerd-proxy.sh"), + HaveField("Path", "/capi/scripts/configure-proxy.sh"), HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), HaveField("Path", "/capi/etc/config.yaml"), - HaveField("Path", "/capi/etc/containerd-http-proxy"), - HaveField("Path", "/capi/etc/containerd-https-proxy"), - HaveField("Path", "/capi/etc/containerd-no-proxy"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), HaveField("Path", "/capi/etc/microcluster-address"), HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), HaveField("Path", "/capi/etc/snapstore-proxy-domain"), From d4931ba81fffcf663773053ead122d0bcfde7928 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Wed, 16 Oct 2024 19:43:27 +0200 Subject: [PATCH 04/13] tests fix --- pkg/cloudinit/controlplane_init_test.go | 2 ++ pkg/cloudinit/controlplane_join_test.go | 2 ++ pkg/cloudinit/worker_join_test.go | 2 ++ 3 files changed, 6 insertions(+) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 43c6cf3f..96c0e27d 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -141,6 +141,7 @@ func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { "prerun1", "prerun2", "/capi/scripts/install.sh", + "/capi/scripts/disable-host-services.sh", "/capi/scripts/bootstrap.sh", "/capi/scripts/load-images.sh", "/capi/scripts/wait-apiserver-ready.sh", @@ -154,6 +155,7 @@ func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_init.go file. g.Expect(config.WriteFiles).To(ConsistOf( + HaveField("Path", "/capi/scripts/disable-host-services.sh"), HaveField("Path", "/capi/scripts/install.sh"), HaveField("Path", "/capi/scripts/bootstrap.sh"), HaveField("Path", "/capi/scripts/load-images.sh"), diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index 2c1f79e6..ba0ff9f2 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -123,6 +123,7 @@ func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { "prerun1", "prerun2", "/capi/scripts/install.sh", + "/capi/scripts/disable-host-services.sh", "/capi/scripts/load-images.sh", "/capi/scripts/join-cluster.sh", "/capi/scripts/wait-apiserver-ready.sh", @@ -135,6 +136,7 @@ func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_join.go file. g.Expect(config.WriteFiles).To(ConsistOf( HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/disable-host-services.sh"), HaveField("Path", "/capi/scripts/bootstrap.sh"), HaveField("Path", "/capi/scripts/load-images.sh"), HaveField("Path", "/capi/scripts/join-cluster.sh"), diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index aee29e4f..e611d1b0 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -124,6 +124,7 @@ func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { "prerun1", "prerun2", "/capi/scripts/install.sh", + "/capi/scripts/disable-host-services.sh", "/capi/scripts/load-images.sh", "/capi/scripts/join-cluster.sh", "/capi/scripts/configure-node-token.sh", @@ -135,6 +136,7 @@ func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { // NOTE (mateoflorido): Keep this test in sync with the expected paths in the worker_join.go file. g.Expect(config.WriteFiles).To(ConsistOf( HaveField("Path", "/capi/scripts/install.sh"), + HaveField("Path", "/capi/scripts/disable-host-services.sh"), HaveField("Path", "/capi/scripts/bootstrap.sh"), HaveField("Path", "/capi/scripts/load-images.sh"), HaveField("Path", "/capi/scripts/join-cluster.sh"), From 58fdc2a1b6be98f75aed1c486dda02e8a59bb4f0 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 10:04:47 +0200 Subject: [PATCH 05/13] reduce test scope --- pkg/cloudinit/controlplane_init_test.go | 82 ++++--------------------- pkg/cloudinit/controlplane_join_test.go | 79 ++++-------------------- pkg/cloudinit/worker_join_test.go | 79 ++++-------------------- 3 files changed, 35 insertions(+), 205 deletions(-) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 96c0e27d..36dfbd3f 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -99,89 +99,29 @@ func TestNewInitControlPlane(t *testing.T) { ), "Some /capi/scripts files are missing") } -func TestNewInitControlPlaneWithOptionalProxies(t *testing.T) { +func TestNewInitControlPlaneWithOptionalProxySettings(t *testing.T) { g := NewWithT(t) - format.MaxLength = 20000 config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - BootCommands: []string{"bootcmd"}, - PreRunCommands: []string{"prerun1", "prerun2"}, - PostRunCommands: []string{"postrun1", "postrun2"}, - ExtraFiles: []cloudinit.File{{ - Path: "/tmp/file", - Content: "test file", - Permissions: "0400", - Owner: "root:root", - }}, - SnapstoreProxyScheme: "http", - SnapstoreProxyDomain: "snapstore.io", - SnapstoreProxyID: "abcd-1234-xyz", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - ConfigFileContents: "### config file ###", - MicroclusterAddress: "10.0.0.0/8", + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", }, - AuthToken: "test-token", - K8sdProxyDaemonSet: "test-daemonset", }) g.Expect(err).ToNot(HaveOccurred()) - - // Verify the boot commands. - g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) - - // Verify the run commands. - g.Expect(config.RunCommands).To(Equal([]string{ - "set -x", - "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-proxy.sh", - "prerun1", - "prerun2", - "/capi/scripts/install.sh", - "/capi/scripts/disable-host-services.sh", - "/capi/scripts/bootstrap.sh", - "/capi/scripts/load-images.sh", - "/capi/scripts/wait-apiserver-ready.sh", - "/capi/scripts/deploy-manifests.sh", - "/capi/scripts/configure-auth-token.sh", - "/capi/scripts/configure-node-token.sh", - "/capi/scripts/create-sentinel-bootstrap.sh", - "postrun1", - "postrun2", - })) - - // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_init.go file. - g.Expect(config.WriteFiles).To(ConsistOf( - HaveField("Path", "/capi/scripts/disable-host-services.sh"), - HaveField("Path", "/capi/scripts/install.sh"), - HaveField("Path", "/capi/scripts/bootstrap.sh"), - HaveField("Path", "/capi/scripts/load-images.sh"), - HaveField("Path", "/capi/scripts/join-cluster.sh"), - HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), - HaveField("Path", "/capi/scripts/deploy-manifests.sh"), - HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + // Verify proxy run command. + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements( HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-node-token.sh"), - HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), - HaveField("Path", "/capi/etc/config.yaml"), HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), HaveField("Path", "/capi/etc/no-proxy"), - HaveField("Path", "/capi/etc/microcluster-address"), - HaveField("Path", "/capi/etc/node-name"), - HaveField("Path", "/capi/etc/node-token"), - HaveField("Path", "/capi/etc/token"), - HaveField("Path", "/capi/etc/snap-channel"), - HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), - HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), - HaveField("Path", "/capi/etc/snapstore-proxy-domain"), - HaveField("Path", "/capi/etc/snapstore-proxy-id"), - HaveField("Path", "/tmp/file"), - ), "Some /capi/scripts files are missing") + ), "Required files in /capi directory are missing") } func TestUserSuppliedBootstrapConfig(t *testing.T) { diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index ba0ff9f2..bda84ecb 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -83,84 +83,29 @@ func TestNewJoinControlPlane(t *testing.T) { ), "Some /capi/scripts files are missing") } -func TestNewJoinControlPlaneWithOptionalProxies(t *testing.T) { +func TestNewJoinControlPlaneOptionalProxySettings(t *testing.T) { g := NewWithT(t) config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - BootCommands: []string{"bootcmd"}, - PreRunCommands: []string{"prerun1", "prerun2"}, - PostRunCommands: []string{"postrun1", "postrun2"}, - ExtraFiles: []cloudinit.File{{ - Path: "/tmp/file", - Content: "test file", - Permissions: "0400", - Owner: "root:root", - }}, - SnapstoreProxyScheme: "http", - SnapstoreProxyDomain: "snapstore.io", - SnapstoreProxyID: "abcd-1234-xyz", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - ConfigFileContents: "### config file ###", - MicroclusterAddress: "10.0.0.11", + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", }, - JoinToken: "test-token", }) - g.Expect(err).NotTo(HaveOccurred()) - - // Verify the boot commands. - g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) - - // Verify the run commands. - g.Expect(config.RunCommands).To(Equal([]string{ - "set -x", - "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-proxy.sh", - "prerun1", - "prerun2", - "/capi/scripts/install.sh", - "/capi/scripts/disable-host-services.sh", - "/capi/scripts/load-images.sh", - "/capi/scripts/join-cluster.sh", - "/capi/scripts/wait-apiserver-ready.sh", - "/capi/scripts/configure-node-token.sh", - "/capi/scripts/create-sentinel-bootstrap.sh", - "postrun1", - "postrun2", - })) - - // NOTE (mateoflorido): Keep this test in sync with the expected paths in the controlplane_join.go file. - g.Expect(config.WriteFiles).To(ConsistOf( - HaveField("Path", "/capi/scripts/install.sh"), - HaveField("Path", "/capi/scripts/disable-host-services.sh"), - HaveField("Path", "/capi/scripts/bootstrap.sh"), - HaveField("Path", "/capi/scripts/load-images.sh"), - HaveField("Path", "/capi/scripts/join-cluster.sh"), - HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), - HaveField("Path", "/capi/scripts/deploy-manifests.sh"), - HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + g.Expect(err).ToNot(HaveOccurred()) + // Verify proxy run command. + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements( HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-node-token.sh"), - HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), - HaveField("Path", "/capi/etc/config.yaml"), HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), HaveField("Path", "/capi/etc/no-proxy"), - HaveField("Path", "/capi/etc/microcluster-address"), - HaveField("Path", "/capi/etc/node-name"), - HaveField("Path", "/capi/etc/node-token"), - HaveField("Path", "/capi/etc/join-token"), - HaveField("Path", "/capi/etc/snap-channel"), - HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), - HaveField("Path", "/capi/etc/snapstore-proxy-domain"), - HaveField("Path", "/capi/etc/snapstore-proxy-id"), - HaveField("Path", "/tmp/file"), - ), "Some /capi/scripts files are missing") + ), "Required files in /capi directory are missing") } func TestNewJoinControlPlaneInvalidVersionError(t *testing.T) { diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index e611d1b0..5efd70a6 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -83,84 +83,29 @@ func TestNewJoinWorker(t *testing.T) { ), "Some /capi/scripts files are missing") } -func TestNewJoinWorkerWithOptionalProxies(t *testing.T) { +func TestNewJoinWorkerWithProxySettings(t *testing.T) { g := NewWithT(t) config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - BootCommands: []string{"bootcmd"}, - PreRunCommands: []string{"prerun1", "prerun2"}, - PostRunCommands: []string{"postrun1", "postrun2"}, - ExtraFiles: []cloudinit.File{{ - Path: "/tmp/file", - Content: "test file", - Permissions: "0400", - Owner: "root:root", - }}, - SnapstoreProxyScheme: "http", - SnapstoreProxyDomain: "snapstore.io", - SnapstoreProxyID: "abcd-1234-xyz", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - ConfigFileContents: "### config file ###", - MicroclusterAddress: "10.0.0.10", - MicroclusterPort: 8080, + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", }, - JoinToken: "test-token", }) - g.Expect(err).NotTo(HaveOccurred()) - - // Verify the boot commands. - g.Expect(config.BootCommands).To(Equal([]string{"bootcmd"})) - - // Verify the run commands. - g.Expect(config.RunCommands).To(Equal([]string{ - "set -x", - "/capi/scripts/configure-snapstore-proxy.sh", - "/capi/scripts/configure-proxy.sh", - "prerun1", - "prerun2", - "/capi/scripts/install.sh", - "/capi/scripts/disable-host-services.sh", - "/capi/scripts/load-images.sh", - "/capi/scripts/join-cluster.sh", - "/capi/scripts/configure-node-token.sh", - "/capi/scripts/create-sentinel-bootstrap.sh", - "postrun1", - "postrun2", - })) - - // NOTE (mateoflorido): Keep this test in sync with the expected paths in the worker_join.go file. - g.Expect(config.WriteFiles).To(ConsistOf( - HaveField("Path", "/capi/scripts/install.sh"), - HaveField("Path", "/capi/scripts/disable-host-services.sh"), - HaveField("Path", "/capi/scripts/bootstrap.sh"), - HaveField("Path", "/capi/scripts/load-images.sh"), - HaveField("Path", "/capi/scripts/join-cluster.sh"), - HaveField("Path", "/capi/scripts/wait-apiserver-ready.sh"), - HaveField("Path", "/capi/scripts/deploy-manifests.sh"), - HaveField("Path", "/capi/scripts/configure-auth-token.sh"), + g.Expect(err).ToNot(HaveOccurred()) + // Verify proxy run command. + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements( HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-node-token.sh"), - HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), - HaveField("Path", "/capi/etc/config.yaml"), HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), HaveField("Path", "/capi/etc/no-proxy"), - HaveField("Path", "/capi/etc/microcluster-address"), - HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), - HaveField("Path", "/capi/etc/snapstore-proxy-domain"), - HaveField("Path", "/capi/etc/snapstore-proxy-id"), - HaveField("Path", "/capi/etc/node-name"), - HaveField("Path", "/capi/etc/node-token"), - HaveField("Path", "/capi/etc/join-token"), - HaveField("Path", "/capi/etc/snap-channel"), - HaveField("Path", "/tmp/file"), - ), "Some /capi/scripts files are missing") + ), "Required files in /capi directory are missing") } func TestNewJoinWorkerInvalidVersionError(t *testing.T) { From 50012e5e36048d471e77c1cf34281e2b447ab55d Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 10:09:48 +0200 Subject: [PATCH 06/13] revert changes in out of scope tests --- pkg/cloudinit/controlplane_init_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index 36dfbd3f..cea8319e 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -34,10 +34,13 @@ func TestNewInitControlPlane(t *testing.T) { config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - BootCommands: []string{"bootcmd"}, - PreRunCommands: []string{"prerun1", "prerun2"}, - PostRunCommands: []string{"postrun1", "postrun2"}, + KubernetesVersion: "v1.30.0", + BootCommands: []string{"bootcmd"}, + PreRunCommands: []string{"prerun1", "prerun2"}, + PostRunCommands: []string{"postrun1", "postrun2"}, + SnapstoreProxyScheme: "http", + SnapstoreProxyDomain: "snapstore.io", + SnapstoreProxyID: "abcd-1234-xyz", ExtraFiles: []cloudinit.File{{ Path: "/tmp/file", Content: "test file", @@ -59,6 +62,7 @@ func TestNewInitControlPlane(t *testing.T) { // Verify the run commands. g.Expect(config.RunCommands).To(Equal([]string{ "set -x", + "/capi/scripts/configure-snapstore-proxy.sh", "prerun1", "prerun2", "/capi/scripts/install.sh", @@ -85,9 +89,9 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/scripts/deploy-manifests.sh"), HaveField("Path", "/capi/scripts/configure-auth-token.sh"), HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/scripts/configure-node-token.sh"), HaveField("Path", "/capi/scripts/create-sentinel-bootstrap.sh"), + HaveField("Path", "/capi/scripts/configure-snapstore-proxy.sh"), HaveField("Path", "/capi/etc/config.yaml"), HaveField("Path", "/capi/etc/microcluster-address"), HaveField("Path", "/capi/etc/node-name"), @@ -95,6 +99,9 @@ func TestNewInitControlPlane(t *testing.T) { HaveField("Path", "/capi/etc/token"), HaveField("Path", "/capi/etc/snap-channel"), HaveField("Path", "/capi/manifests/00-k8sd-proxy.yaml"), + HaveField("Path", "/capi/etc/snapstore-proxy-scheme"), + HaveField("Path", "/capi/etc/snapstore-proxy-domain"), + HaveField("Path", "/capi/etc/snapstore-proxy-id"), HaveField("Path", "/tmp/file"), ), "Some /capi/scripts files are missing") } From f9a3b29c26d6bac1a1e46c86b80dc78a7502b469 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 10:11:27 +0200 Subject: [PATCH 07/13] typo fix --- pkg/cloudinit/common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index 81818b53..506d8964 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -102,8 +102,8 @@ func NewBaseCloudConfig(data BaseUserData) (CloudConfig, error) { } // proxy configuration - if containerdProxyConfigFiles := getProxyConfigFiles(data); containerdProxyConfigFiles != nil { - config.WriteFiles = append(config.WriteFiles, containerdProxyConfigFiles...) + if proxyConfigFiles := getProxyConfigFiles(data); proxyConfigFiles != nil { + config.WriteFiles = append(config.WriteFiles, proxyConfigFiles...) config.RunCommands = append(config.RunCommands, "/capi/scripts/configure-proxy.sh") } From 36bc87d0e883e354d5d24a386a99d8ed2c093069 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 10:45:25 +0200 Subject: [PATCH 08/13] added multiple test cases for proxy test --- pkg/cloudinit/common.go | 2 +- pkg/cloudinit/controlplane_init_test.go | 113 +++++++++++++++++++----- pkg/cloudinit/controlplane_join_test.go | 111 +++++++++++++++++++---- pkg/cloudinit/worker_join_test.go | 111 +++++++++++++++++++---- 4 files changed, 278 insertions(+), 59 deletions(-) diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index 506d8964..70ccae15 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -206,7 +206,7 @@ func getSnapstoreProxyConfigFiles(data BaseUserData) []File { // If the HTTPProxy or HTTPPSProxy is not set, it returns nil. // Nil indicates that no files are returned. func getProxyConfigFiles(data BaseUserData) []File { - if data.HTTPProxy == "" || data.HTTPSProxy == "" { + if data.HTTPProxy == "" && data.HTTPSProxy == "" { return nil } return []File{ diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index cea8319e..cd335b33 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -18,6 +18,7 @@ package cloudinit_test import ( "fmt" + "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" @@ -108,27 +109,99 @@ func TestNewInitControlPlane(t *testing.T) { func TestNewInitControlPlaneWithOptionalProxySettings(t *testing.T) { g := NewWithT(t) - - config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{ - BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", + for _, tc := range []struct { + name string + baseUserData cloudinit.BaseUserData + expectRunCommand bool + expectWriteFiles []types.GomegaMatcher + }{ + { + name: "AllFieldsSet", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, }, - }) - - g.Expect(err).ToNot(HaveOccurred()) - // Verify proxy run command. - g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) - // Verify proxy files present. - g.Expect(config.WriteFiles).To(ContainElements( - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - ), "Required files in /capi directory are missing") + { + name: "HTTPSProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "HTTPProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "NoProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + { + name: "noFields", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + config, err := cloudinit.NewInitControlPlane(cloudinit.InitControlPlaneInput{BaseUserData: tc.baseUserData}) + + g.Expect(err).ToNot(HaveOccurred()) + // Verify proxy run command. + if tc.expectRunCommand { + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + } else { + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/configure-proxy.sh")) + } + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements(tc.expectWriteFiles), + "Required files in /capi directory are missing") + }) + } } func TestUserSuppliedBootstrapConfig(t *testing.T) { diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index bda84ecb..1ecca7e2 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -2,6 +2,7 @@ package cloudinit_test import ( "fmt" + "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" @@ -85,27 +86,99 @@ func TestNewJoinControlPlane(t *testing.T) { func TestNewJoinControlPlaneOptionalProxySettings(t *testing.T) { g := NewWithT(t) - - config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{ - BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", + for _, tc := range []struct { + name string + baseUserData cloudinit.BaseUserData + expectRunCommand bool + expectWriteFiles []types.GomegaMatcher + }{ + { + name: "AllFieldsSet", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, }, - }) + { + name: "HTTPSProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "HTTPProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "NoProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + { + name: "noFields", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + config, err := cloudinit.NewJoinControlPlane(cloudinit.JoinControlPlaneInput{BaseUserData: tc.baseUserData}) - g.Expect(err).ToNot(HaveOccurred()) - // Verify proxy run command. - g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) - // Verify proxy files present. - g.Expect(config.WriteFiles).To(ContainElements( - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - ), "Required files in /capi directory are missing") + g.Expect(err).ToNot(HaveOccurred()) + // Verify proxy run command. + if tc.expectRunCommand { + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + } else { + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/configure-proxy.sh")) + } + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements(tc.expectWriteFiles), + "Required files in /capi directory are missing") + }) + } } func TestNewJoinControlPlaneInvalidVersionError(t *testing.T) { diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index 5efd70a6..785634c5 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -2,6 +2,7 @@ package cloudinit_test import ( "fmt" + "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" @@ -85,27 +86,99 @@ func TestNewJoinWorker(t *testing.T) { func TestNewJoinWorkerWithProxySettings(t *testing.T) { g := NewWithT(t) - - config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{ - BaseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", - HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", + for _, tc := range []struct { + name string + baseUserData cloudinit.BaseUserData + expectRunCommand bool + expectWriteFiles []types.GomegaMatcher + }{ + { + name: "AllFieldsSet", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, }, - }) + { + name: "HTTPSProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPSProxy: "https://proxy.internal", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "HTTPProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + HTTPProxy: "http://proxy.internal", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: true, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + HaveField("Path", "/capi/etc/http-proxy"), + HaveField("Path", "/capi/etc/https-proxy"), + HaveField("Path", "/capi/etc/no-proxy"), + }, + }, + { + name: "NoProxy", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + { + name: "noFields", + baseUserData: cloudinit.BaseUserData{ + KubernetesVersion: "v1.30.0", + MicroclusterAddress: "10.0.0.0/8", + }, + expectRunCommand: false, + expectWriteFiles: []types.GomegaMatcher{ + HaveField("Path", "/capi/scripts/configure-proxy.sh"), + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{BaseUserData: tc.baseUserData}) - g.Expect(err).ToNot(HaveOccurred()) - // Verify proxy run command. - g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) - // Verify proxy files present. - g.Expect(config.WriteFiles).To(ContainElements( - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - ), "Required files in /capi directory are missing") + g.Expect(err).ToNot(HaveOccurred()) + // Verify proxy run command. + if tc.expectRunCommand { + g.Expect(config.RunCommands).To(ContainElement("/capi/scripts/configure-proxy.sh")) + } else { + g.Expect(config.RunCommands).NotTo(ContainElement("/capi/scripts/configure-proxy.sh")) + } + // Verify proxy files present. + g.Expect(config.WriteFiles).To(ContainElements(tc.expectWriteFiles), + "Required files in /capi directory are missing") + }) + } } func TestNewJoinWorkerInvalidVersionError(t *testing.T) { From f980a8356b30bc3a4ac4423f7c5642f5e9730201 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 10:54:31 +0200 Subject: [PATCH 09/13] lint fix --- pkg/cloudinit/controlplane_init_test.go | 2 +- pkg/cloudinit/controlplane_join_test.go | 2 +- pkg/cloudinit/worker_join_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index cd335b33..b5387d7c 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -18,12 +18,12 @@ package cloudinit_test import ( "fmt" - "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" format "github.com/onsi/gomega/format" "github.com/onsi/gomega/gstruct" + "github.com/onsi/gomega/types" "github.com/canonical/cluster-api-k8s/pkg/cloudinit" ) diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index 1ecca7e2..ada92b5f 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -2,11 +2,11 @@ package cloudinit_test import ( "fmt" - "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" "github.com/onsi/gomega/gstruct" + "github.com/onsi/gomega/types" "github.com/canonical/cluster-api-k8s/pkg/cloudinit" ) diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index 785634c5..5ed79850 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -2,11 +2,11 @@ package cloudinit_test import ( "fmt" - "github.com/onsi/gomega/types" "testing" . "github.com/onsi/gomega" "github.com/onsi/gomega/gstruct" + "github.com/onsi/gomega/types" "github.com/canonical/cluster-api-k8s/pkg/cloudinit" ) From 185e265accaab7271a36ec4dd959d1ea84b0c2d1 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 15:23:56 +0200 Subject: [PATCH 10/13] change proxy files filter review fixes --- pkg/cloudinit/common.go | 31 ++++++++++++-------- pkg/cloudinit/controlplane_init_test.go | 36 ++---------------------- pkg/cloudinit/controlplane_join_test.go | 36 ++---------------------- pkg/cloudinit/scripts/configure-proxy.sh | 23 +++++++-------- pkg/cloudinit/worker_join_test.go | 35 ++--------------------- 5 files changed, 40 insertions(+), 121 deletions(-) diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index 70ccae15..b9ef5a0a 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -202,31 +202,38 @@ func getSnapstoreProxyConfigFiles(data BaseUserData) []File { return []File{schemeFile, domainFile, storeIDFile} } -// getProxyConfigFiles returns the node proxy config files. -// If the HTTPProxy or HTTPPSProxy is not set, it returns nil. +// getProxyConfigFiles returns the proxy config files. +// Returns slice of files for each proxy parameters are present in data structure with corresponding value // Nil indicates that no files are returned. func getProxyConfigFiles(data BaseUserData) []File { - if data.HTTPProxy == "" && data.HTTPSProxy == "" { - return nil - } - return []File{ - { + var files []File + if data.HTTPProxy != "" { + files = append(files, File{ Path: "/capi/etc/http-proxy", Content: data.HTTPProxy, Permissions: "0400", Owner: "root:root", - }, - { + }) + } + if data.HTTPSProxy != "" { + files = append(files, File{ Path: "/capi/etc/https-proxy", Content: data.HTTPSProxy, Permissions: "0400", Owner: "root:root", - }, - { + }) + } + if data.NoProxy != "" { + files = append(files, File{ Path: "/capi/etc/no-proxy", Content: data.NoProxy, Permissions: "0400", Owner: "root:root", - }, + }) + } + + if len(files) == 0 { + return nil } + return files } diff --git a/pkg/cloudinit/controlplane_init_test.go b/pkg/cloudinit/controlplane_init_test.go index b5387d7c..676fe9ae 100644 --- a/pkg/cloudinit/controlplane_init_test.go +++ b/pkg/cloudinit/controlplane_init_test.go @@ -116,7 +116,7 @@ func TestNewInitControlPlaneWithOptionalProxySettings(t *testing.T) { expectWriteFiles []types.GomegaMatcher }{ { - name: "AllFieldsSet", + name: "AllProxyFieldsSet", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPProxy: "http://proxy.internal", @@ -133,50 +133,20 @@ func TestNewInitControlPlaneWithOptionalProxySettings(t *testing.T) { }, }, { - name: "HTTPSProxy", + name: "HTTPSProxyOnly", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: true, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "HTTPProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", MicroclusterAddress: "10.0.0.0/8", }, expectRunCommand: true, expectWriteFiles: []types.GomegaMatcher{ HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "NoProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: false, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), }, }, { - name: "noFields", + name: "NoProxyFields", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", MicroclusterAddress: "10.0.0.0/8", diff --git a/pkg/cloudinit/controlplane_join_test.go b/pkg/cloudinit/controlplane_join_test.go index ada92b5f..57eca5c5 100644 --- a/pkg/cloudinit/controlplane_join_test.go +++ b/pkg/cloudinit/controlplane_join_test.go @@ -93,7 +93,7 @@ func TestNewJoinControlPlaneOptionalProxySettings(t *testing.T) { expectWriteFiles []types.GomegaMatcher }{ { - name: "AllFieldsSet", + name: "AllProxyFieldsSet", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPProxy: "http://proxy.internal", @@ -110,50 +110,20 @@ func TestNewJoinControlPlaneOptionalProxySettings(t *testing.T) { }, }, { - name: "HTTPSProxy", + name: "HTTPSProxyOnly", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPSProxy: "https://proxy.internal", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: true, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "HTTPProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", MicroclusterAddress: "10.0.0.0/8", }, expectRunCommand: true, expectWriteFiles: []types.GomegaMatcher{ HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "NoProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: false, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), }, }, { - name: "noFields", + name: "NoProxyFields", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", MicroclusterAddress: "10.0.0.0/8", diff --git a/pkg/cloudinit/scripts/configure-proxy.sh b/pkg/cloudinit/scripts/configure-proxy.sh index dfe31703..bbe07c40 100644 --- a/pkg/cloudinit/scripts/configure-proxy.sh +++ b/pkg/cloudinit/scripts/configure-proxy.sh @@ -3,28 +3,29 @@ # Assumptions: # - runs before install k8s -# - /capi/etc/http-proxy contains http proxy value -# - /capi/etc/https-proxy contains https proxy value -# - /capi/etc/no-proxy contains no proxy value - +HTTP_PROXY_FILE="/capi/etc/http-proxy" +HTTPS_PROXY_FILE="/capi/etc/https-proxy" +NO_PROXY_FILE="/capi/etc/no-proxy" +ENVIRONMENT_FILE="/etc/environment" -HTTP_PROXY=$(cat /capi/etc/http-proxy) -HTTPS_PROXY=$(cat /capi/etc/https-proxy) -NO_PROXY=$(cat /capi/etc/no-proxy) -ENVIRONMENT_FILE="/etc/environment" -if [[ "${HTTP_PROXY}" != "" ]]; then +if [ -f HTTP_PROXY_FILE ]; then + local HTTP_PROXY=$(cat HTTP_PROXY_FILE) echo "http_proxy=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" echo "HTTP_PROXY=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" fi -if [[ "${HTTPS_PROXY}" != "" ]]; then + +if [ -f HTTPS_PROXY_FILE ]; then + local HTTPS_PROXY=$(cat HTTPS_PROXY_FILE) echo "https_proxy=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" echo "HTTPS_PROXY=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" fi -if [[ "${NO_PROXY}" != "" ]]; then + +if [ -f NO_PROXY_FILE ]; then + local NO_PROXY=$(cat NO_PROXY_FILE) echo "no_proxy=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" echo "NO_PROXY=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" fi diff --git a/pkg/cloudinit/worker_join_test.go b/pkg/cloudinit/worker_join_test.go index 5ed79850..3fbd06f6 100644 --- a/pkg/cloudinit/worker_join_test.go +++ b/pkg/cloudinit/worker_join_test.go @@ -93,7 +93,7 @@ func TestNewJoinWorkerWithProxySettings(t *testing.T) { expectWriteFiles []types.GomegaMatcher }{ { - name: "AllFieldsSet", + name: "AllProxyFieldsSet", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPProxy: "http://proxy.internal", @@ -110,7 +110,7 @@ func TestNewJoinWorkerWithProxySettings(t *testing.T) { }, }, { - name: "HTTPSProxy", + name: "HTTPSProxyOnly", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", HTTPSProxy: "https://proxy.internal", @@ -120,40 +120,11 @@ func TestNewJoinWorkerWithProxySettings(t *testing.T) { expectRunCommand: true, expectWriteFiles: []types.GomegaMatcher{ HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "HTTPProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - HTTPProxy: "http://proxy.internal", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: true, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), - HaveField("Path", "/capi/etc/http-proxy"), - HaveField("Path", "/capi/etc/https-proxy"), - HaveField("Path", "/capi/etc/no-proxy"), - }, - }, - { - name: "NoProxy", - baseUserData: cloudinit.BaseUserData{ - KubernetesVersion: "v1.30.0", - NoProxy: "10.0.0.0/8,10.152.183.1,192.168.0.0/16", - MicroclusterAddress: "10.0.0.0/8", - }, - expectRunCommand: false, - expectWriteFiles: []types.GomegaMatcher{ - HaveField("Path", "/capi/scripts/configure-proxy.sh"), }, }, { - name: "noFields", + name: "NoProxyFields", baseUserData: cloudinit.BaseUserData{ KubernetesVersion: "v1.30.0", MicroclusterAddress: "10.0.0.0/8", From f198d4dff2bb036d6ddb6982a2d7491e6adf1e2e Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Thu, 17 Oct 2024 17:35:18 +0200 Subject: [PATCH 11/13] shell script fix --- pkg/cloudinit/scripts/configure-proxy.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/cloudinit/scripts/configure-proxy.sh b/pkg/cloudinit/scripts/configure-proxy.sh index bbe07c40..5aa6c597 100644 --- a/pkg/cloudinit/scripts/configure-proxy.sh +++ b/pkg/cloudinit/scripts/configure-proxy.sh @@ -1,4 +1,4 @@ -#!/bin/bash -xe +#!/bin/bash -e # Assumptions: # - runs before install k8s @@ -10,22 +10,22 @@ ENVIRONMENT_FILE="/etc/environment" -if [ -f HTTP_PROXY_FILE ]; then - local HTTP_PROXY=$(cat HTTP_PROXY_FILE) +if [ -f ${HTTP_PROXY_FILE} ]; then + HTTP_PROXY=$(cat ${HTTP_PROXY_FILE}) echo "http_proxy=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" echo "HTTP_PROXY=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" fi -if [ -f HTTPS_PROXY_FILE ]; then - local HTTPS_PROXY=$(cat HTTPS_PROXY_FILE) +if [ -f ${HTTPS_PROXY_FILE} ]; then + HTTPS_PROXY=$(cat ${HTTPS_PROXY_FILE}) echo "https_proxy=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" echo "HTTPS_PROXY=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" fi -if [ -f NO_PROXY_FILE ]; then - local NO_PROXY=$(cat NO_PROXY_FILE) +if [ -f ${NO_PROXY_FILE} ]; then + NO_PROXY=$(cat ${NO_PROXY_FILE}) echo "no_proxy=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" echo "NO_PROXY=${NO_PROXY}" >> "${ENVIRONMENT_FILE}" fi From 95145dbeb9cd637c47d8a572b21a724e2128910a Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Fri, 18 Oct 2024 20:14:01 +0200 Subject: [PATCH 12/13] review fixes --- pkg/cloudinit/common.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/cloudinit/common.go b/pkg/cloudinit/common.go index b9ef5a0a..bd3320b8 100644 --- a/pkg/cloudinit/common.go +++ b/pkg/cloudinit/common.go @@ -232,8 +232,5 @@ func getProxyConfigFiles(data BaseUserData) []File { }) } - if len(files) == 0 { - return nil - } return files } From 1dabf63fbc534188f1c79a5e92f104ac8ea5c100 Mon Sep 17 00:00:00 2001 From: Maciej Golaszewski Date: Fri, 18 Oct 2024 20:20:01 +0200 Subject: [PATCH 13/13] review fixes2 --- pkg/cloudinit/scripts/configure-proxy.sh | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg/cloudinit/scripts/configure-proxy.sh b/pkg/cloudinit/scripts/configure-proxy.sh index 5aa6c597..f473e2a8 100644 --- a/pkg/cloudinit/scripts/configure-proxy.sh +++ b/pkg/cloudinit/scripts/configure-proxy.sh @@ -3,12 +3,10 @@ # Assumptions: # - runs before install k8s -HTTP_PROXY_FILE="/capi/etc/http-proxy" -HTTPS_PROXY_FILE="/capi/etc/https-proxy" -NO_PROXY_FILE="/capi/etc/no-proxy" -ENVIRONMENT_FILE="/etc/environment" - - +HTTP_PROXY_FILE="/tmp/capi/etc/http-proxy" +HTTPS_PROXY_FILE="/tmp/capi/etc/https-proxy" +NO_PROXY_FILE="/tmp/capi/etc/no-proxy" +ENVIRONMENT_FILE="/tmp/etc/environment" if [ -f ${HTTP_PROXY_FILE} ]; then HTTP_PROXY=$(cat ${HTTP_PROXY_FILE}) @@ -16,14 +14,12 @@ if [ -f ${HTTP_PROXY_FILE} ]; then echo "HTTP_PROXY=${HTTP_PROXY}" >> "${ENVIRONMENT_FILE}" fi - if [ -f ${HTTPS_PROXY_FILE} ]; then HTTPS_PROXY=$(cat ${HTTPS_PROXY_FILE}) echo "https_proxy=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" echo "HTTPS_PROXY=${HTTPS_PROXY}" >> "${ENVIRONMENT_FILE}" fi - if [ -f ${NO_PROXY_FILE} ]; then NO_PROXY=$(cat ${NO_PROXY_FILE}) echo "no_proxy=${NO_PROXY}" >> "${ENVIRONMENT_FILE}"