Skip to content

Commit

Permalink
Merge pull request #153 from vshn/add/mariadb_metrics
Browse files Browse the repository at this point in the history
Add/mariadb metrics
  • Loading branch information
wejdross authored Apr 29, 2024
2 parents 8a1fea4 + 43bab13 commit e4c123a
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 4 deletions.
1 change: 0 additions & 1 deletion apis/vshn/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func init() {
&XVSHNKeycloakList{},
&VSHNKeycloakList{},
&VSHNKeycloak{},

&XVSHNMariaDB{},
&XVSHNMariaDBList{},
&VSHNMariaDB{},
Expand Down
21 changes: 18 additions & 3 deletions cmd/sliexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
maintenancecontroller "github.com/vshn/appcat/v4/pkg/sliexporter/maintenance_controller"
"github.com/vshn/appcat/v4/pkg/sliexporter/probes"
vshnkeycloakcontroller "github.com/vshn/appcat/v4/pkg/sliexporter/vshnkeycloak_controller"
vshnmariadbcontroller "github.com/vshn/appcat/v4/pkg/sliexporter/vshnmariadb_controller"
vshnminiocontroller "github.com/vshn/appcat/v4/pkg/sliexporter/vshnminio_controller"
vshnpostgresqlcontroller "github.com/vshn/appcat/v4/pkg/sliexporter/vshnpostgresql_controller"
vshnrediscontroller "github.com/vshn/appcat/v4/pkg/sliexporter/vshnredis_controller"
Expand All @@ -22,9 +23,9 @@ import (
)

type sliProber struct {
scheme *runtime.Scheme
metricsAddr, probeAddr string
leaderElect, enableVSHNPostgreSQL, enableVSHNRedis, enableVSHNMinio, enableMaintenanceStatus, enableKeycloak bool
scheme *runtime.Scheme
metricsAddr, probeAddr string
leaderElect, enableVSHNPostgreSQL, enableVSHNRedis, enableVSHNMinio, enableMaintenanceStatus, enableKeycloak, enableMariaDB bool
}

var s = sliProber{
Expand Down Expand Up @@ -52,6 +53,7 @@ func init() {
SLIProberCMD.Flags().BoolVar(&s.enableMaintenanceStatus, "vshn-track-oc-maintenance-status", getEnvBool("APPCAT_SLI_TRACK_OC_MAINTENANCE_STATUS"),
"Enable oc maintenance status observer. Will set the labels 'maintenance' accordingly.")
SLIProberCMD.Flags().BoolVar(&s.enableKeycloak, "vshn-keycloak", getEnvBool("APPCAT_SLI_VSHNKEYCLOAK"), "Enable probing of VSHNKeycloak instances")
SLIProberCMD.Flags().BoolVar(&s.enableMariaDB, "vshn-mariadb", getEnvBool("APPCAT_SLI_VSHNMARIADB"), "Enable probing of VSHNMariaDB instances")
}

func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -144,6 +146,19 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return err
}
}
if s.enableMariaDB {
log.Info("Enabling VSHNMariaDB controller")
if err = (&vshnmariadbcontroller.VSHNMariaDBReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ProbeManager: &probeManager,
StartupGracePeriod: 1 * time.Minute,
MariaDBDialer: probes.NewMariaDB,
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "VSHNMariadb")
return err
}
}

//+kubebuilder:scaffold:builder

Expand Down
28 changes: 28 additions & 0 deletions config/sliexporter/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ rules:
- get
- list
- watch
- apiGroups:
- vshn.appcat.vshn.io
resources:
- vshnmariadbs
verbs:
- get
- list
- watch
- apiGroups:
- vshn.appcat.vshn.io
resources:
- vshnmariadbs/status
verbs:
- get
- apiGroups:
- vshn.appcat.vshn.io
resources:
Expand Down Expand Up @@ -84,6 +98,20 @@ rules:
- xvshnkeycloaks/status
verbs:
- get
- apiGroups:
- vshn.appcat.vshn.io
resources:
- xvshnmariadbs
verbs:
- get
- list
- watch
- apiGroups:
- vshn.appcat.vshn.io
resources:
- xvshnmariadbs/status
verbs:
- get
- apiGroups:
- vshn.appcat.vshn.io
resources:
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ replace github.com/deepmap/oapi-codegen => github.com/sljeff/oapi-codegen v1.5.1

