Skip to content

Commit

Permalink
PMM-13629 fix pmm managed npe issue (#3404)
Browse files Browse the repository at this point in the history
* PMM-13629 Fix pmm-managed npe issue.

* PMM-13629 make gen.

* PMM-13629 fix tests.

* PMM-13629 Add comments.

* PMM-13629 Fix tests.

* PMM-13629 Fix linters.

* PMM-13629 Remove encryption.key.
  • Loading branch information
BupycHuk authored Jan 10, 2025
1 parent 438aff4 commit 07df3de
Show file tree
Hide file tree
Showing 33 changed files with 501 additions and 675 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ api-tests/pmm-api-tests-output.txt
api-tests/pmm-api-tests-junit-report.xml

packer.log
encryption.key
96 changes: 29 additions & 67 deletions managed/models/agent_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ type MySQLOptionsParams interface {
}

// MySQLOptionsFromRequest creates MySQLOptions object from request.
func MySQLOptionsFromRequest(params MySQLOptionsParams) *MySQLOptions {
if params.GetTlsCa() != "" || params.GetTlsCert() != "" || params.GetTlsKey() != "" {
return &MySQLOptions{
TLSCa: params.GetTlsCa(),
TLSCert: params.GetTlsCert(),
TLSKey: params.GetTlsKey(),
}
func MySQLOptionsFromRequest(params MySQLOptionsParams) MySQLOptions {
return MySQLOptions{
TLSCa: params.GetTlsCa(),
TLSCert: params.GetTlsCert(),
TLSKey: params.GetTlsKey(),
}
return &MySQLOptions{}
}

// PostgreSQLOptionsParams contains methods to create PostgreSQLOptions object.
Expand All @@ -69,13 +66,11 @@ type PostgreSQLExtendedOptionsParams interface {
}

// PostgreSQLOptionsFromRequest creates PostgreSQLOptions object from request.
func PostgreSQLOptionsFromRequest(params PostgreSQLOptionsParams) *PostgreSQLOptions {
res := &PostgreSQLOptions{}
if params.GetTlsCa() != "" || params.GetTlsCert() != "" || params.GetTlsKey() != "" {
res.SSLCa = params.GetTlsCa()
res.SSLCert = params.GetTlsCert()
res.SSLKey = params.GetTlsKey()
}
func PostgreSQLOptionsFromRequest(params PostgreSQLOptionsParams) PostgreSQLOptions {
res := PostgreSQLOptions{}
res.SSLCa = params.GetTlsCa()
res.SSLCert = params.GetTlsCert()
res.SSLKey = params.GetTlsKey()

// PostgreSQL exporter has these parameters but they are not needed for QAN agent.
if extendedOptions, ok := params.(PostgreSQLExtendedOptionsParams); ok && extendedOptions != nil {
Expand Down Expand Up @@ -103,20 +98,15 @@ type MongoDBExtendedOptionsParams interface {
}

// MongoDBOptionsFromRequest creates MongoDBOptionsParams object from request.
func MongoDBOptionsFromRequest(params MongoDBOptionsParams) *MongoDBOptions {
mdbOptions := &MongoDBOptions{}
func MongoDBOptionsFromRequest(params MongoDBOptionsParams) MongoDBOptions {
mdbOptions := MongoDBOptions{}

if params.GetTlsCertificateKey() != "" || params.GetTlsCertificateKeyFilePassword() != "" || params.GetTlsCa() != "" {
mdbOptions = &MongoDBOptions{}
mdbOptions.TLSCertificateKey = params.GetTlsCertificateKey()
mdbOptions.TLSCertificateKeyFilePassword = params.GetTlsCertificateKeyFilePassword()
mdbOptions.TLSCa = params.GetTlsCa()
}
mdbOptions.TLSCertificateKey = params.GetTlsCertificateKey()
mdbOptions.TLSCertificateKeyFilePassword = params.GetTlsCertificateKeyFilePassword()
mdbOptions.TLSCa = params.GetTlsCa()

if params.GetAuthenticationMechanism() != "" || params.GetAuthenticationDatabase() != "" {
mdbOptions.AuthenticationMechanism = params.GetAuthenticationMechanism()
mdbOptions.AuthenticationDatabase = params.GetAuthenticationDatabase()
}
mdbOptions.AuthenticationMechanism = params.GetAuthenticationMechanism()
mdbOptions.AuthenticationDatabase = params.GetAuthenticationDatabase()

// MongoDB exporter has these parameters but they are not needed for QAN agent.
if extendedOptions, ok := params.(MongoDBExtendedOptionsParams); ok {
Expand All @@ -140,18 +130,18 @@ type AzureOptionsParams interface {
}

// AzureOptionsFromRequest creates AzureOptions object from request.
func AzureOptionsFromRequest(params AzureOptionsParams) *AzureOptions {
func AzureOptionsFromRequest(params AzureOptionsParams) AzureOptions {
if params.GetAzureSubscriptionId() != "" || params.GetAzureClientId() != "" || params.GetAzureClientSecret() != "" ||
params.GetAzureTenantId() != "" || params.GetAzureResourceGroup() != "" {
return &AzureOptions{
return AzureOptions{
SubscriptionID: params.GetAzureSubscriptionId(),
ClientID: params.GetAzureClientId(),
ClientSecret: params.GetAzureClientSecret(),
TenantID: params.GetAzureTenantId(),
ResourceGroup: params.GetAzureResourceGroup(),
}
}
return nil
return AzureOptions{}
}

func checkUniqueAgentID(q *reform.Querier, id string) error {
Expand Down Expand Up @@ -644,7 +634,7 @@ func CreateNodeExporter(q *reform.Querier,
PMMAgentID: &pmmAgentID,
NodeID: pmmAgent.RunsOnNodeID,
AgentPassword: agentPassword,
ExporterOptions: &ExporterOptions{
ExporterOptions: ExporterOptions{
ExposeExporter: exposeExporter,
PushMetrics: pushMetrics,
DisabledCollectors: disableCollectors,
Expand Down Expand Up @@ -734,7 +724,7 @@ func CreateExternalExporter(q *reform.Querier, params *CreateExternalExporterPar
Username: pointer.ToStringOrNil(params.Username),
Password: pointer.ToStringOrNil(params.Password),
ListenPort: pointer.ToUint16(uint16(params.ListenPort)),
ExporterOptions: &ExporterOptions{
ExporterOptions: ExporterOptions{
PushMetrics: params.PushMetrics,
MetricsPath: metricsPath,
MetricsScheme: scheme,
Expand Down Expand Up @@ -765,13 +755,13 @@ type CreateAgentParams struct {
TLS bool
TLSSkipVerify bool
LogLevel string
ExporterOptions *ExporterOptions
QANOptions *QANOptions
AWSOptions *AWSOptions
AzureOptions *AzureOptions
MongoDBOptions *MongoDBOptions
MySQLOptions *MySQLOptions
PostgreSQLOptions *PostgreSQLOptions
ExporterOptions ExporterOptions
QANOptions QANOptions
AWSOptions AWSOptions
AzureOptions AzureOptions
MongoDBOptions MongoDBOptions
MySQLOptions MySQLOptions
PostgreSQLOptions PostgreSQLOptions
}

func compatibleNodeAndAgent(nodeType NodeType, agentType AgentType) bool {
Expand Down Expand Up @@ -852,41 +842,13 @@ func compatibleServiceAndAgent(serviceType ServiceType, agentType AgentType) boo
return false
}

func prepareOptionsParams(params *CreateAgentParams) *CreateAgentParams {
if params.ExporterOptions == nil {
params.ExporterOptions = &ExporterOptions{}
}
if params.QANOptions == nil {
params.QANOptions = &QANOptions{}
}
if params.AWSOptions == nil {
params.AWSOptions = &AWSOptions{}
}
if params.AzureOptions == nil {
params.AzureOptions = &AzureOptions{}
}
if params.MongoDBOptions == nil {
params.MongoDBOptions = &MongoDBOptions{}
}
if params.MySQLOptions == nil {
params.MySQLOptions = &MySQLOptions{}
}
if params.PostgreSQLOptions == nil {
params.PostgreSQLOptions = &PostgreSQLOptions{}
}

return params
}

// CreateAgent creates Agent with given type.
func CreateAgent(q *reform.Querier, agentType AgentType, params *CreateAgentParams) (*Agent, error) { //nolint:unparam
id := uuid.New().String()
if err := checkUniqueAgentID(q, id); err != nil {
return nil, err
}

params = prepareOptionsParams(params)

pmmAgent, err := FindAgentByID(q, params.PMMAgentID)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 07df3de

Please sign in to comment.