• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dtaniwaki / git-kustomize-diff / 6105694635

07 Sep 2023 05:02AM UTC coverage: 61.39% (+9.9%) from 51.515%
6105694635

push

github

web-flow
feat: support kustomize-load-restrictor flag (#1)

44 of 44 new or added lines in 2 files covered. (100.0%)

318 of 518 relevant lines covered (61.39%)

6.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.9
/pkg/gitkustomizediff/diff.go
1
/*
2
Copyright 2021 Daisuke Taniwaki.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package gitkustomizediff
18

19
import (
20
        "path/filepath"
21
        "regexp"
22

23
        "github.com/dtaniwaki/git-kustomize-diff/pkg/utils"
24
        "github.com/pkg/errors"
25
        log "github.com/sirupsen/logrus"
26
        "sigs.k8s.io/kustomize/api/krusty"
27
        "sigs.k8s.io/kustomize/api/types"
28
        "sigs.k8s.io/kustomize/kyaml/filesys"
29
)
30

31
type DiffOpts struct {
32
        IncludeRegexp           *regexp.Regexp
33
        ExcludeRegexp           *regexp.Regexp
34
        KustomizePath           string
35
        KustomizeLoadRestrictor string
36
}
37

38
func Diff(baseDirPath, targetDirPath string, opts DiffOpts) (*DiffMap, error) {
4✔
39
        log.Info("Start diff")
4✔
40
        listOpts := utils.ListKustomizeDirsOpts{
4✔
41
                IncludeRegexp: opts.IncludeRegexp,
4✔
42
                ExcludeRegexp: opts.ExcludeRegexp,
4✔
43
        }
4✔
44
        baseKDirs, err := utils.ListKustomizeDirs(baseDirPath, listOpts)
4✔
45
        if err != nil {
4✔
46
                return nil, err
×
47
        }
×
48
        log.Debugf("base dirs: %+v", baseKDirs)
4✔
49
        targetKDirs, err := utils.ListKustomizeDirs(targetDirPath, listOpts)
4✔
50
        if err != nil {
4✔
51
                return nil, err
×
52
        }
×
53
        log.Debugf("target dirs: %+v", targetKDirs)
4✔
54
        kDirs := map[string]struct{}{}
4✔
55
        for _, kDir := range append(baseKDirs, targetKDirs...) {
16✔
56
                kDirs[kDir] = struct{}{}
12✔
57
        }
12✔
58
        diffMap := NewDiffMap()
4✔
59
        for kDir := range kDirs {
10✔
60
                baseKDirPath := filepath.Join(baseDirPath, kDir)
6✔
61
                if !utils.KustomizationExists(baseKDirPath) {
6✔
62
                        err := utils.MakeKustomizeDir(baseKDirPath)
×
63
                        if err != nil {
×
64
                                diffMap.Results[kDir] = &DiffError{err}
×
65
                                continue
×
66
                        }
67
                }
68
                targetKDirPath := filepath.Join(targetDirPath, kDir)
6✔
69
                if !utils.KustomizationExists(targetKDirPath) {
6✔
70
                        err := utils.MakeKustomizeDir(targetKDirPath)
×
71
                        if err != nil {
×
72
                                diffMap.Results[kDir] = &DiffError{err}
×
73
                                continue
×
74
                        }
75
                }
76
                baseYaml, err := Build(baseKDirPath, BuildOpts{opts.KustomizePath, opts.KustomizeLoadRestrictor})
6✔
77
                if err != nil {
8✔
78
                        diffMap.Results[kDir] = &DiffError{err}
2✔
79
                        continue
2✔
80
                }
81
                targetYaml, err := Build(targetKDirPath, BuildOpts{opts.KustomizePath, opts.KustomizeLoadRestrictor})
4✔
82
                if err != nil {
4✔
83
                        diffMap.Results[kDir] = &DiffError{err}
×
84
                        continue
×
85
                }
86

87
                content, err := utils.Diff(baseYaml, targetYaml)
4✔
88
                if err != nil {
4✔
89
                        diffMap.Results[kDir] = &DiffError{err}
×
90
                        continue
×
91
                }
92
                diffMap.Results[kDir] = &DiffContent{content}
4✔
93
        }
94
        return diffMap, nil
4✔
95
}
96

97
func MakeBuildOptions(kustomizeLoadRestrictor string) (*krusty.Options, error) {
18✔
98
        var err error
18✔
99
        options := krusty.MakeDefaultOptions()
18✔
100
        if kustomizeLoadRestrictor == "" {
29✔
101
                return options, err
11✔
102
        }
11✔
103
        switch kustomizeLoadRestrictor {
7✔
104
        case "LoadRestrictionsUnknown":
1✔
105
                {
2✔
106
                        options.LoadRestrictions = types.LoadRestrictionsUnknown
1✔
107
                }
1✔
108
        case "LoadRestrictionsRootOnly":
1✔
109
                {
2✔
110
                        options.LoadRestrictions = types.LoadRestrictionsRootOnly
1✔
111
                }
1✔
112
        case "LoadRestrictionsNone":
4✔
113
                {
8✔
114
                        options.LoadRestrictions = types.LoadRestrictionsNone
4✔
115
                }
4✔
116
        default:
1✔
117
                {
2✔
118
                        err := errors.Errorf("unknown LoadRestrictions type given by kustomizeLoadRestrictor: %q", kustomizeLoadRestrictor)
1✔
119
                        return nil, err
1✔
120
                }
1✔
121
        }
122
        return options, err
6✔
123
}
124

125
type BuildOpts struct {
126
        KustomizePath           string
127
        KustomizeLoadRestrictor string
128
}
129

130
func Build(dirPath string, opts BuildOpts) (string, error) {
13✔
131
        if opts.KustomizePath != "" {
13✔
132
                buildArgs := []string {"build"}
×
133
                if (opts.KustomizeLoadRestrictor != "") {
×
134
                        buildArgs = append(buildArgs, "--load-restrictor")
×
135
                        buildArgs = append(buildArgs, opts.KustomizeLoadRestrictor)
×
136
                }
×
137
                buildArgs = append(buildArgs, dirPath)
×
138
                stdout, _, err := (&utils.WorkDir{}).RunCommand(opts.KustomizePath, buildArgs...)
×
139
                if err != nil {
×
140
                        return "", err
×
141
                }
×
142
                return stdout, nil
×
143
        }
144
        options, err := MakeBuildOptions(opts.KustomizeLoadRestrictor)
13✔
145
        if err != nil {
13✔
146
                return "", errors.WithStack(err)
×
147
        }
×
148
        k := krusty.MakeKustomizer(
13✔
149
                options,
13✔
150
        )
13✔
151
        fSys := filesys.MakeFsOnDisk()
13✔
152
        resMap, err := k.Run(fSys, dirPath)
13✔
153
        if err != nil {
16✔
154
                return "", errors.WithStack(err)
3✔
155
        }
3✔
156
        bs, err := resMap.AsYaml()
10✔
157
        if err != nil {
10✔
158
                return "", errors.WithStack(err)
×
159
        }
×
160
        return string(bs), nil
10✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc