From cc121f0752a483ea9e0faaf640843a58e6c0ef3c Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 12 Jul 2024 17:05:39 +0200 Subject: [PATCH] Move RemoveHost into daemon --- go/bootstrap/garage_global_bucket.go | 35 ------------------------- go/cmd/entrypoint/hosts.go | 38 +++++++++++++++------------- go/daemon/daemon.go | 19 +++++++++++++- go/daemon/global_bucket.go | 20 +++++++++++++-- go/daemon/rpc.go | 16 +++++++++++- 5 files changed, 71 insertions(+), 57 deletions(-) delete mode 100644 go/bootstrap/garage_global_bucket.go diff --git a/go/bootstrap/garage_global_bucket.go b/go/bootstrap/garage_global_bucket.go deleted file mode 100644 index 548872f..0000000 --- a/go/bootstrap/garage_global_bucket.go +++ /dev/null @@ -1,35 +0,0 @@ -package bootstrap - -import ( - "context" - "isle/garage" - "path/filepath" - - "github.com/minio/minio-go/v7" -) - -// Paths within garage's global bucket. -// -// TODO this is getting moved into daemon package. -const ( - garageGlobalBucketBootstrapHostsDirPath = "bootstrap/hosts" -) - -// RemoveGarageBootstrapHost removes the .json.signed for the given -// host from garage. -// -// The given client should be for the global bucket. -func RemoveGarageBootstrapHost( - ctx context.Context, client garage.S3APIClient, hostName string, -) error { - - filePath := filepath.Join( - garageGlobalBucketBootstrapHostsDirPath, - hostName+".json.signed", - ) - - return client.RemoveObject( - ctx, garage.GlobalBucket, filePath, - minio.RemoveObjectOptions{}, - ) -} diff --git a/go/cmd/entrypoint/hosts.go b/go/cmd/entrypoint/hosts.go index 1c105e2..a260c31 100644 --- a/go/cmd/entrypoint/hosts.go +++ b/go/cmd/entrypoint/hosts.go @@ -4,7 +4,9 @@ import ( "errors" "fmt" "isle/bootstrap" + "isle/daemon" "isle/jsonutil" + "isle/nebula" "os" "sort" ) @@ -45,39 +47,39 @@ var subCmdHostsList = subCmd{ }, } -var subCmdHostsDelete = subCmd{ - name: "delete", - descr: "Deletes a host from the network", +var subCmdHostsRemove = subCmd{ + name: "remove", + descr: "Removes a host from the network", do: func(subCmdCtx subCmdCtx) error { + var ( + flags = subCmdCtx.flagSet(false) + hostName nebula.HostName + ) - flags := subCmdCtx.flagSet(false) - - hostName := flags.StringP( - "hostname", "h", "", - "Name of the host to delete", + hostNameF := flags.VarPF( + textUnmarshalerFlag{&hostName}, + "hostname", "h", + "Name of the host to remove", ) if err := flags.Parse(subCmdCtx.args); err != nil { return fmt.Errorf("parsing flags: %w", err) } - if *hostName == "" { + if !hostNameF.Changed { return errors.New("--hostname is required") } - var clientParams bootstrap.GarageClientParams err := subCmdCtx.daemonRCPClient.Call( - subCmdCtx.ctx, &clientParams, "GetGarageClientParams", nil, + subCmdCtx.ctx, nil, "RemoveHost", daemon.RemoveHostRequest{ + HostName: hostName, + }, ) if err != nil { - return fmt.Errorf("calling GetGarageClientParams: %w", err) + return fmt.Errorf("calling RemoveHost: %w", err) } - client := clientParams.GlobalBucketS3APIClient() - - // TODO do this within the daemon, along with first checking if the host - // has any assigned network resources. - return bootstrap.RemoveGarageBootstrapHost(subCmdCtx.ctx, client, *hostName) + return nil }, } @@ -86,7 +88,7 @@ var subCmdHosts = subCmd{ descr: "Sub-commands having to do with configuration of hosts in the network", do: func(subCmdCtx subCmdCtx) error { return subCmdCtx.doSubCmd( - subCmdHostsDelete, + subCmdHostsRemove, subCmdHostsList, ) }, diff --git a/go/daemon/daemon.go b/go/daemon/daemon.go index 160c34e..674fc2a 100644 --- a/go/daemon/daemon.go +++ b/go/daemon/daemon.go @@ -52,6 +52,9 @@ type Daemon interface { // GetBootstraps returns the currently active Bootstrap. GetBootstrap(context.Context) (bootstrap.Bootstrap, error) + // RemoveHost removes the host of the given name from the network. + RemoveHost(context.Context, nebula.HostName) error + // Shutdown blocks until all resources held or created by the daemon, // including child processes it has started, have been cleaned up. // @@ -372,7 +375,7 @@ func (d *daemon) postInit(ctx context.Context) bool { d.logger, "Updating host info in garage", func(ctx context.Context) error { - return d.putGarageBoostrapHost(ctx) + return putGarageBoostrapHost(ctx, d.logger, d.currBootstrap) }, ) { return false @@ -575,6 +578,20 @@ func (d *daemon) GetBootstrap(ctx context.Context) (bootstrap.Bootstrap, error) }) } +func (d *daemon) RemoveHost(ctx context.Context, hostName nebula.HostName) error { + // TODO RemoveHost should publish a certificate revocation for the host + // being removed. + _, err := withCurrBootstrap(d, func( + currBootstrap bootstrap.Bootstrap, + ) ( + struct{}, error, + ) { + client := currBootstrap.GarageClientParams().GlobalBucketS3APIClient() + return struct{}{}, removeGarageBootstrapHost(ctx, client, hostName) + }) + return err +} + func (d *daemon) Shutdown() error { d.l.Lock() defer d.l.Unlock() diff --git a/go/daemon/global_bucket.go b/go/daemon/global_bucket.go index 083bad7..d663087 100644 --- a/go/daemon/global_bucket.go +++ b/go/daemon/global_bucket.go @@ -62,9 +62,11 @@ func garageInitializeGlobalBucket( // putGarageBoostrapHost places the .json.signed file for this host // into garage so that other hosts are able to see relevant configuration for // it. -func (d *daemon) putGarageBoostrapHost(ctx context.Context) error { +func putGarageBoostrapHost( + ctx context.Context, logger *mlog.Logger, currBootstrap bootstrap.Bootstrap, +) error { var ( - b = d.currBootstrap + b = currBootstrap host = b.ThisHost() client = b.GarageClientParams().GlobalBucketS3APIClient() ) @@ -160,3 +162,17 @@ func getGarageBootstrapHosts( return hosts, nil } + +func removeGarageBootstrapHost( + ctx context.Context, client garage.S3APIClient, hostName nebula.HostName, +) error { + + filePath := filepath.Join( + garageGlobalBucketBootstrapHostsDirPath, + string(hostName)+".json.signed", + ) + + return client.RemoveObject( + ctx, garage.GlobalBucket, filePath, minio.RemoveObjectOptions{}, + ) +} diff --git a/go/daemon/rpc.go b/go/daemon/rpc.go index 886763c..9968faf 100644 --- a/go/daemon/rpc.go +++ b/go/daemon/rpc.go @@ -86,7 +86,8 @@ func (r *RPC) GetHosts( return GetHostsResult{hosts}, nil } -// GetGarageClientParams passes through to the Daemon method of the same name. +// GetGarageClientParams returns a GarageClientParams which can be used to +// interact with garage. func (r *RPC) GetGarageClientParams( ctx context.Context, req struct{}, ) ( @@ -102,6 +103,7 @@ func (r *RPC) GetGarageClientParams( return b.GarageClientParams(), nil } +// GetNebulaCAPublicCredentials returns the CAPublicCredentials for the network. func (r *RPC) GetNebulaCAPublicCredentials( ctx context.Context, req struct{}, ) ( @@ -116,3 +118,15 @@ func (r *RPC) GetNebulaCAPublicCredentials( return b.CAPublicCredentials, nil } + +// RemoveHostRequest contains the arguments to the RemoveHost method. +// +// All fields are required. +type RemoveHostRequest struct { + HostName nebula.HostName +} + +// RemoveHost passes the call through to the Daemon method of the same name. +func (r *RPC) RemoveHost(ctx context.Context, req RemoveHostRequest) (struct{}, error) { + return struct{}{}, r.daemon.RemoveHost(ctx, req.HostName) +}