• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

diranged / oz / 4661924665

10 Apr 2023 10:46PM UTC coverage: 45.771% (+1.1%) from 44.648%
4661924665

push

github

GitHub
feat: log kubernetes events (#96)

145 of 145 new or added lines in 9 files covered. (100.0%)

947 of 2069 relevant lines covered (45.77%)

1.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.29
/internal/controllers/templatecontroller/types.go
1
// Package templatecontroller implements a TemplateReconciler that can
2
// reconcile Access Templates in a general sense.
3
package templatecontroller
4

5
import (
6
        "context"
7
        "reflect"
8
        "time"
9

10
        "github.com/diranged/oz/internal/api/v1alpha1"
11
        "github.com/diranged/oz/internal/builders"
12
        "github.com/diranged/oz/internal/controllers"
13
        "github.com/go-logr/logr"
14
        "k8s.io/apimachinery/pkg/runtime"
15
        "k8s.io/client-go/tools/record"
16
        ctrl "sigs.k8s.io/controller-runtime"
17
        "sigs.k8s.io/controller-runtime/pkg/client"
18
        "sigs.k8s.io/controller-runtime/pkg/manager"
19
)
20

21
// TemplateReconciler is configured to watch for a particular type
22
// (TemplateType) of Access Template and then execute the reconciler logic
23
// against them.
24
//
25
// Unlike Access Requests, we don't believe that Templates need significant
26
// enough validation logic that they warrant their own IBuilder class.
27
type TemplateReconciler struct {
28
        client.Client
29
        Scheme   *runtime.Scheme
30
        recorder record.EventRecorder
31

32
        // APIReader should be generated with mgr.GetAPIReader() to create a non-cached client object.
33
        // This is used for certain Get() calls where we need to ensure we are getting the latest
34
        // version from the API, and not a cached object.
35
        //
36
        // See https://github.com/kubernetes-sigs/controller-runtime/issues/585#issuecomment-528102351
37
        //
38
        APIReader client.Reader
39

40
        // TemplateType informs the RequestReconciler what "Kind" of objects it
41
        // is going to Watch for, and how to retrive them from the Kubernetes API.
42
        TemplateType v1alpha1.ITemplateResource
43

44
        // Builder provides an IBuilder compatible object for handling the RequestType reconciliation
45
        Builder builders.IBuilder
46

47
        // Frequency to re-reconcile successfully reconciled templates
48
        ReconciliationInterval time.Duration
49
}
50

51
// NewTemplateReconciler returns a pointer to a TemplateReconciler.
52
func NewTemplateReconciler(
53
        mgr manager.Manager,
54
        res v1alpha1.ITemplateResource,
55
        interval int,
56
) *TemplateReconciler {
×
57
        return &TemplateReconciler{
×
58
                Client:                 mgr.GetClient(),
×
59
                Scheme:                 mgr.GetScheme(),
×
60
                APIReader:              mgr.GetAPIReader(),
×
61
                recorder:               mgr.GetEventRecorderFor(controllers.EventRecorderName),
×
62
                TemplateType:           res,
×
63
                ReconciliationInterval: time.Duration(interval) * time.Minute,
×
64
        }
×
65
}
×
66

67
// GetAPIReader conforms to the internal.status.hasStatusReconciler interface.
68
func (r *TemplateReconciler) GetAPIReader() client.Reader {
12✔
69
        return r.APIReader
12✔
70
}
12✔
71

72
// RequestContext represents a reconciliation request context.
73
type RequestContext struct {
74
        context.Context
75

76
        resourceType string
77
        obj          v1alpha1.ITemplateResource
78
        req          ctrl.Request
79
        log          logr.Logger
80
}
81

82
func newRequestContext(
83
        ctx context.Context,
84
        sourceObj v1alpha1.ITemplateResource,
85
        req ctrl.Request,
86
) *RequestContext {
11✔
87
        // Determine the Resource Type string which will be used for the logger
11✔
88
        resourceType := reflect.TypeOf(sourceObj).String()
11✔
89

11✔
90
        // Create an empty object that we'll be using for the duration of this reconciliation
11✔
91
        emptyObj := sourceObj.DeepCopyObject().(v1alpha1.ITemplateResource)
11✔
92

11✔
93
        return &RequestContext{
11✔
94
                Context:      ctx,
11✔
95
                resourceType: resourceType,
11✔
96
                obj:          emptyObj,
11✔
97
                req:          req,
11✔
98
                log:          ctrl.LoggerFrom(ctx).WithName("TemplateReconciler"),
11✔
99
        }
11✔
100
}
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc