66 "regexp"
77 "time"
88
9- "github.com/openshift/library-go/pkg/operator/v1helpers"
109 "github.com/spf13/cobra"
1110 "github.com/spf13/pflag"
1211
@@ -25,6 +24,13 @@ const providerName = "kms-health-reporter"
2524// mounted at, e.g. unix:///var/run/kmsplugin/kms-1.sock.
2625var kmsSocketPattern = regexp .MustCompile (`^unix:///var/run/kmsplugin/kms-(\d+)\.sock$` )
2726
27+ // NewEncryptionStatusWriterFunc builds the EncryptionStatusWriter for a target
28+ // CR. The caller binds the concrete CR; this package only supplies the rest
29+ // config and a reporterID. reporterID is a stable, per-node identifier the write
30+ // path uses to claim ownership of its own entries; with server-side apply, pass
31+ // it as the field manager.
32+ type NewEncryptionStatusWriterFunc func (restConfig * rest.Config , reporterID string ) (EncryptionStatusWriter , error )
33+
2834// options' flag-bound fields are exported so the struct can be logged as a
2935// whole via klog.InfoS, which JSON-marshals its values.
3036type options struct {
@@ -35,21 +41,21 @@ type options struct {
3541 NodeName string
3642 Kubeconfig string
3743
38- newOperatorClient func ( * rest. Config ) (v1helpers. OperatorClient , error )
44+ newWriter NewEncryptionStatusWriterFunc
3945}
4046
4147type Config struct {
42- operatorClient v1helpers. OperatorClient
43- prober * prober
48+ writeStatus EncryptionStatusWriter
49+ prober * prober
4450
4551 interval time.Duration
4652 writeTimeout time.Duration
4753 nodeName string
4854}
4955
50- func NewCommand (ctx context.Context , newOperatorClient func ( * rest. Config ) (v1helpers. OperatorClient , error ) ) * cobra.Command {
56+ func NewCommand (ctx context.Context , newWriter NewEncryptionStatusWriterFunc ) * cobra.Command {
5157 o := & options {
52- newOperatorClient : newOperatorClient ,
58+ newWriter : newWriter ,
5359 }
5460
5561 cmd := & cobra.Command {
@@ -126,9 +132,12 @@ func (o *options) Config(ctx context.Context) (*Config, error) {
126132 return nil , fmt .Errorf ("build rest config: %w" , err )
127133 }
128134
129- operatorClient , err := o .newOperatorClient (restCfg )
135+ // reporterID is the per-node ownership identity. The naming convention lives
136+ // here, not in the caller's writer, so all three operators stay uniform.
137+ reporterID := "kms-health-reporter-" + o .NodeName
138+ writeStatus , err := o .newWriter (restCfg , reporterID )
130139 if err != nil {
131- return nil , fmt .Errorf ("build operator client : %w" , err )
140+ return nil , fmt .Errorf ("build encryption status writer : %w" , err )
132141 }
133142
134143 plugins , err := buildPlugins (ctx , o .KMSSockets , o .ReadTimeout )
@@ -137,21 +146,25 @@ func (o *options) Config(ctx context.Context) (*Config, error) {
137146 }
138147
139148 return & Config {
140- operatorClient : operatorClient ,
141- prober : newProber (plugins ),
142- interval : o .Interval ,
143- writeTimeout : o .WriteTimeout ,
144- nodeName : o .NodeName ,
149+ writeStatus : writeStatus ,
150+ prober : newProber (plugins ),
151+ interval : o .Interval ,
152+ writeTimeout : o .WriteTimeout ,
153+ nodeName : o .NodeName ,
145154 }, nil
146155}
147156
148157func (c * Config ) Run (ctx context.Context ) error {
149158 wait .JitterUntilWithContext (ctx , func (ctx context.Context ) {
150159 // Each Status RPC enforces the read timeout internally (set at dial
151160 // time); ctx here only carries shutdown cancellation.
152- conditions := c .prober .probeAll (ctx )
153- // TODO: hand conditions to the writer once it lands; logging is a placeholder.
154- klog .InfoS ("kms plugin health" , "conditions" , conditions )
161+ reports := c .prober .probeAll (ctx )
162+
163+ writeCtx , cancel := context .WithTimeout (ctx , c .writeTimeout )
164+ defer cancel ()
165+ if err := c .writeStatus (writeCtx , buildEncryptionStatus (c .nodeName , reports )); err != nil {
166+ klog .ErrorS (err , "failed to publish kms plugin health" )
167+ }
155168 }, c .interval , 0.1 , false )
156169
157170 return nil
0 commit comments