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`.

Addresses kubernetes-sigs#541
  • Loading branch information
rajathagasthya committed Aug 16, 2019
1 parent d7467fc commit fd5c30a
Show file tree
Hide file tree
Showing 5 changed files with 66 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
41 changes: 41 additions & 0 deletions pkg/envtest/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package envtest

import apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"

// mergePaths merges two string slices containing paths.
// This function makes no guarantees about order of the merged slice.
func mergePaths(s1, s2 []string) []string {
m := make(map[string]struct{})
for _, s := range s1 {
m[s] = struct{}{}
}
for _, s := range s2 {
m[s] = struct{}{}
}
merged := make([]string, len(m))
i := 0
for key, _ := range m {
merged[i] = key
i++
}
return merged
}

// mergeCRDs merges two CRD slices using their names.
// This function makes no guarantees about order of the merged slice.
func mergeCRDs(s1, s2 []*apiextensionsv1beta1.CustomResourceDefinition) []*apiextensionsv1beta1.CustomResourceDefinition {
m := make(map[string]*apiextensionsv1beta1.CustomResourceDefinition)
for _, crd := range s1 {
m[crd.Name] = crd
}
for _, crd := range s2 {
m[crd.Name] = crd
}
merged := make([]*apiextensionsv1beta1.CustomResourceDefinition, len(m))
i := 0
for _, crd := range m {
merged[i] = crd
i++
}
return merged
}
16 changes: 11 additions & 5 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ type Environment struct {
// loading.
Config *rest.Config

// CRDs is a list of CRDs to install
// CRDInstallOptions are the options for installing CRDs.
CRDInstallOptions CRDInstallOptions

// CRDs is a list of CRDs to install.
// If both this field and CRDs field in CRDInstallOptions are specified, the
// values are merged.
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, the
// values are merged.
CRDDirectoryPaths []string

// UseExisting indicates that this environments should use an
Expand Down Expand Up @@ -203,10 +210,9 @@ func (te *Environment) Start() (*rest.Config, error) {
}

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

Expand Down

0 comments on commit fd5c30a

Please sign in to comment.