1+ /*
2+ Copyright 2024 RajSingh.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package controller
18+
19+ import (
20+ "context"
21+ homerv1alpha1 "github.com/rajsinghtech/homer-operator.git/api/v1alpha1"
22+ networkingv1 "k8s.io/api/networking/v1"
23+ "k8s.io/apimachinery/pkg/runtime"
24+ ctrl "sigs.k8s.io/controller-runtime"
25+ "sigs.k8s.io/controller-runtime/pkg/client"
26+ homer "github.com/rajsinghtech/homer-operator.git/pkg/homer"
27+ corev1 "k8s.io/api/core/v1"
28+ "sigs.k8s.io/controller-runtime/pkg/log"
29+ )
30+
31+ // IngressReconciler reconciles a Ingress object
32+ type IngressReconciler struct {
33+ client.Client
34+ Scheme * runtime.Scheme
35+ }
36+
37+ //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete
38+ //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/status,verbs=get;update;patch
39+ //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/finalizers,verbs=update
40+
41+ // Reconcile is part of the main kubernetes reconciliation loop which aims to
42+ // move the current state of the cluster closer to the desired state.
43+ // TODO(user): Modify the Reconcile function to compare the state specified by
44+ // the Ingress object against the actual cluster state, and then
45+ // perform operations to make the cluster state reflect the state specified by
46+ // the user.
47+ //
48+ // For more details, check Reconcile and its Result here:
49+ // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 50+ func (r * IngressReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
51+ log := log .FromContext (ctx )
52+ var ingress networkingv1.Ingress
53+ if err := r .Get (ctx , req .NamespacedName , & ingress ); err != nil {
54+ if client .IgnoreNotFound (err ) != nil {
55+ log .Error (err , "unable to fetch Ingress" , "ingress" , req .NamespacedName )
56+ return ctrl.Result {}, client .IgnoreNotFound (err )
57+ }
58+ }
59+ dashboardList , error := getAllDashboard (ctx , r )
60+ if error != nil {
61+ log .Error (error , "unable to fetch DashboardList" )
62+ return ctrl.Result {}, error
63+ }
64+ for _ , dashboard := range dashboardList .Items {
65+ // Check if dashboard annotations are a subset of the ingress annotations
66+ delete (dashboard .Annotations , "kubectl.kubernetes.io/last-applied-configuration" )
67+ if isSubset (ingress .Annotations , dashboard .Annotations ) {
68+ configMap := corev1.ConfigMap {}
69+ log .Info ("Dashboard annotations are a subset of the ingress annotations" , "dashboard" , dashboard .Name )
70+ if error := r .Get (ctx , client.ObjectKey {Namespace : dashboard .Namespace , Name : dashboard .Name }, & configMap ); error != nil {
71+ log .Error (error , "unable to fetch ConfigMap" , "configmap" , dashboard .Name )
72+ return ctrl.Result {}, error
73+ }
74+ homer .UpdateConfigMapIngress (& configMap , ingress )
75+ if error := r .Update (ctx , & configMap ); error != nil {
76+ log .Error (error , "unable to update ConfigMap" , "configmap" , dashboard .Name )
77+ return ctrl.Result {}, error
78+ }
79+ log .Info ("Updated ConfigMap" , "configmap" , dashboard .Name )
80+ }
81+ }
82+
83+ return ctrl.Result {}, nil
84+ }
85+
86+ // isSubset checks if the first map is a subset of the second map
87+ func isSubset (map1 , map2 map [string ]string ) bool {
88+ for key , value := range map2 {
89+ if map1 [key ] != value {
90+ return false
91+ }
92+ }
93+ return true
94+ }
95+
96+ // SetupWithManager sets up the controller with the Manager.
97+ func (r * IngressReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
98+ return ctrl .NewControllerManagedBy (mgr ).
99+ For (& networkingv1.Ingress {}).
100+ Complete (r )
101+ }
102+
103+ func getAllDashboard (ctx context.Context , r * IngressReconciler ) (* homerv1alpha1.DashboardList , error ) {
104+ var dashboardList homerv1alpha1.DashboardList
105+ if err := r .List (ctx , & dashboardList ); err != nil {
106+ return nil , err
107+ }
108+ return & dashboardList , nil
109+ }
0 commit comments