Skip to content

Commit

Permalink
Expose CRDInstallOptions via envtest.Environment
Browse files Browse the repository at this point in the history
This change provides better control for installing CRDs in `envtest`.

Fixes kubernetes-sigs#541
  • Loading branch information
rajathagasthya committed Jul 28, 2019
1 parent ee41a80 commit 8d857aa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/builder/builder_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ func addCRDToEnvironment(env *envtest.Environment, gvks ...schema.GroupVersionKi
},
},
}
env.CRDs = append(env.CRDs, crd)
env.CRDInstallOptions.CRDs = append(env.CRDInstallOptions.CRDs, crd)
}
}
20 changes: 10 additions & 10 deletions pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

// CRDInstallOptions are the options for installing CRDs
type CRDInstallOptions struct {
// Paths is the path to the directory containing CRDs
// Paths is a list of paths to the directories containing CRDs
Paths []string

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

// maxTime is the max time to wait
maxTime time.Duration
// MaxTime is the max time to wait
MaxTime time.Duration

// pollInterval is the interval to check
pollInterval time.Duration
// PollInterval is the interval to check
PollInterval time.Duration
}

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

// defaultCRDOptions sets the default values for CRDs
func defaultCRDOptions(o *CRDInstallOptions) {
if o.maxTime == 0 {
o.maxTime = defaultMaxWait
if o.MaxTime == 0 {
o.MaxTime = defaultMaxWait
}
if o.pollInterval == 0 {
o.pollInterval = defaultPollInterval
if o.PollInterval == 0 {
o.PollInterval = defaultPollInterval
}
}

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

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

// poller checks if all the resources have been found in discovery, and returns false if not
Expand Down
6 changes: 3 additions & 3 deletions pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var _ = Describe("Test", func() {
}},
},
},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -178,7 +178,7 @@ var _ = Describe("Test", func() {
}},
},
},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
)
Expect(err).To(HaveOccurred())

Expand Down Expand Up @@ -209,7 +209,7 @@ var _ = Describe("Test", func() {
Plural: "fake",
}},
}},
CRDInstallOptions{maxTime: 50 * time.Millisecond, pollInterval: 15 * time.Millisecond},
CRDInstallOptions{MaxTime: 50 * time.Millisecond, PollInterval: 15 * time.Millisecond},
)
Expect(err).To(HaveOccurred())

Expand Down
24 changes: 19 additions & 5 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,23 @@ type Environment struct {
// loading.
Config *rest.Config

// CRDs is a list of CRDs to install
// CRDs is a list of CRDs to install.
// If both this field and CRDs field in CRDInstallOptions are specified, this
// value this ignored.
//
// Deprecated: Use CRDInstallOptions
CRDs []*apiextensionsv1beta1.CustomResourceDefinition

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

// CRDInstallOptions are the options for installing CRDs.
CRDInstallOptions CRDInstallOptions

// UseExisting indicates that this environments should use an
// existing kubeconfig, instead of trying to stand up a new control plane.
// This is useful in cases that need aggregated API servers and the like.
Expand Down Expand Up @@ -203,10 +214,13 @@ func (te *Environment) Start() (*rest.Config, error) {
}

log.V(1).Info("installing CRDs")
_, err := InstallCRDs(te.Config, CRDInstallOptions{
Paths: te.CRDDirectoryPaths,
CRDs: te.CRDs,
})
if len(te.CRDInstallOptions.CRDs) == 0 {
te.CRDInstallOptions.CRDs = te.CRDs
}
if len(te.CRDInstallOptions.Paths) == 0 {
te.CRDInstallOptions.Paths = te.CRDDirectoryPaths
}
_, err := InstallCRDs(te.Config, te.CRDInstallOptions)
return te.Config, err
}

Expand Down

0 comments on commit 8d857aa

Please sign in to comment.