-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to internalclientset - interesting changes
- Loading branch information
Showing
24 changed files
with
337 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,75 @@ | ||
package testclient | ||
|
||
import ( | ||
"k8s.io/kubernetes/pkg/apimachinery/registered" | ||
"io/ioutil" | ||
|
||
"k8s.io/kubernetes/pkg/api/meta" | ||
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" | ||
kclient "k8s.io/kubernetes/pkg/client/unversioned" | ||
"k8s.io/kubernetes/pkg/client/unversioned/testclient" | ||
ktestclient "k8s.io/kubernetes/pkg/client/unversioned/testclient" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
"k8s.io/kubernetes/pkg/util/yaml" | ||
|
||
osclient "github.com/openshift/origin/pkg/client" | ||
"github.com/openshift/origin/pkg/client" | ||
projectapi "github.com/openshift/origin/pkg/project/api" | ||
) | ||
|
||
// NewFixtureClients returns mocks of the OpenShift and Kubernetes clients | ||
func NewFixtureClients(o testclient.ObjectRetriever) (osclient.Interface, kclient.Interface) { | ||
oc := &Fake{} | ||
oc.AddReactor("*", "*", testclient.ObjectReaction(o, registered.RESTMapper())) | ||
|
||
kc := &testclient.Fake{} | ||
kc.AddReactor("*", "*", testclient.ObjectReaction(o, registered.RESTMapper())) | ||
// with data populated from provided path. | ||
func NewFixtureClients(decoder runtime.Decoder, namespace, path string, objects ...runtime.Object) (client.Interface, kclientset.Interface, kclient.Interface, error) { | ||
var objs []runtime.Object | ||
if len(path) > 0 { | ||
var err error | ||
objs, err = readObjectsFromPath(path, decoder) | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
} | ||
objs = append(objs, objects...) | ||
kobjs := []runtime.Object{} | ||
for _, o := range objs { | ||
if itemMeta, err := meta.Accessor(o); err == nil { | ||
// we can't set namespace for everything, some resources are non-namespaced | ||
if _, ok := o.(*projectapi.Project); ok { | ||
continue | ||
} | ||
if len(itemMeta.GetNamespace()) == 0 { | ||
itemMeta.SetNamespace(namespace) | ||
} | ||
// NewSimpleClientset accepts only runtime.Objects | ||
kobjs = append(kobjs, o) | ||
} | ||
} | ||
oc := NewSimpleFake(objs...) | ||
kc := fake.NewSimpleClientset(kobjs...) | ||
oldK := ktestclient.NewSimpleFake(kobjs...) | ||
return oc, kc, oldK, nil | ||
} | ||
|
||
return oc, kc | ||
func readObjectsFromPath(path string, decoder runtime.Decoder) ([]runtime.Object, error) { | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data, err = yaml.ToJSON(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
obj, err := runtime.Decode(decoder, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !meta.IsListType(obj) { | ||
return []runtime.Object{obj}, nil | ||
} | ||
list, err := meta.ExtractList(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
errs := runtime.DecodeList(list, decoder) | ||
if len(errs) > 0 { | ||
return nil, errs[0] | ||
} | ||
return list, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.