Skip to content

Commit

Permalink
Adding documentation to the NFD controller resources file
Browse files Browse the repository at this point in the history
Adding documentation describing the funcs, vars, etc. in the NFD
controller resources file so that users and contributors can
understand how they all work.
  • Loading branch information
courtneypacheco committed May 14, 2021
1 parent dfcf161 commit d61b601
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion controllers/nodefeaturediscovery_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ import (
"k8s.io/kubectl/pkg/scheme"
)

// assetsFromFile is a list where each item in the list contains the
// contents of a given file as a list of bytes
type assetsFromFile []byte

// Resources holds objects owned by NFD
// Resources holds objects owned by NFD. This struct is used with the
// 'NFD' struct to assist in the process of checking if NFD's resources
// are 'Ready' or 'NotReady'.
type Resources struct {
Namespace corev1.Namespace
ServiceAccount corev1.ServiceAccount
Expand All @@ -58,8 +62,17 @@ func Add3dpartyResourcesToScheme(scheme *runtime.Scheme) error {
return nil
}

// filePathWalkDir takes a path as an input and finds all files
// in that path, but not directories
func filePathWalkDir(root string) ([]string, error) {

// files contains the list of files found in the path
// 'root'
var files []string

// Walk through the files in 'path', and if the os.FileInfo object
// states that the item is not a directory, append it to the list
// of files
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
Expand All @@ -69,34 +82,66 @@ func filePathWalkDir(root string) ([]string, error) {
return files, err
}

// getAssetsFrom takes a path as an input and grabs all of the
// file names in that path, then returns a list of the manifests
// it found in that path.
func getAssetsFrom(path string) []assetsFromFile {

// manifests is a list type where each item in the list
// contains the contents of a given asset (manifest)
manifests := []assetsFromFile{}
assets := path

// For the given path, find a list of all the files
files, err := filePathWalkDir(assets)
if err != nil {
panic(err)
}

// For each file in the 'files' list, read the file
// and store its contents in 'manifests'
for _, file := range files {

// Read the file and return its contents in 'buffer'
buffer, err := ioutil.ReadFile(file)

// If we have an error, then something unexpectedly went
// wrong when reading the file's contents
if err != nil {
panic(err)
}

// If the reading goes smoothly, then append the buffer
// (the file's contents) to the list of manifests
manifests = append(manifests, buffer)
}
return manifests
}


func addResourcesControls(path string) (Resources, controlFunc) {

// res is a Resources object that contains information about
// a given manifest, such as the Namespace and ServiceAccount
// being used
res := Resources{}

// ctrl is a controlFunc object that contains a function
// that returns information about the status of a resource
// (i.e., Ready or NotReady)
ctrl := controlFunc{}

// Get the list of manifests from the given path
manifests := getAssetsFrom(path)

// s and reg are used later on to parse the manifest YAML
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme,
scheme.Scheme)
reg, _ := regexp.Compile(`\b(\w*kind:\w*)\B.*\b`)

// For each manifest, find its kind, then append the appropriate
// function (e.g., 'Namespace' or 'Role') to ctrl so that the
// Namespace, Role, etc. can be parsed
for _, m := range manifests {
kind := reg.FindString(string(m))
slce := strings.Split(kind, ":")
Expand Down Expand Up @@ -153,6 +198,7 @@ func addResourcesControls(path string) (Resources, controlFunc) {
return res, ctrl
}

// Trigger a panic if an error occurs
func panicIfError(err error) {
if err != nil {
panic(err)
Expand Down

0 comments on commit d61b601

Please sign in to comment.