Crane 推荐组件源码笔记
选自我的笔记,项目非常庞大,希望给各位同好一点启发,这里主要还是推荐部分模块的源码走读
架构
The project structure is organized into several directories and files, each serving a specific purpose. Here’s a brief overview of the main components
- cmd/
- metric-adapter/: Contains code related to the metric adapter component
- craned/: Contains the main application code for the
craned
command- app/: Likely contains application-specific logic and configurations
main.go
: The entry point for thecraned
application
- crane-agent/: Contains code related to the crane agent component
- deploy/: Contains deployment scripts and configurations for deploying the application
- docs/: Documentation files for the project
- examples/: Example configurations or usage examples for the project
- hack/: Scripts and utilities for development and maintenance tasks
- overrides/: Possibly contains configuration overrides or custom settings
- pkg/: Contains the core library code, which can be used by other applications or commands
- site/: Could be related to the project’s website or documentation site
- tools/: Additional tools or scripts that support the project
- Files:
go.mod
andgo.sum
: Go module files that manage dependenciesREADME.md
andREADME_zh.md
: Documentation files providing an overview and instructions for the projectMakefile
: Contains build instructions and tasks for the projectDockerfile
: Instructions for building a Docker image of the application.gitignore
: Specifies files and directories to be ignored by Git.golangci.yaml
: Configuration for GolangCI, a Go linterCONTRIBUTING.md
andCODE_OF_CONDUCT.md
: Guidelines for contributing to the project and expected behavior
Manager 部分
- Command Initialization:
NewManagerCommand(ctx context.Context) *cobra.Command
:- Creates a new Cobra command for
craned
- Initializes options using
options.NewOptions()
- Sets up command flags and feature gates
- Creates a new Cobra command for
- Command Execution:
- The
Run
function within the Cobra command is executed:- Calls
opts.Complete()
to complete option setup - Validates options with
opts.Validate()
- Executes
Run(ctx, opts)
to start the manager
- Calls
- The
- Run Function:
Run(ctx context.Context, opts *options.Options) error
:- Configures the controller manager with
ctrl.GetConfigOrDie()
- Sets up controller options, including leader election and metrics binding
- Creates a new manager with
ctrl.NewManager(config, ctrlOptions)
- Configures the controller manager with
- Health Checks:
- Adds health and readiness checks using
mgr.AddHealthzCheck
andmgr.AddReadyzCheck
- Adds health and readiness checks using
- Data Sources and Predictor Initialization:
initDataSources(mgr, opts)
: Initializes real-time and historical data sourcesinitPredictorManager(opts, realtimeDataSources, historyDataSources)
: Sets up the predictor manager using the initialized data sources
- Scheme and Indexer Initialization:
initScheme()
: Registers API types with the runtime schemeinitFieldIndexer(mgr)
: Sets up field indexers for efficient querying
- Webhooks Initialization:
initWebhooks(mgr, opts)
: Configures webhooks if necessary
- Pod OOM Recorder:
- Initializes
oom.PodOOMRecorder
to track out-of-memory events - Sets up the recorder with the manager and runs it in a separate goroutine
- Initializes
- Controller Initialization:
initControllers(ctx, podOOMRecorder, mgr, opts, predictorMgr, historyDataSources[providers.PrometheusDataSource])
: Initializes various controllers, including analytics and recommendation controllers
- Metric Collector Initialization:
initMetricCollector(mgr)
: Sets up custom metrics collection
- Running All Components:
runAll(ctx, mgr, predictorMgr, dataSourceProviders[providers.PrometheusDataSource], opts)
:- Starts all components, ensuring that the manager and predictor manager are running
Controllers 部分
PodOOMRecorder
- SetupWithManager:
SetupWithManager(mgr ctrl.Manager) error
:- This function is called to register the
PodOOMRecorder
with the controller manager - It sets up the reconciler to watch for pod events and trigger the
Reconcile
function
- This function is called to register the
- Reconcile Function:
Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error)
:- Triggered by the controller manager for each pod event
- Retrieves the pod specified in the request
- Checks if the pod was terminated due to an OOM event using
IsOOMKilled(pod)
- If OOMKilled, iterates over container statuses to find the terminated container
- For each OOMKilled container, checks if memory requests are set
- Increments the OOM count metric and adds an
OOMRecord
to the queue
- Run Function:
Run(stopCh <-chan struct{}) error
:- Continuously processes the OOM records from the queue
- Uses a ticker to periodically clean old OOM records to prevent exceeding ConfigMap storage limits
- Retrieves OOM records using
GetOOMRecord
- Updates OOM records with
updateOOMRecord
- GetOOMRecord Function:
GetOOMRecord() ([]OOMRecord, error)
:- Retrieves OOM records from the cache or ConfigMap
- If the cache is nil, it fetches the ConfigMap and unmarshals the OOM records
- cleanOOMRecords Function:
cleanOOMRecords(oomRecords []OOMRecord) []OOMRecord
:- Cleans up old OOM records to maintain the maximum number specified by
OOMRecordMaxNumber
- Cleans up old OOM records to maintain the maximum number specified by
- updateOOMRecord Function:
updateOOMRecord(oomRecord OOMRecord, saved []OOMRecord) error
:- Updates the OOM records in the cache or storage with new records
- IsOOMKilled Function:
IsOOMKilled(pod *v1.Pod) bool
:- Helper function to determine if a pod was terminated due to an OOM event
Detailed Flow
- Initialization: The
PodOOMRecorder
is set up with the manager usingSetupWithManager
, which registers it to watch for pod events - Event Handling: For each pod event,
Reconcile
is called to check for OOM events and record them - Record Management: The
Run
function processes the queue of OOM records, periodically cleans old records, and updates the storage - Data Retrieval:
GetOOMRecord
fetches the current OOM records, either from the cache or the ConfigMap
This detailed explanation provides a comprehensive view of the PodOOMRecorder
’s functionality and its role in tracking OOM events in a Kubernetes cluster