-
Notifications
You must be signed in to change notification settings - Fork 458
/
unique_token.go
56 lines (47 loc) · 1.91 KB
/
unique_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"context"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/controller/generic/transform"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"go.uber.org/zap"
"github.com/siderolabs/talos/internal/pkg/meta"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// UniqueMachineTokenController provides a unique token the machine.
type UniqueMachineTokenController = transform.Controller[*runtime.MetaLoaded, *runtime.UniqueMachineToken]
// NewUniqueMachineTokenController instanciates the controller.
func NewUniqueMachineTokenController() *UniqueMachineTokenController {
return transform.NewController(
transform.Settings[*runtime.MetaLoaded, *runtime.UniqueMachineToken]{
Name: "runtime.UniqueMachineTokenController",
MapMetadataFunc: func(in *runtime.MetaLoaded) *runtime.UniqueMachineToken {
return runtime.NewUniqueMachineToken()
},
TransformFunc: func(ctx context.Context, r controller.Reader, logger *zap.Logger, _ *runtime.MetaLoaded, out *runtime.UniqueMachineToken) error {
uniqueToken, err := safe.ReaderGetByID[*runtime.MetaKey](ctx, r, runtime.MetaKeyTagToID(meta.UniqueMachineToken))
if state.IsNotFoundError(err) {
out.TypedSpec().Token = ""
return nil
} else if err != nil {
return err
}
out.TypedSpec().Token = uniqueToken.TypedSpec().Value
return nil
},
},
transform.WithExtraInputs(
controller.Input{
Namespace: runtime.NamespaceName,
Type: runtime.MetaKeyType,
ID: optional.Some(runtime.MetaKeyTagToID(meta.UniqueMachineToken)),
Kind: controller.InputWeak,
},
),
)
}