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

Add logic to update configMaps on operator #10

Merged
merged 14 commits into from
Aug 12, 2021
4 changes: 0 additions & 4 deletions operator/controllers/httpscaledobject_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ func (rec *HTTPScaledObjectReconciler) removeApplicationResources(
rec.RoutingTable,
httpso.Spec.Host,
httpso.ObjectMeta.Namespace,
appInfo.InterceptorConfig.ServiceName,
appInfo.InterceptorConfig.AdminPortString(),
); err != nil {
return err
}
Expand Down Expand Up @@ -131,8 +129,6 @@ func (rec *HTTPScaledObjectReconciler) createOrUpdateApplicationResources(
httpso.Spec.ScaleTargetRef.Deployment,
),
httpso.ObjectMeta.Namespace,
appInfo.InterceptorConfig.ServiceName,
appInfo.InterceptorConfig.AdminPortString(),
); err != nil {
return err
}
Expand Down
75 changes: 52 additions & 23 deletions operator/controllers/routing_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package controllers

import (
"context"
"net/http"

"github.com/go-logr/logr"
"github.com/kedacore/http-add-on/pkg/k8s"
"github.com/kedacore/http-add-on/pkg/routing"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const routingTableName = "keda-http-routing-table"

func removeAndUpdateRoutingTable(
ctx context.Context,
lggr logr.Logger,
cl client.Client,
table *routing.Table,
host,
namespace,
interceptorSvcName,
interceptorSvcPort string,
namespace string,
) error {
lggr = lggr.WithName("removeAndUpdateRoutingTable")
if err := table.RemoveTarget(host); err != nil {
Expand All @@ -28,14 +28,8 @@ func removeAndUpdateRoutingTable(
host,
)
}
return pingInterceptors(
ctx,
cl,
http.DefaultClient,
namespace,
interceptorSvcName,
interceptorSvcPort,
)

return updateRoutingMap(ctx, lggr, cl, namespace, table)
}

func addAndUpdateRoutingTable(
Expand All @@ -45,9 +39,7 @@ func addAndUpdateRoutingTable(
table *routing.Table,
host string,
target routing.Target,
namespace,
interceptorSvcName,
interceptorSvcPort string,
namespace string,
) error {
lggr = lggr.WithName("addAndUpdateRoutingTable")
if err := table.AddTarget(host, target); err != nil {
Expand All @@ -58,12 +50,49 @@ func addAndUpdateRoutingTable(
host,
)
}
return pingInterceptors(
ctx,
cl,
http.DefaultClient,
namespace,
interceptorSvcName,
interceptorSvcPort,
)
return updateRoutingMap(ctx, lggr, cl, namespace, table)
}

func updateRoutingMap(
ctx context.Context,
lggr logr.Logger,
cl client.Client,
namespace string,
table *routing.Table,
) error {
tableAsJSON, marshalErr := table.MarshalJSON()
if marshalErr != nil {
return marshalErr
}

routingConfigMap, err := k8s.GetConfigMap(ctx, cl, namespace, routingTableName)
if err != nil {
return err
}

// if the routing table doesn't exist, we need to create it with the latest data
if routingConfigMap == nil {
routingTableData := map[string]string{
"routing-table": string(tableAsJSON),
}

routingTableLabels := map[string]string{
"control-plane": "operator",
"keda.sh/addon": "http-add-on",
"app": "http-add-on",
"name": "http-add-on-routing-table",
}

if err := k8s.CreateConfigMap(ctx, lggr, cl, k8s.NewConfigMap(namespace, routingTableName, routingTableLabels, routingTableData)); err != nil {
return err
}
} else {
newRoutingTable := routingConfigMap.DeepCopy()
newRoutingTable.Data["routing-table"] = string(tableAsJSON)
if _, patchErr := k8s.PatchConfigMap(ctx, lggr, cl, routingConfigMap, newRoutingTable); patchErr != nil {
return patchErr
}
}

return nil
}
91 changes: 91 additions & 0 deletions pkg/k8s/configMap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package k8s

import (
"context"
"fmt"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// 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
}

func CreateConfigMap (
ctx context.Context,
logger logr.Logger,
cl client.Client,
configMap *corev1.ConfigMap,
) error {
existentConfigMap, err := GetConfigMap(ctx, cl, configMap.Namespace, configMap.Name)
if err == nil {
return err
}
if existentConfigMap.Name != "" {
return fmt.Errorf("ConfigMap %s already exists", configMap.Name)
}

createErr := cl.Create(ctx, configMap)
if createErr != nil {
return createErr
}
return nil
}

func DeleteConfigMap (
ctx context.Context,
cl client.Client,
configMap *corev1.ConfigMap,
logger logr.Logger,
) error {
return cl.Delete(ctx, configMap)
}

func PatchConfigMap (
ctx context.Context,
logger logr.Logger,
cl client.Client,
originalConfigMap *corev1.ConfigMap,
patchConfigMap *corev1.ConfigMap,
) (*corev1.ConfigMap,error) {
patchErr := cl.Patch(ctx, patchConfigMap, client.MergeFrom(originalConfigMap))
if patchErr != nil { return nil, patchErr }
return patchConfigMap, nil
}

func GetConfigMap (
ctx context.Context,
cl client.Client,
namespace string,
name string,
) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{}
err := cl.Get(ctx, client.ObjectKey{ Name: name, Namespace: namespace }, configMap)
if err != nil {
return nil, err
}
return configMap, nil
}