Skip to content

Commit

Permalink
Merge pull request #94 from metal-stack/sig-not-match-event
Browse files Browse the repository at this point in the history
emit proper event when signature doesn't match
  • Loading branch information
GrigoriyMikhalkin authored Jun 16, 2021
2 parents ff41101 + 7b13a94 commit 3d7f1fe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
5 changes: 3 additions & 2 deletions controllers/clusterwidenetworkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/go-logr/logr"
firewallv1 "github.com/metal-stack/firewall-controller/api/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -51,13 +52,13 @@ func (r *ClusterwideNetworkPolicyReconciler) Reconcile(req ctrl.Request) (ctrl.R
// if network policy does not belong to the namespace where clusterwide network policies are stored:
// update status with error message
if req.Namespace != firewallv1.ClusterwideNetworkPolicyNamespace {
r.recorder.Event(&clusterNP, "Warning", "Unapplicable", fmt.Sprintf("cluster wide network policies must be defined in namespace %s otherwise they won't take effect", firewallv1.ClusterwideNetworkPolicyNamespace))
r.recorder.Event(&clusterNP, corev1.EventTypeWarning, "Unapplicable", fmt.Sprintf("cluster wide network policies must be defined in namespace %s otherwise they won't take effect", firewallv1.ClusterwideNetworkPolicyNamespace))
return ctrl.Result{}, nil
}

err := clusterNP.Spec.Validate()
if err != nil {
r.recorder.Event(&clusterNP, "Warning", "Unapplicable", fmt.Sprintf("cluster wide network policy is not valid: %v", err))
r.recorder.Event(&clusterNP, corev1.EventTypeWarning, "Unapplicable", fmt.Sprintf("cluster wide network policy is not valid: %v", err))
return ctrl.Result{}, nil
}

