-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support idling messages from core
- Loading branch information
1 parent
27b41c1
commit 07f40b7
Showing
7 changed files
with
310 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,113 @@ | ||
/* | ||
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 Idled 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 | ||
} | ||
idling := Idled{ | ||
Idled: idled, | ||
} | ||
idlingJSON, _ := json.Marshal(idling) | ||
msg := schema.LagoonMessage{ | ||
Type: "idling", | ||
Namespace: namespace.Name, | ||
Meta: &schema.LagoonLogMeta{ | ||
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 | ||
} |
This file contains 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,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 | ||
} |
This file contains 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 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 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