* feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> * feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> --------- Signed-off-by: ci-bot <ci-bot@kubesphere.io> Co-authored-by: ks-ci-bot <ks-ci-bot@example.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
/*
|
|
* Please refer to the LICENSE file in the root directory of the project.
|
|
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
|
|
*/
|
|
|
|
package options
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"kubesphere.io/kubesphere/pkg/utils/net"
|
|
)
|
|
|
|
type ServerRunOptions struct {
|
|
// server bind address
|
|
BindAddress string
|
|
|
|
// insecure port number
|
|
InsecurePort int
|
|
|
|
// secure port number
|
|
SecurePort int
|
|
|
|
// tls cert file
|
|
TlsCertFile string
|
|
|
|
// tls private key file
|
|
TlsPrivateKey string
|
|
}
|
|
|
|
func NewServerRunOptions() *ServerRunOptions {
|
|
// create default server run options
|
|
s := ServerRunOptions{
|
|
BindAddress: "0.0.0.0",
|
|
InsecurePort: 9090,
|
|
SecurePort: 0,
|
|
TlsCertFile: "",
|
|
TlsPrivateKey: "",
|
|
}
|
|
|
|
return &s
|
|
}
|
|
|
|
func (s *ServerRunOptions) Validate() []error {
|
|
var errs []error
|
|
|
|
if s.SecurePort == 0 && s.InsecurePort == 0 {
|
|
errs = append(errs, fmt.Errorf("insecure and secure port can not be disabled at the same time"))
|
|
}
|
|
|
|
if net.IsValidPort(s.SecurePort) {
|
|
if s.TlsCertFile == "" {
|
|
errs = append(errs, fmt.Errorf("tls cert file is empty while secure serving"))
|
|
} else {
|
|
if _, err := os.Stat(s.TlsCertFile); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
if s.TlsPrivateKey == "" {
|
|
errs = append(errs, fmt.Errorf("tls private key file is empty while secure serving"))
|
|
} else {
|
|
if _, err := os.Stat(s.TlsPrivateKey); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return errs
|
|
}
|
|
|
|
func (s *ServerRunOptions) AddFlags(fs *pflag.FlagSet, c *ServerRunOptions) {
|
|
|
|
fs.StringVar(&s.BindAddress, "bind-address", c.BindAddress, "server bind address")
|
|
fs.IntVar(&s.InsecurePort, "insecure-port", c.InsecurePort, "insecure port number")
|
|
fs.IntVar(&s.SecurePort, "secure-port", s.SecurePort, "secure port number")
|
|
fs.StringVar(&s.TlsCertFile, "tls-cert-file", c.TlsCertFile, "tls cert file")
|
|
fs.StringVar(&s.TlsPrivateKey, "tls-private-key", c.TlsPrivateKey, "tls private key")
|
|
}
|