Skip to content

Commit

Permalink
refactor sources to source pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning authored and ConnorDoyle committed Sep 6, 2017
1 parent b34ad30 commit 0ea8ff6
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 153 deletions.
238 changes: 232 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"regexp"
"strings"

"github.com/docopt/docopt-go"
"github.com/kubernetes-incubator/node-feature-discovery/source"
"github.com/kubernetes-incubator/node-feature-discovery/source/cpuid"
"github.com/kubernetes-incubator/node-feature-discovery/source/fake"
"github.com/kubernetes-incubator/node-feature-discovery/source/panic_fake"
"github.com/kubernetes-incubator/node-feature-discovery/source/pstate"
"github.com/kubernetes-incubator/node-feature-discovery/source/rdt"
k8sclient "k8s.io/client-go/kubernetes"
api "k8s.io/client-go/pkg/api/v1"
restclient "k8s.io/client-go/rest"
"github.com/docopt/docopt-go"
)

const (
Expand Down Expand Up @@ -128,21 +134,22 @@ func argsParse(argv []string) (noPublish bool, sourcesArg []string, whiteListArg

// configureParameters returns all the variables required to perform feature
// discovery based on command line arguments.
func configureParameters(sourcesArg []string, whiteListArg string) (sources []FeatureSource, labelWhiteList *regexp.Regexp, err error) {
func configureParameters(sourcesArg []string, whiteListArg string) (sources []source.FeatureSource, labelWhiteList *regexp.Regexp, err error) {
enabledSources := map[string]struct{}{}
for _, s := range sourcesArg {
enabledSources[strings.TrimSpace(s)] = struct{}{}
}

// Configure feature sources.
allSources := []FeatureSource{
cpuidSource{},
rdtSource{},
pstateSource{},
fakeSource{},
allSources := []source.FeatureSource{
cpuid.Source{},
rdt.Source{},
pstate.Source{},
fake.Source{},
panic_fake.Source{},
}

sources = []FeatureSource{}
sources = []source.FeatureSource{}
for _, s := range allSources {
if _, enabled := enabledSources[s.Name()]; enabled {
sources = append(sources, s)
Expand All @@ -161,7 +168,7 @@ func configureParameters(sourcesArg []string, whiteListArg string) (sources []Fe

// createFeatureLabels returns the set of feature labels from the enabled
// sources and the whitelist argument.
func createFeatureLabels(sources []FeatureSource, labelWhiteList *regexp.Regexp) (labels Labels) {
func createFeatureLabels(sources []source.FeatureSource, labelWhiteList *regexp.Regexp) (labels Labels) {
labels = Labels{}
// Add the version of this discovery code as a node label
versionLabel := fmt.Sprintf("%s/%s.version", Namespace, ProgramName)
Expand Down Expand Up @@ -208,7 +215,7 @@ func updateNodeWithFeatureLabels(helper APIHelpers, noPublish bool, labels Label

// getFeatureLabels returns node labels for features discovered by the
// supplied source.
func getFeatureLabels(source FeatureSource) (labels Labels, err error) {
func getFeatureLabels(source source.FeatureSource) (labels Labels, err error) {
defer func() {
if r := recover(); r != nil {
stderrLogger.Printf("panic occurred during discovery of source [%s]: %v", source.Name(), r)
Expand Down
17 changes: 10 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"regexp"
"testing"

"github.com/kubernetes-incubator/node-feature-discovery/source"
"github.com/kubernetes-incubator/node-feature-discovery/source/fake"
"github.com/kubernetes-incubator/node-feature-discovery/source/panic_fake"
. "github.com/smartystreets/goconvey/convey"
"github.com/vektra/errors"
k8sclient "k8s.io/client-go/kubernetes"
Expand All @@ -20,7 +23,7 @@ func TestDiscoveryWithMockSources(t *testing.T) {
for _, f := range fakeFeatures {
fakeFeatureLabels[fmt.Sprintf("%s-testSource-%s", prefix, f)] = "true"
}
fakeFeatureSource := FeatureSource(mockFeatureSource)
fakeFeatureSource := source.FeatureSource(mockFeatureSource)

Convey("When I successfully get the labels from the mock source", func() {
mockFeatureSource.On("Name").Return(fakeFeatureSourceName)
Expand Down Expand Up @@ -194,7 +197,7 @@ func TestConfigureParameters(t *testing.T) {
})
Convey("Proper sources are returned", func() {
So(len(sources), ShouldEqual, 1)
So(sources[0], ShouldHaveSameTypeAs, fakeSource{})
So(sources[0], ShouldHaveSameTypeAs, fake.Source{})
So(labelWhiteList, ShouldResemble, emptyRegexp)
})
})
Expand Down Expand Up @@ -232,8 +235,8 @@ func TestCreateFeatureLabels(t *testing.T) {
Convey("When creating feature labels from the configured sources", t, func() {
Convey("When fake feature source is configured", func() {
emptyLabelWL, _ := regexp.Compile("")
fakeFeatureSource := FeatureSource(new(fakeSource))
sources := []FeatureSource{}
fakeFeatureSource := source.FeatureSource(new(fake.Source))
sources := []source.FeatureSource{}
sources = append(sources, fakeFeatureSource)
labels := createFeatureLabels(sources, emptyLabelWL)

Expand All @@ -246,8 +249,8 @@ func TestCreateFeatureLabels(t *testing.T) {
})
Convey("When fake feature source is configured with a whitelist that doesn't match", func() {
emptyLabelWL, _ := regexp.Compile(".*rdt.*")
fakeFeatureSource := FeatureSource(new(fakeSource))
sources := []FeatureSource{}
fakeFeatureSource := source.FeatureSource(new(fake.Source))
sources := []source.FeatureSource{}
sources = append(sources, fakeFeatureSource)
labels := createFeatureLabels(sources, emptyLabelWL)

Expand Down Expand Up @@ -326,7 +329,7 @@ func TestRemoveLabels(t *testing.T) {

func TestGetFeatureLabels(t *testing.T) {
Convey("When I get feature labels and panic occurs during discovery of a feature source", t, func() {
fakePanicFeatureSource := FeatureSource(new(fakePanicSource))
fakePanicFeatureSource := source.FeatureSource(new(panic_fake.Source))

returnedLabels, err := getFeatureLabels(fakePanicFeatureSource)
Convey("No label is returned", func() {
Expand Down
Loading

0 comments on commit 0ea8ff6

Please sign in to comment.