only create resourceMap on demand

making resourceMap a singleton object and only initialize it once
if one or more CRDs have a resourceName annotation in them.

Added copyright header for checkpoint/checkpoint.go.
Replaced fmt.Errorf with logging.

Change-Id: I54628d69324833e70a75dcf6533e6642dedde9b5
This commit is contained in:
Abdul Halim
2018-09-18 13:14:58 +01:00
committed by Tomofumi Hayashi
parent 2cfaa19dda
commit 5988b7a82b
2 changed files with 55 additions and 17 deletions

View File

@@ -1,10 +1,25 @@
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package checkpoint
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/intel/multus-cni/logging"
"github.com/intel/multus-cni/types"
)
@@ -38,27 +53,41 @@ func getPodEntries() ([]PodDevicesEntry, error) {
cpd := &Data{}
rawBytes, err := ioutil.ReadFile(checkPointfile)
if err != nil {
return podEntries, fmt.Errorf("getPodEntries(): error reading file %s\n%v\n", checkPointfile, err)
return podEntries, logging.Errorf("getPodEntries(): error reading file %s\n%v\n", checkPointfile, err)
}
if err = json.Unmarshal(rawBytes, cpd); err != nil {
return podEntries, fmt.Errorf("getPodEntries(): error unmarshalling raw bytes %v", err)
return podEntries, logging.Errorf("getPodEntries(): error unmarshalling raw bytes %v", err)
}
return cpd.Data.PodDeviceEntries, nil
}
// GetComputeDeviceMap returns a map of resourceName to list of device IDs
var instance map[string]*types.ResourceInfo
// GetComputeDeviceMap returns an instance of a map of ResourceInfo
func GetComputeDeviceMap(podID string) (map[string]*types.ResourceInfo, error) {
if instance == nil {
if resourceMap, err := getResourceMapFromFile(podID); err == nil {
logging.Debugf("GetComputeDeviceMap(): created new instance of resourceMap for Pod: %s", podID)
instance = resourceMap
} else {
logging.Errorf("GetComputeDeviceMap(): error creating resourceMap instance %v", err)
return nil, err
}
}
logging.Debugf("GetComputeDeviceMap(): resourceMap instance: %+v", instance)
return instance, nil
}
func getResourceMapFromFile(podID string) (map[string]*types.ResourceInfo, error) {
resourceMap := make(map[string]*types.ResourceInfo)
podEntires, err := getPodEntries()
if err != nil {
return nil, err
}
for _, pod := range podEntires {
if pod.PodUID == podID {
entry, ok := resourceMap[pod.ResourceName]
@@ -71,6 +100,5 @@ func GetComputeDeviceMap(podID string) (map[string]*types.ResourceInfo, error) {
}
}
}
return resourceMap, nil
}