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

feat: support idling messages from core #245

Draft
wants to merge 2 commits into
base: main-v1beta2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
111 changes: 111 additions & 0 deletions controllers/namespace/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*

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/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 IdlingState struct {
Idled bool `json:"idled"`
}

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.ObjectMeta.Labels["idling.amazee.io/idled"]; ok {
idled, _ := strconv.ParseBool(val)
opLog.Info(fmt.Sprintf("environment %s idle state %t", namespace.Name, idled))
if r.EnableMQ {
var projectName, environmentName string
if p, ok := namespace.ObjectMeta.Labels["lagoon.sh/project"]; ok {
projectName = p
}
if e, ok := namespace.ObjectMeta.Labels["lagoon.sh/environment"]; ok {
environmentName = e
}
// create the message payload
idleMsgBytes, _ := json.Marshal(&IdlingState{Idled: idled})
msg := schema.LagoonMessage{
Type: "environmentIdling",
Namespace: namespace.Name,
Meta: &schema.LagoonLogMeta{
Environment: environmentName,
Project: projectName,
Cluster: r.LagoonTargetName,
AdvancedData: base64.StdEncoding.EncodeToString(idleMsgBytes),
},
}
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
}
38 changes: 38 additions & 0 deletions controllers/namespace/predicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package namespace

import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// NamespacePredicates defines the funcs for predicates
type NamespacePredicates struct {
predicate.Funcs
}

// Create is used when a creation event is received by the controller.
func (n NamespacePredicates) Create(e event.CreateEvent) bool {
return false
}

// Delete is used when a deletion event is received by the controller.
func (n NamespacePredicates) Delete(e event.DeleteEvent) bool {
return false
}

// Update is used when an update event is received by the controller.
func (n NamespacePredicates) Update(e event.UpdateEvent) bool {
if oldIdled, ok := e.ObjectOld.GetLabels()["idling.amazee.io/idled"]; ok {
if newIdled, ok := e.ObjectNew.GetLabels()["idling.amazee.io/idled"]; ok {
if oldIdled != newIdled {
return true
}
}
}
return false
}

// Generic is used when any other event is received by the controller.
func (n NamespacePredicates) Generic(e event.GenericEvent) bool {
return false
}
16 changes: 16 additions & 0 deletions internal/messenger/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,22 @@ func (m *Messenger) Consumer(targetName string) { //error {
message.Ack(false) // ack to remove from queue
return
}
case "deploytarget:environment:idling":
// idle or unidle an environment, optionally forcible scale it so it can't be unidled by the ingress
err := m.ScaleOrIdleEnvironment(ctx, opLog, namespace, jobSpec)
if err != nil {
//@TODO: send msg back to lagoon and update task to failed?
message.Ack(false) // ack to remove from queue
return
}
case "deploytarget:environment:service":
// idle an environment, optionally forcible scale it so it can't be unidled by the ingress
err := m.EnvironmentServiceState(ctx, opLog, namespace, jobSpec)
if err != nil {
//@TODO: send msg back to lagoon and update task to failed?
message.Ack(false) // ack to remove from queue
return
}
default:
// if we get something that we don't know about, spit out the entire message
opLog.Info(
Expand Down
147 changes: 147 additions & 0 deletions internal/messenger/tasks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/go-logr/logr"
lagoonv1beta2 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta2"
"github.com/uselagoon/remote-controller/internal/helpers"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -17,6 +23,38 @@ type ActiveStandbyPayload struct {
DestinationNamespace string `json:"destinationNamespace"`
}

// TaskType const for the status type
type ServiceState string

// These are valid states for a service.
const (
StateStop ServiceState = "stop"
StateStart ServiceState = "start"
StateRestart ServiceState = "restart"
)

type LagoonServiceInfo struct {
ServiceName string `json:"name,omitempty"`
ServiceState ServiceState `json:"state,omitempty"`
}

type LagoonIdling struct {
ForceIdle bool `json:"foreIdle,omitempty"`
ForceScale bool `json:"forceScale,omitempty"`
}

type ServiceStateEvent struct {
Type string `json:"type"` // defines the action type
EventType string `json:"eventType"` // defines the eventtype field in the event notification
Data LagoonServiceInfo `json:"data"` // contains the payload for the action, this could be any json so using a map
}

type IdlingEvent struct {
Type string `json:"type"` // defines the action type
EventType string `json:"eventType"` // defines the eventtype field in the event notification
Data LagoonIdling `json:"data"` // contains the payload for the action, this could be any json so using a map
}

// IngressRouteMigration handles running the ingress migrations.
func (m *Messenger) IngressRouteMigration(namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error {
// always set these to true for ingress migration tasks
Expand Down Expand Up @@ -91,3 +129,112 @@ func createAdvancedTask(namespace string, jobSpec *lagoonv1beta2.LagoonTaskSpec,
}
return nil
}

func (m *Messenger) ScaleOrIdleEnvironment(ctx context.Context, opLog logr.Logger, ns string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error {
opLog.Info(
fmt.Sprintf(
"Received environment idling request for project %s, environment %s - %s",
jobSpec.Project.Name,
jobSpec.Environment.Name,
ns,
),
)
namespace := &corev1.Namespace{}
err := m.Client.Get(ctx, types.NamespacedName{
Name: ns,
}, namespace)
if err != nil {
return err
}
retPol := &IdlingEvent{}
if err := json.Unmarshal(jobSpec.Misc.MiscResource, retPol); err != nil {
return err
}
if retPol.Data.ForceIdle {
if retPol.Data.ForceScale {
// this would be nice to be a lagoon label :)
namespace.ObjectMeta.Labels["idling.amazee.io/force-scaled"] = "true"
} else {
// this would be nice to be a lagoon label :)
namespace.ObjectMeta.Labels["idling.amazee.io/force-idled"] = "true"
}
} else {
// this would be nice to be a lagoon label :)
namespace.ObjectMeta.Labels["idling.amazee.io/unidle"] = "true"
}
if err := m.Client.Update(context.Background(), namespace); err != nil {
opLog.Error(err,
fmt.Sprintf(
"Unable to update namespace %s to set idle state.",
ns,
),
)
return err
}
return nil
}

func (m *Messenger) EnvironmentServiceState(ctx context.Context, opLog logr.Logger, ns string, jobSpec *lagoonv1beta2.LagoonTaskSpec) error {
retPol := &ServiceStateEvent{}
if err := json.Unmarshal(jobSpec.Misc.MiscResource, retPol); err != nil {
return err
}
opLog.Info(
fmt.Sprintf(
"Received environment service request for project %s, environment %s service %s - %s",
jobSpec.Project.Name,
jobSpec.Environment.Name,
retPol.Data.ServiceName,
ns,
),
)
deployment := &appsv1.Deployment{}
err := m.Client.Get(ctx, types.NamespacedName{
Name: retPol.Data.ServiceName,
Namespace: ns,
}, deployment)
if err != nil {
return err
}
update := false
switch retPol.Data.ServiceState {
case StateRestart:
deployment.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)
update = true
case StateStop:
if *deployment.Spec.Replicas > 0 {
// if the service has replicas, then save the replica count and scale it to 0
deployment.ObjectMeta.Annotations["service.lagoon.sh/replicas"] = strconv.FormatInt(int64(*deployment.Spec.Replicas), 10)
replicas := int32(0)
deployment.Spec.Replicas = &replicas
update = true
}
case StateStart:
if *deployment.Spec.Replicas == 0 {
// if the service has no replicas, set it back to what the previous replica value was
prevReplicas, err := strconv.Atoi(deployment.ObjectMeta.Annotations["service.lagoon.sh/replicas"])
if err != nil {
return err
}
replicas := int32(prevReplicas)
deployment.Spec.Replicas = &replicas
delete(deployment.ObjectMeta.Annotations, "service.lagoon.sh/replicas")
update = true
}
default:
// nothing to do
return nil
}
if update {
if err := m.Client.Update(ctx, deployment); err != nil {
opLog.Error(err,
fmt.Sprintf(
"Unable to update deployment %s to change its state.",
ns,
),
)
return err
}
}
return nil
}
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
lagoonv1beta1 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta1"
lagoonv1beta2 "github.com/uselagoon/remote-controller/apis/lagoon/v1beta2"
harborctrl "github.com/uselagoon/remote-controller/controllers/harbor"
"github.com/uselagoon/remote-controller/controllers/namespace"
lagoonv1beta1ctrl "github.com/uselagoon/remote-controller/controllers/v1beta1"
lagoonv1beta2ctrl "github.com/uselagoon/remote-controller/controllers/v1beta2"
"github.com/uselagoon/remote-controller/internal/messenger"
Expand Down Expand Up @@ -864,6 +865,18 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "LagoonTask")
os.Exit(1)
}
// start the namespace reconciler
if err = (&namespace.NamespaceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("namespace").WithName("Namespace"),
Scheme: mgr.GetScheme(),
EnableMQ: enableMQ,
Messaging: messaging,
LagoonTargetName: lagoonTargetName,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Namespace")
os.Exit(1)
}

// v1beta2 is the latest version
if err = (&lagoonv1beta2ctrl.LagoonBuildReconciler{
Expand Down
Loading