require (
dario.cat/mergo v1.0.0
github.com/alecthomas/assert/v2 v2.1.0
github.com/appuio/appuio-cloud-reporting v0.15.0
github.com/appuio/openshift-upgrade-controller v0.5.1
github.com/blang/semver/v4 v4.0.0
Expand Down Expand Up @@ -52,6 +53,7 @@ require (
require (
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.29 // indirect
Expand All @@ -65,6 +67,7 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/alecthomas/kong v0.8.1 // indirect
github.com/alecthomas/repr v0.1.0 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
Expand Down Expand Up @@ -120,6 +123,7 @@ require (
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20230919002926-dbcd01c402b2 // indirect
github.com/google/go-containerregistry/pkg/authn/kubernetes v0.0.0-20230516205744-dbecb1de8cfa // indirect
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
Expand Down Expand Up @@ -183,6 +187,7 @@ require (
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-sql-driver/mysql v1.8.1
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGB
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
Expand Down Expand Up @@ -275,6 +277,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
Expand Down
3 changes: 3 additions & 0 deletions pkg/comp-functions/functions/vshnmariadb/mariadb_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func newValues(ctx context.Context, svc *runtime.ServiceRuntime, comp *vshnv1.VS
"containerSecurityContext": map[string]interface{}{
"enabled": !svc.GetBoolFromCompositionConfig("isOpenshift"),
},
"serviceMonitor": map[string]interface{}{
"enabled": true,
},
},
"securityContext": map[string]interface{}{
"enabled": !svc.GetBoolFromCompositionConfig("isOpenshift"),
Expand Down
99 changes: 99 additions & 0 deletions pkg/sliexporter/probes/mariadb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package probes

import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"errors"
"fmt"

"github.com/go-sql-driver/mysql"
)

var _ Prober = MariaDB{}

// MariaDB is a prober to test the uptime of a MariaDB instance.
type MariaDB struct {
db *sql.DB

Service string
Name string
Namespace string
Organization string
HighAvailable bool
ServiceLevel string
}

// Close closes open connections to the MariaDB server.
func (p MariaDB) Close() error {
if p.db != nil {
p.db.Close()
}
return nil
}

// GetInfo returns the prober infos
func (p MariaDB) GetInfo() ProbeInfo {
return ProbeInfo{
Service: p.Service,
Name: p.Name,
Namespace: p.Namespace,
Organization: p.Organization,
HighAvailable: p.HighAvailable,
ServiceLevel: p.ServiceLevel,
}
}

// Probe sends a test query to the configured MariaDB server.
// Will return an error if the prober does not have a valid db connection.
func (p MariaDB) Probe(ctx context.Context) error {
if p.db == nil {
return errors.New("invalid credentials")
}
_, err := p.db.Exec("SELECT 1")
return err
}

// NewMariaDB connects to the provided dsn and returns a prober
func NewMariaDB(service, name, namespace, dsn, organization, caCRT, serviceLevel string, ha, TLSEnabled bool) (*MariaDB, error) {
// regardless of the TLS setting, ca.crt is present in connection secret, therefore, it's safe to keep self-signed cert in a pool
rootCAs := x509.NewCertPool()
// open connection to MariaDB
if ok := rootCAs.AppendCertsFromPEM([]byte(caCRT)); !ok {
return nil, fmt.Errorf("failed to append PEM")
}

mysql.RegisterTLSConfig(name, &tls.Config{
RootCAs: rootCAs,
})
if TLSEnabled {
// tls must be set to custom name, not into "custom" as it's not supported by the driver
// name is unique, so collision is impossible
dsn = dsn + "?tls=" + name
}
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}

return &MariaDB{
db: db,
Service: service,
Name: name,
Namespace: namespace,
Organization: organization,
HighAvailable: ha,
ServiceLevel: serviceLevel,
}, nil
}

// NewFailingMariaDB creates a prober that will fail.
// Can be used if the controller can't access valid credentials.
func NewFailingMariaDB(service, name, namespace string) (*MariaDB, error) {
return &MariaDB{
Service: service,
Name: name,
Namespace: namespace,
}, nil
}
Loading

0 comments on commit e4c123a

Please sign in to comment.