Skip to content

Commit ee081f9

Browse files
author
Eric Stroczynski
authored
internal/pkg/scaffold/crd.go: remove CRD cache (#1636) (#1712)
* internal/pkg/scaffold/crd.go: remove CRD cache so manifests are generated with the correct domain * CHANGELOG.md: bug fix for CRD domain names
1 parent 2087bd1 commit ee081f9

File tree

2 files changed

+28
-45
lines changed

2 files changed

+28
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Generated CSV's that include a deployment install strategy will be checked for a reference to `metadata.annotations['olm.targetNamespaces']`, and if one is not found a reference will be added to the `WATCH_NAMESPACE` env var for all containers in the deployment. This is a bug because any other value that references the CSV's namespace is incorrect. ([#1396](https://github.com/operator-framework/operator-sdk/pull/1396))
1515
- Build `-trimpath` was not being respected. `$GOPATH` was not expanding because `exec.Cmd{}` is not executed in a shell environment. ([#1535](https://github.com/operator-framework/operator-sdk/pull/1535))
1616
- Running the [scorecard](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#up) with `--olm-deployed` will now only use the first CR set in either the `cr-manifest` config option or the CSV's `metadata.annotations['alm-examples']` as was intended, and access manifests correctly from the config. ([#1565](https://github.com/operator-framework/operator-sdk/pull/1565))
17+
- Use the correct domain names when generating CRD's instead that of the first CRD to be parsed. ([#1636](https://github.com/operator-framework/operator-sdk/pull/1636))
1718

1819
## v0.8.1
1920

internal/pkg/scaffold/crd.go

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,9 @@ func (s *CRD) GetInput() (input.Input, error) {
6464
s.Resource.LowerKind)
6565
s.Path = filepath.Join(CRDsDir, fileName)
6666
}
67-
initCache()
6867
return s.Input, nil
6968
}
7069

71-
type fsCache struct {
72-
afero.Fs
73-
}
74-
75-
func (c *fsCache) fileExists(path string) bool {
76-
_, err := c.Stat(path)
77-
return err == nil
78-
}
79-
80-
var (
81-
// Global cache so users can use new CRD structs.
82-
cache *fsCache
83-
once sync.Once
84-
)
85-
86-
func initCache() {
87-
once.Do(func() {
88-
cache = &fsCache{Fs: afero.NewMemMapFs()}
89-
})
90-
}
91-
9270
var _ CustomRenderer = &CRD{}
9371

9472
func (s *CRD) SetFS(fs afero.Fs) { s.initFS(fs) }
@@ -101,30 +79,34 @@ func (s *CRD) CustomRender() ([]byte, error) {
10179

10280
crd := &apiextv1beta1.CustomResourceDefinition{}
10381
if s.IsOperatorGo {
82+
// This sets domain as empty string when we can't extract it from FullGroup.
83+
// In turn this defaults to extracting the domain from project root file
84+
// in controller-tools.
85+
fg := strings.SplitN(s.Resource.FullGroup, ".", 2)
86+
domain := s.Resource.FullGroup
87+
if len(fg) > 1 {
88+
domain = fg[1]
89+
}
90+
fs := afero.NewMemMapFs()
91+
g := &crdgenerator.Generator{
92+
RootPath: s.AbsProjectPath,
93+
Domain: domain,
94+
Repo: s.Repo,
95+
OutputDir: ".",
96+
SkipMapValidation: false,
97+
OutFs: fs,
98+
}
99+
if err := g.ValidateAndInitFields(); err != nil {
100+
return nil, err
101+
}
102+
if err := g.Do(); err != nil {
103+
return nil, err
104+
}
105+
104106
// controller-tools generates crd file names with no _crd.yaml suffix:
105107
// <group>_<version>_<kind>.yaml.
106108
path := strings.Replace(filepath.Base(i.Path), "_crd.yaml", ".yaml", 1)
107-
108-
// controller-tools' generators read and make crds for all apis in pkg/apis,
109-
// so generate crds in a cached, in-memory fs to extract the data we need.
110-
if !cache.fileExists(path) {
111-
g := &crdgenerator.Generator{
112-
RootPath: s.AbsProjectPath,
113-
Domain: strings.SplitN(s.Resource.FullGroup, ".", 2)[1],
114-
Repo: s.Repo,
115-
OutputDir: ".",
116-
SkipMapValidation: false,
117-
OutFs: cache,
118-
}
119-
if err := g.ValidateAndInitFields(); err != nil {
120-
return nil, err
121-
}
122-
if err := g.Do(); err != nil {
123-
return nil, err
124-
}
125-
}
126-
127-
b, err := afero.ReadFile(cache, path)
109+
b, err := afero.ReadFile(fs, path)
128110
if err != nil {
129111
if os.IsNotExist(err) {
130112
return nil, fmt.Errorf("no API exists for Group %s Version %s Kind %s",
@@ -166,11 +148,11 @@ func (s *CRD) CustomRender() ([]byte, error) {
166148
func newCRDForResource(r *Resource) *apiextv1beta1.CustomResourceDefinition {
167149
crd := &apiextv1beta1.CustomResourceDefinition{
168150
TypeMeta: metav1.TypeMeta{
169-
APIVersion: "apiextensions.k8s.io/v1beta1",
151+
APIVersion: apiextv1beta1.SchemeGroupVersion.String(),
170152
Kind: "CustomResourceDefinition",
171153
},
172154
ObjectMeta: metav1.ObjectMeta{
173-
Name: r.Resource + "." + r.FullGroup,
155+
Name: fmt.Sprintf("%s.%s", r.Resource, r.FullGroup),
174156
},
175157
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
176158
Group: r.FullGroup,

0 commit comments

Comments
 (0)