コード例 #1
0
ファイル: fake.go プロジェクト: neujie/kubernetes
func (f *fakeAPIFactory) ClientSet() (*internalclientset.Clientset, error) {
	// Swap out the HTTP client out of the client with the fake's version.
	fakeClient := f.tf.Client.(*fake.RESTClient)
	restClient, err := restclient.RESTClientFor(f.tf.ClientConfig)
	if err != nil {
		panic(err)
	}
	restClient.Client = fakeClient.Client
	return internalclientset.New(restClient), f.tf.Err
}
コード例 #2
0
ファイル: cmd_test.go プロジェクト: ncdc/kubernetes
func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.NegotiatedSerializer) {
	t := &testFactory{
		Validator: validation.NullSchema{},
	}

	f := &cmdutil.Factory{
		Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
			return testapi.Default.RESTMapper(), api.Scheme
		},
		UnstructuredObject: func() (meta.RESTMapper, runtime.ObjectTyper, error) {
			groupResources := testDynamicResources()
			mapper := discovery.NewRESTMapper(groupResources, meta.InterfacesForUnstructured)
			typer := discovery.NewUnstructuredObjectTyper(groupResources)

			return cmdutil.NewShortcutExpander(mapper, nil), typer, nil
		},
		ClientSet: func() (*internalclientset.Clientset, error) {
			// Swap out the HTTP client out of the client with the fake's version.
			fakeClient := t.Client.(*fake.RESTClient)
			restClient, err := restclient.RESTClientFor(t.ClientConfig)
			if err != nil {
				panic(err)
			}
			restClient.Client = fakeClient.Client
			return internalclientset.New(restClient), t.Err
		},
		RESTClient: func() (*restclient.RESTClient, error) {
			// Swap out the HTTP client out of the client with the fake's version.
			fakeClient := t.Client.(*fake.RESTClient)
			restClient, err := restclient.RESTClientFor(t.ClientConfig)
			if err != nil {
				panic(err)
			}
			restClient.Client = fakeClient.Client
			return restClient, t.Err
		},
		ClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) {
			return t.Client, t.Err
		},
		UnstructuredClientForMapping: func(*meta.RESTMapping) (resource.RESTClient, error) {
			return t.Client, t.Err
		},
		Decoder: func(bool) runtime.Decoder {
			return testapi.Default.Codec()
		},
		JSONEncoder: func() runtime.Encoder {
			return testapi.Default.Codec()
		},
		Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
			return t.Describer, t.Err
		},
		Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) {
			return t.Printer, t.Err
		},
		Validator: func(validate bool, cacheDir string) (validation.Schema, error) {
			return t.Validator, t.Err
		},
		DefaultNamespace: func() (string, bool, error) {
			return t.Namespace, false, t.Err
		},
		ClientConfig: func() (*restclient.Config, error) {
			return t.ClientConfig, t.Err
		},
		Generators: func(cmdName string) map[string]kubectl.Generator {
			return cmdutil.DefaultGenerators(cmdName)
		},
		LogsForObject: func(object, options runtime.Object) (*restclient.Request, error) {
			fakeClient := t.Client.(*fake.RESTClient)
			c := client.NewOrDie(t.ClientConfig)
			c.Client = fakeClient.Client

			switch t := object.(type) {
			case *api.Pod:
				opts, ok := options.(*api.PodLogOptions)
				if !ok {
					return nil, errors.New("provided options object is not a PodLogOptions")
				}
				return c.Pods(t.Namespace).GetLogs(t.Name, opts), nil
			default:
				fqKinds, _, err := api.Scheme.ObjectKinds(object)
				if err != nil {
					return nil, err
				}
				return nil, fmt.Errorf("cannot get the logs from %v", fqKinds[0])
			}
		},
	}
	rf := cmdutil.NewFactory(nil)
	f.MapBasedSelectorForObject = rf.MapBasedSelectorForObject
	f.PortsForObject = rf.PortsForObject
	f.ProtocolsForObject = rf.ProtocolsForObject
	f.LabelsForObject = rf.LabelsForObject
	f.CanBeExposed = rf.CanBeExposed
	f.PrintObjectSpecificMessage = rf.PrintObjectSpecificMessage
	return f, t, testapi.Default.Codec(), testapi.Default.NegotiatedSerializer()
}