|
| 1 | +/* |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package namespace |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/base64" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "strconv" |
| 24 | + |
| 25 | + "github.com/go-logr/logr" |
| 26 | + "github.com/uselagoon/machinery/api/schema" |
| 27 | + "github.com/uselagoon/remote-controller/internal/messenger" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + ctrl "sigs.k8s.io/controller-runtime" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 34 | +) |
| 35 | + |
| 36 | +// NamespaceReconciler reconciles idling |
| 37 | +type NamespaceReconciler struct { |
| 38 | + client.Client |
| 39 | + Log logr.Logger |
| 40 | + Scheme *runtime.Scheme |
| 41 | + EnableMQ bool |
| 42 | + Messaging *messenger.Messenger |
| 43 | + LagoonTargetName string |
| 44 | +} |
| 45 | + |
| 46 | +type Idled struct { |
| 47 | + Idled bool `json:"idled"` |
| 48 | +} |
| 49 | + |
| 50 | +func (r *NamespaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 51 | + opLog := r.Log.WithValues("namespace", req.NamespacedName) |
| 52 | + |
| 53 | + var namespace corev1.Namespace |
| 54 | + if err := r.Get(ctx, req.NamespacedName, &namespace); err != nil { |
| 55 | + return ctrl.Result{}, ignoreNotFound(err) |
| 56 | + } |
| 57 | + |
| 58 | + // this would be nice to be a lagoon label :) |
| 59 | + if val, ok := namespace.ObjectMeta.Labels["idling.amazee.io/idled"]; ok { |
| 60 | + idled, _ := strconv.ParseBool(val) |
| 61 | + opLog.Info(fmt.Sprintf("environment %s idle state %t", namespace.Name, idled)) |
| 62 | + if r.EnableMQ { |
| 63 | + var projectName, environmentName string |
| 64 | + if p, ok := namespace.ObjectMeta.Labels["lagoon.sh/project"]; ok { |
| 65 | + projectName = p |
| 66 | + } |
| 67 | + if e, ok := namespace.ObjectMeta.Labels["lagoon.sh/environment"]; ok { |
| 68 | + environmentName = e |
| 69 | + } |
| 70 | + idling := Idled{ |
| 71 | + Idled: idled, |
| 72 | + } |
| 73 | + idlingJSON, _ := json.Marshal(idling) |
| 74 | + msg := schema.LagoonMessage{ |
| 75 | + Type: "idling", |
| 76 | + Namespace: namespace.Name, |
| 77 | + Meta: &schema.LagoonLogMeta{ |
| 78 | + Environment: environmentName, |
| 79 | + Project: projectName, |
| 80 | + Cluster: r.LagoonTargetName, |
| 81 | + AdvancedData: base64.StdEncoding.EncodeToString(idlingJSON), |
| 82 | + }, |
| 83 | + } |
| 84 | + msgBytes, err := json.Marshal(msg) |
| 85 | + if err != nil { |
| 86 | + opLog.Error(err, "Unable to encode message as JSON") |
| 87 | + } |
| 88 | + // @TODO: if we can't publish the message because for some reason, log the error and move on |
| 89 | + // this may result in the state being out of sync in lagoon but eventually will be consistent |
| 90 | + if err := r.Messaging.Publish("lagoon-tasks:controller", msgBytes); err != nil { |
| 91 | + return ctrl.Result{}, nil |
| 92 | + } |
| 93 | + } |
| 94 | + return ctrl.Result{}, nil |
| 95 | + } |
| 96 | + return ctrl.Result{}, nil |
| 97 | +} |
| 98 | + |
| 99 | +// SetupWithManager sets up the watch on the namespace resource with an event filter (see predicates.go) |
| 100 | +func (r *NamespaceReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 101 | + return ctrl.NewControllerManagedBy(mgr). |
| 102 | + For(&corev1.Namespace{}). |
| 103 | + WithEventFilter(NamespacePredicates{}). |
| 104 | + Complete(r) |
| 105 | +} |
| 106 | + |
| 107 | +// will ignore not found errors |
| 108 | +func ignoreNotFound(err error) error { |
| 109 | + if apierrors.IsNotFound(err) { |
| 110 | + return nil |
| 111 | + } |
| 112 | + return err |
| 113 | +} |
0 commit comments