Skip to content

Add orbit status&event #2

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

Open
wants to merge 1 commit 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: 2 additions & 0 deletions api/v1alpha1/orbit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ type OrbitStatus struct {
Status string `json:"Status"`
}

//+kubebuilder:printcolumn:JSONPath=".status.Status",name=Status,type=string
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// Orbit is the Schema for the orbits API
type Orbit struct {
metav1.TypeMeta `json:",inline"`
Expand Down
9 changes: 8 additions & 1 deletion config/crd/bases/network.kubeorbit.io_orbits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ spec:
singular: orbit
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.Status
name: Status
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
schema:
openAPIV3Schema:
description: Orbit is the Schema for the orbits API
Expand Down
21 changes: 18 additions & 3 deletions controllers/orbit_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package controllers
import (
"context"
"fmt"
"log"

"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"log"
"k8s.io/client-go/tools/record"

"github.com/go-logr/logr"
"github.com/gogo/protobuf/types"
"istio.io/api/networking/v1alpha3"
istiov1 "istio.io/client-go/pkg/apis/networking/v1alpha3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
orbitv1alpha1 "kubeorbit.io/api/v1alpha1"
Expand All @@ -38,8 +41,9 @@ import (
// OrbitReconciler reconciles a Orbit object
type OrbitReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Scheme *runtime.Scheme
Log logr.Logger
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=network.kubeorbit.io,resources=orbits,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -64,10 +68,20 @@ func (r *OrbitReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
log.Println(err, "unable to fetch object")
} else {
if err := r.reconcileEnvoyFilter(obj, req); err != nil {
obj.Status.Status = "failed"
if err := r.Status().Update(context.Background(), obj); err != nil {
log.Println(err, "unable to update status")
}
r.Recorder.Event(obj, corev1.EventTypeWarning, "FailedCreate", "Unable to create EnvoyFilter on kubeorbit")
return ctrl.Result{}, fmt.Errorf("reconcileEnvoyFilter failed: %w", err)
}
}
obj.Status.Status = "success"
if err := r.Status().Update(context.Background(), obj); err != nil {
log.Println(err, "unable to update status")

}
r.Recorder.Event(obj, corev1.EventTypeNormal, "Created", "EnvoyFilter created successfully on kubeorbit")
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -113,6 +127,7 @@ func (r *OrbitReconciler) reconcileEnvoyFilter(orbit *orbitv1alpha1.Orbit, req c
clone.Spec = newSpec
err = r.Update(context.TODO(), clone)
if err != nil {
r.Recorder.Event(orbit, corev1.EventTypeWarning, "FailedUpdate", "Unable to update EnvoyFilter on kubeorbit")
return fmt.Errorf("EnvoyFilter %s.%s update error: %w", envoyName, orbit.Namespace, err)
}
r.Log.WithValues("orbit", fmt.Sprintf("%s.%s", orbit.Name, orbit.Namespace)).
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package main

import (
"flag"
v1 "kubeorbit.io/api/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/webhook"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand All @@ -33,7 +31,9 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"

v1 "kubeorbit.io/api/v1"
orbitv1alpha1 "kubeorbit.io/api/v1alpha1"
routev1alpha1 "kubeorbit.io/api/v1alpha1"
"kubeorbit.io/controllers"
Expand Down Expand Up @@ -84,9 +84,10 @@ func main() {
}

if err = (&controllers.OrbitReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: mgr.GetLogger(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: mgr.GetLogger(),
Recorder: mgr.GetEventRecorderFor("orbit-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Orbit")
os.Exit(1)
Expand Down