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

implemented adaptive scaling #5

Merged
merged 8 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import (
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// BasicAuthenticatorReconciler reconciles a BasicAuthenticator object
Expand All @@ -41,6 +45,7 @@ type BasicAuthenticatorReconciler struct {
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete

func (r *BasicAuthenticatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
Expand All @@ -56,5 +61,29 @@ func (r *BasicAuthenticatorReconciler) SetupWithManager(mgr ctrl.Manager) error
Owns(&appv1.Deployment{}).
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Watches(
&source.Kind{Type: &appv1.Deployment{}},
handler.EnqueueRequestsFromMapFunc(r.findExternallyManagedDeployments),
).
Complete(r)
}

func (r *BasicAuthenticatorReconciler) findExternallyManagedDeployments(deployment client.Object) []reconcile.Request {
deploy, ok := deployment.(*appv1.Deployment)
if !ok {
return nil
}
if deploy.ObjectMeta.Annotations == nil {
return nil
}
basicAuthName, exists := deploy.ObjectMeta.Annotations[ExternallyManaged]
if !exists {
return nil
}

return []reconcile.Request{
{
NamespacedName: types.NamespacedName{Name: basicAuthName, Namespace: deploy.Namespace},
},
}
}
3 changes: 2 additions & 1 deletion internal/controller/basic_authenticator/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const (
nginxDefaultContainerName = "nginx"
SecretAnnotation = "authenticator.snappcloud.io/secret.name"
ConfigmapAnnotation = "authenticator.snappcloud.io/configmap.name"
basicAuthenticatorFinalizer = "basicauthenticators.authenticator.snappcloud.io/finalizer"
basicAuthenticatorFinalizer = "basicauthenticator.snappcloud.io/finalizer"
ExternallyManaged = "basicauthenticator.snappcloud.io/externally.managed"
)
69 changes: 69 additions & 0 deletions internal/controller/basic_authenticator/provision.go
hoptical marked this conversation as resolved.
Show resolved Hide resolved
sinamna marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"math"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -194,12 +196,26 @@ func (r *BasicAuthenticatorReconciler) CreateDeploymentAuthenticator(ctx context
logger.Error(err, "failed to set deployment owner")
return subreconciler.RequeueWithError(err)
}
if basicAuthenticator.Spec.AdaptiveScale && basicAuthenticator.Spec.AppService != "" {
replica, err := r.AcquireTargetReplica(ctx, basicAuthenticator)
if err != nil {
logger.Error(err, "failed to acquire target replica using adaptiveScale")
return subreconciler.RequeueWithError(err)
}
newDeployment.Spec.Replicas = &replica
}

//create deployment
err := r.Create(ctx, newDeployment)
if err != nil {
logger.Error(err, "failed to create new deployment")
return subreconciler.RequeueWithError(err)
}
err = r.Get(ctx, types.NamespacedName{Name: foundDeployment.Name, Namespace: basicAuthenticator.Namespace}, foundDeployment)
if err != nil {
logger.Error(err, "failed to refetch")
return subreconciler.RequeueWithError(err)
}
logger.Info("created deployment")

return subreconciler.Requeue()
Expand All @@ -210,15 +226,32 @@ func (r *BasicAuthenticatorReconciler) CreateDeploymentAuthenticator(ctx context
}
} else {
//update deployment
targetReplica := newDeployment.Spec.Replicas
if basicAuthenticator.Spec.AdaptiveScale && basicAuthenticator.Spec.AppService != "" {
replica, err := r.AcquireTargetReplica(ctx, basicAuthenticator)
if err != nil {
logger.Error(err, "failed to acquire target replica using adaptiveScale")
}
targetReplica = &replica
}

if !reflect.DeepEqual(newDeployment.Spec, foundDeployment.Spec) {
logger.Info("updating deployment")

foundDeployment.Spec = newDeployment.Spec
foundDeployment.Spec.Replicas = targetReplica

err := r.Update(ctx, foundDeployment)
if err != nil {
logger.Error(err, "failed to update deployment")
return subreconciler.RequeueWithError(err)
}
err = r.Get(ctx, types.NamespacedName{Name: foundDeployment.Name, Namespace: basicAuthenticator.Namespace}, foundDeployment)
if err != nil {
logger.Error(err, "failed to refetch")
return subreconciler.RequeueWithError(err)
}

}
logger.Info("updating ready replicas")
basicAuthenticator.Status.ReadyReplicas = int(foundDeployment.Status.ReadyReplicas)
Expand Down Expand Up @@ -251,3 +284,39 @@ func (r *BasicAuthenticatorReconciler) CreateSidecarAuthenticator(ctx context.Co
}
return subreconciler.ContinueReconciling()
}

func (r *BasicAuthenticatorReconciler) AcquireTargetReplica(ctx context.Context, basicAuthenticator *v1alpha1.BasicAuthenticator) (int32, error) {
var targetService corev1.Service
// service should be in same ns with basic auth
if err := r.Get(ctx, types.NamespacedName{Name: basicAuthenticator.Spec.AppService, Namespace: basicAuthenticator.ObjectMeta.Namespace}, &targetService); err != nil {
return -1, err
}
labelSelector := targetService.Spec.Selector

deployments := &appv1.DeploymentList{}
if err := r.List(ctx, deployments, client.MatchingLabels(labelSelector)); err != nil {
return -1, err
}

if len(deployments.Items) == 0 {
return -1, defaultError.New("no deployment is selected by appService")
}

targetDeploy := deployments.Items[0] //we expect it to be single deployment
if targetDeploy.ObjectMeta.Annotations == nil {
targetDeploy.ObjectMeta.Annotations = make(map[string]string)
}
log.FromContext(context.Background()).Info("----- debug", "deployment revision:", targetDeploy.ResourceVersion)
hoptical marked this conversation as resolved.
Show resolved Hide resolved

targetDeploy.ObjectMeta.Annotations[ExternallyManaged] = basicAuthenticator.Name

err := r.Update(ctx, &targetDeploy)
if err != nil {
return -1, err
}
replicas := deployments.Items[0].Spec.Replicas
targetReplica := math.Floor(float64((*replicas + 1) / 2))
log.FromContext(context.Background()).Info("-------debug", "target Replica inside", targetReplica)

return int32(targetReplica), nil
}
hoptical marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion internal/controller/basic_authenticator/utility.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package basic_authenticator

import "github.com/snapp-incubator/simple-authenticator/api/v1alpha1"
import (
"github.com/snapp-incubator/simple-authenticator/api/v1alpha1"
)

func assignAnnotation(authenticator *v1alpha1.BasicAuthenticator, key, value string) {
if authenticator.ObjectMeta.Annotations == nil {
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/adaptive-scaling/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: authenticator.snappcloud.io/v1alpha1
kind: BasicAuthenticator
metadata:
name: basicauthenticator-sample
status:
readyReplicas: 2

52 changes: 52 additions & 0 deletions tests/e2e/adaptive-scaling/00-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: authenticator.snappcloud.io/v1alpha1
kind: BasicAuthenticator
metadata:
labels:
app.kubernetes.io/name: basicauthenticator
app.kubernetes.io/instance: basicauthenticator-sample
app.kubernetes.io/part-of: basicauthenticator
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: basicauthenticator
name: basicauthenticator-sample
spec:
type: deployment
replicas: 1
appPort: 8080
appService: my-service
adaptiveScale: true
authenticatorPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: curl-deployment
labels:
test: fal
foo: bar
spec:
replicas: 4
selector:
matchLabels:
foo: bar
template:
metadata:
labels:
foo: bar
spec:
containers:
- name: curl-container
image: curlimages/curl:latest
command: ["sleep", "infinity"]
---
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
foo: bar
ports:
- protocol: TCP
port: 8080
targetPort: 8080
type: ClusterIP
7 changes: 7 additions & 0 deletions tests/e2e/adaptive-scaling/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: authenticator.snappcloud.io/v1alpha1
kind: BasicAuthenticator
metadata:
name: basicauthenticator-sample
status:
readyReplicas: 3

8 changes: 8 additions & 0 deletions tests/e2e/adaptive-scaling/01-increase-deploy-replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: curl-deployment
labels:
foo: bar
spec:
replicas: 6
7 changes: 7 additions & 0 deletions tests/e2e/adaptive-scaling/02-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: authenticator.snappcloud.io/v1alpha1
kind: BasicAuthenticator
metadata:
name: basicauthenticator-sample
status:
readyReplicas: 2

8 changes: 8 additions & 0 deletions tests/e2e/adaptive-scaling/02-decrease-deploy-replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: curl-deployment
labels:
foo: bar
spec:
replicas: 4