Implement odo api-server command (#6952)

* Implement odo api-server command

* Make api-server an experimental mode command

* Add --api-server-port flag

* Support DELETE /instance

* Review
This commit is contained in:
Philippe Martin
2023-07-06 16:03:15 +02:00
committed by GitHub
parent 5911930a11
commit f6bb4e2689
3 changed files with 99 additions and 2 deletions

View File

@@ -0,0 +1,94 @@
package apiserver
import (
"context"
"fmt"
apiserver_impl "github.com/redhat-developer/odo/pkg/apiserver-impl"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/spf13/cobra"
"k8s.io/klog"
)
const (
RecommendedCommandName = "api-server"
)
type ApiServerOptions struct {
clientset *clientset.Clientset
portFlag int
}
func NewApiServerOptions() *ApiServerOptions {
return &ApiServerOptions{}
}
var _ genericclioptions.Runnable = (*ApiServerOptions)(nil)
var _ genericclioptions.SignalHandler = (*ApiServerOptions)(nil)
var _ genericclioptions.Cleanuper = (*ApiServerOptions)(nil)
func (o *ApiServerOptions) SetClientset(clientset *clientset.Clientset) {
o.clientset = clientset
}
func (o *ApiServerOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) error {
return nil
}
func (o *ApiServerOptions) Validate(ctx context.Context) error {
return nil
}
func (o *ApiServerOptions) Run(ctx context.Context) (err error) {
err = o.clientset.StateClient.Init(ctx)
if err != nil {
err = fmt.Errorf("unable to save state file: %w", err)
return err
}
ctx, cancel := context.WithCancel(ctx)
_ = apiserver_impl.StartServer(
ctx,
cancel,
o.portFlag,
nil,
nil,
o.clientset.StateClient,
)
<-ctx.Done()
return nil
}
func (o *ApiServerOptions) Cleanup(ctx context.Context, commandError error) error {
err := o.clientset.StateClient.SaveExit(ctx)
if err != nil {
klog.V(1).Infof("unable to persist dev state: %v", err)
}
return nil
}
func (o *ApiServerOptions) HandleSignal(ctx context.Context, cancelFunc context.CancelFunc) error {
cancelFunc()
return nil
}
// NewCmdApiServer implements the odo api-server command
func NewCmdApiServer(ctx context.Context, name, fullName string, testClientset clientset.Clientset) *cobra.Command {
o := NewApiServerOptions()
apiserverCmd := &cobra.Command{
Use: name,
Short: "Start the API server",
Long: "Start the API server",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return genericclioptions.GenericRun(o, testClientset, cmd, args)
},
}
clientset.Add(apiserverCmd,
clientset.STATE,
)
apiserverCmd.Flags().IntVar(&o.portFlag, "port", 0, "Define custom port for API Server.")
return apiserverCmd
}

View File

@@ -9,6 +9,8 @@ import (
"strings"
"unicode"
"github.com/redhat-developer/odo/pkg/odo/cli/apiserver"
"github.com/redhat-developer/odo/pkg/odo/cli/feature"
"github.com/redhat-developer/odo/pkg/odo/cli/logs"
"github.com/redhat-developer/odo/pkg/odo/cli/run"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
@@ -202,6 +204,9 @@ func odoRootCmd(ctx context.Context, name, fullName string, unknownCmdHandler fu
completion.NewCmdCompletion(completion.RecommendedCommandName, util.GetFullName(fullName, completion.RecommendedCommandName)),
run.NewCmdRun(run.RecommendedCommandName, util.GetFullName(fullName, run.RecommendedCommandName), testClientset),
)
if feature.IsExperimentalModeEnabled(ctx) {
rootCmdList = append(rootCmdList, apiserver.NewCmdApiServer(ctx, apiserver.RecommendedCommandName, util.GetFullName(fullName, apiserver.RecommendedCommandName), testClientset))
}
// Add all subcommands to base commands
rootCmd.AddCommand(rootCmdList...)

View File

@@ -640,8 +640,6 @@ func StartSignalWatcher(watchSignals []os.Signal, handle func(receivedSignal os.
receivedSignal := <-signals
handle(receivedSignal)
// exit here to stop spinners from rotating
os.Exit(1)
}
// cleanDir cleans the original folder during events like interrupted copy etc