diff --git a/operator/controllers/config/config.go b/operator/controllers/config/config.go index 051b90856..7b4f0a817 100644 --- a/operator/controllers/config/config.go +++ b/operator/controllers/config/config.go @@ -22,7 +22,8 @@ type ExternalScaler struct { } type Base struct { - TargetPendingRequests int32 `envconfig:"TARGET_PENDING_REQUESTS" default:"100"` + TargetPendingRequests int32 `envconfig:"TARGET_PENDING_REQUESTS" default:"100"` + Namespace string `envconfig:"NAMESPACE" required:"true"` } func NewBaseFromEnv() (*Base, error) { diff --git a/operator/controllers/routing_table.go b/operator/controllers/routing_table.go index ab55c8402..8ca26291d 100644 --- a/operator/controllers/routing_table.go +++ b/operator/controllers/routing_table.go @@ -7,7 +7,6 @@ import ( "github.com/kedacore/http-add-on/pkg/k8s" "github.com/kedacore/http-add-on/pkg/routing" pkgerrs "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -62,55 +61,18 @@ func updateRoutingMap( ) error { lggr = lggr.WithName("updateRoutingMap") routingConfigMap, err := k8s.GetConfigMap(ctx, cl, namespace, routing.ConfigMapRoutingTableName) - if err != nil && !errors.IsNotFound(err) { - // if there is an error other than not found on the ConfigMap, we should - // fail - lggr.Error( - err, - "other issue fetching the routing table ConfigMap", - "configMapName", - routing.ConfigMapRoutingTableName, - ) + if err != nil { + lggr.Error(err, "Error getting configmap", "configMapName", routing.ConfigMapRoutingTableName) return pkgerrs.Wrap(err, "routing table ConfigMap fetch error") - } else if errors.IsNotFound(err) || routingConfigMap == nil { - // if either the routing table ConfigMap doesn't exist or for some reason it's - // nil in memory, we need to create it - lggr.Info( - "routing table ConfigMap didn't exist, creating it", - "configMapName", - routing.ConfigMapRoutingTableName, - ) - routingTableLabels := map[string]string{ - "control-plane": "operator", - "keda.sh/addon": "http-add-on", - "app": "http-add-on", - "name": "http-add-on-routing-table", - } - cm := k8s.NewConfigMap( - namespace, - routing.ConfigMapRoutingTableName, - routingTableLabels, - map[string]string{}, - ) - if err := routing.SaveTableToConfigMap(table, cm); err != nil { - return err - } - if err := k8s.CreateConfigMap( - ctx, - lggr, - cl, - cm, - ); err != nil { - return err - } - } else { - newCM := routingConfigMap.DeepCopy() - if err := routing.SaveTableToConfigMap(table, newCM); err != nil { - return err - } - if _, patchErr := k8s.PatchConfigMap(ctx, lggr, cl, routingConfigMap, newCM); patchErr != nil { - return patchErr - } + } + newCM := routingConfigMap.DeepCopy() + if err := routing.SaveTableToConfigMap(table, newCM); err != nil { + lggr.Error(err, "couldn't save new routing table to ConfigMap", "configMap", routing.ConfigMapRoutingTableName) + return pkgerrs.Wrap(err, "ConfigMap save error") + } + if _, err := k8s.PatchConfigMap(ctx, lggr, cl, routingConfigMap, newCM); err != nil { + lggr.Error(err, "couldn't save new routing table ConfigMap to Kubernetes", "configMap", routing.ConfigMapRoutingTableName) + return pkgerrs.Wrap(err, "saving routing table ConfigMap to Kubernetes") } return nil diff --git a/operator/main.go b/operator/main.go index 0c8995077..9f0a5d334 100644 --- a/operator/main.go +++ b/operator/main.go @@ -35,6 +35,7 @@ import ( "github.com/kedacore/http-add-on/operator/controllers" "github.com/kedacore/http-add-on/operator/controllers/config" kedahttp "github.com/kedacore/http-add-on/pkg/http" + "github.com/kedacore/http-add-on/pkg/k8s" "github.com/kedacore/http-add-on/pkg/routing" // +kubebuilder:scaffold:imports ) @@ -66,9 +67,29 @@ func main() { "The port on which to run the admin server. This is the port on which RPCs will be accepted to get the routing table", ) flag.Parse() + interceptorCfg, err := config.NewInterceptorFromEnv() + if err != nil { + setupLog.Error(err, "unable to get interceptor configuration") + os.Exit(1) + } + externalScalerCfg, err := config.NewExternalScalerFromEnv() + if err != nil { + setupLog.Error(err, "unable to get external scaler configuration") + os.Exit(1) + } + baseConfig, err := config.NewBaseFromEnv() + if err != nil { + setupLog.Error( + err, + "unable to get base configuration", + ) + os.Exit(1) + } ctrl.SetLogger(zap.New(zap.UseDevMode(true))) + ctx := context.Background() + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, @@ -81,27 +102,22 @@ func main() { os.Exit(1) } - interceptorCfg, err := config.NewInterceptorFromEnv() - if err != nil { - setupLog.Error(err, "unable to get interceptor configuration") - os.Exit(1) - } - externalScalerCfg, err := config.NewExternalScalerFromEnv() - if err != nil { - setupLog.Error(err, "unable to get external scaler configuration") - os.Exit(1) - } - baseConfig, err := config.NewBaseFromEnv() - if err != nil { + cl := mgr.GetClient() + namespace := baseConfig.Namespace + // crash if the routing table ConfigMap couldn't be found + if _, err := k8s.GetConfigMap(ctx, cl, namespace, routing.ConfigMapRoutingTableName); err != nil { setupLog.Error( err, - "unable to get base configuration", + "Couldn't fetch routing table config map", + "configMapName", + routing.ConfigMapRoutingTableName, ) os.Exit(1) } + routingTable := routing.NewTable() if err := (&controllers.HTTPScaledObjectReconciler{ - Client: mgr.GetClient(), + Client: cl, Log: ctrl.Log.WithName("controllers").WithName("HTTPScaledObject"), Scheme: mgr.GetScheme(), InterceptorConfig: *interceptorCfg, @@ -114,7 +130,6 @@ func main() { } // +kubebuilder:scaffold:builder - ctx := context.Background() errGrp, _ := errgroup.WithContext(ctx) // start the control loop diff --git a/pkg/k8s/config_map.go b/pkg/k8s/config_map.go index a4a46dffa..abd32d05f 100644 --- a/pkg/k8s/config_map.go +++ b/pkg/k8s/config_map.go @@ -38,71 +38,6 @@ type ConfigMapGetterWatcher interface { ConfigMapWatcher } -// newConfigMap creates a new configMap structure -func NewConfigMap( - namespace string, - name string, - labels map[string]string, - data map[string]string, -) *corev1.ConfigMap { - - configMap := &corev1.ConfigMap{ - TypeMeta: metav1.TypeMeta{ - Kind: "ConfigMap", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - Labels: labels, - }, - Data: data, - } - - return configMap -} - -// CreateConfigMap sends a request to Kubernetes using the client cl -// to create configMap. Returns a non-nil error if anything failed with the creation, -// including if the config map already existed. -func CreateConfigMap( - ctx context.Context, - logger logr.Logger, - cl client.Writer, - configMap *corev1.ConfigMap, -) error { - logger = logger.WithName("pkg.k8s.CreateConfigMap") - if err := cl.Create(ctx, configMap); err != nil { - logger.Error( - err, - "failed to create ConfigMap", - "configMap", - *configMap, - ) - return err - } - return nil -} - -func DeleteConfigMap( - ctx context.Context, - cl client.Writer, - configMap *corev1.ConfigMap, - logger logr.Logger, -) error { - logger = logger.WithName("pkg.k8s.DeleteConfigMap") - err := cl.Delete(ctx, configMap) - if err != nil { - logger.Error( - err, - "failed to delete configmap", - "configMap", - *configMap, - ) - return err - } - return nil -} - func PatchConfigMap( ctx context.Context, logger logr.Logger,