-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): make regexp for excluded namespace configurable (#79)
- Loading branch information
Showing
10 changed files
with
152 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package operator | ||
|
||
import ( | ||
"reflect" | ||
"regexp" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// stringToRegexpHookFunc returns a DecodeHookFunc that converts strings to regexp.Regexp. | ||
func stringToRegexpHookFunc() mapstructure.DecodeHookFunc { | ||
return func( | ||
f reflect.Type, | ||
t reflect.Type, | ||
data interface{}) (interface{}, error) { | ||
if f.Kind() != reflect.String { | ||
return data, nil | ||
} | ||
|
||
if t != reflect.TypeOf(®exp.Regexp{}) { | ||
return data, nil | ||
} | ||
|
||
return regexp.Compile(data.(string)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package operator | ||
|
||
import ( | ||
"flag" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/statnett/image-scanner-operator/internal/config" | ||
) | ||
|
||
var _ = Describe("Operator config from flags", func() { | ||
var ( | ||
opr Operator = Operator{} | ||
fs *flag.FlagSet | ||
cfg *config.Config | ||
) | ||
|
||
BeforeEach(func() { | ||
fs = flag.NewFlagSet("test", flag.ExitOnError) | ||
cfg = &config.Config{} | ||
Expect(opr.BindFlags(cfg, fs)).To(Succeed()) | ||
}) | ||
|
||
Context("Using scan-namespace-exclude-regexp flag", func() { | ||
It("Should have correct default", func() { | ||
Expect(fs.Parse(nil)).To(Succeed()) | ||
Expect(opr.UnmarshalConfig(cfg)).To(Succeed()) | ||
Expect(cfg.ScanNamespaceExcludeRegexp).NotTo(BeNil()) | ||
Expect(cfg.ScanNamespaceExcludeRegexp.String()).To(Equal("^(kube-|openshift-).*")) | ||
}) | ||
|
||
It("Should be configurable", func() { | ||
args := []string{"--scan-namespace-exclude-regexp=^$"} | ||
Expect(fs.Parse(args)).To(Succeed()) | ||
Expect(opr.UnmarshalConfig(cfg)).To(Succeed()) | ||
Expect(cfg.ScanNamespaceExcludeRegexp).NotTo(BeNil()) | ||
Expect(cfg.ScanNamespaceExcludeRegexp.String()).To(Equal("^$")) | ||
}) | ||
|
||
It("Should error on invalid regexp", func() { | ||
args := []string{"--scan-namespace-exclude-regexp=["} | ||
Expect(fs.Parse(args)).To(Succeed()) | ||
Expect(opr.UnmarshalConfig(cfg)).To(MatchError(ContainSubstring("error parsing regexp"))) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package operator | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestOperator(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Operator Suite") | ||
} |