mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-19 04:26:39 +00:00
improve log system in appconfig (#1758)
This commit is contained in:
+1
-3
@@ -27,7 +27,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"k8s.io/klog/v2"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/healthz"
|
||||
@@ -52,7 +51,6 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
setupLog = ctrl.Log.WithName(kubevelaName)
|
||||
scheme = common.Scheme
|
||||
waitSecretTimeout = 90 * time.Second
|
||||
waitSecretInterval = 2 * time.Second
|
||||
@@ -190,7 +188,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if err = oamv1alpha2.Setup(mgr, controllerArgs, logging.NewLogrLogger(setupLog)); err != nil {
|
||||
if err = oamv1alpha2.Setup(mgr, controllerArgs); err != nil {
|
||||
klog.ErrorS(err, "Unable to setup the oam core controller")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/pkg/errors"
|
||||
istioapiv1beta1 "istio.io/api/networking/v1beta1"
|
||||
istioclientv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
|
||||
@@ -553,7 +552,7 @@ func removeString(slice []string, s string) (result []string) {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles AppDeployment.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
r := NewReconciler(mgr.GetClient(), mgr.GetScheme(), args.DiscoveryMapper)
|
||||
return r.SetupWithManager(mgr)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/meta"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
@@ -270,7 +269,7 @@ func (r *Reconciler) UpdateStatus(ctx context.Context, app *v1beta1.Application,
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles AppRollout.
|
||||
func Setup(mgr ctrl.Manager, args core.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args core.Args) error {
|
||||
reconciler := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/go-logr/logr"
|
||||
terraformv1beta1 "github.com/oam-dev/terraform-controller/api/v1beta1"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@@ -152,7 +151,6 @@ var _ = BeforeSuite(func(done Done) {
|
||||
For(&v1alpha2.Component{}).
|
||||
Watches(&source.Kind{Type: &v1alpha2.Component{}}, &applicationconfiguration.ComponentHandler{
|
||||
Client: ctlManager.GetClient(),
|
||||
Logger: logging.NewLogrLogger(ctrl.Log.WithName("application-testsuite-component-handler")),
|
||||
RevisionLimit: 100,
|
||||
CustomRevisionHookURL: "",
|
||||
}).Complete(&NoOpReconciler{
|
||||
|
||||
+30
-35
@@ -26,7 +26,6 @@ import (
|
||||
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/meta"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/resource"
|
||||
"github.com/pkg/errors"
|
||||
@@ -43,6 +42,7 @@ import (
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
oamtype "github.com/oam-dev/kubevela/apis/types"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/common"
|
||||
core "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
|
||||
@@ -84,14 +84,13 @@ const (
|
||||
)
|
||||
|
||||
// Setup adds a controller that reconciles ApplicationConfigurations.
|
||||
func Setup(mgr ctrl.Manager, args core.Args, l logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args core.Args) error {
|
||||
name := "oam/" + strings.ToLower(v1alpha2.ApplicationConfigurationGroupKind)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
Named(name).
|
||||
For(&v1alpha2.ApplicationConfiguration{}).
|
||||
Complete(NewReconciler(mgr, args.DiscoveryMapper,
|
||||
l.WithValues("controller", name),
|
||||
WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
|
||||
WithApplyOnceOnlyMode(args.ApplyMode)))
|
||||
}
|
||||
@@ -104,7 +103,6 @@ type OAMApplicationReconciler struct {
|
||||
workloads WorkloadApplicator
|
||||
gc GarbageCollector
|
||||
scheme *runtime.Scheme
|
||||
log logging.Logger
|
||||
record event.Recorder
|
||||
preHooks map[string]ControllerHooks
|
||||
postHooks map[string]ControllerHooks
|
||||
@@ -168,7 +166,7 @@ func WithApplyOnceOnlyMode(mode core.ApplyOnceOnlyMode) ReconcilerOption {
|
||||
|
||||
// NewReconciler returns an OAMApplicationReconciler that reconciles ApplicationConfigurations
|
||||
// by rendering and instantiating their Components and Traits.
|
||||
func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, log logging.Logger, o ...ReconcilerOption) *OAMApplicationReconciler {
|
||||
func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, o ...ReconcilerOption) *OAMApplicationReconciler {
|
||||
r := &OAMApplicationReconciler{
|
||||
client: m.GetClient(),
|
||||
scheme: m.GetScheme(),
|
||||
@@ -185,7 +183,6 @@ func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, log loggi
|
||||
dm: dm,
|
||||
},
|
||||
gc: GarbageCollectorFn(eligible),
|
||||
log: log,
|
||||
record: event.NewNopRecorder(),
|
||||
preHooks: make(map[string]ControllerHooks),
|
||||
postHooks: make(map[string]ControllerHooks),
|
||||
@@ -206,8 +203,7 @@ func NewReconciler(m ctrl.Manager, dm discoverymapper.DiscoveryMapper, log loggi
|
||||
// Reconcile an OAM ApplicationConfigurations by rendering and instantiating its
|
||||
// Components and Traits.
|
||||
func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
|
||||
log := r.log.WithValues("request", req)
|
||||
log.Debug("Reconciling")
|
||||
klog.InfoS("Reconcile applicationConfiguration", "applicationConfiguration", klog.KRef(req.Namespace, req.Name))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), reconcileTimeout)
|
||||
defer cancel()
|
||||
@@ -224,13 +220,13 @@ func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.R
|
||||
ctx = util.SetNamespaceInCtx(ctx, ac.Namespace)
|
||||
if ac.ObjectMeta.DeletionTimestamp.IsZero() {
|
||||
if registerFinalizers(ac) {
|
||||
log.Debug("Register new finalizers", "finalizers", ac.ObjectMeta.Finalizers)
|
||||
klog.V(common.LogDebug).InfoS("Register new finalizers", "finalizers", ac.ObjectMeta.Finalizers)
|
||||
return reconcile.Result{}, errors.Wrap(r.client.Update(ctx, ac), errUpdateAppConfigStatus)
|
||||
}
|
||||
} else {
|
||||
if err := r.workloads.Finalize(ctx, ac); err != nil {
|
||||
log.Debug("Failed to finalize workloads", "workloads status", ac.Status.Workloads,
|
||||
"error", err)
|
||||
klog.V(common.LogDebug).InfoS("Failed to finalize workloads", "workloads status", ac.Status.Workloads,
|
||||
"err", err)
|
||||
r.record.Event(ac, event.Warning(reasonCannotFinalizeWorkloads, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errFinalizeWorkloads)))
|
||||
return reconcile.Result{}, errors.Wrap(r.UpdateStatus(ctx, ac), errUpdateAppConfigStatus)
|
||||
@@ -238,7 +234,7 @@ func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.R
|
||||
return reconcile.Result{}, errors.Wrap(r.client.Update(ctx, ac), errUpdateAppConfigStatus)
|
||||
}
|
||||
|
||||
reconResult := r.ACReconcile(ctx, ac, log)
|
||||
reconResult := r.ACReconcile(ctx, ac)
|
||||
// always update ac status and set the error
|
||||
err := errors.Wrap(r.UpdateStatus(ctx, ac), errUpdateAppConfigStatus)
|
||||
// use the controller build-in backoff mechanism if an error occurs
|
||||
@@ -249,17 +245,16 @@ func (r *OAMApplicationReconciler) Reconcile(req reconcile.Request) (reconcile.R
|
||||
}
|
||||
|
||||
// ACReconcile contains all the reconcile logic of an AC, it can be used by other controller
|
||||
func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2.ApplicationConfiguration,
|
||||
log logging.Logger) (result reconcile.Result) {
|
||||
func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (result reconcile.Result) {
|
||||
|
||||
acPatch := ac.DeepCopy()
|
||||
// execute the posthooks at the end no matter what
|
||||
defer func() {
|
||||
updateObservedGeneration(ac)
|
||||
for name, hook := range r.postHooks {
|
||||
exeResult, err := hook.Exec(ctx, ac, log)
|
||||
exeResult, err := hook.Exec(ctx, ac)
|
||||
if err != nil {
|
||||
log.Debug("Failed to execute post-hooks", "hook name", name, "error", err,
|
||||
klog.V(common.LogDebug).InfoS("Failed to execute post-hooks", "hook name", name, "error", err,
|
||||
"requeue-after", result.RequeueAfter)
|
||||
r.record.Event(ac, event.Warning(reasonCannotExecutePosthooks, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errExecutePosthooks)))
|
||||
@@ -273,9 +268,9 @@ func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2
|
||||
|
||||
// execute the prehooks
|
||||
for name, hook := range r.preHooks {
|
||||
result, err := hook.Exec(ctx, ac, log)
|
||||
result, err := hook.Exec(ctx, ac)
|
||||
if err != nil {
|
||||
log.Debug("Failed to execute pre-hooks", "hook name", name, "error", err, "requeue-after", result.RequeueAfter)
|
||||
klog.V(common.LogDebug).InfoS("Failed to execute pre-hooks", "hook name", name, "error", err, "requeue-after", result.RequeueAfter)
|
||||
r.record.Event(ac, event.Warning(reasonCannotExecutePrehooks, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errExecutePrehooks)))
|
||||
return result
|
||||
@@ -283,13 +278,13 @@ func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2
|
||||
r.record.Event(ac, event.Normal(reasonExecutePrehook, "Successfully executed a prehook", "prehook name ", name))
|
||||
}
|
||||
|
||||
log = log.WithValues("uid", ac.GetUID(), "version", ac.GetResourceVersion())
|
||||
klog.InfoS("ApplicationConfiguration", "uid", ac.GetUID(), "version", ac.GetResourceVersion())
|
||||
|
||||
// we have special logics for application generated applicationConfiguration
|
||||
if isControlledByApp(ac) {
|
||||
if ac.GetAnnotations()[oam.AnnotationAppRevision] == strconv.FormatBool(true) {
|
||||
msg := "Encounter an application revision, no need to reconcile"
|
||||
log.Info(msg)
|
||||
klog.Info(msg)
|
||||
r.record.Event(ac, event.Normal(reasonRevision, msg))
|
||||
ac.SetConditions(v1alpha1.Unavailable())
|
||||
ac.Status.RollingStatus = oamtype.InactiveAfterRollingCompleted
|
||||
@@ -300,18 +295,18 @@ func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2
|
||||
|
||||
workloads, depStatus, err := r.components.Render(ctx, ac)
|
||||
if err != nil {
|
||||
log.Info("Cannot render components", "error", err)
|
||||
klog.InfoS("Cannot render components", "err", err)
|
||||
r.record.Event(ac, event.Warning(reasonCannotRenderComponents, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errRenderComponents)))
|
||||
return reconcile.Result{}
|
||||
}
|
||||
log.Debug("Successfully rendered components", "workloads", len(workloads))
|
||||
klog.V(common.LogDebug).InfoS("Successfully rendered components", "workloads", len(workloads))
|
||||
r.record.Event(ac, event.Normal(reasonRenderComponents, "Successfully rendered components",
|
||||
"workloads", strconv.Itoa(len(workloads))))
|
||||
|
||||
applyOpts := []apply.ApplyOption{apply.MustBeControllableBy(ac.GetUID()), applyOnceOnly(ac, r.applyOnceOnlyMode, log)}
|
||||
applyOpts := []apply.ApplyOption{apply.MustBeControllableBy(ac.GetUID()), applyOnceOnly(ac, r.applyOnceOnlyMode)}
|
||||
if err := r.workloads.Apply(ctx, ac.Status.Workloads, workloads, applyOpts...); err != nil {
|
||||
log.Debug("Cannot apply workload", "error", err)
|
||||
klog.V(common.LogDebug).InfoS("Cannot apply workload", "err", err)
|
||||
r.record.Event(ac, event.Warning(reasonCannotApplyComponents, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errApplyComponents)))
|
||||
return reconcile.Result{}
|
||||
@@ -322,7 +317,7 @@ func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2
|
||||
klog.InfoS("mark the ac rolling status as templated", "appConfig", klog.KRef(ac.Namespace, ac.Name))
|
||||
ac.Status.RollingStatus = oamtype.RollingTemplated
|
||||
}
|
||||
log.Debug("Successfully applied components", "workloads", len(workloads))
|
||||
klog.V(common.LogDebug).InfoS("Successfully applied components", "workloads", len(workloads))
|
||||
r.record.Event(ac, event.Normal(reasonApplyComponents, "Successfully applied components",
|
||||
"workloads", strconv.Itoa(len(workloads))))
|
||||
|
||||
@@ -333,24 +328,24 @@ func (r *OAMApplicationReconciler) ACReconcile(ctx context.Context, ac *v1alpha2
|
||||
for _, e := range r.gc.Eligible(ac.GetNamespace(), ac.Status.Workloads, workloads) {
|
||||
// https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
|
||||
e := e
|
||||
|
||||
log := log.WithValues("kind", e.GetKind(), "name", e.GetName())
|
||||
klog.InfoS("Collect garbage ", "resource", klog.KRef(e.GetNamespace(), e.GetName()),
|
||||
"apiVersion", e.GetAPIVersion(), "kind", e.GetKind())
|
||||
record := r.record.WithAnnotations("kind", e.GetKind(), "name", e.GetName())
|
||||
|
||||
err := r.confirmDeleteOnApplyOnceMode(ctx, ac.GetNamespace(), &e)
|
||||
if err != nil {
|
||||
log.Debug("confirm component can't be garbage collected", "error", err)
|
||||
klog.V(common.LogDebug).InfoS("Confirm component can't be garbage collected", "err", err)
|
||||
record.Event(ac, event.Warning(reasonCannotGGComponents, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errGCComponent)))
|
||||
return reconcile.Result{}
|
||||
}
|
||||
if err := r.client.Delete(ctx, &e); resource.IgnoreNotFound(err) != nil {
|
||||
log.Debug("Cannot garbage collect component", "error", err)
|
||||
klog.V(common.LogDebug).InfoS("Cannot garbage collect component", "err", err)
|
||||
record.Event(ac, event.Warning(reasonCannotGGComponents, err))
|
||||
ac.SetConditions(v1alpha1.ReconcileError(errors.Wrap(err, errGCComponent)))
|
||||
return reconcile.Result{}
|
||||
}
|
||||
log.Debug("Garbage collected resource")
|
||||
klog.V(common.LogDebug).Info("Garbage collected resource")
|
||||
record.Event(ac, event.Normal(reasonGGComponent, "Successfully garbage collected component"))
|
||||
}
|
||||
|
||||
@@ -689,7 +684,7 @@ func (e *GenerationUnchanged) Error() string {
|
||||
|
||||
// applyOnceOnly is an ApplyOption that controls the applying mechanism for workload and trait.
|
||||
// More detail refers to the ApplyOnceOnlyMode type annotation
|
||||
func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnlyMode, log logging.Logger) apply.ApplyOption {
|
||||
func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnlyMode) apply.ApplyOption {
|
||||
return func(_ context.Context, existing, desired runtime.Object) error {
|
||||
if mode == core.ApplyOnceOnlyOff {
|
||||
return nil
|
||||
@@ -705,7 +700,7 @@ func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnl
|
||||
dLabels[oam.LabelOAMResourceType] != oam.ResourceTypeTrait {
|
||||
// this ApplyOption only works for workload and trait
|
||||
// skip if the resource is not workload nor trait, e.g., scope
|
||||
log.Info("ignore apply only once check, because resourceType is not workload or trait",
|
||||
klog.InfoS("Ignore apply only once check, because resourceType is not workload or trait",
|
||||
oam.LabelOAMResourceType, dLabels[oam.LabelOAMResourceType])
|
||||
return nil
|
||||
}
|
||||
@@ -714,7 +709,7 @@ func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnl
|
||||
if existing == nil {
|
||||
if mode != core.ApplyOnceOnlyForce {
|
||||
// non-force mode will always create the resource if not exist.
|
||||
log.Info("apply only once with mode:" + string(mode) + ", but old resource not exist, will create a new one")
|
||||
klog.InfoS("Apply only once with mode:" + string(mode) + ", but old resource not exist, will create a new one")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -759,7 +754,7 @@ func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnl
|
||||
if createdBefore {
|
||||
message = "apply only once with mode: force, but resource updated, will create new"
|
||||
}
|
||||
log.Info(message, "appConfig", ac.Name, "gvk", desired.GetObjectKind().GroupVersionKind(), "name", d.GetName(),
|
||||
klog.InfoS(message, "appConfig", ac.Name, "gvk", desired.GetObjectKind().GroupVersionKind(), "name", d.GetName(),
|
||||
"resourceType", dLabels[oam.LabelOAMResourceType], "appliedCompRevision", appliedRevision,
|
||||
"labeledCompRevision", dLabels[oam.LabelAppComponentRevision],
|
||||
"appliedGeneration", appliedGeneration, "labeledGeneration", dAnnots[oam.AnnotationAppGeneration])
|
||||
@@ -780,7 +775,7 @@ func applyOnceOnly(ac *v1alpha2.ApplicationConfiguration, mode core.ApplyOnceOnl
|
||||
// that means its spec is not changed
|
||||
if (e.GetAnnotations()[oam.AnnotationAppGeneration] != dAnnots[oam.AnnotationAppGeneration]) ||
|
||||
(eLabels[oam.LabelAppComponentRevision] != dLabels[oam.LabelAppComponentRevision]) {
|
||||
log.Info("apply only once with mode: "+string(mode)+", but new generation or revision created, will create new",
|
||||
klog.InfoS("Apply only once with mode: "+string(mode)+", but new generation or revision created, will create new",
|
||||
oam.AnnotationAppGeneration, e.GetAnnotations()[oam.AnnotationAppGeneration]+"/"+dAnnots[oam.AnnotationAppGeneration],
|
||||
oam.LabelAppComponentRevision, eLabels[oam.LabelAppComponentRevision]+"/"+dLabels[oam.LabelAppComponentRevision])
|
||||
// its spec is changed, so apply new configuration to it
|
||||
|
||||
+13
-14
@@ -24,7 +24,6 @@ import (
|
||||
"time"
|
||||
|
||||
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/test"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
@@ -346,13 +345,13 @@ func TestReconciler(t *testing.T) {
|
||||
WithGarbageCollector(GarbageCollectorFn(func(_ string, _ []v1alpha2.WorkloadStatus, _ []Workload) []unstructured.Unstructured {
|
||||
return []unstructured.Unstructured{*trait}
|
||||
})),
|
||||
WithPrehook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPrehook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, nil
|
||||
})),
|
||||
WithPrehook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPrehook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, errBoom
|
||||
})),
|
||||
WithPosthook("postHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("postHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
})),
|
||||
},
|
||||
@@ -420,10 +419,10 @@ func TestReconciler(t *testing.T) {
|
||||
WithGarbageCollector(GarbageCollectorFn(func(_ string, _ []v1alpha2.WorkloadStatus, _ []Workload) []unstructured.Unstructured {
|
||||
return []unstructured.Unstructured{*trait}
|
||||
})),
|
||||
WithPosthook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
})),
|
||||
WithPosthook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, errBoom
|
||||
})),
|
||||
},
|
||||
@@ -465,16 +464,16 @@ func TestReconciler(t *testing.T) {
|
||||
WithGarbageCollector(GarbageCollectorFn(func(_ string, _ []v1alpha2.WorkloadStatus, _ []Workload) []unstructured.Unstructured {
|
||||
return []unstructured.Unstructured{*trait}
|
||||
})),
|
||||
WithPrehook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPrehook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, nil
|
||||
})),
|
||||
WithPrehook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPrehook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, errBoom
|
||||
})),
|
||||
WithPosthook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("preHookSuccess", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
})),
|
||||
WithPosthook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("preHookFailed", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{RequeueAfter: 15 * time.Second}, errBoom
|
||||
})),
|
||||
},
|
||||
@@ -538,10 +537,10 @@ func TestReconciler(t *testing.T) {
|
||||
WithGarbageCollector(GarbageCollectorFn(func(_ string, _ []v1alpha2.WorkloadStatus, _ []Workload) []unstructured.Unstructured {
|
||||
return []unstructured.Unstructured{*trait}
|
||||
})),
|
||||
WithPrehook("preHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPrehook("preHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
})),
|
||||
WithPosthook("postHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
WithPosthook("postHook", ControllerHooksFn(func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return reconcile.Result{}, nil
|
||||
})),
|
||||
},
|
||||
@@ -666,7 +665,7 @@ func TestReconciler(t *testing.T) {
|
||||
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
r := NewReconciler(tc.args.m, nil, logging.NewNopLogger(), tc.args.o...)
|
||||
r := NewReconciler(tc.args.m, nil, tc.args.o...)
|
||||
got, err := r.Reconcile(reconcile.Request{})
|
||||
|
||||
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
|
||||
@@ -1879,7 +1878,7 @@ func TestUpdateStatus(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
r := NewReconciler(m, nil, logging.NewNopLogger())
|
||||
r := NewReconciler(m, nil)
|
||||
|
||||
ac := &v1alpha2.ApplicationConfiguration{}
|
||||
err := r.client.Get(context.Background(), types.NamespacedName{Name: "example-appconfig"}, ac)
|
||||
|
||||
+2
-5
@@ -20,9 +20,6 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
@@ -311,7 +308,7 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
|
||||
When("ApplyOnceOnlyForce is enabled", func() {
|
||||
It("tests the situation where workload is not applied at the first because of unsatisfied dependency",
|
||||
func() {
|
||||
componentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100, Logger: logging.NewLogrLogger(ctrl.Log.WithName("component-handler"))}
|
||||
componentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100}
|
||||
|
||||
By("Enable ApplyOnceOnlyForce")
|
||||
reconciler.applyOnceOnlyMode = core.ApplyOnceOnlyForce
|
||||
@@ -461,7 +458,7 @@ var _ = Describe("Test apply (workloads/traits) once only", func() {
|
||||
|
||||
It("tests the situation where workload is not applied at the first because of unsatisfied dependency and revision specified",
|
||||
func() {
|
||||
componentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100, Logger: logging.NewLogrLogger(ctrl.Log.WithName("component-handler"))}
|
||||
componentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100}
|
||||
|
||||
By("Enable ApplyOnceOnlyForce")
|
||||
reconciler.applyOnceOnlyMode = core.ApplyOnceOnlyForce
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -46,7 +45,6 @@ const ControllerRevisionComponentLabel = "controller.oam.dev/component"
|
||||
// ComponentHandler will watch component change and generate Revision automatically.
|
||||
type ComponentHandler struct {
|
||||
Client client.Client
|
||||
Logger logging.Logger
|
||||
RevisionLimit int
|
||||
CustomRevisionHookURL string
|
||||
}
|
||||
@@ -108,7 +106,7 @@ func (c *ComponentHandler) getRelatedAppConfig(object metav1.Object) []reconcile
|
||||
var appConfigs v1alpha2.ApplicationConfigurationList
|
||||
err := c.Client.List(context.Background(), &appConfigs)
|
||||
if err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("error list all applicationConfigurations %v", err))
|
||||
klog.Info(fmt.Sprintf("error list all applicationConfigurations %v", err))
|
||||
return nil
|
||||
}
|
||||
var reqs []reconcile.Request
|
||||
@@ -131,7 +129,7 @@ func (c *ComponentHandler) IsRevisionDiff(mt klog.KMetadata, curComp *v1alpha2.C
|
||||
// TODO: this might be a bug that we treat all errors getting from k8s as a new revision
|
||||
// but the client go event handler doesn't handle an error. We need to see if we can retry this
|
||||
if err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("Failed to compare the component with its latest revision with err = %+v", err),
|
||||
klog.InfoS(fmt.Sprintf("Failed to compare the component with its latest revision with err = %+v", err),
|
||||
"component", mt.GetName(), "latest revision", curComp.Status.LatestRevision.Name)
|
||||
return true, curComp.Status.LatestRevision.Revision
|
||||
}
|
||||
@@ -154,7 +152,7 @@ func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj runtim
|
||||
reqs := c.getRelatedAppConfig(mt)
|
||||
// Hook to custom revision service if exist
|
||||
if err := c.customComponentRevisionHook(reqs, comp); err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("fail to hook from custom revision service(%s) %v", c.CustomRevisionHookURL, err), "componentName", mt.GetName())
|
||||
klog.InfoS(fmt.Sprintf("fail to hook from custom revision service(%s) %v", c.CustomRevisionHookURL, err), "componentName", mt.GetName())
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -194,21 +192,21 @@ func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj runtim
|
||||
// TODO: we should update the status first. otherwise, the subsequent create will all fail if the update fails
|
||||
err := c.Client.Create(context.TODO(), &revision)
|
||||
if err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("error create controllerRevision %v", err), "componentName", mt.GetName())
|
||||
klog.InfoS(fmt.Sprintf("error create controllerRevision %v", err), "componentName", mt.GetName())
|
||||
return nil, false
|
||||
}
|
||||
|
||||
err = c.UpdateStatus(context.Background(), comp)
|
||||
if err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("update component status latestRevision %s err %v", revisionName, err), "componentName", mt.GetName())
|
||||
klog.InfoS(fmt.Sprintf("update component status latestRevision %s err %v", revisionName, err), "componentName", mt.GetName())
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.Logger.Info(fmt.Sprintf("ControllerRevision %s created", revisionName))
|
||||
klog.InfoS("Create ControllerRevision", "name", revisionName)
|
||||
// garbage collect
|
||||
if int64(c.RevisionLimit) < nextRevision {
|
||||
if err := c.cleanupControllerRevision(comp); err != nil {
|
||||
c.Logger.Info(fmt.Sprintf("failed to clean up revisions of Component %v.", err))
|
||||
klog.Info(fmt.Sprintf("failed to clean up revisions of Component %v.", err))
|
||||
}
|
||||
}
|
||||
return reqs, true
|
||||
@@ -280,7 +278,7 @@ func (c *ComponentHandler) cleanupControllerRevision(curComp *v1alpha2.Component
|
||||
if err := c.Client.Delete(context.TODO(), &revisionToClean); err != nil {
|
||||
return err
|
||||
}
|
||||
c.Logger.Info(fmt.Sprintf("ControllerRevision %s deleted", revision.Name))
|
||||
klog.InfoS("Delete controllerRevision", "name", revision.Name)
|
||||
toKill--
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
@@ -31,7 +30,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/event"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
@@ -122,7 +120,6 @@ func TestComponentHandler(t *testing.T) {
|
||||
return nil
|
||||
}),
|
||||
},
|
||||
Logger: logging.NewLogrLogger(ctrl.Log.WithName("test")),
|
||||
RevisionLimit: 2,
|
||||
}
|
||||
comp := &v1alpha2.Component{
|
||||
|
||||
@@ -19,7 +19,6 @@ package applicationconfiguration
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
@@ -27,13 +26,13 @@ import (
|
||||
|
||||
// A ControllerHooks provide customized reconcile logic for an ApplicationConfiguration
|
||||
type ControllerHooks interface {
|
||||
Exec(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error)
|
||||
Exec(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error)
|
||||
}
|
||||
|
||||
// ControllerHooksFn reconciles an ApplicationConfiguration
|
||||
type ControllerHooksFn func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error)
|
||||
type ControllerHooksFn func(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error)
|
||||
|
||||
// Exec the customized reconcile logic on the ApplicationConfiguration
|
||||
func (fn ControllerHooksFn) Exec(ctx context.Context, ac *v1alpha2.ApplicationConfiguration, logger logging.Logger) (reconcile.Result, error) {
|
||||
return fn(ctx, ac, logger)
|
||||
func (fn ControllerHooksFn) Exec(ctx context.Context, ac *v1alpha2.ApplicationConfiguration) (reconcile.Result, error) {
|
||||
return fn(ctx, ac)
|
||||
}
|
||||
|
||||
+1
-3
@@ -26,14 +26,12 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/utils/pointer"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
@@ -370,7 +368,7 @@ var _ = Describe("Test Component Revision Enabled with custom component revision
|
||||
It("custom component change revision lead to revision difference, it should not loop infinitely create", func() {
|
||||
srv := httptest.NewServer(RevisionHandler)
|
||||
defer srv.Close()
|
||||
customComponentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100, Logger: logging.NewLogrLogger(ctrl.Log.WithName("component-handler")), CustomRevisionHookURL: srv.URL}
|
||||
customComponentHandler := &ComponentHandler{Client: k8sClient, RevisionLimit: 100, CustomRevisionHookURL: srv.URL}
|
||||
getDeploy := func(image string) *v1.Deployment {
|
||||
return &v1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
|
||||
@@ -26,7 +26,6 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
@@ -176,8 +175,8 @@ var _ = BeforeSuite(func(done Done) {
|
||||
}, time.Second*30, time.Millisecond*500).Should(BeNil())
|
||||
Expect(mapping.Resource.Resource).Should(Equal("foo"))
|
||||
|
||||
reconciler = NewReconciler(mgr, dm, logging.NewLogrLogger(ctrl.Log.WithName("suit-test-appconfig")))
|
||||
componentHandler = &ComponentHandler{Client: k8sClient, RevisionLimit: 100, Logger: logging.NewLogrLogger(ctrl.Log.WithName("component-handler"))}
|
||||
reconciler = NewReconciler(mgr, dm)
|
||||
componentHandler = &ComponentHandler{Client: k8sClient, RevisionLimit: 100}
|
||||
|
||||
By("Creating workload definition and trait definition")
|
||||
wd := v1alpha2.WorkloadDefinition{
|
||||
|
||||
+3
-7
@@ -23,7 +23,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/pkg/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
ktype "k8s.io/apimachinery/pkg/types"
|
||||
@@ -54,7 +53,6 @@ const reconcileTimeout = 1 * time.Minute
|
||||
// application configuration and reuse its reconcile logic
|
||||
type Reconciler struct {
|
||||
client client.Client
|
||||
log logging.Logger
|
||||
record event.Recorder
|
||||
mgr ctrl.Manager
|
||||
applyMode core.ApplyOnceOnlyMode
|
||||
@@ -105,8 +103,8 @@ func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, err
|
||||
// makes sure that the appConfig's owner is the same as the appContext
|
||||
appConfig.SetOwnerReferences(appContext.GetOwnerReferences())
|
||||
// call into the old ac Reconciler and copy the status back
|
||||
acReconciler := ac.NewReconciler(r.mgr, dm, r.log, ac.WithRecorder(r.record), ac.WithApplyOnceOnlyMode(r.applyMode))
|
||||
reconResult := acReconciler.ACReconcile(ctx, appConfig, r.log)
|
||||
acReconciler := ac.NewReconciler(r.mgr, dm, ac.WithRecorder(r.record), ac.WithApplyOnceOnlyMode(r.applyMode))
|
||||
reconResult := acReconciler.ACReconcile(ctx, appConfig)
|
||||
appContextPatch := client.MergeFrom(appContext.DeepCopy())
|
||||
appContext.Status = appConfig.Status
|
||||
// always update ac status and set the error
|
||||
@@ -133,19 +131,17 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, compHandler *ac.Componen
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles ApplicationContext
|
||||
func Setup(mgr ctrl.Manager, args core.Args, l logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args core.Args) error {
|
||||
name := "oam/" + strings.ToLower(v1alpha2.ApplicationContextGroupKind)
|
||||
record := event.NewAPIRecorder(mgr.GetEventRecorderFor(name))
|
||||
reconciler := Reconciler{
|
||||
client: mgr.GetClient(),
|
||||
mgr: mgr,
|
||||
log: l.WithValues("controller", name),
|
||||
record: record,
|
||||
applyMode: args.ApplyMode,
|
||||
}
|
||||
compHandler := &ac.ComponentHandler{
|
||||
Client: mgr.GetClient(),
|
||||
Logger: l,
|
||||
RevisionLimit: args.RevisionLimit,
|
||||
CustomRevisionHookURL: args.CustomRevisionHookURL,
|
||||
}
|
||||
|
||||
+1
-4
@@ -20,14 +20,12 @@ package applicationcontext
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
@@ -161,7 +159,6 @@ var _ = Describe("Test ApplicationContext Controller", func() {
|
||||
})
|
||||
|
||||
It("Testing Setup", func() {
|
||||
logr := ctrl.Log.WithName("ApplicationContext")
|
||||
Expect(Setup(mgr, core_oam_dev.Args{}, logging.NewLogrLogger(logr).WithValues("suitTest", "Setup"))).Should(BeNil())
|
||||
Expect(Setup(mgr, core_oam_dev.Args{})).Should(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -26,7 +26,6 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/manager"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
@@ -93,17 +92,14 @@ var _ = BeforeSuite(func(done Done) {
|
||||
|
||||
var name = "ApplicationContext"
|
||||
|
||||
logr := ctrl.Log.WithName("ApplicationContext")
|
||||
r = Reconciler{
|
||||
client: mgr.GetClient(),
|
||||
log: logging.NewLogrLogger(logr).WithValues("suitTest", name),
|
||||
mgr: mgr,
|
||||
record: event.NewAPIRecorder(mgr.GetEventRecorderFor(name)),
|
||||
applyMode: core.ApplyOnceOnlyOff,
|
||||
}
|
||||
compHandler := &ac.ComponentHandler{
|
||||
Client: mgr.GetClient(),
|
||||
Logger: logging.NewLogrLogger(logr),
|
||||
RevisionLimit: defRevisionLimit,
|
||||
CustomRevisionHookURL: "",
|
||||
}
|
||||
|
||||
+1
-2
@@ -22,7 +22,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/meta"
|
||||
"github.com/pkg/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -358,7 +357,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles AppRollout.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
reconciler := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
dm: args.DiscoveryMapper,
|
||||
|
||||
+1
-2
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -204,7 +203,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles ComponentDefinition.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
r := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
|
||||
+1
-2
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -193,7 +192,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles PolicyDefinition.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
r := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
|
||||
+14
-27
@@ -22,21 +22,20 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/resource"
|
||||
"github.com/pkg/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/klog/v2"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/resource"
|
||||
|
||||
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
"github.com/oam-dev/kubevela/pkg/controller/common"
|
||||
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -56,14 +55,13 @@ const (
|
||||
)
|
||||
|
||||
// Setup adds a controller that reconciles HealthScope.
|
||||
func Setup(mgr ctrl.Manager, _ controller.Args, l logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, _ controller.Args) error {
|
||||
name := "oam/" + strings.ToLower(v1alpha2.HealthScopeGroupKind)
|
||||
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
Named(name).
|
||||
For(&v1alpha2.HealthScope{}).
|
||||
Complete(NewReconciler(mgr,
|
||||
WithLogger(l.WithValues("controller", name)),
|
||||
WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
|
||||
))
|
||||
}
|
||||
@@ -71,8 +69,6 @@ func Setup(mgr ctrl.Manager, _ controller.Args, l logging.Logger) error {
|
||||
// A Reconciler reconciles OAM Scopes by keeping track of the health status of components.
|
||||
type Reconciler struct {
|
||||
client client.Client
|
||||
|
||||
log logging.Logger
|
||||
record event.Recorder
|
||||
// traitChecker represents checker fetching health condition from HealthCheckTrait
|
||||
traitChecker WorloadHealthChecker
|
||||
@@ -86,13 +82,6 @@ type Reconciler struct {
|
||||
// A ReconcilerOption configures a Reconciler.
|
||||
type ReconcilerOption func(*Reconciler)
|
||||
|
||||
// WithLogger specifies how the Reconciler should log messages.
|
||||
func WithLogger(l logging.Logger) ReconcilerOption {
|
||||
return func(r *Reconciler) {
|
||||
r.log = l
|
||||
}
|
||||
}
|
||||
|
||||
// WithRecorder specifies how the Reconciler should record events.
|
||||
func WithRecorder(er event.Recorder) ReconcilerOption {
|
||||
return func(r *Reconciler) {
|
||||
@@ -121,7 +110,6 @@ func WithChecker(c WorloadHealthChecker) ReconcilerOption {
|
||||
func NewReconciler(m ctrl.Manager, o ...ReconcilerOption) *Reconciler {
|
||||
r := &Reconciler{
|
||||
client: m.GetClient(),
|
||||
log: logging.NewNopLogger(),
|
||||
record: event.NewNopRecorder(),
|
||||
traitChecker: WorkloadHealthCheckFn(CheckByHealthCheckTrait),
|
||||
checkers: []WorloadHealthChecker{
|
||||
@@ -142,8 +130,7 @@ func NewReconciler(m ctrl.Manager, o ...ReconcilerOption) *Reconciler {
|
||||
|
||||
// Reconcile an OAM HealthScope by keeping track of its health status.
|
||||
func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
|
||||
log := r.log.WithValues("request", req)
|
||||
log.Debug("Reconciling")
|
||||
klog.InfoS("Reconcile healthScope", "healthScope", klog.KRef(req.Namespace, req.Name))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), reconcileTimeout)
|
||||
defer cancel()
|
||||
@@ -164,10 +151,10 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
log = log.WithValues("uid", hs.GetUID(), "version", hs.GetResourceVersion())
|
||||
klog.InfoS("healthScope", "uid", hs.GetUID(), "version", hs.GetResourceVersion())
|
||||
|
||||
scopeCondition, wlConditions := r.GetScopeHealthStatus(ctx, hs)
|
||||
log.Debug("Successfully ran health check", "scope", hs.Name)
|
||||
klog.V(common.LogDebug).InfoS("Successfully ran health check", "scope", hs.Name)
|
||||
r.record.Event(hs, event.Normal(reasonHealthCheck, "Successfully ran health check"))
|
||||
|
||||
elapsed := time.Since(start)
|
||||
@@ -179,7 +166,7 @@ func (r *Reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error)
|
||||
|
||||
// GetScopeHealthStatus get the status of the healthscope based on workload resources.
|
||||
func (r *Reconciler) GetScopeHealthStatus(ctx context.Context, healthScope *v1alpha2.HealthScope) (ScopeHealthCondition, []*WorkloadHealthCondition) {
|
||||
log := r.log.WithValues("get scope health status", healthScope.GetName())
|
||||
klog.InfoS("Get scope health status", "name", healthScope.GetName())
|
||||
scopeCondition := ScopeHealthCondition{
|
||||
HealthStatus: StatusHealthy, // if no workload referenced, scope is healthy by default
|
||||
}
|
||||
@@ -207,7 +194,7 @@ func (r *Reconciler) GetScopeHealthStatus(ctx context.Context, healthScope *v1al
|
||||
|
||||
wlHealthCondition = r.traitChecker.Check(ctx, r.client, resRef, healthScope.GetNamespace())
|
||||
if wlHealthCondition != nil {
|
||||
log.Debug("get health condition from health check trait ", "workload", resRef, "healthCondition", wlHealthCondition)
|
||||
klog.V(common.LogDebug).InfoS("Get health condition from health check trait ", "workload", resRef, "healthCondition", wlHealthCondition)
|
||||
// get healthCondition from HealthCheckTrait
|
||||
workloadHealthConditionsC <- wlHealthCondition
|
||||
return
|
||||
@@ -216,14 +203,14 @@ func (r *Reconciler) GetScopeHealthStatus(ctx context.Context, healthScope *v1al
|
||||
for _, checker := range r.checkers {
|
||||
wlHealthCondition = checker.Check(ctxWithTimeout, r.client, resRef, healthScope.GetNamespace())
|
||||
if wlHealthCondition != nil {
|
||||
log.Debug("get health condition from built-in checker", "workload", resRef, "healthCondition", wlHealthCondition)
|
||||
klog.V(common.LogDebug).InfoS("Get health condition from built-in checker", "workload", resRef, "healthCondition", wlHealthCondition)
|
||||
// found matched checker and get health condition
|
||||
workloadHealthConditionsC <- wlHealthCondition
|
||||
return
|
||||
}
|
||||
}
|
||||
// handle unknown workload
|
||||
log.Debug("get unknown workload", "workload", resRef)
|
||||
klog.V(common.LogDebug).InfoS("Gpkg/controller/core.oam.dev/v1alpha2/setup.go:42:69et unknown workload", "workload", resRef)
|
||||
workloadHealthConditionsC <- r.unknownChecker.Check(ctx, r.client, resRef, healthScope.GetNamespace())
|
||||
}(workloadRef)
|
||||
}
|
||||
|
||||
-3
@@ -37,7 +37,6 @@ import (
|
||||
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/test"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
@@ -69,7 +68,6 @@ var _ = Describe("HealthScope Controller Reconcile Test", func() {
|
||||
return &WorkloadHealthCondition{HealthStatus: StatusUnhealthy}
|
||||
})
|
||||
reconciler := NewReconciler(mockMgr,
|
||||
WithLogger(logging.NewNopLogger().WithValues("HealthScopeReconciler")),
|
||||
WithRecorder(event.NewNopRecorder()),
|
||||
WithChecker(MockHealthyChecker),
|
||||
)
|
||||
@@ -162,7 +160,6 @@ var _ = Describe("Test GetScopeHealthStatus", func() {
|
||||
Client: &test.MockClient{},
|
||||
}
|
||||
reconciler := NewReconciler(mockMgr,
|
||||
WithLogger(logging.NewNopLogger().WithValues("HealthScopeReconciler")),
|
||||
WithRecorder(event.NewNopRecorder()),
|
||||
)
|
||||
reconciler.client = test.NewMockClient()
|
||||
|
||||
+13
-19
@@ -23,15 +23,14 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
cpmeta "github.com/crossplane/crossplane-runtime/pkg/meta"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/client-go/discovery"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/kube-openapi/pkg/util/proto"
|
||||
"k8s.io/kubectl/pkg/explain"
|
||||
"k8s.io/kubectl/pkg/util/openapi"
|
||||
@@ -52,12 +51,11 @@ const (
|
||||
)
|
||||
|
||||
// Setup adds a controller that reconciles ContainerizedWorkload.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
reconciler := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
DiscoveryClient: *discovery.NewDiscoveryClientForConfigOrDie(mgr.GetConfig()),
|
||||
dm: args.DiscoveryMapper,
|
||||
log: ctrl.Log.WithName("ManualScalarTrait"),
|
||||
record: event.NewAPIRecorder(mgr.GetEventRecorderFor("ManualScalarTrait")),
|
||||
Scheme: mgr.GetScheme(),
|
||||
}
|
||||
@@ -69,7 +67,6 @@ type Reconciler struct {
|
||||
client.Client
|
||||
discovery.DiscoveryClient
|
||||
dm discoverymapper.DiscoveryMapper
|
||||
log logr.Logger
|
||||
record event.Recorder
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
@@ -83,9 +80,7 @@ type Reconciler struct {
|
||||
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;update;patch;delete
|
||||
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
ctx := context.Background()
|
||||
mLog := r.log.WithValues("manualscalar trait", req.NamespacedName)
|
||||
|
||||
mLog.Info("Reconcile manualscalar trait")
|
||||
klog.InfoS("Reconcile manualscalar trait", "trait", klog.KRef(req.Namespace, req.Name))
|
||||
|
||||
var manualScalar oamv1alpha2.ManualScalerTrait
|
||||
if err := r.Get(ctx, req.NamespacedName, &manualScalar); err != nil {
|
||||
@@ -94,13 +89,13 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
|
||||
ctx = util.SetNamespaceInCtx(ctx, manualScalar.Namespace)
|
||||
|
||||
r.log.Info("Get the manualscalar trait", "ReplicaCount", manualScalar.Spec.ReplicaCount,
|
||||
klog.InfoS("Get the manualscalar trait", "ReplicaCount", manualScalar.Spec.ReplicaCount,
|
||||
"Annotations", manualScalar.GetAnnotations())
|
||||
// find the resource object to record the event to, default is the parent appConfig.
|
||||
eventObj, err := util.LocateParentAppConfig(ctx, r.Client, &manualScalar)
|
||||
if eventObj == nil {
|
||||
// fallback to workload itself
|
||||
mLog.Error(err, "Failed to find the parent resource", "manualScalar", manualScalar.Name)
|
||||
klog.ErrorS(err, "Failed to find the parent resource", "manualScalar", manualScalar.Name)
|
||||
eventObj = &manualScalar
|
||||
}
|
||||
// Fetch the workload instance this trait is referring to
|
||||
@@ -114,7 +109,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
// Fetch the child resources list from the corresponding workload
|
||||
resources, err := util.FetchWorkloadChildResources(ctx, r, r.dm, workload)
|
||||
if err != nil {
|
||||
mLog.Error(err, "Error while fetching the workload child resources", "workload", workload.UnstructuredContent())
|
||||
klog.ErrorS(err, "Error while fetching the workload child resources", "workload", workload.UnstructuredContent())
|
||||
r.record.Event(eventObj, event.Warning(util.ErrFetchChildResources, err))
|
||||
return util.ReconcileWaitResult, util.PatchCondition(ctx, r, &manualScalar,
|
||||
cpv1alpha1.ReconcileError(errors.New(util.ErrFetchChildResources)))
|
||||
@@ -124,7 +119,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
resources = append(resources, workload)
|
||||
}
|
||||
// Scale the child resources that we know how to scale
|
||||
result, err := r.scaleResources(ctx, mLog, manualScalar, resources)
|
||||
result, err := r.scaleResources(ctx, manualScalar, resources)
|
||||
// the scaleResources function will patch error message and should return here to prevent the condition override by the following patch.
|
||||
if result == util.ReconcileWaitResult {
|
||||
return result, err
|
||||
@@ -140,8 +135,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
}
|
||||
|
||||
// identify child resources and scale them
|
||||
func (r *Reconciler) scaleResources(ctx context.Context, mLog logr.Logger,
|
||||
manualScalar oamv1alpha2.ManualScalerTrait, resources []*unstructured.Unstructured) (ctrl.Result, error) {
|
||||
func (r *Reconciler) scaleResources(ctx context.Context, manualScalar oamv1alpha2.ManualScalerTrait, resources []*unstructured.Unstructured) (ctrl.Result, error) {
|
||||
// scale all the resources that we can scale
|
||||
isController := false
|
||||
bod := true
|
||||
@@ -170,27 +164,27 @@ func (r *Reconciler) scaleResources(ctx context.Context, mLog logr.Logger,
|
||||
if locateReplicaField(document, res) {
|
||||
found = true
|
||||
resPatch := client.MergeFrom(res.DeepCopyObject())
|
||||
mLog.Info("Get the resource the trait is going to modify",
|
||||
klog.InfoS("Get the resource the trait is going to modify",
|
||||
"resource name", res.GetName(), "UID", res.GetUID())
|
||||
cpmeta.AddOwnerReference(res, ownerRef)
|
||||
err := unstructured.SetNestedField(res.Object, int64(manualScalar.Spec.ReplicaCount), "spec", "replicas")
|
||||
if err != nil {
|
||||
mLog.Error(err, "Failed to patch a resource for scaling")
|
||||
klog.ErrorS(err, "Failed to patch a resource for scaling")
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &manualScalar, cpv1alpha1.ReconcileError(errors.Wrap(err, errPatchTobeScaledResource)))
|
||||
}
|
||||
// merge patch to scale the resource
|
||||
if err := r.Patch(ctx, res, resPatch, client.FieldOwner(manualScalar.GetUID())); err != nil {
|
||||
mLog.Error(err, "Failed to scale a resource")
|
||||
klog.ErrorS(err, "Failed to scale a resource")
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &manualScalar, cpv1alpha1.ReconcileError(errors.Wrap(err, errScaleResource)))
|
||||
}
|
||||
mLog.Info("Successfully scaled a resource", "resource GVK", res.GroupVersionKind().String(),
|
||||
klog.InfoS("Successfully scaled a resource", "resource GVK", res.GroupVersionKind().String(),
|
||||
"res UID", res.GetUID(), "target replica", manualScalar.Spec.ReplicaCount)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
mLog.Info("Cannot locate any resource", "total resources", len(resources))
|
||||
klog.InfoS("Cannot locate any resource", "total resources", len(resources))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &manualScalar, cpv1alpha1.ReconcileError(errors.New(errScaleResource)))
|
||||
}
|
||||
|
||||
+1
-2
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -206,7 +205,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles TraitDefinition.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
r := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
|
||||
+1
-2
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -193,7 +192,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
}
|
||||
|
||||
// Setup adds a controller that reconciles WorkflowStepDefinition.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
r := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
Scheme: mgr.GetScheme(),
|
||||
|
||||
+13
-17
@@ -23,8 +23,6 @@ import (
|
||||
|
||||
cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/event"
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/pkg/errors"
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
@@ -32,6 +30,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/klog/v2"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/builder"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
@@ -52,10 +51,9 @@ const (
|
||||
)
|
||||
|
||||
// Setup adds a controller that reconciles ContainerizedWorkload.
|
||||
func Setup(mgr ctrl.Manager, _ controller.Args, _ logging.Logger) error {
|
||||
func Setup(mgr ctrl.Manager, _ controller.Args) error {
|
||||
reconciler := Reconciler{
|
||||
Client: mgr.GetClient(),
|
||||
log: ctrl.Log.WithName("ContainerizedWorkload"),
|
||||
record: event.NewAPIRecorder(mgr.GetEventRecorderFor("ContainerizedWorkload")),
|
||||
Scheme: mgr.GetScheme(),
|
||||
}
|
||||
@@ -65,7 +63,6 @@ func Setup(mgr ctrl.Manager, _ controller.Args, _ logging.Logger) error {
|
||||
// Reconciler reconciles a ContainerizedWorkload object
|
||||
type Reconciler struct {
|
||||
client.Client
|
||||
log logr.Logger
|
||||
record event.Recorder
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
@@ -78,27 +75,26 @@ type Reconciler struct {
|
||||
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
|
||||
func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
ctx := context.Background()
|
||||
log := r.log.WithValues("containerizedworkload", req.NamespacedName)
|
||||
log.Info("Reconcile container workload")
|
||||
klog.InfoS("Reconcile containerizedworkload", klog.KRef(req.Namespace, req.Name))
|
||||
|
||||
var workload v1alpha2.ContainerizedWorkload
|
||||
if err := r.Get(ctx, req.NamespacedName, &workload); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
log.Info("Container workload is deleted")
|
||||
klog.Info("Container workload is deleted")
|
||||
}
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
log.Info("Get the workload", "apiVersion", workload.APIVersion, "kind", workload.Kind)
|
||||
klog.InfoS("Get the workload", "apiVersion", workload.APIVersion, "kind", workload.Kind)
|
||||
// find the resource object to record the event to, default is the parent appConfig.
|
||||
eventObj, err := util.LocateParentAppConfig(ctx, r.Client, &workload)
|
||||
if eventObj == nil {
|
||||
// fallback to workload itself
|
||||
log.Error(err, "workload", "name", workload.Name)
|
||||
klog.ErrorS(err, "workload", "name", workload.Name)
|
||||
eventObj = &workload
|
||||
}
|
||||
deploy, err := r.renderDeployment(ctx, &workload)
|
||||
if err != nil {
|
||||
log.Error(err, "Failed to render a deployment")
|
||||
klog.ErrorS(err, "Failed to render a deployment")
|
||||
r.record.Event(eventObj, event.Warning(errRenderWorkload, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderWorkload)))
|
||||
@@ -106,7 +102,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
// server side apply, only the fields we set are touched
|
||||
applyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(workload.GetUID())}
|
||||
if err := r.Patch(ctx, deploy, client.Apply, applyOpts...); err != nil {
|
||||
log.Error(err, "Failed to apply to a deployment")
|
||||
klog.ErrorS(err, "Failed to apply to a deployment")
|
||||
r.record.Event(eventObj, event.Warning(errApplyDeployment, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyDeployment)))
|
||||
@@ -118,14 +114,14 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
configMapApplyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(deploy.GetUID())}
|
||||
configmaps, err := r.renderConfigMaps(ctx, &workload, deploy)
|
||||
if err != nil {
|
||||
log.Error(err, "Failed to render configmaps")
|
||||
klog.ErrorS(err, "Failed to render configmaps")
|
||||
r.record.Event(eventObj, event.Warning(errRenderWorkload, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderWorkload)))
|
||||
}
|
||||
for _, cm := range configmaps {
|
||||
if err := r.Patch(ctx, cm, client.Apply, configMapApplyOpts...); err != nil {
|
||||
log.Error(err, "Failed to apply a configmap")
|
||||
klog.ErrorS(err, "Failed to apply a configmap")
|
||||
r.record.Event(eventObj, event.Warning(errApplyConfigMap, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyConfigMap)))
|
||||
@@ -138,14 +134,14 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
// TODO(rz): remove this after we have service trait
|
||||
service, err := r.renderService(ctx, &workload, deploy)
|
||||
if err != nil {
|
||||
log.Error(err, "Failed to render a service")
|
||||
klog.ErrorS(err, "Failed to render a service")
|
||||
r.record.Event(eventObj, event.Warning(errRenderService, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderService)))
|
||||
}
|
||||
// server side apply the service
|
||||
if err := r.Patch(ctx, service, client.Apply, applyOpts...); err != nil {
|
||||
log.Error(err, "Failed to apply a service")
|
||||
klog.ErrorS(err, "Failed to apply a service")
|
||||
r.record.Event(eventObj, event.Warning(errApplyDeployment, err))
|
||||
return util.ReconcileWaitResult,
|
||||
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyService)))
|
||||
@@ -155,7 +151,7 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
||||
workload.Name, service.Name)))
|
||||
// garbage collect the service/deployments that we created but not needed
|
||||
if err := r.cleanupResources(ctx, &workload, &deploy.UID, &service.UID); err != nil {
|
||||
log.Error(err, "Failed to clean up resources")
|
||||
klog.ErrorS(err, "Failed to clean up resources")
|
||||
r.record.Event(eventObj, event.Warning(errApplyDeployment, err))
|
||||
}
|
||||
workload.Status.Resources = nil
|
||||
|
||||
+7
-7
@@ -24,11 +24,11 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/klog/v2"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/oam"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
)
|
||||
@@ -56,7 +56,7 @@ func (r *Reconciler) renderDeployment(ctx context.Context,
|
||||
}
|
||||
}
|
||||
}
|
||||
r.log.Info("rendered a deployment", "deploy", deploy.Spec.Template.Spec)
|
||||
klog.InfoS("Rendered a deployment", "deploy", deploy.Spec.Template.Spec)
|
||||
|
||||
// set the controller reference so that we can watch this deployment and it will be deleted automatically
|
||||
if err := ctrl.SetControllerReference(workload, deploy, r.Scheme); err != nil {
|
||||
@@ -112,14 +112,14 @@ func (r *Reconciler) renderConfigMaps(ctx context.Context,
|
||||
// nolint:gocyclo
|
||||
func (r *Reconciler) cleanupResources(ctx context.Context,
|
||||
workload *v1alpha2.ContainerizedWorkload, deployUID, serviceUID *types.UID) error {
|
||||
log := r.log.WithValues("gc deployment", workload.Name)
|
||||
klog.InfoS("GC deployment", "workload", klog.KObj(workload))
|
||||
var deploy appsv1.Deployment
|
||||
var service corev1.Service
|
||||
for _, res := range workload.Status.Resources {
|
||||
uid := res.UID
|
||||
if res.Kind == util.KindDeployment && res.APIVersion == appsv1.SchemeGroupVersion.String() {
|
||||
if uid != *deployUID {
|
||||
log.Info("Found an orphaned deployment", "deployment UID", *deployUID, "orphaned UID", uid)
|
||||
klog.InfoS("Found an orphaned deployment", "deployment UID", *deployUID, "orphaned UID", uid)
|
||||
dn := client.ObjectKey{Name: res.Name, Namespace: workload.Namespace}
|
||||
if err := r.Get(ctx, dn, &deploy); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
@@ -130,11 +130,11 @@ func (r *Reconciler) cleanupResources(ctx context.Context,
|
||||
if err := r.Delete(ctx, &deploy); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Removed an orphaned deployment", "deployment UID", *deployUID, "orphaned UID", uid)
|
||||
klog.InfoS("Removed an orphaned deployment", "deployment UID", *deployUID, "orphaned UID", uid)
|
||||
}
|
||||
} else if res.Kind == util.KindService && res.APIVersion == corev1.SchemeGroupVersion.String() {
|
||||
if uid != *serviceUID {
|
||||
log.Info("Found an orphaned service", "orphaned UID", uid)
|
||||
klog.InfoS("Found an orphaned service", "orphaned UID", uid)
|
||||
sn := client.ObjectKey{Name: res.Name, Namespace: workload.Namespace}
|
||||
if err := r.Get(ctx, sn, &service); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
@@ -145,7 +145,7 @@ func (r *Reconciler) cleanupResources(ctx context.Context,
|
||||
if err := r.Delete(ctx, &service); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Removed an orphaned service", "orphaned UID", uid)
|
||||
klog.InfoS("Removed an orphaned service", "orphaned UID", uid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-3
@@ -27,7 +27,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
|
||||
core "github.com/oam-dev/kubevela/apis/core.oam.dev"
|
||||
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
||||
@@ -64,7 +63,6 @@ func TestRenderDeployment(t *testing.T) {
|
||||
|
||||
r := Reconciler{
|
||||
Client: nil,
|
||||
log: ctrl.Log.WithName("ContainerizedWorkload"),
|
||||
record: nil,
|
||||
Scheme: scheme,
|
||||
}
|
||||
@@ -152,7 +150,6 @@ func TestRenderConfigMaps(t *testing.T) {
|
||||
|
||||
r := Reconciler{
|
||||
Client: nil,
|
||||
log: ctrl.Log.WithName("ContainerizedWorkload"),
|
||||
record: nil,
|
||||
Scheme: scheme,
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package v1alpha2
|
||||
|
||||
import (
|
||||
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
|
||||
controller "github.com/oam-dev/kubevela/pkg/controller/core.oam.dev"
|
||||
@@ -36,18 +35,18 @@ import (
|
||||
)
|
||||
|
||||
// Setup workload controllers.
|
||||
func Setup(mgr ctrl.Manager, args controller.Args, l logging.Logger) error {
|
||||
for _, setup := range []func(ctrl.Manager, controller.Args, logging.Logger) error{
|
||||
func Setup(mgr ctrl.Manager, args controller.Args) error {
|
||||
for _, setup := range []func(ctrl.Manager, controller.Args) error{
|
||||
containerizedworkload.Setup, manualscalertrait.Setup, healthscope.Setup,
|
||||
application.Setup, applicationrollout.Setup, applicationcontext.Setup, appdeployment.Setup,
|
||||
traitdefinition.Setup, componentdefinition.Setup, policydefinition.Setup, workflowstepdefinition.Setup,
|
||||
} {
|
||||
if err := setup(mgr, args, l); err != nil {
|
||||
if err := setup(mgr, args); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if args.ApplicationConfigurationInstalled {
|
||||
return applicationconfiguration.Setup(mgr, args, l)
|
||||
return applicationconfiguration.Setup(mgr, args)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user