Skip to content

Commit

Permalink
Make it autoconfigure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Dec 3, 2024
1 parent 7f87c83 commit 4078afb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 65 deletions.
53 changes: 18 additions & 35 deletions cmd/sliexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cmd

import (
"os"
"strconv"
"time"

managedupgradev1beta1 "github.com/appuio/openshift-upgrade-controller/api/v1beta1"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg"
"github.com/vshn/appcat/v4/pkg/common/utils"
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"
Expand All @@ -27,9 +29,9 @@ import (
)

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

var s = sliProber{
Expand All @@ -52,16 +54,6 @@ func init() {
SLIProberCMD.Flags().StringVar(&s.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
SLIProberCMD.Flags().BoolVar(&s.leaderElect, "leader-elect", false, "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
SLIProberCMD.Flags().BoolVar(&s.enableVSHNPostgreSQL, "vshn-postgresql", getEnvBool("APPCAT_SLI_VSHNPOSTGRESQL"),
"Enable probing of VSHNPostgreSQL instances")
SLIProberCMD.Flags().BoolVar(&s.enableVSHNRedis, "vshn-redis", getEnvBool("APPCAT_SLI_VSHNREDIS"),
"Enable probing of VSHNRedis instances")
SLIProberCMD.Flags().BoolVar(&s.enableVSHNMinio, "vshn-minio", getEnvBool("APPCAT_SLI_VSHNMINIO"),
"Enable probing of VSHNMinio instances")
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")
SLIProberCMD.Flags().StringVar(&s.serviceKubeConfig, "service-kubeconfig", os.Getenv("SERVICE_KUBECONFIG"),
"Kubeconfig for the service cluster itself, usually only for debugging purpose. The sliproper should run on the service clusters and thus use the in-cluster-config.")
}
Expand All @@ -70,11 +62,6 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
log := logr.FromContextOrDiscard(cmd.Context())
ctrl.SetLogger(log)

// ctrlConfig, err := getClusterConfig(os.Getenv("KUBECONFIG"))
// if err != nil {
// return err
// }

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: s.scheme,
HealthProbeBindAddress: s.probeAddr,
Expand All @@ -89,7 +76,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return err
}

config, err := getClusterConfig(s.serviceKubeConfig)
config, err := getServiceClusterConfig()
if err != nil {
return err
}
Expand All @@ -113,7 +100,8 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return err
}

if s.enableVSHNPostgreSQL {
if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNPostgreSQL", ctrl.GetConfigOrDie()) {
log.Info("Enabling VSHNKeycloak controller")
if err = (&vshnpostgresqlcontroller.VSHNPostgreSQLReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand All @@ -125,7 +113,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return err
}
}
if s.enableVSHNRedis {
if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNRedis", ctrl.GetConfigOrDie()) {
log.Info("Enabling VSHNRedis controller")
if err = (&vshnrediscontroller.VSHNRedisReconciler{
Client: mgr.GetClient(),
Expand All @@ -139,7 +127,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
}
}

if s.enableVSHNMinio {
if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNMinio", ctrl.GetConfigOrDie()) {
log.Info("Enabling VSHNRedis controller")
if err = (&vshnminiocontroller.VSHNMinioReconciler{
Client: mgr.GetClient(),
Expand All @@ -153,7 +141,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
}
}

if s.enableKeycloak {
if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNKeycloak", ctrl.GetConfigOrDie()) {
log.Info("Enablign VSHNKeycloak controller")
if err = (&vshnkeycloakcontroller.VSHNKeycloakReconciler{
Client: mgr.GetClient(),
Expand All @@ -166,7 +154,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
}
}

if s.enableMaintenanceStatus {
if utils.IsKindAvailable(managedupgradev1beta1.GroupVersion, "UpgradeJob", ctrl.GetConfigOrDie()) {
log.Info("Enable OC maintenance observer")
serviceCluster, err := getServiceCluster()
if err != nil {
Expand All @@ -177,7 +165,7 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return err
}
}
if s.enableMariaDB {
if utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNMariaDB", ctrl.GetConfigOrDie()) {
log.Info("Enabling VSHNMariaDB controller")
if err = (&vshnmariadbcontroller.VSHNMariaDBReconciler{
Client: mgr.GetClient(),
Expand Down Expand Up @@ -210,15 +198,10 @@ func (s *sliProber) executeSLIProber(cmd *cobra.Command, _ []string) error {
return nil
}

func getEnvBool(key string) bool {
b, err := strconv.ParseBool(os.Getenv(key))
return err == nil && b
}

// serviceCluster will create an incluster config by default.
// If serviceKubeConfig is set, it will use that instead.
func getServiceCluster() (*cluster.Cluster, error) {
config, err := getClusterConfig(s.serviceKubeConfig)
config, err := getServiceClusterConfig()
if err != nil {
return nil, err
}
Expand All @@ -228,10 +211,10 @@ func getServiceCluster() (*cluster.Cluster, error) {
return &serviceCluster, err
}

func getClusterConfig(path string) (*rest.Config, error) {
func getServiceClusterConfig() (*rest.Config, error) {

if path != "" {
kubeconfig, err := os.ReadFile(path)
if s.serviceKubeConfig != "" {
kubeconfig, err := os.ReadFile(s.serviceKubeConfig)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/appcat/appcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package appcat
import (
crossplane "github.com/crossplane/crossplane/apis/apiextensions/v1"
appcatv1 "github.com/vshn/appcat/v4/apis/apiserver/v1"
"github.com/vshn/appcat/v4/pkg/apiserver"
"github.com/vshn/appcat/v4/pkg/apiserver/noop"
"github.com/vshn/appcat/v4/pkg/common/utils"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
Expand Down Expand Up @@ -37,7 +37,7 @@ func New() restbuilder.ResourceHandlerProvider {

noopImplementation := noop.New(s, &appcatv1.AppCat{}, &appcatv1.AppCatList{})

if !apiserver.IsTypeAvailable(crossplane.SchemeGroupVersion.String(), "Composition") {
if !utils.IsKindAvailable(crossplane.SchemeGroupVersion, "Composition", loopback.GetLoopbackMasterClientConfig()) {
return noopImplementation, nil
}

Expand Down
20 changes: 0 additions & 20 deletions pkg/apiserver/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"sigs.k8s.io/apiserver-runtime/pkg/util/loopback"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -91,21 +89,3 @@ func GetBackupColumnDefinition() []metav1.TableColumnDefinition {
{Name: "Age", Type: "date", Description: desc["creationTimestamp"]},
}
}

func IsTypeAvailable(gv string, kind string) bool {
d, err := discovery.NewDiscoveryClientForConfig(loopback.GetLoopbackMasterClientConfig())
if err != nil {
return false
}
resources, err := d.ServerResourcesForGroupVersion(gv)
if err != nil {
return false
}

for _, res := range resources.APIResources {
if res.Kind == kind {
return true
}
}
return false
}
4 changes: 2 additions & 2 deletions pkg/apiserver/vshn/mariadb/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
appcatv1 "github.com/vshn/appcat/v4/apis/apiserver/v1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/apiserver"
"github.com/vshn/appcat/v4/pkg/apiserver/noop"
"github.com/vshn/appcat/v4/pkg/apiserver/vshn/k8up"
"github.com/vshn/appcat/v4/pkg/common/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
Expand Down Expand Up @@ -37,7 +37,7 @@ func New() restbuilder.ResourceHandlerProvider {

noopImplementation := noop.New(s, &appcatv1.VSHNMariaDBBackup{}, &appcatv1.VSHNMariaDBBackupList{})

if !apiserver.IsTypeAvailable(vshnv1.GroupVersion.String(), "XVSHNMariaDB") {
if !utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNMariaDB", loopback.GetLoopbackMasterClientConfig()) {
return noopImplementation, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/vshn/nextcloud/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
appcatv1 "github.com/vshn/appcat/v4/apis/apiserver/v1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/apiserver"
"github.com/vshn/appcat/v4/pkg/apiserver/noop"
"github.com/vshn/appcat/v4/pkg/apiserver/vshn/k8up"
"github.com/vshn/appcat/v4/pkg/apiserver/vshn/postgres"
"github.com/vshn/appcat/v4/pkg/common/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
Expand Down Expand Up @@ -41,7 +41,7 @@ func New() restbuilder.ResourceHandlerProvider {

noopImplementation := noop.New(s, &appcatv1.VSHNNextcloudBackup{}, &appcatv1.VSHNNextcloudBackupList{})

if !apiserver.IsTypeAvailable(vshnv1.GroupVersion.String(), "XVSHNNextcloud") {
if !utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNNextcloud", loopback.GetLoopbackMasterClientConfig()) {
return noopImplementation, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/vshn/postgres/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package postgres
import (
appcatv1 "github.com/vshn/appcat/v4/apis/apiserver/v1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/apiserver"
"github.com/vshn/appcat/v4/pkg/apiserver/noop"
"github.com/vshn/appcat/v4/pkg/common/utils"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
Expand All @@ -29,7 +29,7 @@ func New() restbuilder.ResourceHandlerProvider {

noopImplementation := noop.New(s, &appcatv1.VSHNPostgresBackup{}, &appcatv1.VSHNPostgresBackupList{})

if !apiserver.IsTypeAvailable(vshnv1.GroupVersion.String(), "VSHNPostgreSQL") {
if !utils.IsKindAvailable(vshnv1.GroupVersion, "VSHNPostgreSQL", loopback.GetLoopbackMasterClientConfig()) {
return noopImplementation, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/vshn/redis/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
k8upv1 "github.com/k8up-io/k8up/v2/api/v1"
appcatv1 "github.com/vshn/appcat/v4/apis/apiserver/v1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/apiserver"
"github.com/vshn/appcat/v4/pkg/apiserver/noop"
"github.com/vshn/appcat/v4/pkg/apiserver/vshn/k8up"
"github.com/vshn/appcat/v4/pkg/common/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
Expand Down Expand Up @@ -37,7 +37,7 @@ func New() restbuilder.ResourceHandlerProvider {

noopImplementation := noop.New(s, &appcatv1.VSHNRedisBackup{}, &appcatv1.VSHNRedisBackupList{})

if !apiserver.IsTypeAvailable(vshnv1.GroupVersion.String(), "XVSHNRedis") {
if !utils.IsKindAvailable(vshnv1.GroupVersion, "XVSHNRedis", loopback.GetLoopbackMasterClientConfig()) {
return noopImplementation, nil
}

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

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
)

// IsKindAvailable will check if the given type is available
func IsKindAvailable(gv schema.GroupVersion, kind string, config *rest.Config) bool {
d, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return false
}
resources, err := d.ServerResourcesForGroupVersion(gv.String())
if err != nil {
return false
}

for _, res := range resources.APIResources {
if res.Kind == kind {
return true
}
}
return false
}

0 comments on commit 4078afb

Please sign in to comment.