Skip to content

Commit

Permalink
✨ Add pprof option for global hub components (stolostron#907)
Browse files Browse the repository at this point in the history
* Add pprof option for global hub components

Signed-off-by: myan <[email protected]>

* make operator pprof server options

Signed-off-by: myan <[email protected]>

* format code

Signed-off-by: Meng Yan <[email protected]>

* ut

Signed-off-by: Meng Yan <[email protected]>

* fix the ut

Signed-off-by: myan <[email protected]>

* reply review

Signed-off-by: myan <[email protected]>

* fix the sonar

Signed-off-by: myan <[email protected]>

---------

Signed-off-by: myan <[email protected]>
Signed-off-by: Meng Yan <[email protected]>
  • Loading branch information
yanmxa authored May 9, 2024
1 parent 79b2128 commit a98da42
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 59 deletions.
5 changes: 5 additions & 0 deletions agent/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func doMain(ctx context.Context, restConfig *rest.Config, agentConfig *config.Ag
return 1
}

if agentConfig.EnablePprof {
go utils.StartDefaultPprofServer()
}

mgr, err := createManager(ctx, restConfig, agentConfig)
if err != nil {
setupLog.Error(err, "failed to create manager")
Expand Down Expand Up @@ -171,6 +175,7 @@ func parseFlags() *config.AgentConfig {
"QPS for the multicluster global hub agent")
pflag.IntVar(&agentConfig.Burst, "burst", 300,
"Burst for the multicluster global hub agent")
pflag.BoolVar(&agentConfig.EnablePprof, "enable-pprof", false, "Enable the pprof tool.")
pflag.Parse()

// set zap logger
Expand Down
1 change: 1 addition & 0 deletions agent/pkg/config/agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type AgentConfig struct {
EnableGlobalResource bool
QPS float32
Burst int
EnablePprof bool
}
7 changes: 6 additions & 1 deletion manager/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func parseFlags() *managerconfig.ManagerConfig {
"data retention indicates how many months the expired data will kept in the database")
pflag.BoolVar(&managerConfig.EnableGlobalResource, "enable-global-resource", false,
"enable the global resource feature.")

pflag.BoolVar(&managerConfig.EnablePprof, "enable-pprof", false, "Enable the pprof tool.")
pflag.Parse()
// set zap logger
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
Expand Down Expand Up @@ -299,6 +299,11 @@ func doMain(ctx context.Context, restConfig *rest.Config) int {
setupLog.Error(err, "failed to complete configuration")
return 1
}

if managerConfig.EnablePprof {
go utils.StartDefaultPprofServer()
}

utils.PrintVersion(setupLog)
databaseConfig := &database.DatabaseConfig{
URL: managerConfig.DatabaseConfig.ProcessDatabaseURL,
Expand Down
1 change: 1 addition & 0 deletions manager/pkg/config/manager_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ManagerConfig struct {
ElectionConfig *commonobjects.LeaderElectionConfig
EnableGlobalResource bool
LaunchJobNames string
EnablePprof bool
}

type SyncerConfig struct {
Expand Down
5 changes: 5 additions & 0 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func doMain(ctx context.Context, cfg *rest.Config) int {
operatorConfig := parseFlags()
utils.PrintVersion(setupLog)

if operatorConfig.EnablePprof {
go utils.StartDefaultPprofServer()
}

kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
setupLog.Error(err, "failed to create kube client")
Expand Down Expand Up @@ -106,6 +110,7 @@ func parseFlags() *config.OperatorConfig {
"Enable leader election for controller manager. ")
pflag.BoolVar(&config.GlobalResourceEnabled, "global-resource-enabled", false,
"Enable the global resource. It is expermental feature. Do not support upgrade.")
pflag.BoolVar(&config.EnablePprof, "enable-pprof", false, "Enable the pprof tool.")
pflag.Parse()

config.LogLevel = "info"
Expand Down
1 change: 1 addition & 0 deletions operator/pkg/config/operator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ type OperatorConfig struct {
PodNamespace string
LeaderElection bool
GlobalResourceEnabled bool
EnablePprof bool
LogLevel string
}
35 changes: 16 additions & 19 deletions operator/pkg/controllers/addon/addon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ import (
// +kubebuilder:rbac:groups=packages.operators.coreos.com,resources=packagemanifests,verbs=get;list;watch

type AddonController struct {
addonManager addonmanager.AddonManager
kubeConfig *rest.Config
client client.Client
log logr.Logger
EnableGlobalResource bool
LogLevel string
addonManager addonmanager.AddonManager
kubeConfig *rest.Config
client client.Client
log logr.Logger
operatorConfig *config.OperatorConfig
}

// used to create addon manager
Expand All @@ -59,12 +58,11 @@ func NewAddonController(kubeConfig *rest.Config, client client.Client, operatorC
return nil, err
}
return &AddonController{
kubeConfig: kubeConfig,
client: client,
log: log,
addonManager: addonMgr,
EnableGlobalResource: operatorConfig.GlobalResourceEnabled,
LogLevel: operatorConfig.LogLevel,
kubeConfig: kubeConfig,
client: client,
log: log,
addonManager: addonMgr,
operatorConfig: operatorConfig,
}, nil
}

Expand Down Expand Up @@ -93,13 +91,12 @@ func (a *AddonController) Start(ctx context.Context) error {
}

hohAgentAddon := HohAgentAddon{
ctx: ctx,
kubeClient: kubeClient,
client: a.client,
dynamicClient: dynamicClient,
log: a.log.WithName("values"),
EnableGlobalResource: a.EnableGlobalResource,
LogLevel: a.LogLevel,
ctx: ctx,
kubeClient: kubeClient,
client: a.client,
dynamicClient: dynamicClient,
log: a.log.WithName("values"),
operatorConfig: a.operatorConfig,
}
_, err = utils.WaitGlobalHubReady(ctx, a.client, 5*time.Second)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions operator/pkg/controllers/addon/addon_controller_manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ManifestsConfig struct {
AgentQPS float32
AgentBurst int
LogLevel string
EnablePprof bool
// cannot use *corev1.ResourceRequirements, addonfactory.StructToValues removes the real value
Resources *Resources
}
Expand All @@ -81,13 +82,12 @@ type Resources struct {
}

type HohAgentAddon struct {
ctx context.Context
client client.Client
kubeClient kubernetes.Interface
dynamicClient dynamic.Interface
log logr.Logger
EnableGlobalResource bool
LogLevel string
ctx context.Context
client client.Client
kubeClient kubernetes.Interface
dynamicClient dynamic.Interface
log logr.Logger
operatorConfig *config.OperatorConfig
}

func (a *HohAgentAddon) getMulticlusterGlobalHub() (*globalhubv1alpha4.MulticlusterGlobalHub, error) {
Expand Down Expand Up @@ -227,10 +227,11 @@ func (a *HohAgentAddon) GetValues(cluster *clusterv1.ManagedCluster,
RetryPeriod: strconv.Itoa(electionConfig.RetryPeriod),
KlusterletNamespace: "open-cluster-management-agent",
KlusterletWorkSA: "klusterlet-work-sa",
EnableGlobalResource: a.EnableGlobalResource,
EnableGlobalResource: a.operatorConfig.GlobalResourceEnabled,
AgentQPS: agentQPS,
AgentBurst: agentBurst,
LogLevel: a.LogLevel,
LogLevel: a.operatorConfig.LogLevel,
EnablePprof: a.operatorConfig.EnablePprof,
Resources: agentRes,
}

Expand Down
1 change: 1 addition & 0 deletions operator/pkg/controllers/addon/addon_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var _ = BeforeSuite(func() {
addonController, err := addon.NewAddonController(k8sManager.GetConfig(), k8sClient, &config.OperatorConfig{
GlobalResourceEnabled: true,
LogLevel: "info",
EnablePprof: false,
})
Expect(err).ToNot(HaveOccurred())
err = k8sManager.Add(addonController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ spec:
- --enable-global-resource={{.EnableGlobalResource}}
- --qps={{.AgentQPS}}
- --burst={{.AgentBurst}}
- --enable-pprof={{.EnablePprof}}
env:
- name: POD_NAMESPACE
valueFrom:
Expand Down
36 changes: 17 additions & 19 deletions operator/pkg/controllers/hubofhubs/globalhub_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,29 @@ var watchedConfigmap = sets.NewString(
type MulticlusterGlobalHubReconciler struct {
manager.Manager
client.Client
Scheme *runtime.Scheme
addonMgr addonmanager.AddonManager
KubeClient kubernetes.Interface
Log logr.Logger
LogLevel string
MiddlewareConfig *MiddlewareConfig
EnableGlobalResource bool
upgradeOnce sync.Once
KafkaInit bool
KafkaController *KafkaController
Scheme *runtime.Scheme
addonMgr addonmanager.AddonManager
KubeClient kubernetes.Interface
Log logr.Logger
MiddlewareConfig *MiddlewareConfig
OperatorConfig *config.OperatorConfig
upgradeOnce sync.Once
KafkaInit bool
KafkaController *KafkaController
}

func NewMulticlusterGlobalHubReconciler(mgr ctrl.Manager, addonMgr addonmanager.AddonManager,
kubeClient kubernetes.Interface, operatorConfig *config.OperatorConfig,
) *MulticlusterGlobalHubReconciler {
return &MulticlusterGlobalHubReconciler{
Manager: mgr,
Client: mgr.GetClient(),
addonMgr: addonMgr,
KubeClient: kubeClient,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("global-hub-reconciler"),
MiddlewareConfig: &MiddlewareConfig{},
EnableGlobalResource: operatorConfig.GlobalResourceEnabled,
LogLevel: operatorConfig.LogLevel,
Manager: mgr,
Client: mgr.GetClient(),
addonMgr: addonMgr,
KubeClient: kubeClient,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("global-hub-reconciler"),
MiddlewareConfig: &MiddlewareConfig{},
OperatorConfig: operatorConfig,
}
}

Expand Down
2 changes: 1 addition & 1 deletion operator/pkg/controllers/hubofhubs/globalhub_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *MulticlusterGlobalHubReconciler) ReconcileDatabase(ctx context.Context,
return err
}

if r.EnableGlobalResource {
if r.OperatorConfig.GlobalResourceEnabled {
if err := applySQL(ctx, conn, databaseOldFS, "database.old", readonlyUsername); err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions operator/pkg/controllers/hubofhubs/globalhub_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ func (r *MulticlusterGlobalHubReconciler) reconcileManager(ctx context.Context,
Tolerations: mgh.Spec.Tolerations,
RetentionMonth: months,
StatisticLogInterval: config.GetStatisticLogInterval(),
EnableGlobalResource: r.EnableGlobalResource,
LogLevel: r.LogLevel,
EnableGlobalResource: r.OperatorConfig.GlobalResourceEnabled,
EnablePprof: r.OperatorConfig.EnablePprof,
LogLevel: r.OperatorConfig.LogLevel,
Resources: utils.GetResources(operatorconstants.Manager, mgh.Spec.AdvancedConfig),
}, nil
})
Expand Down Expand Up @@ -257,6 +258,7 @@ type ManagerVariables struct {
RetentionMonth int
StatisticLogInterval string
EnableGlobalResource bool
EnablePprof bool
LogLevel string
Resources *corev1.ResourceRequirements
}
1 change: 1 addition & 0 deletions operator/pkg/controllers/hubofhubs/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ var _ = Describe("MulticlusterGlobalHub controller", Ordered, func() {
StatisticLogInterval: config.GetStatisticLogInterval(),
EnableGlobalResource: true,
LaunchJobNames: config.GetLaunchJobNames(mgh),
EnablePprof: false,
LogLevel: "info",
Resources: operatorutils.GetResources(operatorconstants.Manager, mgh.Spec.AdvancedConfig),
}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ spec:
{{- end}}
- --data-retention={{.RetentionMonth}}
- --statistics-log-interval={{.StatisticLogInterval}}
- --enable-pprof={{.EnablePprof}}
{{- if eq .SkipAuth true}}
- --cluster-api-url=
{{- end}}
Expand Down
19 changes: 11 additions & 8 deletions operator/pkg/controllers/hubofhubs/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ var _ = BeforeSuite(func() {
Expect(err).Should(Succeed())

mghReconciler = &hubofhubscontroller.MulticlusterGlobalHubReconciler{
Manager: k8sManager,
Client: k8sManager.GetClient(),
KubeClient: kubeClient,
Scheme: k8sManager.GetScheme(),
Log: ctrl.Log.WithName("multicluster-global-hub-reconciler"),
LogLevel: "info",
MiddlewareConfig: &hubofhubscontroller.MiddlewareConfig{},
EnableGlobalResource: true,
Manager: k8sManager,
Client: k8sManager.GetClient(),
KubeClient: kubeClient,
Scheme: k8sManager.GetScheme(),
Log: ctrl.Log.WithName("multicluster-global-hub-reconciler"),
MiddlewareConfig: &hubofhubscontroller.MiddlewareConfig{},
OperatorConfig: &config.OperatorConfig{
LogLevel: "info",
EnablePprof: false,
GlobalResourceEnabled: true,
},
}
Expect(mghReconciler.SetupWithManager(k8sManager)).ToNot(HaveOccurred())

Expand Down
19 changes: 19 additions & 0 deletions pkg/utils/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import (
"net/http"
_ "net/http/pprof" // #nosec G108
"time"

"k8s.io/klog"
)

func StartDefaultPprofServer() {
server := &http.Server{
Addr: ":6060",
ReadHeaderTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
klog.Error(err, "failed to start the pprof server")
}
}

0 comments on commit a98da42

Please sign in to comment.