Skip to content

Commit

Permalink
refactor: move networkd, timed APIs to machined, remove routerd
Browse files Browse the repository at this point in the history
This moves implementation of the user-facing APIs to the machined, and
as now all the APIs are implemented by machined, remove routerd and
adjust apid to proxy to machined.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Mar 24, 2021
1 parent 6ffabe5 commit b0209fd
Show file tree
Hide file tree
Showing 29 changed files with 378 additions and 682 deletions.
1 change: 0 additions & 1 deletion .conform.yaml
Expand Up @@ -29,7 +29,6 @@ policies:
- timed
- talosctl
- trustd
- routerd
- talosctl
- kernel
- security
Expand Down
15 changes: 1 addition & 14 deletions Dockerfile
Expand Up @@ -213,18 +213,6 @@ WORKDIR /src/internal/app/trustd
RUN --mount=type=cache,target=/.cache/go-build go build -ldflags "-s -w -X ${VERSION_PKG}.Name=Server -X ${VERSION_PKG}.SHA=${SHA} -X ${VERSION_PKG}.Tag=${TAG} -X ${VERSION_PKG}.PkgsVersion=${PKGS} -X ${VERSION_PKG}.ExtrasVersion=${EXTRAS}" -o /trustd
RUN chmod +x /trustd

# The routerd target builds the routerd binary.

FROM base AS routerd-build
ARG SHA
ARG TAG
ARG PKGS
ARG EXTRAS
ARG VERSION_PKG="github.com/talos-systems/talos/pkg/version"
WORKDIR /src/internal/app/routerd
RUN --mount=type=cache,target=/.cache/go-build go build -ldflags "-s -w -X ${VERSION_PKG}.Name=Server -X ${VERSION_PKG}.SHA=${SHA} -X ${VERSION_PKG}.Tag=${TAG} -X ${VERSION_PKG}.PkgsVersion=${PKGS} -X ${VERSION_PKG}.ExtrasVersion=${EXTRAS}" -o /routerd
RUN chmod +x /routerd

# The talosctl targets build the talosctl binaries.

FROM base AS talosctl-linux-amd64-build
Expand Down Expand Up @@ -353,9 +341,8 @@ RUN ln -s /etc/ssl /rootfs/etc/pki
RUN ln -s /etc/ssl /rootfs/usr/share/ca-certificates
RUN ln -s /etc/ssl /rootfs/usr/local/share/ca-certificates
RUN ln -s /etc/ssl /rootfs/etc/ca-certificates
RUN mkdir -pv /rootfs/opt/{apid,routerd,timed,trustd}
RUN mkdir -pv /rootfs/opt/{apid,timed,trustd}
RUN ln /rootfs/sbin/init /rootfs/opt/apid/apid
RUN ln /rootfs/sbin/init /rootfs/opt/routerd/routerd
RUN ln /rootfs/sbin/init /rootfs/opt/timed/timed
RUN ln /rootfs/sbin/init /rootfs/opt/trustd/trustd

Expand Down
2 changes: 1 addition & 1 deletion internal/app/apid/main.go
Expand Up @@ -74,7 +74,7 @@ func Main() {
}

backendFactory := apidbackend.NewAPIDFactory(clientTLSConfig)
localBackend := backend.NewLocal("routerd", constants.RouterdSocketPath)
localBackend := backend.NewLocal("machined", constants.MachineSocketPath)

router := director.NewRouter(backendFactory.Get, localBackend)

