Skip to content

Commit

Permalink
feat: rename namespaces, resources, types etc
Browse files Browse the repository at this point in the history
See cosi-project/runtime#12 for new mnaming
conventions.

No functional changes.

Additionally implements printing extra columns in `talosctl get xyz`.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Mar 2, 2021
1 parent 3a2caca commit 60aa011
Show file tree
Hide file tree
Showing 43 changed files with 524 additions and 285 deletions.
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/apply-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"crypto/tls"
"fmt"
"io/ioutil"
"strings"

"github.com/spf13/cobra"
"github.com/talos-systems/crypto/x509"

"github.com/talos-systems/talos/internal/pkg/tui/installer"
machineapi "github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/client"
"github.com/talos-systems/talos/pkg/resources/config"
)

var applyConfigCmdFlags struct {
Expand Down Expand Up @@ -44,7 +44,7 @@ var applyConfigCmd = &cobra.Command{
if len(args) > 0 {
cmd.Help() //nolint:errcheck

if args[0] != config.Type {
if args[0] != "config" && !strings.EqualFold(args[0], "machineconfig") {
return fmt.Errorf("unknown positional argument %s", args[0])
} else if cmd.CalledAs() == "apply-config" {
return fmt.Errorf("expected no positional arguments")
Expand Down
17 changes: 9 additions & 8 deletions cmd/talosctl/cmd/talos/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var editCmd = &cobra.Command{
Use: "edit <type> [<id>]",
Short: "Edit a resource from the default editor.",
Args: cobra.RangeArgs(1, 2),
Long: `The edit command allows you to directly edit any API resource
you can retrieve via the command line tools.
Long: `The edit command allows you to directly edit any API resource
you can retrieve via the command line tools.
It will open the editor defined by your TALOS_EDITOR,
or EDITOR environment variables, or fall back to 'vi' for Linux
It will open the editor defined by your TALOS_EDITOR,
or EDITOR environment variables, or fall back to 'vi' for Linux
or 'notepad' for Windows.`,
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
Expand All @@ -48,14 +48,15 @@ or 'notepad' for Windows.`,
})

resourceType := args[0]
if resourceType != config.Type {
return fmt.Errorf("only the config resource can be edited")
}

var lastError string

editFn := func(parentCtx context.Context, msg client.ResourceResponse) error {
if msg.Resource == nil {
if msg.Definition.Metadata().ID() != strings.ToLower(config.MachineConfigType) {
return fmt.Errorf("only the machineconfig resource can be edited")
}

return nil
}

Expand All @@ -78,7 +79,7 @@ or 'notepad' for Windows.`,

_, err := w.Write([]byte(
fmt.Sprintf(
"# Editing %s/%s at node %s\n", resourceType, id, msg.Metadata.GetHostname(),
"# Editing %s/%s at node %s\n", msg.Resource.Metadata().Type(), id, msg.Metadata.GetHostname(),
),
))
if err != nil {
Expand Down
48 changes: 44 additions & 4 deletions cmd/talosctl/cmd/talos/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
package output

import (
"bytes"
"fmt"
"os"
"strings"
"text/tabwriter"

"github.com/talos-systems/os-runtime/pkg/resource"
"github.com/talos-systems/os-runtime/pkg/state"
"k8s.io/client-go/util/jsonpath"
)

// Table outputs resources in Table view.
type Table struct {
w tabwriter.Writer
withEvents bool
w tabwriter.Writer
withEvents bool
displayType string
dynamicColumns []dynamicColumn
}

type dynamicColumn func(value interface{}) (string, error)

// NewTable initializes table resource output.
func NewTable() *Table {
output := &Table{}
Expand All @@ -31,13 +37,38 @@ func NewTable() *Table {
// WriteHeader implements output.Writer interface.
func (table *Table) WriteHeader(definition resource.Resource, withEvents bool) error {
table.withEvents = withEvents

fields := []string{"NAMESPACE", "TYPE", "ID", "VERSION"}

if withEvents {
fields = append([]string{"*"}, fields...)
}

resourceDefinitionSpec := definition.(*resource.Any).Value().(map[string]interface{}) //nolint: errcheck

table.displayType = resourceDefinitionSpec["displayType"].(string) //nolint: errcheck

for _, col := range resourceDefinitionSpec["printColumns"].([]interface{}) {
column := col.(map[string]interface{}) //nolint: errcheck
name := column["name"].(string) //nolint: errcheck

fields = append(fields, strings.ToUpper(name))

expr := jsonpath.New(name)
if err := expr.Parse(column["jsonPath"].(string)); err != nil {
return fmt.Errorf("error parsing column %q jsonpath: %w", name, err)
}

table.dynamicColumns = append(table.dynamicColumns, func(val interface{}) (string, error) {
var buf bytes.Buffer

if e := expr.Execute(&buf, val); e != nil {
return "", e
}

return buf.String(), nil
})
}

fields = append([]string{"NODE"}, fields...)

_, err := fmt.Fprintln(&table.w, strings.Join(fields, "\t"))
Expand All @@ -47,7 +78,7 @@ func (table *Table) WriteHeader(definition resource.Resource, withEvents bool) e

// WriteResource implements output.Writer interface.
func (table *Table) WriteResource(node string, r resource.Resource, event state.EventType) error {
values := []string{r.Metadata().Namespace(), r.Metadata().Type(), r.Metadata().ID(), r.Metadata().Version().String()}
values := []string{r.Metadata().Namespace(), table.displayType, r.Metadata().ID(), r.Metadata().Version().String()}

if table.withEvents {
var label string
Expand All @@ -64,6 +95,15 @@ func (table *Table) WriteResource(node string, r resource.Resource, event state.
values = append([]string{label}, values...)
}

for _, dynamicColumn := range table.dynamicColumns {
value, err := dynamicColumn(r.(*resource.Any).Value())
if err != nil {
return err
}

values = append(values, value)
}

values = append([]string{node}, values...)

_, err := fmt.Fprintln(&table.w, strings.Join(values, "\t"))
Expand Down
9 changes: 5 additions & 4 deletions cmd/talosctl/cmd/talos/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,10 +38,6 @@ var patchCmd = &cobra.Command{
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
if args[0] != config.Type {
return fmt.Errorf("only the config resource can be patched")
}

var (
patch jsonpatch.Patch
patchData []byte
Expand Down Expand Up @@ -70,6 +67,10 @@ var patchCmd = &cobra.Command{

patchFn := func(parentCtx context.Context, msg client.ResourceResponse) error {
if msg.Resource == nil {
if msg.Definition.Metadata().ID() != strings.ToLower(config.MachineConfigType) {
return fmt.Errorf("only the machineconfig resource can be edited")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require (
github.com/talos-systems/go-smbios v0.0.0-20200807005123-80196199691e
github.com/talos-systems/grpc-proxy v0.2.0
github.com/talos-systems/net v0.2.1-0.20210212213224-05190541b0fa
github.com/talos-systems/os-runtime v0.0.0-20210216141502-28dd9aaf98d6
github.com/talos-systems/os-runtime v0.0.0-20210302191935-8b3f192ecca2
github.com/talos-systems/talos/pkg/machinery v0.0.0-20200818212414-6a7cc0264819
github.com/u-root/u-root v7.0.0+incompatible
github.com/vmware-tanzu/sonobuoy v0.20.0
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591 h1:0WWUDZ1oxq7NxVyGo8M3KI5jbkiwNAdZFFzAdC68up4=
github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA=
github.com/gertd/go-pluralize v0.1.7 h1:RgvJTJ5W7olOoAks97BOwOlekBFsLEyh00W48Z6ZEZY=
github.com/gertd/go-pluralize v0.1.7/go.mod h1:O4eNeeIf91MHh1GJ2I47DNtaesm66NYvjYgAahcqSDQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -877,6 +879,7 @@ github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b h1:gQZ0qzfKHQIybL
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sethgrid/pester v0.0.0-20190127155807-68a33a018ad0 h1:X9XMOYjxEfAYSy3xK1DzO5dMkkWhs9E9UCcS1IERx2k=
github.com/sethgrid/pester v0.0.0-20190127155807-68a33a018ad0/go.mod h1:Ad7IjTpvzZO8Fl0vh9AzQ+j/jYZfyp2diGwI8m5q+ns=
Expand Down Expand Up @@ -969,8 +972,8 @@ github.com/talos-systems/grpc-proxy v0.2.0 h1:DN75bLfaW4xfhq0r0mwFRnfGhSB+HPhK1L
github.com/talos-systems/grpc-proxy v0.2.0/go.mod h1:sm97Vc/z2cok3pu6ruNeszQej4KDxFrDgfWs4C1mtC4=
github.com/talos-systems/net v0.2.1-0.20210212213224-05190541b0fa h1:XqOMTt0Q6mjsk8Dea5wUpgcdtf+AzesH11m4AozWSxw=
github.com/talos-systems/net v0.2.1-0.20210212213224-05190541b0fa/go.mod h1:VreSAyRmxMtqussAHSKMKkJQa1YwBTSVfkmE4Jydam4=
github.com/talos-systems/os-runtime v0.0.0-20210216141502-28dd9aaf98d6 h1:a3QXMKocLKRDRYn8iBX02wMZGQBH2XgE3EBhYHF4xws=
github.com/talos-systems/os-runtime v0.0.0-20210216141502-28dd9aaf98d6/go.mod h1:+E9CUVoYpReh0nhOEvFpy7pwLiyq0700WF03I06giyk=
github.com/talos-systems/os-runtime v0.0.0-20210302191935-8b3f192ecca2 h1:uWBn8UTaAN6oekZGSKuYhmg6Bha5Lg/T08ApLyjgRTQ=
github.com/talos-systems/os-runtime v0.0.0-20210302191935-8b3f192ecca2/go.mod h1:Z+1phKVJ0IWH+Jd2DGufL8WKqxd3xt1xlcsxcU18ZL0=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
56 changes: 35 additions & 21 deletions internal/app/machined/internal/server/v1alpha1/v1alpha1_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package runtime
import (
"context"
"fmt"
"strings"

"github.com/talos-systems/os-runtime/pkg/resource"
"github.com/talos-systems/os-runtime/pkg/resource/core"
"github.com/talos-systems/os-runtime/pkg/resource/meta"
"github.com/talos-systems/os-runtime/pkg/state"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -58,50 +59,63 @@ type resourceKind struct {
Type resource.Type
}

func (s *ResourceServer) resolveResourceKind(ctx context.Context, kind *resourceKind) (*core.ResourceDefinition, error) {
//nolint: gocyclo
func (s *ResourceServer) resolveResourceKind(ctx context.Context, kind *resourceKind) (*meta.ResourceDefinition, error) {
registeredResources, err := s.server.Controller.Runtime().State().V1Alpha2().Resources().
List(ctx, resource.NewMetadata(core.NamespaceName, core.ResourceDefinitionType, "", resource.VersionUndefined))
List(ctx, resource.NewMetadata(meta.NamespaceName, meta.ResourceDefinitionType, "", resource.VersionUndefined))
if err != nil {
return nil, err
}

matched := []*meta.ResourceDefinition{}

for _, item := range registeredResources.Items {
resourceDefinition, ok := item.(*core.ResourceDefinition)
resourceDefinition, ok := item.(*meta.ResourceDefinition)
if !ok {
return nil, fmt.Errorf("unexpected resource definition type")
}

spec := resourceDefinition.Spec().(core.ResourceDefinitionSpec) //nolint: errcheck
if strings.EqualFold(resourceDefinition.Metadata().ID(), kind.Type) {
matched = append(matched, resourceDefinition)

continue
}

spec := resourceDefinition.Spec().(meta.ResourceDefinitionSpec) //nolint: errcheck

matches := resourceDefinition.Metadata().ID() == kind.Type || spec.Type == kind.Type
if !matches {
for _, alias := range spec.Aliases {
if alias == kind.Type {
matches = true
for _, alias := range spec.Aliases {
if strings.EqualFold(alias, kind.Type) {
matched = append(matched, resourceDefinition)

break
}
break
}
}
}

if !matches {
continue
switch {
case len(matched) == 1:
kind.Type = matched[0].Spec().(meta.ResourceDefinitionSpec).Type

if kind.Namespace == "" {
kind.Namespace = matched[0].Spec().(meta.ResourceDefinitionSpec).DefaultNamespace
}

kind.Type = resourceDefinition.Metadata().ID()
return matched[0], nil
case len(matched) > 1:
matchedTypes := make([]string, len(matched))

if kind.Namespace == "" {
kind.Namespace = spec.DefaultNamespace
for i := range matched {
matchedTypes[i] = matched[i].Metadata().ID()
}

return resourceDefinition, nil
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("resource type %q is ambiguous: %v", kind.Type, matchedTypes))
default:
return nil, status.Error(codes.NotFound, fmt.Sprintf("resource %q is not registered", kind.Type))
}

return nil, status.Error(codes.NotFound, fmt.Sprintf("resource %q is not registered", kind.Type))
}

func (s *ResourceServer) checkReadAccess(ctx context.Context, kind *resourceKind) error {
registeredNamespaces, err := s.server.Controller.Runtime().State().V1Alpha2().Resources().List(ctx, resource.NewMetadata(core.NamespaceName, core.NamespaceType, "", resource.VersionUndefined))
registeredNamespaces, err := s.server.Controller.Runtime().State().V1Alpha2().Resources().List(ctx, resource.NewMetadata(meta.NamespaceName, meta.NamespaceType, "", resource.VersionUndefined))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Run
if err := r.UpdateDependencies([]controller.Dependency{
{
Namespace: config.NamespaceName,
Type: config.V1Alpha1Type,
Type: config.MachineConfigType,
ID: pointer.ToString(config.V1Alpha1ID),
Kind: controller.DependencyWeak,
},
Expand All @@ -65,7 +65,7 @@ func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Run
case <-r.EventCh():
}

cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.V1Alpha1Type, config.V1Alpha1ID, resource.VersionUndefined))
cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.MachineConfigType, config.V1Alpha1ID, resource.VersionUndefined))
if err != nil {
if state.IsNotFoundError(err) {
if err = ctrl.teardownAll(ctx, r, logger); err != nil {
Expand All @@ -78,7 +78,7 @@ func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Run
return fmt.Errorf("error getting config: %w", err)
}

cfgProvider := cfg.(*config.V1Alpha1).Config()
cfgProvider := cfg.(*config.MachineConfig).Config()

machineTypeRes, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.MachineTypeType, config.MachineTypeID, resource.VersionUndefined))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (suite *K8sControlPlaneSuite) TestReconcileDefaults() {
u, err := url.Parse("https://foo:6443")
suite.Require().NoError(err)

cfg := config.NewV1Alpha1(&v1alpha1.Config{
cfg := config.NewMachineConfig(&v1alpha1.Config{
ConfigVersion: "v1alpha1",
MachineConfig: &v1alpha1.MachineConfig{},
ClusterConfig: &v1alpha1.ClusterConfig{
Expand Down Expand Up @@ -131,7 +131,7 @@ func (suite *K8sControlPlaneSuite) TestReconcileExtraVolumes() {
u, err := url.Parse("https://foo:6443")
suite.Require().NoError(err)

cfg := config.NewV1Alpha1(&v1alpha1.Config{
cfg := config.NewMachineConfig(&v1alpha1.Config{
ConfigVersion: "v1alpha1",
MachineConfig: &v1alpha1.MachineConfig{},
ClusterConfig: &v1alpha1.ClusterConfig{
Expand Down
6 changes: 3 additions & 3 deletions internal/app/machined/pkg/controllers/config/machine_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (ctrl *MachineTypeController) Run(ctx context.Context, r controller.Runtime
if err := r.UpdateDependencies([]controller.Dependency{
{
Namespace: config.NamespaceName,
Type: config.V1Alpha1Type,
Type: config.MachineConfigType,
ID: pointer.ToString(config.V1Alpha1ID),
Kind: controller.DependencyWeak,
},
Expand All @@ -54,13 +54,13 @@ func (ctrl *MachineTypeController) Run(ctx context.Context, r controller.Runtime

var machineType machine.Type

cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.V1Alpha1Type, config.V1Alpha1ID, resource.VersionUndefined))
cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.MachineConfigType, config.V1Alpha1ID, resource.VersionUndefined))
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting config: %w", err)
}
} else {
machineType = cfg.(*config.V1Alpha1).Config().Machine().Type()
machineType = cfg.(*config.MachineConfig).Config().Machine().Type()
}

if err = r.Update(ctx, config.NewMachineType(), func(r resource.Resource) error {
Expand Down

0 comments on commit 60aa011

Please sign in to comment.