-
Notifications
You must be signed in to change notification settings - Fork 2
feat: support idling messages from core #245
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
Open
shreddedbacon
wants to merge
3
commits into
main
Choose a base branch
from
idling-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package deployments | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/uselagoon/machinery/api/schema" | ||
| "github.com/uselagoon/remote-controller/internal/helpers" | ||
| "github.com/uselagoon/remote-controller/internal/messenger" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| ) | ||
|
|
||
| // DeploymentsReconciler reconciles idling | ||
| type DeploymentsReconciler struct { | ||
| client.Client | ||
| Log logr.Logger | ||
| Scheme *runtime.Scheme | ||
| EnableMQ bool | ||
| Messaging *messenger.Messenger | ||
| LagoonTargetName string | ||
| } | ||
|
|
||
| type ServiceState struct { | ||
| Name string `json:"name"` | ||
| Type string `json:"type"` | ||
| Replicas int32 `json:"replicas"` | ||
| } | ||
|
|
||
| func (r *DeploymentsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| opLog := r.Log.WithValues("deployment", req.NamespacedName) | ||
|
|
||
| var deployment appsv1.Deployment | ||
| if err := r.Get(ctx, req.NamespacedName, &deployment); err != nil { | ||
| return ctrl.Result{}, ignoreNotFound(err) | ||
| } | ||
| // this would be nice to be a lagoon label :) | ||
| if val, ok := deployment.Labels["idling.amazee.io/idled"]; ok { | ||
| var namespace corev1.Namespace | ||
| if err := r.Get(ctx, types.NamespacedName{ | ||
| Name: deployment.Namespace, | ||
| }, &namespace); err != nil { | ||
| return ctrl.Result{}, ignoreNotFound(err) | ||
| } | ||
| opLog.Info(fmt.Sprintf("deployment %s idle state %v", deployment.Name, val)) | ||
| if r.EnableMQ { | ||
| environmentName := namespace.Labels["lagoon.sh/environment"] | ||
| eID, _ := strconv.Atoi(namespace.Labels["lagoon.sh/environmentId"]) | ||
| envID := helpers.UintPtr(uint(eID)) | ||
| projectName := namespace.Labels["lagoon.sh/project"] | ||
| pID, _ := strconv.Atoi(namespace.Labels["lagoon.sh/projectId"]) | ||
| projectID := helpers.UintPtr(uint(pID)) | ||
| serviceName := deployment.Labels["lagoon.sh/service"] | ||
| serviceType := deployment.Labels["lagoon.sh/service-type"] | ||
| state := ServiceState{ | ||
| Name: serviceName, | ||
| Type: serviceType, | ||
| Replicas: *deployment.Spec.Replicas, | ||
| } | ||
| stateJSON, _ := json.Marshal(state) | ||
| msg := schema.LagoonMessage{ | ||
| Type: "servicestate", | ||
| Namespace: namespace.Name, | ||
| Meta: &schema.LagoonLogMeta{ | ||
| EnvironmentID: envID, | ||
| ProjectID: projectID, | ||
| Environment: environmentName, | ||
| Project: projectName, | ||
| Cluster: r.LagoonTargetName, | ||
| AdvancedData: base64.StdEncoding.EncodeToString(stateJSON), | ||
| }, | ||
| } | ||
| msgBytes, err := json.Marshal(msg) | ||
| if err != nil { | ||
| opLog.Error(err, "Unable to encode message as JSON") | ||
| } | ||
| // @TODO: if we can't publish the message because for some reason, log the error and move on | ||
| // this may result in the state being out of sync in lagoon but eventually will be consistent | ||
| if err := r.Messaging.Publish("lagoon-tasks:controller", msgBytes); err != nil { | ||
| return ctrl.Result{}, nil | ||
| } | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // SetupWithManager sets up the watch on the namespace resource with an event filter (see predicates.go) | ||
| func (r *DeploymentsReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&appsv1.Deployment{}). | ||
| WithEventFilter(DeploymentsPredicates{}). | ||
| Complete(r) | ||
| } | ||
|
|
||
| // will ignore not found errors | ||
| func ignoreNotFound(err error) error { | ||
| if apierrors.IsNotFound(err) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package deployments | ||
|
|
||
| import ( | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| ) | ||
|
|
||
| // DeploymentsPredicates defines the funcs for predicates | ||
| type DeploymentsPredicates struct { | ||
| predicate.Funcs | ||
| } | ||
|
|
||
| // Create is used when a creation event is received by the controller. | ||
| func (n DeploymentsPredicates) Create(e event.CreateEvent) bool { | ||
| return false | ||
| } | ||
|
|
||
| // Delete is used when a deletion event is received by the controller. | ||
| func (n DeploymentsPredicates) Delete(e event.DeleteEvent) bool { | ||
| return false | ||
| } | ||
|
|
||
| // Update is used when an update event is received by the controller. | ||
| func (n DeploymentsPredicates) Update(e event.UpdateEvent) bool { | ||
| if _, ok := e.ObjectOld.GetLabels()["lagoon.sh/service"]; ok { | ||
| oldDeep := e.ObjectOld.(*appsv1.Deployment) | ||
| newDep := e.ObjectNew.(*appsv1.Deployment) | ||
| if *oldDeep.Spec.Replicas != *newDep.Spec.Replicas { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Generic is used when any other event is received by the controller. | ||
| func (n DeploymentsPredicates) Generic(e event.GenericEvent) bool { | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package namespace | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/uselagoon/machinery/api/schema" | ||
| "github.com/uselagoon/remote-controller/internal/helpers" | ||
| "github.com/uselagoon/remote-controller/internal/messenger" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| ) | ||
|
|
||
| // NamespaceReconciler reconciles idling | ||
| type NamespaceReconciler struct { | ||
| client.Client | ||
| Log logr.Logger | ||
| Scheme *runtime.Scheme | ||
| EnableMQ bool | ||
| Messaging *messenger.Messenger | ||
| LagoonTargetName string | ||
| } | ||
|
|
||
| type IdleState string | ||
|
|
||
| const ( | ||
| ActiveState IdleState = "ACTIVE" | ||
| IdledState IdleState = "IDLED" | ||
| ScaledState IdleState = "SCALED" | ||
| ) | ||
|
|
||
| type Idled struct { | ||
| IdleState IdleState `json:"idleState"` | ||
| } | ||
|
|
||
| func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| opLog := r.Log.WithValues("namespace", req.NamespacedName) | ||
|
|
||
| var namespace corev1.Namespace | ||
| if err := r.Get(ctx, req.NamespacedName, &namespace); err != nil { | ||
| return ctrl.Result{}, ignoreNotFound(err) | ||
| } | ||
|
|
||
| // this would be nice to be a lagoon label :) | ||
| if val, ok := namespace.Labels["idling.amazee.io/idled"]; ok { | ||
| idledState := ActiveState | ||
| idled, _ := strconv.ParseBool(val) | ||
| if idled { | ||
| idledState = IdledState | ||
| } | ||
| // check if force scaled | ||
| if fval, ok := namespace.Labels["idling.amazee.io/force-scaled"]; ok { | ||
| fscaled, _ := strconv.ParseBool(fval) | ||
| if fscaled { | ||
| idledState = ScaledState | ||
| } | ||
| } | ||
| opLog.Info(fmt.Sprintf("environment %s idle state %t", namespace.Name, idled)) | ||
| if r.EnableMQ { | ||
| environmentName := namespace.Labels["lagoon.sh/environment"] | ||
| eID, _ := strconv.Atoi(namespace.Labels["lagoon.sh/environmentId"]) | ||
| envID := helpers.UintPtr(uint(eID)) | ||
| projectName := namespace.Labels["lagoon.sh/project"] | ||
| pID, _ := strconv.Atoi(namespace.Labels["lagoon.sh/projectId"]) | ||
| projectID := helpers.UintPtr(uint(pID)) | ||
| idling := Idled{ | ||
| IdleState: idledState, | ||
| } | ||
| idlingJSON, _ := json.Marshal(idling) | ||
| msg := schema.LagoonMessage{ | ||
| Type: "idling", | ||
| Namespace: namespace.Name, | ||
| Meta: &schema.LagoonLogMeta{ | ||
| EnvironmentID: envID, | ||
| ProjectID: projectID, | ||
| Environment: environmentName, | ||
| Project: projectName, | ||
| Cluster: r.LagoonTargetName, | ||
| AdvancedData: base64.StdEncoding.EncodeToString(idlingJSON), | ||
| }, | ||
| } | ||
| msgBytes, err := json.Marshal(msg) | ||
| if err != nil { | ||
| opLog.Error(err, "Unable to encode message as JSON") | ||
| } | ||
| // @TODO: if we can't publish the message because for some reason, log the error and move on | ||
| // this may result in the state being out of sync in lagoon but eventually will be consistent | ||
| if err := r.Messaging.Publish("lagoon-tasks:controller", msgBytes); err != nil { | ||
| return ctrl.Result{}, nil | ||
| } | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // SetupWithManager sets up the watch on the namespace resource with an event filter (see predicates.go) | ||
| func (r *NamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&corev1.Namespace{}). | ||
| WithEventFilter(NamespacePredicates{}). | ||
| Complete(r) | ||
| } | ||
|
|
||
| // will ignore not found errors | ||
| func ignoreNotFound(err error) error { | ||
| if apierrors.IsNotFound(err) { | ||
| return nil | ||
| } | ||
| return err | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this label required? It's not checked here or modified in
EnvironmentServiceState. It would be nice if managing services wasn't tied to havingaergiainstalled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than re-creating all the logic for handling idling and unidling in the remote-controller, this does unfortunately mean aergia is a requirement, with a caveat.
Eventually, aergia will use lagoon based labels and be installed as part of lagoon-remote by default, however the ingress listener and automatic idling would be disabled. Effectively it would just handle manual idling and unidling messages from the API.