Skip to content

Commit f18db79

Browse files
Expose CRDInstallOptions via envtest.Environment
This change provides better control for installing CRDs in `envtest`. Closes #541
1 parent ee41a80 commit f18db79

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

pkg/builder/builder_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ func addCRDToEnvironment(env *envtest.Environment, gvks ...schema.GroupVersionKi
103103
},
104104
},
105105
}
106-
env.CRDs = append(env.CRDs, crd)
106+
env.CRDInstallOptions.CRDs = append(env.CRDInstallOptions.CRDs, crd)
107107
}
108108
}

pkg/envtest/crd.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
// CRDInstallOptions are the options for installing CRDs
3939
type CRDInstallOptions struct {
40-
// Paths is the path to the directory containing CRDs
40+
// Paths is a list of paths to the directories containing CRDs
4141
Paths []string
4242

4343
// CRDs is a list of CRDs to install
@@ -46,11 +46,11 @@ type CRDInstallOptions struct {
4646
// ErrorIfPathMissing will cause an error if a Path does not exist
4747
ErrorIfPathMissing bool
4848

49-
// maxTime is the max time to wait
50-
maxTime time.Duration
49+
// MaxTime is the max time to wait
50+
MaxTime time.Duration
5151

52-
// pollInterval is the interval to check
53-
pollInterval time.Duration
52+
// PollInterval is the interval to check
53+
PollInterval time.Duration
5454
}
5555

5656
const defaultPollInterval = 100 * time.Millisecond
@@ -97,11 +97,11 @@ func readCRDFiles(options *CRDInstallOptions) error {
9797

9898
// defaultCRDOptions sets the default values for CRDs
9999
func defaultCRDOptions(o *CRDInstallOptions) {
100-
if o.maxTime == 0 {
101-
o.maxTime = defaultMaxWait
100+
if o.MaxTime == 0 {
101+
o.MaxTime = defaultMaxWait
102102
}
103-
if o.pollInterval == 0 {
104-
o.pollInterval = defaultPollInterval
103+
if o.PollInterval == 0 {
104+
o.PollInterval = defaultPollInterval
105105
}
106106
}
107107

@@ -132,7 +132,7 @@ func WaitForCRDs(config *rest.Config, crds []*apiextensionsv1beta1.CustomResourc
132132

133133
// Poll until all resources are found in discovery
134134
p := &poller{config: config, waitingFor: waitingFor}
135-
return wait.PollImmediate(options.pollInterval, options.maxTime, p.poll)
135+
return wait.PollImmediate(options.PollInterval, options.MaxTime, p.poll)
136136
}
137137

138138
// poller checks if all the resources have been found in discovery, and returns false if not

pkg/envtest/envtest_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ var _ = Describe("Test", func() {
145145
}},
146146
},
147147
},
148-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
148+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
149149
)
150150
Expect(err).NotTo(HaveOccurred())
151151

@@ -178,7 +178,7 @@ var _ = Describe("Test", func() {
178178
}},
179179
},
180180
},
181-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
181+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
182182
)
183183
Expect(err).To(HaveOccurred())
184184

@@ -209,7 +209,7 @@ var _ = Describe("Test", func() {
209209
Plural: "fake",
210210
}},
211211
}},
212-
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
212+
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
213213
)
214214
Expect(err).To(HaveOccurred())
215215

pkg/envtest/server.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,23 @@ type Environment struct {
8181
// loading.
8282
Config *rest.Config
8383

84-
// CRDs is a list of CRDs to install
84+
// CRDs is a list of CRDs to install.
85+
// If both this field and CRDs field in CRDInstallOptions are specified, this
86+
// value this ignored.
87+
//
88+
// Deprecated: Use CRDInstallOptions
8589
CRDs []*apiextensionsv1beta1.CustomResourceDefinition
8690

8791
// CRDDirectoryPaths is a list of paths containing CRD yaml or json configs.
92+
// If both this field and Paths field in CRDInstallOptions are specified, this
93+
// value this ignored.
94+
//
95+
// Deprecated: Use CRDInstallOptions
8896
CRDDirectoryPaths []string
8997

98+
// CRDInstallOptions are the options for installing CRDs.
99+
CRDInstallOptions CRDInstallOptions
100+
90101
// UseExisting indicates that this environments should use an
91102
// existing kubeconfig, instead of trying to stand up a new control plane.
92103
// This is useful in cases that need aggregated API servers and the like.
@@ -203,10 +214,13 @@ func (te *Environment) Start() (*rest.Config, error) {
203214
}
204215

205216
log.V(1).Info("installing CRDs")
206-
_, err := InstallCRDs(te.Config, CRDInstallOptions{
207-
Paths: te.CRDDirectoryPaths,
208-
CRDs: te.CRDs,
209-
})
217+
if len(te.CRDInstallOptions.CRDs) == 0 {
218+
te.CRDInstallOptions.CRDs = te.CRDs
219+
}
220+
if len(te.CRDInstallOptions.Paths) == 0 {
221+
te.CRDInstallOptions.Paths = te.CRDDirectoryPaths
222+
}
223+
_, err := InstallCRDs(te.Config, te.CRDInstallOptions)
210224
return te.Config, err
211225
}
212226

0 commit comments

Comments
 (0)