Skip to content

Commit

Permalink
Merge pull request #1113 from stuggi/net_changes_v1.3.x
Browse files Browse the repository at this point in the history
[v1.3.x] Reconcile ipset when osnetconfig changes
  • Loading branch information
openshift-merge-bot[bot] authored Nov 25, 2024
2 parents 5c7a727 + f3c399d commit ffde879
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
9 changes: 9 additions & 0 deletions controllers/openstackclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,16 @@ func (r *OpenStackClientReconciler) podCreateOrUpdate(

isPodUpdate := !pod.ObjectMeta.CreationTimestamp.IsZero()

val, ok := pod.ObjectMeta.Annotations["k8s.v1.cni.cncf.io/networks"]
if ok && val != annotation {
return k8s_errors.NewForbidden(
schema.GroupResource{Group: "", Resource: "pods"}, // Specify the group and resource type
pod.Name,
errors.New("Restart Pod required to get new network attachment configured"),
)
}
pod.ObjectMeta.Annotations["k8s.v1.cni.cncf.io/networks"] = annotation

for k, v := range common.GetLabels(instance, openstackclient.AppLabel, map[string]string{}) {
pod.Labels[k] = v
}
Expand Down
42 changes: 41 additions & 1 deletion controllers/openstackipset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import (
"k8s.io/utils/diff"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/go-logr/logr"
metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
Expand Down Expand Up @@ -321,9 +323,47 @@ func (r *OpenStackIPSetReconciler) getNormalizedStatus(status *ospdirectorv1beta
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackIPSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *OpenStackIPSetReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
Log := r.GetLogger()
ipsetFN := handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
result := []reconcile.Request{}

// For each NetConfig event get the list of all
// OpenStackIPSet to trigger reconcile for the one which has the label on it
// matching the NetConfig (there can only be one) in the same namespace
ipSetList := &ospdirectorv1beta1.OpenStackIPSetList{}

listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
client.MatchingLabels{
shared.OpenStackNetConfigReconcileLabel: o.GetName(),
},
}
if err := r.Client.List(ctx, ipSetList, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve OpenStackIPSetList")
return nil
}

// For each OpenStackIPSet instance create a reconcile request
for _, i := range ipSetList.Items {
name := client.ObjectKey{
Namespace: i.Namespace,
Name: i.Name,
}
result = append(result, reconcile.Request{NamespacedName: name})
}
if len(result) > 0 {
Log.Info("Reconcile request for:", "result", result)

return result
}
return nil
})

return ctrl.NewControllerManagedBy(mgr).
For(&ospdirectorv1beta1.OpenStackIPSet{}).
Watches(&source.Kind{Type: &ospdirectorv1beta1.OpenStackNetConfig{}},
ipsetFN).
Complete(r)
}

Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -316,7 +317,7 @@ func main() {
Kclient: kclient,
Log: ctrl.Log.WithName("controllers").WithName("OpenStackIPSet"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
}).SetupWithManager(context.Background(), mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackIPSet")
os.Exit(1)
}
Expand Down

0 comments on commit ffde879

Please sign in to comment.