Skip to content

Commit

Permalink
Merge pull request kubernetes#1181 from yuanchen8911/namespace
Browse files Browse the repository at this point in the history
clusterloader: enable the use of pre-created namespaces
  • Loading branch information
k8s-ci-robot committed Apr 21, 2020
2 parents b07cbe6 + 7bff496 commit 0039c10
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions clusterloader2/api/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ func (ns *NamespaceConfig) SetDefaults() {
if ns.DeleteAutomanagedNamespaces == nil {
ns.DeleteAutomanagedNamespaces = &defaultDeleteAutoNS
}

defaultEnableExistingNS := false
if ns.EnableExistingNamespaces == nil {
ns.EnableExistingNamespaces = &defaultEnableExistingNS
}
}
6 changes: 4 additions & 2 deletions clusterloader2/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ type NamespaceConfig struct {
// NamePrefix is the name prefix of automanaged namespaces.
// It's optional, if set CL will use it, otherwise generate one with random string.
Prefix string `json: prefix,omitempty`
// DeleteStaleNamespaces specifies whether or not delete stale namepaces
// DeleteStaleNamespaces specifies whether or not delete stale namespaces.
DeleteStaleNamespaces *bool `json: deleteStaleNamespaces,omitempty`
// DeleteAutomanangedNamespaces specifies whether or not delete namepaces after a test
// DeleteAutomanangedNamespaces specifies whether or not delete namespaces after a test.
DeleteAutomanagedNamespaces *bool `json: deleteAutomanagedNamespaces,omitempty`
// EnableExistingNamespaces enables to use pre-created namespaces in a test.
EnableExistingNamespaces *bool `json: enableExistingNamespaces,omitempty`
}

// NamespaceRange specifies the range of namespaces [Min, Max].
Expand Down
17 changes: 15 additions & 2 deletions clusterloader2/pkg/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,23 @@ func (f *Framework) CreateAutomanagedNamespaces(namespaceCount int) error {
if f.automanagedNamespaceCount != 0 {
return fmt.Errorf("automanaged namespaces already created")
}

// get all pre-created namespaces and store in a hash set
namespacesList, err := client.ListNamespaces(f.clientSets.GetClient())
if err != nil {
return err
}
existingNamespaceSet := make(map[string]bool)
for _, ns := range namespacesList {
existingNamespaceSet[ns.Name] = true
}

for i := 1; i <= namespaceCount; i++ {
name := fmt.Sprintf("%v-%d", f.automanagedNamespacePrefix, i)
if err := client.CreateNamespace(f.clientSets.GetClient(), name); err != nil {
return err
if _, created := existingNamespaceSet[name]; !created {
if err := client.CreateNamespace(f.clientSets.GetClient(), name); err != nil {
return err
}
}
f.automanagedNamespaceCount++
}
Expand Down
2 changes: 1 addition & 1 deletion clusterloader2/pkg/test/simple_test_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ste *simpleTestExecutor) ExecuteTestSteps(ctx Context, conf *api.Config) *
if err != nil {
return errors.NewErrorList(fmt.Errorf("automanaged namespaces listing failed: %v", err))
}
if len(automanagedNamespacesList) > 0 {
if len(automanagedNamespacesList) > 0 && *conf.Namespace.EnableExistingNamespaces == false {
return errors.NewErrorList(fmt.Errorf("pre-existing automanaged namespaces found"))
}
var deleteStaleNS = *conf.Namespace.DeleteStaleNamespaces
Expand Down

0 comments on commit 0039c10

Please sign in to comment.