Expand Down
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/bootloader/adv"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub"
"github.com/talos-systems/talos/internal/app/machined/pkg/system"
networkserver "github.com/talos-systems/talos/internal/app/networkd/pkg/server"
storaged "github.com/talos-systems/talos/internal/app/storaged"
"github.com/talos-systems/talos/internal/pkg/configuration"
"github.com/talos-systems/talos/internal/pkg/containers"
Expand All @@ -63,8 +64,10 @@ import (
"github.com/talos-systems/talos/pkg/machinery/api/common"
"github.com/talos-systems/talos/pkg/machinery/api/inspect"
"github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/api/network"
"github.com/talos-systems/talos/pkg/machinery/api/resource"
"github.com/talos-systems/talos/pkg/machinery/api/storage"
timeapi "github.com/talos-systems/talos/pkg/machinery/api/time"
"github.com/talos-systems/talos/pkg/machinery/config"
machinetype "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/machinery/constants"
Expand Down Expand Up @@ -110,6 +113,8 @@ func (s *Server) Register(obj *grpc.Server) {
resource.RegisterResourceServiceServer(obj, &ResourceServer{server: s})
inspect.RegisterInspectServiceServer(obj, &InspectServer{server: s})
storage.RegisterStorageServiceServer(obj, &storaged.Server{})
timeapi.RegisterTimeServiceServer(obj, &TimeServer{ConfigProvider: s.Controller.Runtime()})
network.RegisterNetworkServiceServer(obj, &networkserver.NetworkServer{})
}

// ApplyConfiguration implements machine.MachineService.
Expand Down
71 changes: 71 additions & 0 deletions internal/app/machined/internal/server/v1alpha1/v1alpha1_time.go
@@ -0,0 +1,71 @@
// 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"
"fmt"
"time"

"github.com/beevik/ntp"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"

timeapi "github.com/talos-systems/talos/pkg/machinery/api/time"
"github.com/talos-systems/talos/pkg/machinery/config"
)

// ConfigProvider defines an interface sufficient for the TimeServer.
type ConfigProvider interface {
Config() config.Provider
}

// TimeServer implements TimeService API.
type TimeServer struct {
timeapi.UnimplementedTimeServiceServer

ConfigProvider ConfigProvider
}

// Register implements the factory.Registrator interface.
func (r *TimeServer) Register(s *grpc.Server) {
timeapi.RegisterTimeServiceServer(s, r)
}

// Time issues a query to the configured ntp server and displays the results.
func (r *TimeServer) Time(ctx context.Context, in *empty.Empty) (reply *timeapi.TimeResponse, err error) {
timeServers := r.ConfigProvider.Config().Machine().Time().Servers()

if len(timeServers) == 0 {
return nil, fmt.Errorf("no time servers configured")
}

return r.TimeCheck(ctx, &timeapi.TimeRequest{
Server: timeServers[0],
})
}

// TimeCheck issues a query to the specified ntp server and displays the results.
func (r *TimeServer) TimeCheck(ctx context.Context, in *timeapi.TimeRequest) (reply *timeapi.TimeResponse, err error) {
rt, err := ntp.Query(in.Server)
if err != nil {
return nil, fmt.Errorf("error querying NTP server %q: %w", in.Server, err)
}

if err = rt.Validate(); err != nil {
return nil, fmt.Errorf("error validating NTP response: %w", err)
}

return &timeapi.TimeResponse{
Messages: []*timeapi.Time{
{
Server: in.Server,
Localtime: timestamppb.New(time.Now()),
Remotetime: timestamppb.New(rt.Time),
},
},
}, nil
}
Expand Up @@ -2,7 +2,7 @@
// 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 reg_test
package runtime_test

