Skip to content

Commit 977121a

Browse files
committed
fix: construct properly data secret name
The one provided in the config owner spec is only used for pivoting. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent f8c75c8 commit 977121a

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

controllers/secrets.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,41 +154,39 @@ func (r *TalosConfigReconciler) writeK8sCASecret(ctx context.Context, scope *Tal
154154
}
155155

156156
// writeBootstrapData creates a new secret with the data passed in as input
157-
func (r *TalosConfigReconciler) writeBootstrapData(ctx context.Context, scope *TalosConfigScope, data []byte) error {
158-
// Create ca secret only if it doesn't already exist
157+
func (r *TalosConfigReconciler) writeBootstrapData(ctx context.Context, scope *TalosConfigScope, data []byte) (string, error) {
158+
// Create bootstrap secret only if it doesn't already exist
159159
ownerName := scope.ConfigOwner.GetName()
160+
dataSecretName := ownerName + "-bootstrap-data"
161+
162+
r.Log.Info("handling bootstrap data for ", "owner", ownerName)
160163

161-
if scope.ConfigOwner.DataSecretName() == nil {
162-
return fmt.Errorf("config owner data secret name is nil")
164+
_, err := r.fetchSecret(ctx, scope.Config, dataSecretName)
165+
if err == nil {
166+
return dataSecretName, nil
163167
}
164168

165-
r.Log.Info("handling bootstrap data for ", "owner", ownerName)
169+
if err != nil && !k8serrors.IsNotFound(err) {
170+
return dataSecretName, err
171+
}
166172

167-
_, err := r.fetchSecret(ctx, scope.Config, *scope.ConfigOwner.DataSecretName())
168-
if k8serrors.IsNotFound(err) {
169-
certSecret := &corev1.Secret{
170-
ObjectMeta: metav1.ObjectMeta{
171-
Namespace: scope.Config.Namespace,
172-
Name: *scope.ConfigOwner.DataSecretName(),
173-
Labels: map[string]string{
174-
capiv1.ClusterLabelName: scope.Cluster.Name,
175-
},
176-
OwnerReferences: []metav1.OwnerReference{
177-
*metav1.NewControllerRef(scope.Config, bootstrapv1alpha3.GroupVersion.WithKind("TalosConfig")),
178-
},
173+
certSecret := &corev1.Secret{
174+
ObjectMeta: metav1.ObjectMeta{
175+
Namespace: scope.Config.Namespace,
176+
Name: dataSecretName,
177+
Labels: map[string]string{
178+
capiv1.ClusterLabelName: scope.Cluster.Name,
179179
},
180-
Data: map[string][]byte{
181-
"value": data,
180+
OwnerReferences: []metav1.OwnerReference{
181+
*metav1.NewControllerRef(scope.Config, bootstrapv1alpha3.GroupVersion.WithKind("TalosConfig")),
182182
},
183-
}
184-
185-
err = r.Client.Create(ctx, certSecret)
186-
if err != nil {
187-
return err
188-
}
189-
} else if err != nil {
190-
return err
183+
},
184+
Data: map[string][]byte{
185+
"value": data,
186+
},
191187
}
192188

193-
return nil
189+
err = r.Client.Create(ctx, certSecret)
190+
191+
return dataSecretName, err
194192
}

controllers/talosconfig_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ func (r *TalosConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr
197197
return ctrl.Result{}, errors.New("infra not ready")
198198
}
199199

200+
// Reconcile status for machines that already have a secret reference, but our status isn't up to date.
201+
// This case solves the pivoting scenario (or a backup restore) which doesn't preserve the status subresource on objects.
202+
if owner.DataSecretName() != nil && (!config.Status.Ready || config.Status.DataSecretName == nil) {
203+
config.Status.Ready = true
204+
config.Status.DataSecretName = owner.DataSecretName()
205+
206+
return ctrl.Result{}, nil
207+
}
208+
200209
tcScope := &TalosConfigScope{
201210
Config: config,
202211
ConfigOwner: owner,
@@ -270,12 +279,14 @@ func (r *TalosConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr
270279
}
271280
}
272281

273-
err = r.writeBootstrapData(ctx, tcScope, []byte(retData.BootstrapData))
282+
var dataSecretName string
283+
284+
dataSecretName, err = r.writeBootstrapData(ctx, tcScope, []byte(retData.BootstrapData))
274285
if err != nil {
275286
return ctrl.Result{}, err
276287
}
277288

278-
config.Status.DataSecretName = tcScope.ConfigOwner.DataSecretName()
289+
config.Status.DataSecretName = &dataSecretName
279290
config.Status.TalosConfig = retData.TalosConfig
280291
config.Status.Ready = true
281292

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,6 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
432432
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
433433
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
434434
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
435-
github.com/smira/talos/pkg/machinery v0.0.0-20210920195258-7e63e43eb399 h1:4eO8ltJZZTUOtWGbGi6nKSylWuYC65dSEICHkQqHnDc=
436-
github.com/smira/talos/pkg/machinery v0.0.0-20210920195258-7e63e43eb399/go.mod h1:qX77JMZawrDTQaJucqecdlFsHy+dbnZ9YL8Kw4qL7d4=
437435
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
438436
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
439437
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
@@ -479,8 +477,6 @@ github.com/talos-systems/go-retry v0.1.1-0.20201113203059-8c63d290a688/go.mod h1
479477
github.com/talos-systems/go-retry v0.3.1/go.mod h1:HiXQqyVStZ35uSY/MTLWVvQVmC3lIW2MS5VdDaMtoKM=
480478
github.com/talos-systems/net v0.3.0 h1:TG6PoiNdg9NmSeSjyecSgguUXzoJ8wp5a8RYlIdkq3Y=
481479
github.com/talos-systems/net v0.3.0/go.mod h1:VreSAyRmxMtqussAHSKMKkJQa1YwBTSVfkmE4Jydam4=
482-
github.com/talos-systems/talos/pkg/machinery v0.12.2 h1:pLRVkJ1Xa1rrVUsqJ0RccL0c2q9It268wwBV4cvg8kk=
483-
github.com/talos-systems/talos/pkg/machinery v0.12.2/go.mod h1:qX77JMZawrDTQaJucqecdlFsHy+dbnZ9YL8Kw4qL7d4=
484480
github.com/talos-systems/talos/pkg/machinery v0.12.3-0.20210920195258-7e63e43eb399 h1:mmQ/XAV9xRm3chHx/f4xBZH4I2T960fJh4chkedW+nY=
485481
github.com/talos-systems/talos/pkg/machinery v0.12.3-0.20210920195258-7e63e43eb399/go.mod h1:qX77JMZawrDTQaJucqecdlFsHy+dbnZ9YL8Kw4qL7d4=
486482
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=

internal/integration/helpers_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ import (
1414
"testing"
1515
"time"
1616

17-
"github.com/AlekSi/pointer"
1817
"github.com/stretchr/testify/assert"
1918
"github.com/stretchr/testify/require"
2019
talosclient "github.com/talos-systems/talos/pkg/machinery/client"
2120
talosclientconfig "github.com/talos-systems/talos/pkg/machinery/client/config"
2221
machineconfig "github.com/talos-systems/talos/pkg/machinery/config"
22+
corev1 "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/types"
2525
"k8s.io/client-go/kubernetes/scheme"
2626
capiv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
27-
bsutil "sigs.k8s.io/cluster-api/bootstrap/util"
2827
"sigs.k8s.io/controller-runtime/pkg/client"
2928
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3029

@@ -97,7 +96,6 @@ func createMachine(ctx context.Context, t *testing.T, c client.Client, cluster *
9796
t.Helper()
9897

9998
machineName := generateName(t, "machine")
100-
dataSecretName := fmt.Sprintf("%s-bootstrap-data", machineName)
10199
machine := &capiv1.Machine{
102100
ObjectMeta: metav1.ObjectMeta{
103101
Namespace: cluster.Namespace,
@@ -106,7 +104,10 @@ func createMachine(ctx context.Context, t *testing.T, c client.Client, cluster *
106104
Spec: capiv1.MachineSpec{
107105
ClusterName: cluster.Name,
108106
Bootstrap: capiv1.Bootstrap{
109-
DataSecretName: pointer.ToString(dataSecretName),
107+
ConfigRef: &corev1.ObjectReference{
108+
Kind: "TalosConfig",
109+
APIVersion: bootstrapv1alpha3.GroupVersion.String(),
110+
},
110111
},
111112
},
112113
}
@@ -166,12 +167,6 @@ func waitForReady(ctx context.Context, t *testing.T, c client.Client, talosConfi
166167
t.Log("Waiting ...")
167168
sleepCtx(ctx, 3*time.Second)
168169
}
169-
170-
owner, err := bsutil.GetConfigOwner(ctx, c, talosConfig)
171-
require.NoError(t, err)
172-
173-
assert.Equal(t, pointer.GetString(owner.DataSecretName()), pointer.GetString(talosConfig.Status.DataSecretName), "%+v", talosConfig)
174-
175170
}
176171

177172
// validateClientConfig validates talosctl configuration.

0 commit comments

Comments
 (0)