Skip to content

Commit

Permalink
Add support for "API groups" (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick D'appollonio <[email protected]>
  • Loading branch information
jokestax and patrickdappollonio authored Nov 14, 2024
1 parent eb5af8a commit f392992
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func root() *cobra.Command {
rootCommand.Flags().BoolVar(&opts.IncludeTripleDash, "include-triple-dash", false, "if enabled, the typical \"---\" YAML separator is included at the beginning of resources sliced")
rootCommand.Flags().BoolVar(&opts.PruneOutputDir, "prune", false, "if enabled, the output directory will be pruned before writing the files")
rootCommand.Flags().BoolVar(&opts.RemoveFileComments, "remove-comments", false, "if enabled, comments generated by the app are removed from the sliced files (but keep comments from the original file)")

rootCommand.Flags().StringSliceVar(&opts.IncludedGroups, "include-group", nil, "resource kind to include in the output (singular, case insensitive, glob supported)")
rootCommand.Flags().StringSliceVar(&opts.ExcludedGroups, "exclude-group", nil, "resource kind to exclude in the output (singular, case insensitive, glob supported)")
_ = rootCommand.Flags().MarkHidden("debug")
return rootCommand
}
Expand Down
5 changes: 2 additions & 3 deletions slice/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

const (
folderChmod = 0775
defaultChmod = 0664
folderChmod = 0o775
defaultChmod = 0o664
)

func (s *Split) processSingleFile(file []byte) error {
Expand Down Expand Up @@ -108,7 +108,6 @@ func (s *Split) scan() error {
for {
// Grab a single line
line, err := scanner.ReadString('\n')

// Find if there's an error
if err != nil {
// If we reached the end of file, handle up to this point
Expand Down
8 changes: 4 additions & 4 deletions slice/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestExecute_writeToFileCases(t *testing.T) {
t.Run("truncate existent file", func(tt *testing.T) {
preexistent := filepath.Join(tempdir, "test_no_newline.txt")

require.NoError(tt, os.WriteFile(preexistent, []byte("foobarbaz"), 0644))
require.NoError(tt, os.WriteFile(preexistent, []byte("foobarbaz"), 0o644))
require.NoError(tt, s.writeToFile(preexistent, []byte("test")))

content, err := os.ReadFile(preexistent)
Expand All @@ -232,12 +232,12 @@ func TestExecute_writeToFileCases(t *testing.T) {
})

t.Run("attempt writing to a read only directory", func(tt *testing.T) {
require.NoError(tt, os.MkdirAll(filepath.Join(tempdir, "readonly"), 0444))
require.NoError(tt, os.MkdirAll(filepath.Join(tempdir, "readonly"), 0o444))
require.Error(tt, s.writeToFile(filepath.Join(tempdir, "readonly", "test.txt"), []byte("test")))
})

t.Run("attempt writing to a read only sub-directory", func(tt *testing.T) {
require.NoError(tt, os.MkdirAll(filepath.Join(tempdir, "readonly_sub"), 0444))
require.NoError(tt, os.MkdirAll(filepath.Join(tempdir, "readonly_sub"), 0o444))
require.Error(tt, s.writeToFile(filepath.Join(tempdir, "readonly_sub", "readonly", "test.txt"), []byte("test")))
})
}
Expand Down Expand Up @@ -317,7 +317,7 @@ metadata:
tdoutput := t.TempDir()
require.NotEqual(t, tdinput, tdoutput, "input and output directories should be different")

err := os.WriteFile(filepath.Join(tdinput, "input.yaml"), []byte(tt.input), 0644)
err := os.WriteFile(filepath.Join(tdinput, "input.yaml"), []byte(tt.input), 0o644)
require.NoError(t, err, "error found while writing input file")

s, err := New(Options{
Expand Down
11 changes: 11 additions & 0 deletions slice/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slice

import (
"sort"
"strings"
)

type yamlFile struct {
Expand All @@ -15,6 +16,16 @@ type kubeObjectMeta struct {
Kind string
Name string
Namespace string
Group string
}

func (objectMeta *kubeObjectMeta) GetGroupFromAPIVersion() string {
fields := strings.Split(objectMeta.APIVersion, "/")
if len(fields) == 2 {
return strings.ToLower(fields[0])
}

return ""
}

func (k kubeObjectMeta) empty() bool {
Expand Down
38 changes: 38 additions & 0 deletions slice/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ func (s *Split) parseYAMLManifest(contents []byte) (yamlFile, error) {
}
}

if len(s.opts.IncludedGroups) > 0 || len(s.opts.ExcludedGroups) > 0 {
if k8smeta.APIVersion == "" {
return yamlFile{}, &cantFindFieldErr{fieldName: "apiVersion", fileCount: s.fileCount, meta: k8smeta}
}

var groups []string
if len(s.opts.IncludedGroups) > 0 {
groups = s.opts.IncludedGroups
} else if len(s.opts.ExcludedGroups) > 0 {
groups = s.opts.ExcludedGroups
}

if err := checkGroup(k8smeta, groups, len(s.opts.IncludedGroups) > 0); err != nil {
return yamlFile{}, &skipErr{}
}
}

// Trim the file name
name := strings.TrimSpace(buf.String())

Expand Down Expand Up @@ -151,3 +168,24 @@ func checkKubernetesBasics(manifest map[string]interface{}) kubeObjectMeta {

return metadata
}

func checkGroup(objmeta kubeObjectMeta, groupName []string, included bool) error {

for _, group := range groupName {
if included {
if objmeta.GetGroupFromAPIVersion() == strings.ToLower(group) {
return nil
}
} else {
if objmeta.GetGroupFromAPIVersion() == strings.ToLower(group) {
return &skipErr{}
}
}
}

if included {
return &skipErr{}
} else {
return nil
}
}
3 changes: 3 additions & 0 deletions slice/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ type Options struct {

AllowEmptyNames bool
AllowEmptyKinds bool

IncludedGroups []string
ExcludedGroups []string
}
3 changes: 1 addition & 2 deletions slice/template/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func Test_jsonReplace(t *testing.T) {
}

func Test_env(t *testing.T) {
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
letters := []rune("abcdefghijklmnopqrstuvwxyz")

randSeq := func(n int) string {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
Expand Down Expand Up @@ -417,7 +417,6 @@ func Test_jsonLowerAndUpper(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

lowered, err := jsonLower(tt.args.val)
requireErrorIf(t, tt.wantErr, err)

Expand Down
1 change: 0 additions & 1 deletion slice/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func loadfolder(extensions []string, folderPath string, recurse bool) (*bytes.Bu

return nil
})

if err != nil {
return nil, 0, err
}
Expand Down
4 changes: 4 additions & 0 deletions slice/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (s *Split) validateFilters() error {
return fmt.Errorf("cannot specify both included and excluded")
}

if len(s.opts.ExcludedGroups) > 0 && len(s.opts.IncludedGroups) > 0 {
return fmt.Errorf("cannot specify both included and excluded groups")
}

// Merge all filters into excluded and included.
for _, v := range s.opts.IncludedKinds {
s.opts.Included = append(s.opts.Included, fmt.Sprintf("%s/*", v))
Expand Down

0 comments on commit f392992

Please sign in to comment.