import (
"context"
Expand All @@ -16,11 +16,12 @@ import (
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"

"github.com/talos-systems/talos/internal/app/timed/pkg/ntp"
"github.com/talos-systems/talos/internal/app/timed/pkg/reg"
runtime "github.com/talos-systems/talos/internal/app/machined/internal/server/v1alpha1"
"github.com/talos-systems/talos/pkg/grpc/dialer"
"github.com/talos-systems/talos/pkg/grpc/factory"
timeapi "github.com/talos-systems/talos/pkg/machinery/api/time"
"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
)

type TimedSuite struct {
Expand All @@ -33,14 +34,27 @@ func TestTimedSuite(t *testing.T) {
suite.Run(t, new(TimedSuite))
}

type mockConfigProvider struct {
timeServer string
}

func (provider *mockConfigProvider) Config() config.Provider {
return &v1alpha1.Config{
MachineConfig: &v1alpha1.MachineConfig{
MachineTime: &v1alpha1.TimeConfig{
TimeServers: []string{provider.timeServer},
},
},
}
}

func (suite *TimedSuite) TestTime() {
testServer := "time.cloudflare.com"
// Create ntp client
n, err := ntp.NewNTPClient(ntp.WithServer(testServer))
suite.Assert().NoError(err)

// Create gRPC server
api := reg.NewRegistrator(n)
api := &runtime.TimeServer{
ConfigProvider: &mockConfigProvider{timeServer: testServer},
}
server := factory.NewServer(api)
listener, err := fakeTimedRPC()
suite.Assert().NoError(err)
Expand All @@ -58,24 +72,23 @@ func (suite *TimedSuite) TestTime() {
grpc.WithInsecure(),
grpc.WithContextDialer(dialer.DialUnix()),
)
suite.Assert().NoError(err)
suite.Require().NoError(err)

nClient := timeapi.NewTimeServiceClient(conn)
reply, err := nClient.Time(context.Background(), &empty.Empty{})
suite.Assert().NoError(err)
suite.Require().NoError(err)
suite.Assert().Equal(reply.Messages[0].Server, testServer)
}

func (suite *TimedSuite) TestTimeCheck() {
testServer := "time.cloudflare.com"

// Create ntp client with bogus server
// so we can check that we explicitly check the time of the
// specified server ( testserver )
n, err := ntp.NewNTPClient(ntp.WithServer("127.0.0.1"))
suite.Assert().NoError(err)

// Create gRPC server
api := reg.NewRegistrator(n)
api := &runtime.TimeServer{}
server := factory.NewServer(api)
listener, err := fakeTimedRPC()
suite.Assert().NoError(err)
Expand All @@ -93,11 +106,11 @@ func (suite *TimedSuite) TestTimeCheck() {
grpc.WithInsecure(),
grpc.WithContextDialer(dialer.DialUnix()),
)
suite.Assert().NoError(err)
suite.Require().NoError(err)

nClient := timeapi.NewTimeServiceClient(conn)
reply, err := nClient.TimeCheck(context.Background(), &timeapi.TimeRequest{Server: testServer})
suite.Assert().NoError(err)
suite.Require().NoError(err)
suite.Assert().Equal(reply.Messages[0].Server, testServer)
}

Expand Down
5 changes: 0 additions & 5 deletions internal/app/machined/main.go
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/bootloader"
"github.com/talos-systems/talos/internal/app/machined/pkg/system"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/services"
"github.com/talos-systems/talos/internal/app/routerd"
"github.com/talos-systems/talos/internal/app/timed"
"github.com/talos-systems/talos/internal/app/trustd"
"github.com/talos-systems/talos/internal/pkg/mount"
Expand Down Expand Up @@ -268,10 +267,6 @@ func main() {
case "/apid":
apid.Main()

return
case "/routerd":
routerd.Main()

return
case "/timed":
timed.Main()
Expand Down
Expand Up @@ -664,7 +664,6 @@ func StartAllServices(seq runtime.Sequence, data interface{}) (runtime.TaskExecu

svcs.Load(
&services.APID{},
&services.Routerd{},
&services.Networkd{},
&services.CRI{},
&services.Kubelet{},
Expand Down
2 changes: 1 addition & 1 deletion internal/app/machined/pkg/system/services/apid.go
Expand Up @@ -109,7 +109,7 @@ func (o *APID) Runner(r runtime.Runtime) (runner.Runner, error) {
// Set the mounts.
mounts := []specs.Mount{
{Type: "bind", Destination: "/etc/ssl", Source: "/etc/ssl", Options: []string{"bind", "ro"}},
{Type: "bind", Destination: filepath.Dir(constants.RouterdSocketPath), Source: filepath.Dir(constants.RouterdSocketPath), Options: []string{"rbind", "ro"}},
{Type: "bind", Destination: filepath.Dir(constants.MachineSocketPath), Source: filepath.Dir(constants.MachineSocketPath), Options: []string{"rbind", "ro"}},
{Type: "bind", Destination: filepath.Dir(constants.APISocketPath), Source: filepath.Dir(constants.APISocketPath), Options: []string{"rbind", "rw"}},
}

Expand Down

0 comments on commit b0209fd

Please sign in to comment.