server.go 3.8 KB
Newer Older
L
liqingping 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
Copyright 2021 The OpenDILab authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server

import (
19
	"context"
20
	"flag"
L
liqingping 已提交
21 22

	"github.com/spf13/cobra"
23 24 25
	"k8s.io/apimachinery/pkg/runtime"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
L
liqingping 已提交
26
	ctrl "sigs.k8s.io/controller-runtime"
27
	"sigs.k8s.io/controller-runtime/pkg/healthz"
28
	"sigs.k8s.io/controller-runtime/pkg/log/zap"
L
liqingping 已提交
29 30

	cmdcommon "opendilab.org/di-orchestrator/cmd/common"
31
	div2alpha1 "opendilab.org/di-orchestrator/pkg/api/v2alpha1"
32 33
	dicontext "opendilab.org/di-orchestrator/pkg/context"
	"opendilab.org/di-orchestrator/pkg/server"
L
liqingping 已提交
34 35
)

L
liqingping 已提交
36
type CreateOptions struct {
37
	cmdcommon.GenericFlags
L
liqingping 已提交
38 39

	ServerBindAddress string
40 41
	ProbeAddress      string
	MetricAddress     string
L
liqingping 已提交
42 43
}

44
func NewCreateOptions(genFlags cmdcommon.GenericFlags) *CreateOptions {
L
liqingping 已提交
45
	return &CreateOptions{
46
		GenericFlags:      genFlags,
47 48
		ServerBindAddress: ":8081",
		ProbeAddress:      ":8080",
L
liqingping 已提交
49
		MetricAddress:     ":8443",
L
liqingping 已提交
50 51 52
	}
}

L
liqingping 已提交
53
func (o *CreateOptions) AddFlags(cmd *cobra.Command) {
54
	cmd.Flags().StringVarP(&o.ServerBindAddress, "server-bind-address", "s", o.ServerBindAddress,
L
liqingping 已提交
55
		"The address for server to bind to.")
56 57 58
	cmd.Flags().StringVarP(&o.ProbeAddress, "probe-address", "p", o.ProbeAddress,
		"The address for probe to connect to.")
	cmd.Flags().StringVar(&o.MetricAddress, "metric-addr", o.MetricAddress, "The address the metric endpoint binds to.")
L
liqingping 已提交
59 60 61
}

// serverCmd represents the server command
62 63
func NewCmdServer(genFlags cmdcommon.GenericFlags) *cobra.Command {
	o := NewCreateOptions(genFlags)
L
liqingping 已提交
64 65 66 67 68 69 70
	var serverCmd = &cobra.Command{
		Use:   "server",
		Short: "Command to run di-server ",
		Long: `Run di-server with specified configuration.

Examples:
	# Start di-server with gpu allocation policy and bind address specified.
L
Develop  
liqingping 已提交
71
	di-orchestrator server -p :8080 -s :8081
L
liqingping 已提交
72 73 74 75 76 77 78 79 80 81
`,
		Run: func(cmd *cobra.Command, args []string) {
			cobra.CheckErr(runCommand(cmd, o))
		},
	}

	o.AddFlags(serverCmd)
	return serverCmd
}

82 83 84 85 86 87 88 89 90 91 92 93
var (
	scheme   = runtime.NewScheme()
	setupLog = ctrl.Log.WithName("setup")
)

func init() {
	utilruntime.Must(clientgoscheme.AddToScheme(scheme))

	utilruntime.Must(div2alpha1.AddToScheme(scheme))
	//+kubebuilder:scaffold:scheme
}

L
liqingping 已提交
94
func runCommand(cmd *cobra.Command, options *CreateOptions) error {
95 96
	flag.Parse()
	logger := zap.New(zap.UseFlagOptions(options.GenericFlags.ZapOpts))
97 98 99 100 101 102 103 104
	ctrl.SetLogger(logger)

	config := ctrl.GetConfigOrDie()
	mgr, err := ctrl.NewManager(config, ctrl.Options{
		Scheme:                 scheme,
		MetricsBindAddress:     options.MetricAddress,
		HealthProbeBindAddress: options.ProbeAddress,
	})
L
liqingping 已提交
105
	if err != nil {
106
		setupLog.Error(err, "unable to start manager")
L
liqingping 已提交
107 108 109
		return err
	}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	ctx := dicontext.NewContext(context.Background(),
		config,
		mgr.GetClient(),
		mgr.GetEventRecorderFor("di-operator"),
		ctrl.Log.WithName("di-operator"))
	diServer := server.NewDIServer(ctx, options.ServerBindAddress)
	mgr.Add(diServer)

	if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
		setupLog.Error(err, "unable to set up health check")
		return err
	}
	if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
		setupLog.Error(err, "unable to set up ready check")
		return err
125
	}
L
liqingping 已提交
126

127 128 129 130
	setupLog.Info("starting manager")
	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
		setupLog.Error(err, "problem running manager")
		return err
L
liqingping 已提交
131 132 133
	}
	return nil
}