Skip to content

Commit

Permalink
feat: Normalize options & Add 'dense' options
Browse files Browse the repository at this point in the history
1. Now, all options should be put in the configuration file
2. Dense means that during the reconcil process, the content of status for crd is more abundant.
  • Loading branch information
rainzm committed May 25, 2020
1 parent 958333e commit 6ec0326
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 35 deletions.
15 changes: 10 additions & 5 deletions controllers/ansibleplaybook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import (
"yunion.io/x/pkg/utils"

onecloudv1 "yunion.io/x/onecloud-service-operator/api/v1"
"yunion.io/x/onecloud-service-operator/pkg/options"
"yunion.io/x/onecloud-service-operator/pkg/provider"
)

const (
PendingAfter = 15 * time.Second
WaitingAfter = 15 * time.Second
var (
apPendingAfter = time.Duration(options.Options.AnsiblePlaybookConfig.IntervalPending) * time.Second
apWaitingAfter = time.Duration(options.Options.AnsiblePlaybookConfig.IntervalWaiting) * time.Second
dense = options.Options.AnsiblePlaybookConfig.Dense
)

// AnsiblePlaybookReconciler reconciles a AnsiblePlaybook object
Expand Down Expand Up @@ -255,6 +257,9 @@ func (r *AnsiblePlaybookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er
var recon func(ctx context.Context, ap *onecloudv1.AnsiblePlaybook) (*onecloudv1.AnsiblePlaybookStatus, error)

recon = provider.Provider.APGetStatus
if dense {
recon = provider.Provider.APReconcile
}
apStatus, err := recon(ctx, &ansiblePlaybook)
if err != nil {
return dealErr(err)
Expand All @@ -273,7 +278,7 @@ func (r *AnsiblePlaybookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er

// Pending
if ansiblePlaybook.Status.Phase == onecloudv1.ResourcePending {
return ctrl.Result{Requeue: true, RequeueAfter: PendingAfter}, nil
return ctrl.Result{Requeue: true, RequeueAfter: apPendingAfter}, nil
}

// Unkown
Expand Down Expand Up @@ -372,7 +377,7 @@ func (r *AnsiblePlaybookReconciler) markWaiting(ctx context.Context, log logr.Lo
newStatus.Reason = msg
if !r.requireUpdate(ap, newStatus) {
log.Info(fmt.Sprintf("no need to update, requeue after %d s", 15))
return ctrl.Result{Requeue: true, RequeueAfter: WaitingAfter}, nil
return ctrl.Result{Requeue: true, RequeueAfter: apWaitingAfter}, nil
}
ap.Status = *newStatus
if err := r.Status().Update(ctx, ap); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions controllers/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ import (
"yunion.io/x/pkg/utils"

onecloudv1 "yunion.io/x/onecloud-service-operator/api/v1"
"yunion.io/x/onecloud-service-operator/pkg/options"
"yunion.io/x/onecloud-service-operator/pkg/provider"
"yunion.io/x/onecloud-service-operator/pkg/util"
)

var (
vmPendingAfter = time.Duration(options.Options.VirtualMachineConfig.IntervalPending) * time.Minute
)

// VirtualMachineReconciler reconciles a VirtualMachine object
type VirtualMachineReconciler struct {
client.Client
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ require (
yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3
yunion.io/x/onecloud v0.0.0-20200427025506-7f96ad48447f
yunion.io/x/pkg v0.0.0-20200416145704-22c189971435
yunion.io/x/structarg v0.0.0-20200423163001-168d0687be7e
)

replace (

github.com/mcuadros/go-lookup v0.0.0-20200330054200-b4062b0c4c85 => github.com/rainzm/go-lookup v0.0.0-20200508134129-95189dd17b94
k8s.io/api => k8s.io/api v0.0.0-20190918155943-95b840bb6a1f
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.0.0-20190918161926-8f644eb6e783
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20190913080033-27d36303b655
Expand All @@ -43,6 +46,4 @@ replace (
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.0.0-20190918161442-d4c9c65c82af
k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.0.0-20190918162410-e45c26d066f2
k8s.io/sample-controller => k8s.io/sample-controller v0.0.0-20190918161628-92eb3cb7496c

github.com/mcuadros/go-lookup v0.0.0-20200330054200-b4062b0c4c85 => github.com/rainzm/go-lookup v0.0.0-20200508134129-95189dd17b94
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ github.com/syncthing/syncthing v0.14.48-rc.4/go.mod h1:nw3siZwHPA6M8iSfjDCWQ402e
github.com/syndtr/gocapability v0.0.0-20160928074757-e7cb7fa329f4/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.135+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20191108095731-8ca4b370cde4/go.mod h1:wk2XFUg6egk4tSDNZtXeKfe2G6690UVyt163PuUxBZk=
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e h1:T5PdfK/M1xyrHwynxMIVMWLS7f/qHwfslZphxtGnw7s=
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g=
github.com/thecodeteam/goscaleio v0.1.0/go.mod h1:68sdkZAsK8bvEwBlbQnlLS+xU+hvLYM/iQ8KXej1AwM=
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
Expand Down Expand Up @@ -1017,4 +1018,5 @@ yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8
yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo=
yunion.io/x/sqlchemy v0.0.0-20200312002602-1177cd8fbc57/go.mod h1:FTdwPdGhMgh4E+UFXc9klI1Ok34fMuybTT+jLhOaIjI=
yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=
yunion.io/x/structarg v0.0.0-20200423163001-168d0687be7e h1:pctCe/EPel3F1B83pJ2q9b34Umd1NdbLW1Yd+Lzur2s=
yunion.io/x/structarg v0.0.0-20200423163001-168d0687be7e/go.mod h1:EP6NSv2C0zzqBDTKumv8hPWLb3XvgMZDHQRfyuOrQng=
25 changes: 8 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ limitations under the License.
package main

import (
"flag"
"os"
"time"

"yunion.io/x/onecloud-service-operator/provider"

"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand All @@ -31,6 +28,8 @@ import (

onecloudv1 "yunion.io/x/onecloud-service-operator/api/v1"
"yunion.io/x/onecloud-service-operator/controllers"
"yunion.io/x/onecloud-service-operator/pkg/options"
"yunion.io/x/onecloud-service-operator/pkg/provider"
// +kubebuilder:scaffold:imports
)

Expand All @@ -47,26 +46,18 @@ func init() {
}

func main() {
var metricsAddr string
var enableLeaderElection bool
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
provider.Provider.InitConfig()
flag.Parse()

options.ParseOptions()
provider.Provider.Init()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
syncPeriod := 10 * time.Minute
sp := time.Duration(options.Options.SyncPeriod) * time.Minute

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
MetricsBindAddress: options.Options.MetricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElection: options.Options.EnableLeaderElection,
LeaderElectionID: "7ddf82e9.yunion.io",
SyncPeriod: &syncPeriod,
SyncPeriod: &sp,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand All @@ -81,7 +72,7 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "VirtualMachine")
os.Exit(1)
}
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if options.Options.EnableWebhooks {
if err = (&onecloudv1.VirtualMachine{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "VirtualMachine")
os.Exit(1)
Expand Down
12 changes: 12 additions & 0 deletions pkg/options/demo.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
metrics_addr: 127.0.0.1:8080
enable_leader_election: true
sync_period: 15

admin_project: example
auth_url: https://10.0.1.4:30000/v3
admin_username: goodone
admin_password: cj5ezKSEbbZhYh7C

ap_interval_waiting: 20
ap_dense: true

78 changes: 78 additions & 0 deletions pkg/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package options

import (
"fmt"
"log"
"os"

"yunion.io/x/structarg"
)

type OperatroOptions struct {
structarg.BaseOptions

MetricsAddr string `help:"The address the metric endpoint binds to." default:":8080"`
EnableLeaderElection bool `help:"Enable leader election for controller manager. Enable intensive information collection during the reconcile process." default:"false"`
EnableWebhooks bool `help:"Enable webhooks for controller manager." default:"false"`
SyncPeriod int `help:"The interval between two adjacent local cache refreshes. unit:m" default:"10"`

AdminAuthConfig
AnsiblePlaybookConfig
VirtualMachineConfig
}

type AdminAuthConfig struct {
Region string `help:"Region name or ID"`
AuthURL string `help:"Keystone auth URL"`
AdminUsername string `help:"Admin username"`
AdminPassword string `help:"Admin password"`
AdminDomain string `help:"Admin domain"`
AdminProject string `help:"Admin project"`
}

type AnsiblePlaybookConfig struct {
IntervalPending int `json:"ap_interval_pending" help:"Reconcile interval when the state of the ansibleplaybook is pending." default:"15"`
IntervalWaiting int `json:"ap_interval_waiting" help:"Reconcile interval when the state of the ansibleplaybook is waiting." default:"15"`
Dense bool `json:"ap_dense" help:"Enable intensive information collection during the reconcile process." default:"false"`
}

type VirtualMachineConfig struct {
IntervalPending int `json:"vm_interval_pending" help:"Reconcile interval when the state of the virtualmachine is pending." default:"5"`
}

var Options OperatroOptions

func ParseOptions(args ...string) {
parser, err := structarg.NewArgumentParser(&Options, "", "", "")
if err != nil {
log.Fatalf("Unable to define argument parser: %s.", err.Error())
}

if len(args) == 0 {
args = os.Args[1:]
}

err = parser.ParseArgs2(args, false, false)
if err != nil {
log.Fatalf("Unable to parse args: %s.", err.Error())
}

if Options.Help {
fmt.Println(parser.HelpString())
os.Exit(0)
}

if len(Options.Config) == 0 {
defaultConfig := "/etc/yunion/oso.conf"
Options.Config = defaultConfig
}

log.Printf("Use configuration file '%s'.", Options.Config)

err = parser.ParseFile(Options.Config)
if err != nil {
log.Fatalf("Unable to parse configuration file: %s.", err.Error())
}

parser.SetDefault()
}
35 changes: 35 additions & 0 deletions pkg/options/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package options

import (
"reflect"
"testing"
"yunion.io/x/structarg"
)

func TestParseOptions(t *testing.T) {
want := OperatroOptions{
BaseOptions: structarg.BaseOptions{},
MetricsAddr: "127.0.0.1:8080",
EnableLeaderElection: true,
SyncPeriod: 15,
AdminAuthConfig: AdminAuthConfig{
AdminProject: "example",
AuthURL: "https://10.0.1.4:30000/v3",
AdminUsername: "goodone",
AdminPassword: "cj5ezKSEbbZhYh7C",
},
AnsiblePlaybookConfig: AnsiblePlaybookConfig{
IntervalPending: 15,
IntervalWaiting: 20,
Dense: true,
},
VirtualMachineConfig: VirtualMachineConfig{
IntervalPending: 5,
},
}
ParseOptions("--config", "./demo.conf")
Options.BaseOptions = want.BaseOptions
if !reflect.DeepEqual(want, Options) {
t.Fatalf("want: %#v\n get: %#v", want, Options)
}
}
21 changes: 10 additions & 11 deletions pkg/provider/onecloud_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package provider

import (
"context"
"flag"
"log"

"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"

"yunion.io/x/onecloud-service-operator/pkg/options"
)

var AuthConfig AdminAuthConfig
Expand All @@ -37,16 +38,6 @@ type AdminAuthConfig struct {
AdminProjectDomain string
}

func (op OnecloudProvider) InitConfig() {
flag.StringVar(&AuthConfig.Region, "region", "", "Region name or ID")
flag.StringVar(&AuthConfig.AuthURL, "auth-url", "", "Keystone auth URL")
flag.StringVar(&AuthConfig.AdminUsername, "admin-username", "", "Admin username")
flag.StringVar(&AuthConfig.AdminPassword, "admin-password", "", "Admin password")
flag.StringVar(&AuthConfig.AdminDomain, "admin-domain", "", "Admin domain")
flag.StringVar(&AuthConfig.AdminProject, "admin-project", "", "Admin project")
flag.StringVar(&AuthConfig.AdminProjectDomain, "admin-project-domain", "", "Admin project domain")
}

func (config AdminAuthConfig) ToAuthInfo() *auth.AuthInfo {
log.Println("start to auth info")
if len(config.AuthURL) == 0 {
Expand Down Expand Up @@ -75,6 +66,14 @@ func (config AdminAuthConfig) ToAuthInfo() *auth.AuthInfo {
}

func (op OnecloudProvider) Init() {
AuthConfig = AdminAuthConfig{
Region: options.Options.Region,
AuthURL: options.Options.AuthURL,
AdminUsername: options.Options.AdminUsername,
AdminPassword: options.Options.AdminPassword,
AdminDomain: options.Options.AdminDomain,
AdminProject: options.Options.AdminProject,
}
auth.Init(AuthConfig.ToAuthInfo(), false, true, "", "")
}

Expand Down

0 comments on commit 6ec0326

Please sign in to comment.