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

Fixing issue 271 #300

Merged
merged 10 commits into from
Nov 5, 2021
3 changes: 2 additions & 1 deletion operator/controllers/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 11 additions & 49 deletions operator/controllers/routing_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
45 changes: 30 additions & 15 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -114,7 +130,6 @@ func main() {
}
// +kubebuilder:scaffold:builder

ctx := context.Background()
errGrp, _ := errgroup.WithContext(ctx)

// start the control loop
Expand Down
65 changes: 0 additions & 65 deletions pkg/k8s/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down