Skip to content

Commit d50df47

Browse files
committed
Fix configmap driverProperties loading in GetConfigMap
1 parent d718f3a commit d50df47

1 file changed

Lines changed: 58 additions & 6 deletions

File tree

shifu-sdk-golang/sdk.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Client struct {
4646
configPath string
4747
healthCheckInterval time.Duration
4848
configLoader ConfigLoader
49+
ctx context.Context
4950
}
5051

5152
// Config holds configuration for creating a new Client
@@ -100,6 +101,10 @@ func (d *defaultConfigLoader) LoadInstructions(path string) (*DeviceShifuInstruc
100101

101102
// NewClient creates a new Shifu SDK client with the given configuration
102103
func NewClient(ctx context.Context, cfg *Config) (*Client, error) {
104+
if ctx == nil {
105+
ctx = context.Background()
106+
}
107+
103108
if cfg == nil {
104109
cfg = &Config{}
105110
}
@@ -141,6 +146,7 @@ func NewClient(ctx context.Context, cfg *Config) (*Client, error) {
141146
configPath: cfg.ConfigPath,
142147
healthCheckInterval: cfg.HealthCheckInterval,
143148
configLoader: &defaultConfigLoader{},
149+
ctx: ctx,
144150
}
145151

146152
logger.Infof("Edge device rest client initialized successfully")
@@ -154,6 +160,10 @@ func NewClientFromEnv(ctx context.Context) (*Client, error) {
154160

155161
// Start begins the health check loop
156162
func (c *Client) Start(ctx context.Context) error {
163+
if ctx == nil {
164+
ctx = context.Background()
165+
}
166+
157167
if c.restClient == nil {
158168
return fmt.Errorf("edge device rest client is not initialized")
159169
}
@@ -171,7 +181,7 @@ func (c *Client) Start(ctx context.Context) error {
171181
select {
172182
case <-ticker.C:
173183
phase := c.healthChecker()
174-
if err := c.UpdatePhase(phase); err != nil {
184+
if err := c.updatePhase(ctx, phase); err != nil {
175185
logger.Errorf("failed to update edge device phase: %v", err)
176186
}
177187
case <-ctx.Done():
@@ -182,6 +192,10 @@ func (c *Client) Start(ctx context.Context) error {
182192

183193
// GetEdgeDevice retrieves the EdgeDevice resource
184194
func (c *Client) GetEdgeDevice() (*v1alpha1.EdgeDevice, error) {
195+
return c.getEdgeDevice(c.ctx)
196+
}
197+
198+
func (c *Client) getEdgeDevice(ctx context.Context) (*v1alpha1.EdgeDevice, error) {
185199
if c.restClient == nil {
186200
return nil, fmt.Errorf("rest client is not initialized")
187201
}
@@ -191,7 +205,7 @@ func (c *Client) GetEdgeDevice() (*v1alpha1.EdgeDevice, error) {
191205
Namespace(c.edgedeviceNamespace).
192206
Resource("edgedevices").
193207
Name(c.edgedeviceName).
194-
Do(context.TODO()).
208+
Do(ctx).
195209
Into(ed)
196210
if err != nil {
197211
logger.Errorf("Error GET EdgeDevice resource: %v", err)
@@ -202,11 +216,19 @@ func (c *Client) GetEdgeDevice() (*v1alpha1.EdgeDevice, error) {
202216

203217
// UpdatePhase updates the EdgeDevice phase
204218
func (c *Client) UpdatePhase(phase v1alpha1.EdgeDevicePhase) error {
219+
return c.updatePhase(c.ctx, phase)
220+
}
221+
222+
func (c *Client) updatePhase(ctx context.Context, phase v1alpha1.EdgeDevicePhase) error {
205223
if c.restClient == nil {
206224
return fmt.Errorf("rest client is not initialized")
207225
}
208226

209-
ed, err := c.GetEdgeDevice()
227+
if ctx == nil {
228+
ctx = context.Background()
229+
}
230+
231+
ed, err := c.getEdgeDevice(ctx)
210232
if err != nil {
211233
return err
212234
}
@@ -225,7 +247,7 @@ func (c *Client) UpdatePhase(phase v1alpha1.EdgeDevicePhase) error {
225247
Resource("edgedevices").
226248
Name(c.edgedeviceName).
227249
Body(ed).
228-
Do(context.TODO()).
250+
Do(ctx).
229251
Error()
230252
if err != nil {
231253
logger.Errorf("Error PUT EdgeDevice resource: %v", err)
@@ -245,8 +267,15 @@ func (c *Client) GetConfigMap() (*DeviceShifuConfig[any], error) {
245267
}
246268
}
247269

270+
driverProperties, err := loadDriverPropertiesFromFile(c.configPath + "/driverProperties")
271+
if err != nil {
272+
log.Printf("Warning: Failed to load driver properties from configmap file: %v", err)
273+
driverProperties = &DeviceShifuDriverProperties{}
274+
}
275+
248276
return &DeviceShifuConfig[any]{
249-
Instructions: *instructions,
277+
DriverProperties: *driverProperties,
278+
Instructions: *instructions,
250279
}, nil
251280
}
252281

@@ -260,8 +289,15 @@ func GetConfigMapTyped[T any](c *Client) (*DeviceShifuConfig[T], error) {
260289
}
261290
}
262291

292+
driverProperties, err := loadDriverPropertiesFromFile(c.configPath + "/driverProperties")
293+
if err != nil {
294+
log.Printf("Warning: Failed to load driver properties from configmap file: %v", err)
295+
driverProperties = &DeviceShifuDriverProperties{}
296+
}
297+
263298
return &DeviceShifuConfig[T]{
264-
Instructions: *instructions,
299+
DriverProperties: *driverProperties,
300+
Instructions: *instructions,
265301
}, nil
266302
}
267303

@@ -291,6 +327,22 @@ func loadInstructionsFromFile[T any](filePath string) (*DeviceShifuInstructions[
291327
return &instructions, nil
292328
}
293329

330+
// loadDriverPropertiesFromFile loads driver properties from a file
331+
func loadDriverPropertiesFromFile(filePath string) (*DeviceShifuDriverProperties, error) {
332+
data, err := os.ReadFile(filePath)
333+
if err != nil {
334+
return nil, fmt.Errorf("failed to read driverProperties file: %w", err)
335+
}
336+
337+
var driverProperties DeviceShifuDriverProperties
338+
if err := yaml.Unmarshal(data, &driverProperties); err != nil {
339+
return nil, fmt.Errorf("failed to unmarshal driver properties: %w", err)
340+
}
341+
342+
log.Printf("Loaded driver properties from configmap file")
343+
return &driverProperties, nil
344+
}
345+
294346
// newEdgeDeviceRestClient creates a REST client for EdgeDevice resources
295347
func newEdgeDeviceRestClient(config *rest.Config) (*rest.RESTClient, error) {
296348
if err := v1alpha1.AddToScheme(scheme.Scheme); err != nil {

0 commit comments

Comments
 (0)