Skip to content

Commit

Permalink
fix(region): allow disable default policy
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito committed Nov 13, 2024
1 parent 85cbee5 commit d77f864
Show file tree
Hide file tree
Showing 39 changed files with 94 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/ansibleserver/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func StartService() {
opts := &options.Options
common_options.ParseOptions(opts, os.Args, "ansibleserver.conf", "ansibleserver")
common_options.ParseOptions(opts, os.Args, "ansibleserver.conf", "ansibleserver", nil)

commonOpts := &opts.CommonOptions
common_app.InitAuth(commonOpts, func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apigateway/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func StartService() {
opts := options.Options
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "apigateway.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "apigateway.conf", api.SERVICE_TYPE, nil)
app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete.")
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/apimap/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetOptions() *SOptions {
}

func Init() {
common_options.ParseOptions(&opts, os.Args, "apimap.conf", "apimap")
common_options.ParseOptions(&opts, os.Args, "apimap.conf", "apimap", nil)
options.Options = opts.ComputeOptions
}
func OnOptionsChange(oldO, newO interface{}) bool {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/identity/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ var (
// kubeserver blacklist options
// ############################
"running_mode",
"enable_default_policy",
},
}
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func New() *BaremetalService {
}

func (s *BaremetalService) StartService() {
common_options.ParseOptions(&o.Options, os.Args, "baremetal.conf", "baremetal")
common_options.ParseOptions(&o.Options, os.Args, "baremetal.conf", "baremetal", nil)

if len(o.Options.CachePath) == 0 {
o.Options.CachePath = filepath.Join(filepath.Dir(o.Options.BaremetalsPath), "bm_image_cache")
Expand Down
10 changes: 10 additions & 0 deletions pkg/cloudcommon/consts/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
localTaskWorkerCount int

enableChangeOwnerAutoRename = false

enableDefaultPolicy = true
)

func SetDefaultDB(dialect, connStr string) {
Expand Down Expand Up @@ -81,3 +83,11 @@ func TaskWorkerCount() int {
func LocalTaskWorkerCount() int {
return localTaskWorkerCount
}

func SetDefaultPolicy(enable bool) {
enableDefaultPolicy = enable
}

func IsEnableDefaultPolicy() bool {
return enableDefaultPolicy == true
}
13 changes: 9 additions & 4 deletions pkg/cloudcommon/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -123,6 +123,7 @@ type BaseOptions struct {
EnableAppProfiling bool `help:"enable profiling API" default:"false"`

EnableChangeOwnerAutoRename bool `help:"Allows renaming when changing names" default:"false"`
EnableDefaultPolicy bool `help:"Enable defualt policies" default:"true"`
}

const (
Expand Down Expand Up @@ -220,7 +221,7 @@ func (opt *EtcdOptions) GetEtcdTLSConfig() (*tls.Config, error) {
opt.EtcdUseTLS = true
}
if opt.EtcdCacert != "" {
data, err := ioutil.ReadFile(opt.EtcdCacert)
data, err := os.ReadFile(opt.EtcdCacert)
if err != nil {
return nil, errors.Wrap(err, "read cacert file")
}
Expand Down Expand Up @@ -279,8 +280,11 @@ func ParseOptionsIgnoreNoConfigfile(optStruct interface{}, args []string, config
parseOptions(optStruct, args, configFileName, serviceType, true)
}

func ParseOptions(optStruct interface{}, args []string, configFileName string, serviceType string) {
func ParseOptions(optStruct interface{}, args []string, configFileName string, serviceType string, callback func()) {
parseOptions(optStruct, args, configFileName, serviceType, false)
if callback != nil {
callback()
}
}

func parseOptions(optStruct interface{}, args []string, configFileName string, serviceType string, ignoreNoConfigfile bool) {
Expand Down Expand Up @@ -377,7 +381,7 @@ func parseOptions(optStruct interface{}, args []string, configFileName string, s
h.Init()
log.DisableColors()
log.Logger().AddHook(h)
log.Logger().Out = ioutil.Discard
log.Logger().Out = io.Discard
atexit.Register(atexit.ExitHandler{
Prio: atexit.PRIO_LOG_CLOSE,
Reason: "deinit log rotate hook",
Expand All @@ -397,6 +401,7 @@ func parseOptions(optStruct interface{}, args []string, configFileName string, s

consts.SetTaskWorkerCount(optionsRef.TaskWorkerCount)
consts.SetLocalTaskWorkerCount(optionsRef.LocalTaskWorkerCount)
consts.SetDefaultPolicy(optionsRef.EnableDefaultPolicy)
}

func (self *BaseOptions) HttpTransportProxyFunc() httputils.TransportProxyFunc {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudevent/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

func StartService() {
opts := &options.Options
common_options.ParseOptions(opts, os.Args, "yunionevent.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "yunionevent.conf", api.SERVICE_TYPE, nil)

commonOpts := &opts.CommonOptions
common_app.InitAuth(commonOpts, func() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/cloudid/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/cloudid"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -108,6 +109,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
4 changes: 2 additions & 2 deletions pkg/cloudid/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
_ "yunion.io/x/onecloud/pkg/cloudid/drivers"
"yunion.io/x/onecloud/pkg/cloudid/models"
"yunion.io/x/onecloud/pkg/cloudid/options"
_ "yunion.io/x/onecloud/pkg/cloudid/policy"
"yunion.io/x/onecloud/pkg/cloudid/policy"
"yunion.io/x/onecloud/pkg/cloudid/saml"
_ "yunion.io/x/onecloud/pkg/cloudid/tasks"
"yunion.io/x/onecloud/pkg/mcclient/auth"
Expand All @@ -46,7 +46,7 @@ func StartService() {
dbOpts := &opts.DBOptions
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "cloudid.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "cloudid.conf", api.SERVICE_TYPE, policy.Init)

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudir/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func StartService() {
opts := &options.Options
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "cloudir.conf", "cloudir")
common_options.ParseOptions(opts, os.Args, "cloudir.conf", "cloudir", nil)

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudmon/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
func StartService() {
opts := &options.Options
baseOpts := &options.Options.BaseOptions
common_options.ParseOptions(opts, os.Args, "cloudmon.conf", "cloudmon")
common_options.ParseOptions(opts, os.Args, "cloudmon.conf", "cloudmon", nil)

commonOpts := &opts.CommonOptions
app_common.InitAuth(commonOpts, func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudnet/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

func StartService() {
opts := &options.Options
common_options.ParseOptions(opts, os.Args, "cloudnet.conf", "cloudnet")
common_options.ParseOptions(opts, os.Args, "cloudnet.conf", "cloudnet", nil)

commonOpts := &opts.CommonOptions
app_common.InitAuth(commonOpts, func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudproxy/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Get() *Options {
os.Args,
"cloudproxy.conf",
api.SERVICE_TYPE,
nil,
)
})
return &opts
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloutpost/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func StartService() {
opts := &options.Options
baseOpts := &opts.BaseOptions
commonOpts := &opts.CommonOptions
common_options.ParseOptions(opts, os.Args, "cloutpost.conf", SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "cloutpost.conf", SERVICE_TYPE, nil)

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
7 changes: 5 additions & 2 deletions pkg/compute/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -403,6 +404,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
4 changes: 2 additions & 2 deletions pkg/compute/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
_ "yunion.io/x/onecloud/pkg/compute/hostdrivers"
"yunion.io/x/onecloud/pkg/compute/models"
"yunion.io/x/onecloud/pkg/compute/options"
_ "yunion.io/x/onecloud/pkg/compute/policy"
"yunion.io/x/onecloud/pkg/compute/policy"
_ "yunion.io/x/onecloud/pkg/compute/regiondrivers"
_ "yunion.io/x/onecloud/pkg/compute/storagedrivers"
"yunion.io/x/onecloud/pkg/compute/tasks"
Expand All @@ -66,7 +66,7 @@ func StartServiceWithJobs(jobs func(cron *cronman.SCronJobManager)) {
commonOpts := &options.Options.CommonOptions
baseOpts := &options.Options.BaseOptions
dbOpts := &options.Options.DBOptions
common_options.ParseOptions(opts, os.Args, "region.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "region.conf", api.SERVICE_TYPE, policy.Init)

if opts.PortV2 > 0 {
log.Infof("Port V2 %d is specified, use v2 port", opts.PortV2)
Expand Down
2 changes: 1 addition & 1 deletion pkg/devtool/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func StartService() {
commonOpts := &opts.CommonOptions
dbOpts := &options.Options.DBOptions
baseOpts := &opts.BaseOptions
common_options.ParseOptions(opts, os.Args, "devtool.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "devtool.conf", api.SERVICE_TYPE, nil)

app_common.InitAuth(commonOpts, func() {
log.Infof("Auth complete!!")
Expand Down
2 changes: 1 addition & 1 deletion pkg/esxi/service/esxi_agent_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New() *SExsiAgentService {
}

func (s *SExsiAgentService) StartService() {
options_common.ParseOptions(&options.Options, os.Args, "esxiagent.conf", "esxiagent")
options_common.ParseOptions(&options.Options, os.Args, "esxiagent.conf", "esxiagent", nil)

if len(options.Options.ImageCachePath) == 0 {
options.Options.ImageCachePath = filepath.Join(options.Options.EsxiAgentPath, "image_cache")
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostimage/host_image_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func init() {

func StartService() {
consts.SetServiceType("host-image")
common_options.ParseOptions(&HostImageOptions, os.Args, "host.conf", "host-image")
common_options.ParseOptions(&HostImageOptions, os.Args, "host.conf", "host-image", nil)
if len(HostImageOptions.CommonConfigFile) > 0 {
baseOpt := HostImageOptions.BaseOptions.BaseOptions
commonCfg := new(common_options.CommonOptions)
commonCfg.Config = HostImageOptions.CommonConfigFile
common_options.ParseOptions(commonCfg, []string{"host"}, "common.conf", "host")
common_options.ParseOptions(commonCfg, []string{"host"}, "common.conf", "host", nil)
HostImageOptions.CommonOptions = *commonCfg
HostImageOptions.BaseOptions.BaseOptions = baseOpt
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/hostdeployer/deployserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Parse() SDeployOptions {
if len(hostOpts.CommonConfigFile) > 0 && fileutils2.Exists(hostOpts.CommonConfigFile) {
commonCfg := &host_options.SHostBaseOptions{}
commonCfg.Config = hostOpts.CommonConfigFile
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "host")
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "host", nil)
baseOpt := hostOpts.BaseOptions.BaseOptions
hostOpts.SHostBaseOptions = *commonCfg
// keep base options
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ var (

func Parse() SHostOptions {
var hostOpts SHostOptions
common_options.ParseOptions(&hostOpts, os.Args, "host.conf", "host")
common_options.ParseOptions(&hostOpts, os.Args, "host.conf", "host", nil)
if len(hostOpts.CommonConfigFile) > 0 && fileutils2.Exists(hostOpts.CommonConfigFile) {
commonCfg := &SHostBaseOptions{}
commonCfg.Config = hostOpts.CommonConfigFile
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "host")
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "host", nil)
baseOpt := hostOpts.BaseOptions.BaseOptions
hostOpts.SHostBaseOptions = *commonCfg
// keep base options
Expand Down
7 changes: 5 additions & 2 deletions pkg/image/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/image"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -65,6 +66,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
4 changes: 2 additions & 2 deletions pkg/image/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"yunion.io/x/onecloud/pkg/image/drivers/s3"
"yunion.io/x/onecloud/pkg/image/models"
"yunion.io/x/onecloud/pkg/image/options"
_ "yunion.io/x/onecloud/pkg/image/policy"
"yunion.io/x/onecloud/pkg/image/policy"
_ "yunion.io/x/onecloud/pkg/image/tasks"
"yunion.io/x/onecloud/pkg/image/torrent"
"yunion.io/x/onecloud/pkg/mcclient/auth"
Expand All @@ -54,7 +54,7 @@ func StartService() {
commonOpts := &opts.CommonOptions
baseOpts := &opts.BaseOptions
dbOpts := &opts.DBOptions
common_options.ParseOptions(opts, os.Args, "glance-api.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "glance-api.conf", api.SERVICE_TYPE, policy.Init)

// no need to run glance as root any more
// isRoot := sysutils.IsRootPermission()
Expand Down
7 changes: 5 additions & 2 deletions pkg/keystone/policy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"

api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
common_policy "yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/util/rbacutils"
)
Expand Down Expand Up @@ -191,6 +192,8 @@ var (
}
)

func init() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
func Init() {
if consts.IsEnableDefaultPolicy() {
common_policy.AppendDefaultPolicies(predefinedDefaultPolicies)
}
}
4 changes: 2 additions & 2 deletions pkg/keystone/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"yunion.io/x/onecloud/pkg/keystone/cronjobs"
"yunion.io/x/onecloud/pkg/keystone/models"
"yunion.io/x/onecloud/pkg/keystone/options"
_ "yunion.io/x/onecloud/pkg/keystone/policy"
kpolicy "yunion.io/x/onecloud/pkg/keystone/policy"
"yunion.io/x/onecloud/pkg/keystone/saml"
_ "yunion.io/x/onecloud/pkg/keystone/tasks"
"yunion.io/x/onecloud/pkg/keystone/tokens"
Expand All @@ -62,7 +62,7 @@ func StartService() {
models.InitSyncWorkers()

opts := &options.Options
common_options.ParseOptions(opts, os.Args, "keystone.conf", api.SERVICE_TYPE)
common_options.ParseOptions(opts, os.Args, "keystone.conf", api.SERVICE_TYPE, kpolicy.Init)

if opts.Port == 0 {
opts.Port = 5000 // keystone well-known port
Expand Down
4 changes: 2 additions & 2 deletions pkg/lbagent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func StartService() {
opts := &Options{}
commonOpts := &opts.CommonOptions
{
common_options.ParseOptions(opts, os.Args, "lbagent.conf", "lbagent")
common_options.ParseOptions(opts, os.Args, "lbagent.conf", "lbagent", nil)
if len(opts.CommonConfigFile) > 0 && fileutils2.Exists(opts.CommonConfigFile) {
log.Infof("read common config file: %s", opts.CommonConfigFile)
commonCfg := &LbagentCommonOptions{}
commonCfg.Config = opts.CommonConfigFile
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "lbagent")
common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "lbagent", nil)
baseOpt := opts.BaseOptions.BaseOptions
opts.LbagentCommonOptions = *commonCfg
// keep base options
Expand Down
Loading

0 comments on commit d77f864

Please sign in to comment.