Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMM-13629 fix pmm managed npe issue #3404

Open
wants to merge 8 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading