Skip to content

Commit

Permalink
Use INFO level to log missing namespace in IstioRevision/IstioCNI
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Lukša <mluksa@redhat.com>
  • Loading branch information
luksa committed Apr 19, 2024
1 parent d2b1dfa commit bfce0fb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
27 changes: 20 additions & 7 deletions controllers/istiocni/istiocni_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
"github.com/istio-ecosystem/sail-operator/pkg/kube"
"github.com/istio-ecosystem/sail-operator/pkg/profiles"
"github.com/istio-ecosystem/sail-operator/pkg/reconciler"
"istio.io/istio/pkg/log"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
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"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -92,12 +94,7 @@ func NewReconciler(
func (r *Reconciler) Reconcile(ctx context.Context, cni *v1alpha1.IstioCNI) (ctrl.Result, error) {
log := logf.FromContext(ctx)

if err := validateIstioCNI(cni); err != nil {
return ctrl.Result{}, err
}

log.Info("Installing components")
reconcileErr := r.installHelmChart(ctx, cni)
reconcileErr := r.doReconcile(ctx, cni)

log.Info("Reconciliation done. Updating status.")
statusErr := r.updateStatus(ctx, cni, reconcileErr)
Expand All @@ -109,13 +106,29 @@ func (r *Reconciler) Finalize(ctx context.Context, cni *v1alpha1.IstioCNI) error
return r.uninstallHelmChart(ctx, cni)
}

func validateIstioCNI(cni *v1alpha1.IstioCNI) error {
func (r *Reconciler) doReconcile(ctx context.Context, cni *v1alpha1.IstioCNI) error {
if err := r.validateIstioCNI(ctx, cni); err != nil {
return err
}

log.Info("Installing Helm chart")
return r.installHelmChart(ctx, cni)
}

func (r *Reconciler) validateIstioCNI(ctx context.Context, cni *v1alpha1.IstioCNI) error {
if cni.Spec.Version == "" {
return reconciler.NewValidationError("spec.version not set")
}
if cni.Spec.Namespace == "" {
return reconciler.NewValidationError("spec.namespace not set")
}

if err := r.Client.Get(ctx, types.NamespacedName{Name: cni.Spec.Namespace}, &corev1.Namespace{}); err != nil {
if apierrors.IsNotFound(err) {
return reconciler.NewValidationError(fmt.Sprintf("namespace %q doesn't exist", cni.Spec.Namespace))
}
return err
}
return nil
}

Expand Down
26 changes: 19 additions & 7 deletions controllers/istiorevision/istiorevision_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/istio-ecosystem/sail-operator/pkg/helm"
"github.com/istio-ecosystem/sail-operator/pkg/kube"
"github.com/istio-ecosystem/sail-operator/pkg/reconciler"
"istio.io/istio/pkg/log"
admissionv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
Expand Down Expand Up @@ -100,30 +101,41 @@ func NewReconciler(client client.Client, scheme *runtime.Scheme, resourceDir str
func (r *Reconciler) Reconcile(ctx context.Context, rev *v1alpha1.IstioRevision) (ctrl.Result, error) {
log := logf.FromContext(ctx)

if err := validateIstioRevision(rev); err != nil {
return ctrl.Result{}, err
}

log.Info("Installing components")
reconcileErr := r.installHelmCharts(ctx, rev)
reconcileErr := r.doReconcile(ctx, rev)

log.Info("Reconciliation done. Updating status.")
statusErr := r.updateStatus(ctx, rev, reconcileErr)

return ctrl.Result{}, errors.Join(reconcileErr, statusErr)
}

func (r *Reconciler) doReconcile(ctx context.Context, rev *v1alpha1.IstioRevision) error {
if err := r.validateIstioRevision(ctx, rev); err != nil {
return err
}

log.Info("Installing Helm chart")
return r.installHelmCharts(ctx, rev)
}

func (r *Reconciler) Finalize(ctx context.Context, rev *v1alpha1.IstioRevision) error {
return r.uninstallHelmCharts(ctx, rev)
}

func validateIstioRevision(rev *v1alpha1.IstioRevision) error {
func (r *Reconciler) validateIstioRevision(ctx context.Context, rev *v1alpha1.IstioRevision) error {
if rev.Spec.Version == "" {
return reconciler.NewValidationError("spec.version not set")
}
if rev.Spec.Namespace == "" {
return reconciler.NewValidationError("spec.namespace not set")
}
if err := r.Client.Get(ctx, types.NamespacedName{Name: rev.Spec.Namespace}, &corev1.Namespace{}); err != nil {
if apierrors.IsNotFound(err) {
return reconciler.NewValidationError(fmt.Sprintf("namespace %q doesn't exist", rev.Spec.Namespace))
}
return err
}

if rev.Spec.Values == nil {
return reconciler.NewValidationError("spec.values not set")
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ require (
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
istio.io/api v1.19.0-alpha.1.0.20240224002031-63dcee0970de // indirect
Expand Down

0 comments on commit bfce0fb

Please sign in to comment.