Skip to content

Commit a1c6321

Browse files
Merge pull request #111 from canonical/eaudetcobello/proxy-scheme
add new SnapstoreProxyScheme field
2 parents 44fc40a + d6cf6e7 commit a1c6321

12 files changed

+72
-18
lines changed

apis/v1beta1/microk8sconfig_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ type InitConfiguration struct {
8585
// +optional
8686
DisableDefaultCNI bool `json:"disableDefaultCNI,omitempty"`
8787

88+
// The snap store proxy domain's scheme, e.g. "http" or "https" without "://"
89+
// Defaults to "http".
90+
// +optional
91+
SnapstoreProxyScheme string `json:"snapstoreProxyScheme,omitempty"`
92+
8893
// The snap store proxy domain
8994
// +optional
9095
SnapstoreProxyDomain string `json:"snapstoreProxyDomain,omitempty"`

config/crd/bases/bootstrap.cluster.x-k8s.io_microk8sconfigs.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ spec:
180180
snapstoreProxyId:
181181
description: The snap store proxy ID
182182
type: string
183+
snapstoreProxyScheme:
184+
description: The snap store proxy domain's scheme, e.g. "http"
185+
or "https" without "://" Defaults to "http".
186+
type: string
183187
type: object
184188
type: object
185189
status:

config/crd/bases/bootstrap.cluster.x-k8s.io_microk8sconfigtemplates.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ spec:
191191
snapstoreProxyId:
192192
description: The snap store proxy ID
193193
type: string
194+
snapstoreProxyScheme:
195+
description: The snap store proxy domain's scheme, e.g.
196+
"http" or "https" without "://" Defaults to "http".
197+
type: string
194198
type: object
195199
type: object
196200
type: object

controllers/cloudinit/cloudinit_common_test.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,38 +228,41 @@ func TestCloudConfigInput(t *testing.T) {
228228
t.Run("SnapstoreProxy", func(t *testing.T) {
229229
for _, tc := range []struct {
230230
name string
231-
makeCloudConfig func() (*cloudinit.CloudConfig, error)
231+
makeCloudConfig func(scheme string) (*cloudinit.CloudConfig, error)
232232
}{
233233
{
234234
name: "ControlPlaneInit",
235-
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
235+
makeCloudConfig: func(scheme string) (*cloudinit.CloudConfig, error) {
236236
return cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInitInput{
237237
KubernetesVersion: "v1.25.0",
238238
Token: strings.Repeat("a", 32),
239239
TokenTTL: 100,
240+
SnapstoreProxyScheme: scheme,
240241
SnapstoreProxyDomain: "snapstore.domain.com",
241242
SnapstoreProxyId: "ID123456789",
242243
})
243244
},
244245
},
245246
{
246247
name: "ControlPlaneJoin",
247-
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
248+
makeCloudConfig: func(scheme string) (*cloudinit.CloudConfig, error) {
248249
return cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{
249250
KubernetesVersion: "v1.25.0",
250251
Token: strings.Repeat("a", 32),
251252
TokenTTL: 100,
253+
SnapstoreProxyScheme: scheme,
252254
SnapstoreProxyDomain: "snapstore.domain.com",
253255
SnapstoreProxyId: "ID123456789",
254256
})
255257
},
256258
},
257259
{
258260
name: "Worker",
259-
makeCloudConfig: func() (*cloudinit.CloudConfig, error) {
261+
makeCloudConfig: func(scheme string) (*cloudinit.CloudConfig, error) {
260262
return cloudinit.NewJoinWorker(&cloudinit.WorkerInput{
261263
KubernetesVersion: "v1.25.0",
262264
Token: strings.Repeat("a", 32),
265+
SnapstoreProxyScheme: scheme,
263266
SnapstoreProxyDomain: "snapstore.domain.com",
264267
SnapstoreProxyId: "ID123456789",
265268
})
@@ -268,10 +271,22 @@ func TestCloudConfigInput(t *testing.T) {
268271
} {
269272
t.Run(tc.name, func(t *testing.T) {
270273
g := NewWithT(t)
271-
c, err := tc.makeCloudConfig()
272-
g.Expect(err).NotTo(HaveOccurred())
273274

274-
g.Expect(c.RunCommands).To(ContainElement(`/capi-scripts/00-configure-snapstore-proxy.sh "snapstore.domain.com" "ID123456789"`))
275+
for _, withScheme := range []string{"", "http", "https"} {
276+
t.Run(fmt.Sprintf("withScheme=%q", withScheme), func(t *testing.T) {
277+
c, err := tc.makeCloudConfig(withScheme)
278+
g.Expect(err).NotTo(HaveOccurred())
279+
280+
// if scheme is unspecified, default to http
281+
var expectedScheme string
282+
if withScheme == "" {
283+
expectedScheme = "http"
284+
} else {
285+
expectedScheme = withScheme
286+
}
287+
g.Expect(c.RunCommands).To(ContainElement(fmt.Sprintf(`/capi-scripts/00-configure-snapstore-proxy.sh %q "snapstore.domain.com" "ID123456789"`, expectedScheme)))
288+
})
289+
}
275290
})
276291
}
277292
})

controllers/cloudinit/controlplane_init.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type ControlPlaneInitInput struct {
5959
RiskLevel string
6060
// DisableDefaultCNI specifies whether to disable the default CNI plugin.
6161
DisableDefaultCNI bool
62+
// SnapstoreProxyScheme specifies the scheme (e.g. http or https) of the domain. Defaults to "http".
63+
SnapstoreProxyScheme string
6264
// SnapstoreProxyDomain specifies the domain of the snapstore proxy if one is to be used.
6365
SnapstoreProxyDomain string
6466
// SnapstoreProxyId specifies the snapstore proxy ID if one is to be used.
@@ -88,6 +90,10 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
8890
return nil, fmt.Errorf("join token TTL %q is not a positive number", input.TokenTTL)
8991
}
9092

93+
if input.SnapstoreProxyScheme == "" {
94+
input.SnapstoreProxyScheme = "http"
95+
}
96+
9197
// figure out endpoint type
9298
endpointType := "DNS"
9399
if net.ParseIP(input.ControlPlaneEndpoint) != nil {
@@ -141,7 +147,7 @@ func NewInitControlPlane(input *ControlPlaneInitInput) (*CloudConfig, error) {
141147
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
142148
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
143149
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
144-
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
150+
fmt.Sprintf("%s %q %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyScheme, input.SnapstoreProxyDomain, input.SnapstoreProxyId),
145151
scriptPath(disableHostServicesScript),
146152
fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs),
147153
fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy),

controllers/cloudinit/controlplane_init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestControlPlaneInit(t *testing.T) {
4646
g.Expect(cloudConfig.RunCommands).To(Equal([]string{
4747
`set -x`,
4848
`/capi-scripts/00-configure-snapstore-http-proxy.sh "" ""`,
49-
`/capi-scripts/00-configure-snapstore-proxy.sh "" ""`,
49+
`/capi-scripts/00-configure-snapstore-proxy.sh "http" "" ""`,
5050
`/capi-scripts/00-disable-host-services.sh`,
5151
`/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic"`,
5252
`/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`,

controllers/cloudinit/controlplane_join.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type ControlPlaneJoinInput struct {
5555
RiskLevel string
5656
// DisableDefaultCNI specifies whether to use the default CNI plugin.
5757
DisableDefaultCNI bool
58+
// SnapstoreProxyScheme specifies the scheme (e.g. http or https) of the domain. Defaults to "http".
59+
SnapstoreProxyScheme string
5860
// SnapstoreProxyDomain specifies the domain of the snapstore proxy if one is to be used.
5961
SnapstoreProxyDomain string
6062
// SnapstoreProxyId specifies the snapstore proxy ID if one is to be used.
@@ -102,6 +104,10 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
102104
}
103105
installArgs := createInstallArgs(input.Confinement, input.RiskLevel, kubernetesVersion)
104106

107+
if input.SnapstoreProxyScheme == "" {
108+
input.SnapstoreProxyScheme = "http"
109+
}
110+
105111
cloudConfig := NewBaseCloudConfig()
106112
cloudConfig.WriteFiles = append(cloudConfig.WriteFiles, input.ExtraWriteFiles...)
107113
if args := input.ExtraKubeletArgs; len(args) > 0 {
@@ -123,7 +129,7 @@ func NewJoinControlPlane(input *ControlPlaneJoinInput) (*CloudConfig, error) {
123129
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
124130
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
125131
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
126-
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
132+
fmt.Sprintf("%s %q %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyScheme, input.SnapstoreProxyDomain, input.SnapstoreProxyId),
127133
scriptPath(disableHostServicesScript),
128134
fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs),
129135
fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy),

controllers/cloudinit/controlplane_join_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestControlPlaneJoin(t *testing.T) {
4444
g.Expect(cloudConfig.RunCommands).To(Equal([]string{
4545
`set -x`,
4646
`/capi-scripts/00-configure-snapstore-http-proxy.sh "" ""`,
47-
`/capi-scripts/00-configure-snapstore-proxy.sh "" ""`,
47+
`/capi-scripts/00-configure-snapstore-proxy.sh "http" "" ""`,
4848
`/capi-scripts/00-disable-host-services.sh`,
4949
`/capi-scripts/00-install-microk8s.sh "--channel 1.25 --classic"`,
5050
`/capi-scripts/10-configure-containerd-proxy.sh "" "" ""`,
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#!/bin/bash -xe
22

33
# Usage:
4-
# $0 $snapstore-domain $snapstore-id
4+
# $0 $snapstore-scheme $snapstore-domain $snapstore-id
5+
#
6+
# Arguments:
7+
# $snapstore-scheme The scheme for the domain (e.g. https or http without the ://)
8+
# $snapstore-domain The domain name (e.g. snapstore.domain.com)
9+
# $snapstore-id The store id (e.g. ID123456789)
510
#
611
# Assumptions:
712
# - snapd is installed
813

9-
if [ "$#" -ne 2 ] || [ -z "${1}" ] || [ -z "${2}" ] ; then
14+
if [ "$#" -ne 3 ] || [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ; then
1015
echo "Using the default snapstore"
1116
exit 0
1217
fi
@@ -18,12 +23,12 @@ if ! type -P curl ; then
1823
done
1924
fi
2025

21-
while ! curl -sL http://"${1}"/v2/auth/store/assertions | snap ack /dev/stdin ; do
26+
while ! curl -sL "${1}"://"${2}"/v2/auth/store/assertions | snap ack /dev/stdin ; do
2227
echo "Failed to ACK store assertions, will retry"
2328
sleep 5
2429
done
2530

26-
while ! snap set core proxy.store="${2}" ; do
27-
echo "Failed to configure snapd with stire ID, will retry"
31+
while ! snap set core proxy.store="${3}" ; do
32+
echo "Failed to configure snapd with store ID, will retry"
2833
sleep 5
2934
done

controllers/cloudinit/worker_join.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type WorkerInput struct {
4646
Confinement string
4747
// RiskLevel specifies the risk level (strict, candidate, beta, edge) for the snap channels.
4848
RiskLevel string
49+
// SnapstoreProxyScheme specifies the scheme (e.g http or https) of the domain. Defaults to http.
50+
SnapstoreProxyScheme string
4951
// SnapstoreProxyDomain specifies the domain of the snapstore proxy if one is to be used.
5052
SnapstoreProxyDomain string
5153
// SnapstoreProxyId specifies the snapstore proxy ID if one is to be used.
@@ -83,6 +85,10 @@ func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) {
8385
return nil, fmt.Errorf("strict confinement is only available for microk8s v1.25+")
8486
}
8587

88+
if input.SnapstoreProxyScheme == "" {
89+
input.SnapstoreProxyScheme = "http"
90+
}
91+
8692
stopApiServerProxyRefreshes := "no"
8793
if kubernetesVersion.Minor() > 24 {
8894
stopApiServerProxyRefreshes = "yes"
@@ -110,7 +116,7 @@ func NewJoinWorker(input *WorkerInput) (*CloudConfig, error) {
110116
cloudConfig.RunCommands = append(cloudConfig.RunCommands, input.PreRunCommands...)
111117
cloudConfig.RunCommands = append(cloudConfig.RunCommands,
112118
fmt.Sprintf("%s %q %q", scriptPath(snapstoreHTTPProxyScript), input.SnapstoreHTTPProxy, input.SnapstoreHTTPSProxy),
113-
fmt.Sprintf("%s %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyDomain, input.SnapstoreProxyId),
119+
fmt.Sprintf("%s %q %q %q", scriptPath(snapstoreProxyScript), input.SnapstoreProxyScheme, input.SnapstoreProxyDomain, input.SnapstoreProxyId),
114120
scriptPath(disableHostServicesScript),
115121
fmt.Sprintf("%s %q", scriptPath(installMicroK8sScript), installArgs),
116122
fmt.Sprintf("%s %q %q %q", scriptPath(configureContainerdProxyScript), input.ContainerdHTTPProxy, input.ContainerdHTTPSProxy, input.ContainerdNoProxy),

0 commit comments

Comments
 (0)