Skip to content

Commit

Permalink
fix: HTTPScaledObject is the owner of the underlying ScaledObject (#704)
Browse files Browse the repository at this point in the history
* fix: HTTPScaledObject is the owner of the underlying ScaledObject

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

* remove not needed code

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>

* update e2e tests

Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>

---------

Signed-off-by: Jorge Turrado <jorge.turrado@scrm.lidl>
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer authored Jun 16, 2023
1 parent d333a0e commit ce37228
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 117 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This changelog keeps track of work items that have been completed and are ready

### Fixes

- **General**: HTTPScaledObject is the owner of the underlying ScaledObject ([#703](https://github.com/kedacore/http-add-on/issues/703))
- **Routing**: Lookup host without port ([#608](https://github.com/kedacore/http-add-on/issues/608))
- **Controller**: Use kedav1alpha1.ScaledObject default values ([#607](https://github.com/kedacore/http-add-on/issues/607))
- **General**: Changes to HTTPScaledObjects now take effect ([#605](https://github.com/kedacore/http-add-on/issues/605))
Expand Down
73 changes: 2 additions & 71 deletions operator/controllers/http/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,14 @@ import (
"context"

"github.com/go-logr/logr"
apierrs "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
"github.com/kedacore/http-add-on/operator/controllers/http/config"
)

func removeApplicationResources(
ctx context.Context,
logger logr.Logger,
cl client.Client,
httpso *v1alpha1.HTTPScaledObject,
) error {
defer SaveStatus(context.Background(), logger, cl, httpso)
// Set initial statuses
AddCondition(
httpso,
*SetMessage(
CreateCondition(
v1alpha1.Terminating,
v1.ConditionUnknown,
v1alpha1.TerminatingResources,
),
"Received termination signal",
),
)

logger = logger.WithValues(
"reconciler.appObjects",
"removeObjects",
"HTTPScaledObject.name",
httpso.Name,
"HTTPScaledObject.namespace",
httpso.Namespace,
)

// Delete App ScaledObject
scaledObject := &unstructured.Unstructured{}
scaledObject.SetNamespace(httpso.Namespace)
scaledObject.SetName(httpso.Name)
scaledObject.SetGroupVersionKind(schema.GroupVersionKind{
Group: "keda.sh",
Kind: "ScaledObject",
Version: "v1alpha1",
})
if err := cl.Delete(ctx, scaledObject); err != nil {
if apierrs.IsNotFound(err) {
logger.Info("App ScaledObject not found, moving on")
} else {
logger.Error(err, "Deleting scaledobject")
AddCondition(
httpso,
*SetMessage(
CreateCondition(
v1alpha1.Error,
v1.ConditionFalse,
v1alpha1.AppScaledObjectTerminationError,
),
err.Error(),
),
)
return err
}
}
AddCondition(httpso, *CreateCondition(
v1alpha1.Terminated,
v1.ConditionTrue,
v1alpha1.AppScaledObjectTerminated,
))

return nil
}

func createOrUpdateApplicationResources(
func (r *HTTPScaledObjectReconciler) createOrUpdateApplicationResources(
ctx context.Context,
logger logr.Logger,
cl client.Client,
Expand Down Expand Up @@ -114,7 +45,7 @@ func createOrUpdateApplicationResources(
// the app deployment and the interceptor deployment.
// this needs to be submitted so that KEDA will scale both the app and
// interceptor
return createOrUpdateScaledObject(
return r.createOrUpdateScaledObject(
ctx,
cl,
logger,
Expand Down
36 changes: 3 additions & 33 deletions operator/controllers/http/httpscaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

if httpso.GetDeletionTimestamp() != nil {
logger.Info("Deletion timestamp found", "httpscaledobject", *httpso)
// if it was marked deleted, delete all the related objects
// and don't schedule for another reconcile. Kubernetes
// will finalize them
// TODO: move this function call into `finalizeScaledObject`
removeErr := removeApplicationResources(
ctx,
logger,
r.Client,
httpso,
)
if removeErr != nil {
// if we failed to remove app resources, reschedule a reconcile so we can try
// again
logger.Error(removeErr, "Removing application objects")
return ctrl.Result{
RequeueAfter: 1000 * time.Millisecond,
}, removeErr
}
// after we've deleted app objects, we can finalize
return ctrl.Result{}, finalizeScaledObject(ctx, logger, r.Client, httpso)
}

Expand All @@ -120,25 +100,15 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req
)

// Create required app objects for the application defined by the CRD
if err := createOrUpdateApplicationResources(
err := r.createOrUpdateApplicationResources(
ctx,
logger,
r.Client,
r.BaseConfig,
r.ExternalScalerConfig,
httpso,
); err != nil {
// if we failed to create app resources, remove what we've created and exit
logger.Error(err, "Removing app resources")
if removeErr := removeApplicationResources(
ctx,
logger,
r.Client,
httpso,
); removeErr != nil {
logger.Error(removeErr, "Removing previously created resources")
}

)
if err != nil {
return ctrl.Result{}, err
}

Expand Down
12 changes: 9 additions & 3 deletions operator/controllers/http/scaled_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

httpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
"github.com/kedacore/http-add-on/pkg/k8s"
Expand All @@ -18,7 +19,7 @@ import (
// according to the given parameters. If the create failed because the
// ScaledObject already exists, attempts to patch the scaledobject.
// otherwise, fails.
func createOrUpdateScaledObject(
func (r *HTTPScaledObjectReconciler) createOrUpdateScaledObject(
ctx context.Context,
cl client.Client,
logger logr.Logger,
Expand Down Expand Up @@ -46,6 +47,11 @@ func createOrUpdateScaledObject(
httpso.Spec.CooldownPeriod,
)

// Set HTTPScaledObject instance as the owner and controller
if err := controllerutil.SetControllerReference(httpso, appScaledObject, r.Scheme); err != nil {
return err
}

logger.Info("Creating App ScaledObject", "ScaledObject", *appScaledObject)
if err := cl.Create(ctx, appScaledObject); err != nil {
if errors.IsAlreadyExists(err) {
Expand Down Expand Up @@ -98,11 +104,11 @@ func createOrUpdateScaledObject(
),
)

return purgeLegacySO(ctx, cl, logger, httpso)
return r.purgeLegacySO(ctx, cl, logger, httpso)
}

// TODO(pedrotorres): delete this on v0.6.0
func purgeLegacySO(
func (r *HTTPScaledObjectReconciler) purgeLegacySO(
ctx context.Context,
cl client.Client,
logger logr.Logger,
Expand Down
19 changes: 15 additions & 4 deletions operator/controllers/http/scaled_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
"github.com/kedacore/http-add-on/operator/controllers/http/config"
)

const externalScalerHostName = "mysvc.myns.svc.cluster.local:9090"

func TestCreateOrUpdateScaledObject(t *testing.T) {
r := require.New(t)
const externalScalerHostName = "mysvc.myns.svc.cluster.local:9090"

testInfra := newCommonTestInfra("testns", "testapp")
r.NoError(createOrUpdateScaledObject(
reconciller := &HTTPScaledObjectReconciler{
Client: testInfra.cl,
Scheme: testInfra.cl.Scheme(),
ExternalScalerConfig: config.ExternalScaler{},
BaseConfig: config.Base{},
}

r.NoError(reconciller.createOrUpdateScaledObject(
testInfra.ctx,
testInfra.cl,
testInfra.logger,
Expand Down Expand Up @@ -46,6 +54,9 @@ func TestCreateOrUpdateScaledObject(t *testing.T) {
)
r.NoError(err)

// check that the app ScaledObject has the correct owner
r.Len(retSO.OwnerReferences, 1, "ScaledObject should have the owner reference")

metadata := retSO.ObjectMeta
spec := retSO.Spec

Expand Down Expand Up @@ -87,7 +98,7 @@ func TestCreateOrUpdateScaledObject(t *testing.T) {
}
*testInfra.httpso.Spec.Replicas.Min++
*testInfra.httpso.Spec.Replicas.Max++
r.NoError(createOrUpdateScaledObject(
r.NoError(reconciller.createOrUpdateScaledObject(
testInfra.ctx,
testInfra.cl,
testInfra.logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down
2 changes: 1 addition & 1 deletion tests/checks/internal_service/internal_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down
2 changes: 1 addition & 1 deletion tests/checks/multiple_hosts/multiple_hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down
2 changes: 1 addition & 1 deletion tests/checks/path_prefix/path_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down
2 changes: 1 addition & 1 deletion tests/checks/single_host/single_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ metadata:
labels:
app: {{.DeploymentName}}
spec:
replicas: 1
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
Expand Down

0 comments on commit ce37228

Please sign in to comment.