-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement APIs to write to META
This allows to put keys to META partition. META contents can be viewed with `talosctl get metakeys`. There is not real usecase for it yet, but the next PRs will introduce two special keys which can be written: * platform network config for `metal` * `${code}` variable Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
- Loading branch information
Showing
12 changed files
with
2,424 additions
and
570 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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 talos | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/client" | ||
) | ||
|
||
var metaCmd = &cobra.Command{ | ||
Use: "meta", | ||
Short: "Write and delete keys in the META partition", | ||
Long: ``, | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
var metaWriteCmd = &cobra.Command{ | ||
Use: "write key value", | ||
Short: "Write a key-value pair to the META partition.", | ||
Long: ``, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
key, err := strconv.ParseUint(args[0], 0, 8) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.MetaWrite(ctx, uint8(key), []byte(args[1])) | ||
}) | ||
}, | ||
} | ||
|
||
var metaDeleteCmd = &cobra.Command{ | ||
Use: "delete key", | ||
Short: "Delete a key from the META partition.", | ||
Long: ``, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
key, err := strconv.ParseUint(args[0], 0, 8) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.MetaDelete(ctx, uint8(key)) | ||
}) | ||
}, | ||
} | ||
|
||
func init() { | ||
metaCmd.AddCommand(metaWriteCmd) | ||
metaCmd.AddCommand(metaDeleteCmd) | ||
addCommand(metaCmd) | ||
} |
82 changes: 82 additions & 0 deletions
82
internal/app/machined/internal/server/v1alpha1/v1alpha1_meta.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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" | ||
"os" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime" | ||
"github.com/siderolabs/talos/pkg/machinery/api/machine" | ||
) | ||
|
||
// MetaWrite implements the machine.MachineServer interface. | ||
func (s *Server) MetaWrite(ctx context.Context, req *machine.MetaWriteRequest) (*machine.MetaWriteResponse, error) { | ||
if err := s.checkSupported(runtime.MetaKV); err != nil { | ||
return nil, err | ||
} | ||
|
||
if uint32(uint8(req.Key)) != req.Key { | ||
return nil, status.Errorf(codes.InvalidArgument, "key must be a uint8") | ||
} | ||
|
||
ok, err := s.Controller.Runtime().State().Machine().Meta().SetTagBytes(ctx, uint8(req.Key), req.Value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !ok { | ||
// META overflowed | ||
return nil, status.Errorf(codes.ResourceExhausted, "meta write failed") | ||
} | ||
|
||
err = s.Controller.Runtime().State().Machine().Meta().Flush() | ||
if err != nil && !os.IsNotExist(err) { | ||
// ignore not exist error, as it's possible that the meta partition is not created yet | ||
return nil, err | ||
} | ||
|
||
return &machine.MetaWriteResponse{ | ||
Messages: []*machine.MetaWrite{ | ||
{}, | ||
}, | ||
}, nil | ||
} | ||
|
||
// MetaDelete implements the machine.MachineServer interface. | ||
func (s *Server) MetaDelete(ctx context.Context, req *machine.MetaDeleteRequest) (*machine.MetaDeleteResponse, error) { | ||
if err := s.checkSupported(runtime.MetaKV); err != nil { | ||
return nil, err | ||
} | ||
|
||
if uint32(uint8(req.Key)) != req.Key { | ||
return nil, status.Errorf(codes.InvalidArgument, "key must be a uint8") | ||
} | ||
|
||
ok, err := s.Controller.Runtime().State().Machine().Meta().DeleteTag(ctx, uint8(req.Key)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !ok { | ||
// META key not found | ||
return nil, status.Errorf(codes.NotFound, "meta key not found") | ||
} | ||
|
||
err = s.Controller.Runtime().State().Machine().Meta().Flush() | ||
if err != nil && !os.IsNotExist(err) { | ||
// ignore not exist error, as it's possible that the meta partition is not created yet | ||
return nil, err | ||
} | ||
|
||
return &machine.MetaDeleteResponse{ | ||
Messages: []*machine.MetaDelete{ | ||
{}, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// 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/. | ||
|
||
//go:build integration_cli | ||
|
||
package cli | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/siderolabs/talos/internal/integration/base" | ||
) | ||
|
||
// MetaSuite verifies meta sub-commands. | ||
type MetaSuite struct { | ||
base.CLISuite | ||
} | ||
|
||
// SuiteName ... | ||
func (suite *MetaSuite) SuiteName() string { | ||
return "cli.MetaSuite" | ||
} | ||
|
||
// TestKey writes a META key, deletes it, verifies via resources. | ||
func (suite *MetaSuite) TestKey() { | ||
node := suite.RandomDiscoveredNodeInternalIP() | ||
|
||
// detect docker platform and skip the test | ||
stdout, _ := suite.RunCLI([]string{"--nodes", node, "get", "platformmetadata"}) | ||
if strings.Contains(stdout, "container") { | ||
suite.T().Skip("skipping on container platform") | ||
} | ||
|
||
key := "0x05" // unused/reserved key | ||
value := uuid.New().String() | ||
|
||
suite.RunCLI([]string{"--nodes", node, "meta", "write", key, value}, | ||
base.StdoutEmpty()) | ||
|
||
suite.RunCLI([]string{"--nodes", node, "get", "metakeys", key}, | ||
base.StdoutShouldMatch(regexp.MustCompile(key)), | ||
base.StdoutShouldMatch(regexp.MustCompile(value)), | ||
) | ||
|
||
suite.RunCLI([]string{"--nodes", node, "meta", "delete", key}, | ||
base.StdoutEmpty()) | ||
|
||
suite.RunCLI([]string{"--nodes", node, "get", "metakeys", key}, | ||
base.ShouldFail(), | ||
base.StderrShouldMatch(regexp.MustCompile("NotFound")), | ||
) | ||
} | ||
|
||
func init() { | ||
allSuites = append(allSuites, new(MetaSuite)) | ||
} |
Oops, something went wrong.