Expand Down
30 changes: 14 additions & 16 deletions controllers/firewall_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -94,7 +92,7 @@ func (r *FirewallReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

var f firewallv1.Firewall
if err := r.Get(ctx, req.NamespacedName, &f); err != nil {
if apierrors.IsNotFound(err) {
if errors.IsNotFound(err) {
defaultFw := nftables.NewDefaultFirewall()
log.Info("flushing k8s firewall rules")
err := defaultFw.Flush()
Expand All @@ -108,15 +106,15 @@ func (r *FirewallReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

if err := r.validateFirewall(ctx, f); err != nil {
r.recorder.Event(&f, "Warning", "Unapplicable", err.Error())
r.recorder.Event(&f, corev1.EventTypeWarning, "Unapplicable", err.Error())
// don't requeue invalid firewall objects
return done, err
}

log.Info("reconciling firewall-controller")
err := updater.UpdateToSpecVersion(f, log, r.recorder)
if err != nil {
r.recorder.Eventf(&f, "Warning", "Self-Reconcilation", "failed with error: %v", err)
r.recorder.Eventf(&f, corev1.EventTypeWarning, "Self-Reconcilation", "failed with error: %v", err)
return requeue, err
}

Expand All @@ -134,9 +132,9 @@ func (r *FirewallReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
log.Info("reconciling network settings")
changed, err := network.ReconcileNetwork(f, log)
if changed && err == nil {
r.recorder.Event(&f, "Normal", "Network settings", "reconcilation succeeded (frr.conf)")
r.recorder.Event(&f, corev1.EventTypeNormal, "Network settings", "reconcilation succeeded (frr.conf)")
} else if changed && err != nil {
r.recorder.Event(&f, "Warning", "Network settings", fmt.Sprintf("reconcilation failed (frr.conf): %v", err))
r.recorder.Event(&f, corev1.EventTypeWarning, "Network settings", fmt.Sprintf("reconcilation failed (frr.conf): %v", err))
}

if err != nil {
Expand All @@ -154,11 +152,11 @@ func (r *FirewallReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

if errors.ErrorOrNil() != nil {
r.recorder.Event(&f, "Warning", "Error", multierror.Flatten(errors).Error())
r.recorder.Event(&f, corev1.EventTypeWarning, "Error", multierror.Flatten(errors).Error())
return requeue, errors
}

r.recorder.Event(&f, "Normal", "Reconciled", "nftables rules and statistics successfully")
r.recorder.Event(&f, corev1.EventTypeNormal, "Reconciled", "nftables rules and statistics successfully")
log.Info("reconciled firewall")
return requeue, nil
}
Expand Down Expand Up @@ -239,7 +237,7 @@ func (r *FirewallReconciler) reconcileRules(ctx context.Context, f firewallv1.Fi
return err
}

var services v1.ServiceList
var services corev1.ServiceList
if err := r.List(ctx, &services); err != nil {
return err
}
Expand Down Expand Up @@ -292,14 +290,14 @@ func (r *FirewallReconciler) reconcileFirewallService(ctx context.Context, s fir
Labels: map[string]string{exporterLabelKey: s.name},
}

var currentSvc v1.Service
var currentSvc corev1.Service
err := r.Get(ctx, nn, &currentSvc)

if err != nil && !errors.IsNotFound(err) {
return err
}

svc := v1.Service{
svc := corev1.Service{
ObjectMeta: meta,
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Expand Down Expand Up @@ -352,16 +350,16 @@ func (r *FirewallReconciler) reconcileFirewallService(ctx context.Context, s fir
return fmt.Errorf("private firewall network contains no ip")
}

endpoints := v1.Endpoints{
endpoints := corev1.Endpoints{
ObjectMeta: meta,
Subsets: []v1.EndpointSubset{
Subsets: []corev1.EndpointSubset{
{
Addresses: []corev1.EndpointAddress{
{
IP: privateNet.Ips[0],
},
},
Ports: []v1.EndpointPort{
Ports: []corev1.EndpointPort{
{
Name: s.namedPort,
Port: s.port,
Expand All @@ -372,7 +370,7 @@ func (r *FirewallReconciler) reconcileFirewallService(ctx context.Context, s fir
},
}

var currentEndpoints v1.Endpoints
var currentEndpoints corev1.Endpoints
err = r.Get(ctx, nn, &currentEndpoints)
if err != nil && !errors.IsNotFound(err) {
return err
Expand Down
5 changes: 3 additions & 2 deletions pkg/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/google/go-github/github"
firewallv1 "github.com/metal-stack/firewall-controller/api/v1"
"github.com/metal-stack/v"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
)

Expand All @@ -37,7 +38,7 @@ func UpdateToSpecVersion(f firewallv1.Firewall, log logr.Logger, recorder record
return nil
}

recorder.Eventf(&f, "Normal", "Self-Reconcilation", "replacing firewall-controller version %s with version %s", v.Version, f.Spec.ControllerVersion)
recorder.Eventf(&f, corev1.EventTypeNormal, "Self-Reconcilation", "replacing firewall-controller version %s with version %s", v.Version, f.Spec.ControllerVersion)
asset, err := DetermineGithubAsset(f.Spec.ControllerVersion)
if err != nil {
return err
Expand All @@ -53,7 +54,7 @@ func UpdateToSpecVersion(f firewallv1.Firewall, log logr.Logger, recorder record
return fmt.Errorf("could not replace firewall-controller with version %s, err: %w", f.Spec.ControllerVersion, err)
}

recorder.Eventf(&f, "Normal", "Self-Reconcilation", "replaced firewall-controller version %s with version %s successfully", v.Version, f.Spec.ControllerVersion)
recorder.Eventf(&f, corev1.EventTypeNormal, "Self-Reconcilation", "replaced firewall-controller version %s with version %s successfully", v.Version, f.Spec.ControllerVersion)

// after a successful self-reconcilation of the firewall-controller binary we want to get restarted by exiting and letting systemd restart the process.
os.Exit(0)
Expand Down

0 comments on commit 3d7f1fe

Please sign in to comment.