Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller: don't put the error in the status #433

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: '1.20'
go-version: '1.21'

- name: Build image
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
go: ["1.20"]
go: ["1.21"]
name: Go ${{ matrix.go }}
steps:
- name: Checkout Metal LB Operator
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1.2

FROM --platform=$BUILDPLATFORM docker.io/golang:1.20 AS builder
FROM --platform=$BUILDPLATFORM docker.io/golang:1.21 AS builder
ARG GIT_COMMIT=dev
ARG GIT_BRANCH=dev

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ deploy-prometheus:
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1
go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.14.0
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
Expand Down
1,717 changes: 768 additions & 949 deletions bin/metallb-operator.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ metadata:
categories: Networking
certified: "false"
containerImage: quay.io/metallb/metallb-operator
createdAt: "2024-02-26T17:05:26Z"
createdAt: "2024-03-05T10:00:08Z"
description: An operator for deploying MetalLB on a kubernetes cluster.
operators.operatorframework.io/builder: operator-sdk-v1.26.1
operators.operatorframework.io/project_layout: go.kubebuilder.io/v2
Expand Down
1,713 changes: 768 additions & 945 deletions bundle/manifests/metallb.io_metallbs.yaml

Large diffs are not rendered by default.

1,714 changes: 768 additions & 946 deletions config/crd/bases/metallb.io_metallbs.yaml

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: metallb-manager-role
rules:
- apiGroups:
Expand Down Expand Up @@ -106,7 +105,6 @@ rules:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: metallb-manager-role
namespace: metallb-system
rules:
Expand Down
1 change: 0 additions & 1 deletion config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
creationTimestamp: null
name: metallb-operator-webhook-configuration
webhooks:
- admissionReviewVersions:
Expand Down
15 changes: 8 additions & 7 deletions controllers/metallb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *MetalLBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if req.Name != defaultMetalLBCrName {
err := fmt.Errorf("MetalLB resource name must be '%s'", defaultMetalLBCrName)
logger.Error(err, "Invalid MetalLB resource name", "name", req.Name)
if err := status.Update(context.TODO(), r.Client, instance, status.ConditionDegraded, "IncorrectMetalLBResourceName", fmt.Sprintf("Incorrect MetalLB resource name: %s", req.Name)); err != nil {
if err := status.Update(context.TODO(), r.Client, instance, status.ConditionDegraded, "IncorrectMetalLBResourceName"); err != nil {
logger.Error(err, "Failed to update metallb status", "Desired status", status.ConditionDegraded)
return ctrl.Result{}, nil // Return success to avoid requeue
}
Expand All @@ -108,15 +108,16 @@ func (r *MetalLBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

result, condition, err := r.reconcileResource(ctx, req, instance)
if err != nil {
logger.Error(err, "Reconcile failed", "MetalLB", *instance)
}

if condition != "" {
errorMsg, wrappedErrMsg := condition, ""
errorMsg := condition
if err != nil {
errorMsg = "internal error"
if errors.Unwrap(err) != nil {
wrappedErrMsg = errors.Unwrap(err).Error()
}
errorMsg = "internalerror"
}
if err := status.Update(context.TODO(), r.Client, instance, condition, errorMsg, wrappedErrMsg); err != nil {
if err := status.Update(context.TODO(), r.Client, instance, condition, errorMsg); err != nil {
logger.Error(err, "Failed to update metallb status", "Desired status", condition)
return ctrl.Result{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/metallb/metallb-operator

go 1.20
go 1.21

require (
github.com/go-logr/logr v1.4.1
Expand Down
35 changes: 35 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ const (
ConditionUpgradeable = "Upgradeable"
)

func Update(ctx context.Context, client k8sclient.Client, metallb *metallbv1beta1.MetalLB, condition string, reason string, message string) error {
conditions := getConditions(condition, reason, message)
func Update(ctx context.Context, client k8sclient.Client, metallb *metallbv1beta1.MetalLB, condition, reason string) error {
conditions := getConditions(condition, reason)
if equality.Semantic.DeepEqual(conditions, metallb.Status.Conditions) {
return nil
}
metallb.Status.Conditions = getConditions(condition, reason, message)
metallb.Status.Conditions = getConditions(condition, reason)

if err := client.Status().Update(ctx, metallb); err != nil {
return errors.Wrapf(err, "could not update status for object %+v", metallb)
}
return nil
}

func getConditions(condition string, reason string, message string) []metav1.Condition {
func getConditions(condition string, reason string) []metav1.Condition {
conditions := getBaseConditions()
switch condition {
case ConditionAvailable:
Expand All @@ -55,11 +55,9 @@ func getConditions(condition string, reason string, message string) []metav1.Con
case ConditionProgressing:
conditions[2].Status = metav1.ConditionTrue
conditions[2].Reason = reason
conditions[2].Message = message
case ConditionDegraded:
conditions[3].Status = metav1.ConditionTrue
conditions[3].Reason = reason
conditions[3].Message = message
}
return conditions
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestGetConditionsAvailable(t *testing.T) {
g := NewGomegaWithT(t)
conditions := getConditions(ConditionAvailable, "testReason", "testMessage")
conditions := getConditions(ConditionAvailable, "testReason")
validateUnsetConditions(g, conditions, []int{2, 3})
validateConditionTypes(g, conditions)
g.Expect(conditions[0].Status).To(Equal(metav1.ConditionTrue))
Expand All @@ -18,21 +18,19 @@ func TestGetConditionsAvailable(t *testing.T) {

func TestGetConditionsProgressing(t *testing.T) {
g := NewGomegaWithT(t)
conditions := getConditions(ConditionProgressing, "testReason", "testMessage")
conditions := getConditions(ConditionProgressing, "testReason")
validateUnsetConditions(g, conditions, []int{0, 1, 3})
validateConditionTypes(g, conditions)
g.Expect(conditions[2].Status).To(Equal(metav1.ConditionTrue))
g.Expect(conditions[2].Message).To(Equal("testMessage"))
g.Expect(conditions[2].Reason).To(Equal("testReason"))
}

func TestGetConditionsDegraded(t *testing.T) {
g := NewGomegaWithT(t)
conditions := getConditions(ConditionDegraded, "testReason", "testMessage")
conditions := getConditions(ConditionDegraded, "testReason")
validateUnsetConditions(g, conditions, []int{0, 1, 2})
validateConditionTypes(g, conditions)
g.Expect(conditions[3].Status).To(Equal(metav1.ConditionTrue))
g.Expect(conditions[3].Message).To(Equal("testMessage"))
g.Expect(conditions[3].Reason).To(Equal("testReason"))
}

Expand Down
Loading