Skip to content

Commit 8c7fec8

Browse files
committed
fix: don't write incomplete <cluster>-ca secret for configtype none
Fixes #97 Skip creating the secret if the supplied user config doesn't have full cluster CA. Cluster secret will be created once the controlplane machine configuration is passed in. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent f46c83d commit 8c7fec8

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

controllers/talosconfig_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,11 @@ func (r *TalosConfigReconciler) userConfigs(ctx context.Context, scope *TalosCon
324324
}
325325

326326
// Create the secret with kubernetes certs so a kubeconfig can be generated
327-
if err = r.writeK8sCASecret(ctx, scope, userConfig.Cluster().CA()); err != nil {
328-
return retBundle, err
327+
// but do this only when machineconfig contains full Kubernetes CA secret (controlplane nodes)
328+
if userConfig.Cluster().CA() != nil && len(userConfig.Cluster().CA().Crt) > 0 && len(userConfig.Cluster().CA().Key) > 0 {
329+
if err = r.writeK8sCASecret(ctx, scope, userConfig.Cluster().CA()); err != nil {
330+
return retBundle, err
331+
}
329332
}
330333

331334
userConfigStr, err := userConfig.String()

internal/integration/integration_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
1313
bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
14+
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/generate"
1415
talosmachine "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
1516
corev1 "k8s.io/api/core/v1"
1617
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -218,6 +219,84 @@ func TestIntegration(t *testing.T) {
218219
assert.Equal(t, "-----BEGIN CERTIFICATE-----\nMIIBPzCB8qADAgECAhEArv8iYjWXC8Mataa8e2pezDAFBgMrZXAwEDEOMAwGA1UE\nChMFdGFsb3MwHhcNMjEwOTIwMTg0MTQ5WhcNMzEwOTE4MTg0MTQ5WjAQMQ4wDAYD\nVQQKEwV0YWxvczAqMAUGAytlcAMhAOCRMlGNjsdQmgls2PCSgMdMeAIB8fAKsnCp\naXX3rfUKo2EwXzAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYBBQUHAwEG\nCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFIDgT1HeMDtWHHXl\nmVhYqUPDU0JoMAUGAytlcANBAD2GLO2vG9MHGxt9658X4xZLSYNldAgDy2tHmZ7l\nnAjAR0npZoQXBVhorrQEcea7g6To9BDmtzrF0StW895d0Ak=\n-----END CERTIFICATE-----\n", string(provider.Machine().Security().CA().Crt))
219220
})
220221

222+
t.Run("ConfigTypeNone", func(t *testing.T) {
223+
t.Parallel()
224+
225+
namespaceName := setupTest(ctx, t, c)
226+
cluster := createCluster(ctx, t, c, namespaceName, nil)
227+
228+
secretsBundle, err := generate.NewSecretsBundle(generate.NewClock())
229+
require.NoError(t, err)
230+
231+
input, err := generate.NewInput(cluster.Name, "https://example.com:6443/", "v1.22.2", secretsBundle)
232+
require.NoError(t, err)
233+
234+
workers := []*bootstrapv1alpha3.TalosConfig{}
235+
236+
for i := 0; i < 4; i++ {
237+
machine := createMachine(ctx, t, c, cluster)
238+
239+
machineconfig, err := generate.Config(talosmachine.TypeWorker, input)
240+
require.NoError(t, err)
241+
242+
configdata, err := machineconfig.Bytes()
243+
require.NoError(t, err)
244+
245+
workers = append(workers, createTalosConfig(ctx, t, c, machine, bootstrapv1alpha3.TalosConfigSpec{
246+
GenerateType: "none",
247+
Data: string(configdata),
248+
}))
249+
}
250+
251+
controlplanes := []*bootstrapv1alpha3.TalosConfig{}
252+
253+
for i := 0; i < 3; i++ {
254+
machine := createMachine(ctx, t, c, cluster)
255+
256+
machineType := talosmachine.TypeInit
257+
258+
if i > 0 {
259+
machineType = talosmachine.TypeControlPlane
260+
}
261+
262+
machineconfig, err := generate.Config(machineType, input)
263+
require.NoError(t, err)
264+
265+
configdata, err := machineconfig.Bytes()
266+
require.NoError(t, err)
267+
268+
controlplanes = append(controlplanes, createTalosConfig(ctx, t, c, machine, bootstrapv1alpha3.TalosConfigSpec{
269+
GenerateType: "none",
270+
Data: string(configdata),
271+
}))
272+
}
273+
274+
for i, talosConfig := range append(append([]*bootstrapv1alpha3.TalosConfig{}, controlplanes...), workers...) {
275+
waitForReady(ctx, t, c, talosConfig)
276+
277+
// Note, for config type none we don't generate talosconfig (why?)
278+
279+
provider := assertMachineConfiguration(ctx, t, c, talosConfig)
280+
281+
switch {
282+
case i == 0:
283+
assert.Equal(t, talosmachine.TypeInit, provider.Machine().Type())
284+
case i < len(controlplanes):
285+
assert.Equal(t, talosmachine.TypeControlPlane, provider.Machine().Type())
286+
default:
287+
assert.Equal(t, talosmachine.TypeWorker, provider.Machine().Type())
288+
}
289+
}
290+
291+
assertClusterCA(ctx, t, c, cluster, assertMachineConfiguration(ctx, t, c, controlplanes[0]))
292+
293+
// compare control plane secrets completely
294+
assertSameMachineConfigSecrets(ctx, t, c, controlplanes...)
295+
296+
// compare all configs in more relaxed mode
297+
assertCompatibleMachineConfigs(ctx, t, c, append(append([]*bootstrapv1alpha3.TalosConfig{}, controlplanes...), workers...)...)
298+
})
299+
221300
}
222301

223302
// legacy cluster secret format

0 commit comments

Comments
 (0)