本文整理匯總了Golang中k8s/io/kubernetes/pkg/runtime.Encode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Encode函數的具體用法?Golang Encode怎麽用?Golang Encode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Encode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: roundTrip
func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
//t.Logf("codec: %#v", codec)
printer := spew.ConfigState{DisableMethods: true}
name := reflect.TypeOf(item).Elem().Name()
data, err := runtime.Encode(codec, item)
if err != nil {
t.Errorf("%v: %v (%s)", name, err, printer.Sprintf("%#v", item))
return
}
obj2, err := runtime.Decode(codec, data)
if err != nil {
t.Errorf("0: %v: %v\nCodec: %v\nData: %s\nSource: %#v", name, err, codec, string(data), printer.Sprintf("%#v", item))
return
}
if !api.Semantic.DeepEqual(item, obj2) {
t.Errorf("\n1: %v: diff: %v\nCodec: %v\nSource:\n\n%#v\n\nEncoded:\n\n%s\n\nFinal:\n\n%#v", name, diff.ObjectGoPrintDiff(item, obj2), codec, printer.Sprintf("%#v", item), string(data), printer.Sprintf("%#v", obj2))
return
}
obj3 := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
if err := runtime.DecodeInto(codec, data, obj3); err != nil {
t.Errorf("2: %v: %v", name, err)
return
}
if !api.Semantic.DeepEqual(item, obj3) {
t.Errorf("3: %v: diff: %v\nCodec: %v", name, diff.ObjectDiff(item, obj3), codec)
return
}
}
示例2: TestVersionedEncoding
func TestVersionedEncoding(t *testing.T) {
s, _ := GetTestScheme()
cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
info, _ := runtime.SerializerInfoForMediaType(cf.SupportedMediaTypes(), runtime.ContentTypeJSON)
encoder := info.Serializer
codec := cf.CodecForVersions(encoder, nil, schema.GroupVersion{Version: "v2"}, nil)
out, err := runtime.Encode(codec, &TestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{"myVersionKey":"v2","myKindKey":"TestType1"}`+"\n" {
t.Fatal(string(out))
}
codec = cf.CodecForVersions(encoder, nil, schema.GroupVersion{Version: "v3"}, nil)
_, err = runtime.Encode(codec, &TestType1{})
if err == nil {
t.Fatal(err)
}
// unversioned encode with no versions is written directly to wire
codec = cf.CodecForVersions(encoder, nil, runtime.InternalGroupVersioner, nil)
out, err = runtime.Encode(codec, &TestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{}`+"\n" {
t.Fatal(string(out))
}
}
示例3: setFinalizersRuntimeObject
func setFinalizersRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object) (string, []byte) {
originalAccessor, err := meta.Accessor(originalObj)
if err != nil {
t.Fatal(err)
}
originalFinalizers := []string{"a/a"}
originalAccessor.SetFinalizers(originalFinalizers)
original, err := runtime.Encode(testapi.Default.Codec(), originalObj)
if err != nil {
t.Fatal(err)
}
currentAccessor, err := meta.Accessor(currentObj)
if err != nil {
t.Fatal(err)
}
currentFinalizers := []string{"b/b"}
currentAccessor.SetFinalizers(currentFinalizers)
currentAnnotations := currentAccessor.GetAnnotations()
if currentAnnotations == nil {
currentAnnotations = make(map[string]string)
}
currentAnnotations[annotations.LastAppliedConfigAnnotation] = string(original)
currentAccessor.SetAnnotations(currentAnnotations)
current, err := runtime.Encode(testapi.Default.Codec(), currentObj)
if err != nil {
t.Fatal(err)
}
return currentAccessor.GetName(), current
}
示例4: TestProcessTemplateParameters
func TestProcessTemplateParameters(t *testing.T) {
var template, expectedTemplate api.Template
jsonData, _ := ioutil.ReadFile("../../test/templates/testdata/guestbook.json")
if err := runtime.DecodeInto(kapi.Codecs.UniversalDecoder(), jsonData, &template); err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedData, _ := ioutil.ReadFile("../../test/templates/testdata/guestbook_list.json")
if err := runtime.DecodeInto(kapi.Codecs.UniversalDecoder(), expectedData, &expectedTemplate); err != nil {
t.Fatalf("unexpected error: %v", err)
}
generators := map[string]generator.Generator{
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(1337))),
}
processor := NewProcessor(generators)
// Define custom parameter for the transformation:
AddParameter(&template, makeParameter("CUSTOM_PARAM1", "1", "", false))
// Transform the template config into the result config
errs := processor.Process(&template)
if len(errs) > 0 {
t.Fatalf("unexpected error: %v", errs)
}
result, err := runtime.Encode(kapi.Codecs.LegacyCodec(v1.SchemeGroupVersion), &template)
if err != nil {
t.Fatalf("unexpected error during encoding Config: %#v", err)
}
exp, _ := runtime.Encode(kapi.Codecs.LegacyCodec(v1.SchemeGroupVersion), &expectedTemplate)
if string(result) != string(exp) {
t.Errorf("unexpected output: %s", diff.StringDiff(string(exp), string(result)))
}
}
示例5: TestCreate
func TestCreate(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
server := etcdtesting.NewEtcdTestClientServer(t)
defer server.Terminate(t)
helper := newEtcdHelper(server.Client, testapi.Default.Codec(), etcdtest.PathPrefix())
returnedObj := &api.Pod{}
err := helper.Create(context.TODO(), "/some/key", obj, returnedObj, 5)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
_, err = runtime.Encode(testapi.Default.Codec(), obj)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
err = helper.Get(context.TODO(), "/some/key", returnedObj, false)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
_, err = runtime.Encode(testapi.Default.Codec(), returnedObj)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
if obj.Name != returnedObj.Name {
t.Errorf("Wanted %v, got %v", obj.Name, returnedObj.Name)
}
}
示例6: convert_runtime_Object_To_runtime_RawExtension
// Convert_runtime_Object_To_runtime_RawExtension is conversion function that assumes that the runtime.Object you've embedded is in
// the same GroupVersion that your containing type is in. This is signficantly better than simply breaking.
// Given an ordered list of preferred external versions for a given encode or conversion call, the behavior of this function could be
// made generic, predictable, and controllable.
func convert_runtime_Object_To_runtime_RawExtension(in runtime.Object, out *runtime.RawExtension, s conversion.Scope) error {
if in == nil {
return nil
}
externalObject, err := internal.Scheme.ConvertToVersion(in, s.Meta().DestVersion)
if runtime.IsNotRegisteredError(err) {
switch cast := in.(type) {
case *runtime.Unknown:
out.Raw = cast.Raw
return nil
case *runtime.Unstructured:
bytes, err := runtime.Encode(runtime.UnstructuredJSONScheme, externalObject)
if err != nil {
return err
}
out.Raw = bytes
return nil
}
}
if err != nil {
return err
}
bytes, err := runtime.Encode(codec, externalObject)
if err != nil {
return err
}
out.Raw = bytes
out.Object = externalObject
return nil
}
示例7: TestVersionedEncoding
func TestVersionedEncoding(t *testing.T) {
s, _ := GetTestScheme()
cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
encoder, _ := cf.SerializerForFileExtension("json")
codec := cf.CodecForVersions(encoder, nil, unversioned.GroupVersion{Version: "v2"}, nil)
out, err := runtime.Encode(codec, &TestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{"myVersionKey":"v2","myKindKey":"TestType1"}`+"\n" {
t.Fatal(string(out))
}
codec = cf.CodecForVersions(encoder, nil, unversioned.GroupVersion{Version: "v3"}, nil)
_, err = runtime.Encode(codec, &TestType1{})
if err == nil {
t.Fatal(err)
}
// unversioned encode with no versions is written directly to wire
codec = cf.CodecForVersions(encoder, nil, runtime.InternalGroupVersioner, nil)
out, err = runtime.Encode(codec, &TestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{}`+"\n" {
t.Fatal(string(out))
}
}
示例8: TestVersionedEncoding
func TestVersionedEncoding(t *testing.T) {
s, codec := GetTestScheme()
out, err := runtime.Encode(codec, &TestType1{}, unversioned.GroupVersion{Version: "v2"})
if err != nil {
t.Fatal(err)
}
if string(out) != `{"myVersionKey":"v2","myKindKey":"TestType1"}`+"\n" {
t.Fatal(string(out))
}
_, err = runtime.Encode(codec, &TestType1{}, unversioned.GroupVersion{Version: "v3"})
if err == nil {
t.Fatal(err)
}
cf := newCodecFactory(s, testMetaFactory{})
encoder, _ := cf.SerializerForFileExtension("json")
// codec that is unversioned uses the target version
unversionedCodec := cf.CodecForVersions(encoder, nil, nil)
_, err = runtime.Encode(unversionedCodec, &TestType1{}, unversioned.GroupVersion{Version: "v3"})
if err == nil || !runtime.IsNotRegisteredError(err) {
t.Fatal(err)
}
// unversioned encode with no versions is written directly to wire
out, err = runtime.Encode(unversionedCodec, &TestType1{})
if err != nil {
t.Fatal(err)
}
if string(out) != `{"myVersionKey":"__internal","myKindKey":"TestType1"}`+"\n" {
t.Fatal(string(out))
}
}
示例9: CalculatePatches
// CalculatePatches calls the mutation function on each provided info object, and generates a strategic merge patch for
// the changes in the object. Encoder must be able to encode the info into the appropriate destination type. If mutateFn
// returns false, the object is not included in the final list of patches.
func CalculatePatches(infos []*resource.Info, encoder runtime.Encoder, mutateFn func(*resource.Info) (bool, error)) []*Patch {
var patches []*Patch
for _, info := range infos {
patch := &Patch{Info: info}
patch.Before, patch.Err = runtime.Encode(encoder, info.Object)
ok, err := mutateFn(info)
if !ok {
continue
}
if err != nil {
patch.Err = err
}
patches = append(patches, patch)
if patch.Err != nil {
continue
}
patch.After, patch.Err = runtime.Encode(encoder, info.Object)
if patch.Err != nil {
continue
}
// TODO: should be via New
versioned, err := info.Mapping.ConvertToVersion(info.Object, info.Mapping.GroupVersionKind.GroupVersion())
if err != nil {
patch.Err = err
continue
}
patch.Patch, patch.Err = strategicpatch.CreateTwoWayMergePatch(patch.Before, patch.After, versioned)
}
return patches
}
示例10: annotateRuntimeObject
func annotateRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object, kind string) (string, []byte) {
originalAccessor, err := meta.Accessor(originalObj)
if err != nil {
t.Fatal(err)
}
originalLabels := originalAccessor.GetLabels()
originalLabels["DELETE_ME"] = "DELETE_ME"
originalAccessor.SetLabels(originalLabels)
original, err := runtime.Encode(testapi.Default.Codec(), originalObj)
if err != nil {
t.Fatal(err)
}
currentAccessor, err := meta.Accessor(currentObj)
if err != nil {
t.Fatal(err)
}
currentAnnotations := currentAccessor.GetAnnotations()
if currentAnnotations == nil {
currentAnnotations = make(map[string]string)
}
currentAnnotations[annotations.LastAppliedConfigAnnotation] = string(original)
currentAccessor.SetAnnotations(currentAnnotations)
current, err := runtime.Encode(testapi.Default.Codec(), currentObj)
if err != nil {
t.Fatal(err)
}
return currentAccessor.GetName(), current
}
示例11: TestUnversionedTypes
func TestUnversionedTypes(t *testing.T) {
internalGV := schema.GroupVersion{Group: "test.group", Version: runtime.APIVersionInternal}
externalGV := schema.GroupVersion{Group: "test.group", Version: "testExternal"}
otherGV := schema.GroupVersion{Group: "group", Version: "other"}
scheme := runtime.NewScheme()
scheme.AddUnversionedTypes(externalGV, &InternalSimple{})
scheme.AddKnownTypeWithName(internalGV.WithKind("Simple"), &InternalSimple{})
scheme.AddKnownTypeWithName(externalGV.WithKind("Simple"), &ExternalSimple{})
scheme.AddKnownTypeWithName(otherGV.WithKind("Simple"), &ExternalSimple{})
codec := serializer.NewCodecFactory(scheme).LegacyCodec(externalGV)
if unv, ok := scheme.IsUnversioned(&InternalSimple{}); !unv || !ok {
t.Fatalf("type not unversioned and in scheme: %t %t", unv, ok)
}
kinds, _, err := scheme.ObjectKinds(&InternalSimple{})
if err != nil {
t.Fatal(err)
}
kind := kinds[0]
if kind != externalGV.WithKind("InternalSimple") {
t.Fatalf("unexpected: %#v", kind)
}
test := &InternalSimple{
TestString: "I'm the same",
}
obj := runtime.Object(test)
data, err := runtime.Encode(codec, obj)
if err != nil {
t.Fatal(err)
}
obj2, gvk, err := codec.Decode(data, nil, nil)
if err != nil {
t.Fatal(err)
}
if _, ok := obj2.(*InternalSimple); !ok {
t.Fatalf("Got wrong type")
}
if !reflect.DeepEqual(obj2, test) {
t.Errorf("Expected:\n %#v,\n Got:\n %#v", test, obj2)
}
// object is serialized as an unversioned object (in the group and version it was defined in)
if !reflect.DeepEqual(gvk, &schema.GroupVersionKind{Group: "test.group", Version: "testExternal", Kind: "InternalSimple"}) {
t.Errorf("unexpected gvk returned by decode: %#v", gvk)
}
// when serialized to a different group, the object is kept in its preferred name
codec = serializer.NewCodecFactory(scheme).LegacyCodec(otherGV)
data, err = runtime.Encode(codec, obj)
if err != nil {
t.Fatal(err)
}
if string(data) != `{"apiVersion":"test.group/testExternal","kind":"InternalSimple","testString":"I'm the same"}`+"\n" {
t.Errorf("unexpected data: %s", data)
}
}
示例12: TestDecodeSinglePod
func TestDecodeSinglePod(t *testing.T) {
grace := int64(30)
pod := &api.Pod{
TypeMeta: unversioned.TypeMeta{
APIVersion: "",
},
ObjectMeta: api.ObjectMeta{
Name: "test",
UID: "12345",
Namespace: "mynamespace",
},
Spec: api.PodSpec{
RestartPolicy: api.RestartPolicyAlways,
DNSPolicy: api.DNSClusterFirst,
TerminationGracePeriodSeconds: &grace,
Containers: []api.Container{{
Name: "image",
Image: "test/image",
ImagePullPolicy: "IfNotPresent",
TerminationMessagePath: "/dev/termination-log",
SecurityContext: securitycontext.ValidSecurityContextWithContainerDefaults(),
}},
SecurityContext: &api.PodSecurityContext{},
},
}
json, err := runtime.Encode(testapi.Default.Codec(), pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(json))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(json))
}
if !reflect.DeepEqual(pod, podOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
}
for _, gv := range registered.EnabledVersionsForGroup(api.GroupName) {
s, _ := api.Codecs.SerializerForFileExtension("yaml")
encoder := api.Codecs.EncoderForVersion(s, gv)
yaml, err := runtime.Encode(encoder, pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
}
if !reflect.DeepEqual(pod, podOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
}
}
}
示例13: TestDoRequestNewWayFile
func TestDoRequestNewWayFile(t *testing.T) {
reqObj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
reqBodyExpected, err := runtime.Encode(testapi.Default.Codec(), reqObj)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
file, err := ioutil.TempFile("", "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
defer file.Close()
_, err = file.Write(reqBodyExpected)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expectedObj := &api.Service{Spec: api.ServiceSpec{Ports: []api.ServicePort{{
Protocol: "TCP",
Port: 12345,
TargetPort: intstr.FromInt(12345),
}}}}
expectedBody, _ := runtime.Encode(testapi.Default.Codec(), expectedObj)
fakeHandler := utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: string(expectedBody),
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
// TODO: Uncomment when fix #19254
// defer testServer.Close()
c := testRESTClient(t, testServer)
wasCreated := true
obj, err := c.Verb("POST").
Prefix("foo/bar", "baz").
Timeout(time.Second).
Body(file.Name()).
Do().WasCreated(&wasCreated).Get()
if err != nil {
t.Errorf("Unexpected error: %v %#v", err, err)
return
}
if obj == nil {
t.Error("nil obj")
} else if !api.Semantic.DeepDerivative(expectedObj, obj) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
if wasCreated {
t.Errorf("expected object was not created")
}
tmpStr := string(reqBodyExpected)
requestURL := testapi.Default.ResourcePathWithPrefix("foo/bar/baz", "", "", "")
requestURL += "?timeout=1s"
fakeHandler.ValidateRequest(t, requestURL, "POST", &tmpStr)
}
示例14: Update
func (s scaleUpdater) Update(annotator *ScaleAnnotater, obj runtime.Object, scale *kextapi.Scale) error {
var (
err error
patchBytes, originalObj, newObj []byte
)
originalObj, err = runtime.Encode(s.encoder, obj)
if err != nil {
return err
}
switch typedObj := obj.(type) {
case *deployapi.DeploymentConfig:
if typedObj.Annotations == nil {
typedObj.Annotations = make(map[string]string)
}
annotator.ChangeAnnotations(typedObj.Spec.Replicas, typedObj.Annotations)
typedObj.Spec.Replicas = scale.Spec.Replicas
newObj, err = runtime.Encode(s.encoder, typedObj)
if err != nil {
return err
}
patchBytes, err = strategicpatch.CreateTwoWayMergePatch(originalObj, newObj, &deployapiv1.DeploymentConfig{})
if err != nil {
return err
}
_, err = s.dcGetter.DeploymentConfigs(s.namespace).Patch(typedObj.Name, kapi.StrategicMergePatchType, patchBytes)
case *kapi.ReplicationController:
if typedObj.Annotations == nil {
typedObj.Annotations = make(map[string]string)
}
annotator.ChangeAnnotations(typedObj.Spec.Replicas, typedObj.Annotations)
typedObj.Spec.Replicas = scale.Spec.Replicas
newObj, err = runtime.Encode(s.encoder, typedObj)
if err != nil {
return err
}
patchBytes, err = strategicpatch.CreateTwoWayMergePatch(originalObj, newObj, &kapiv1.ReplicationController{})
if err != nil {
return err
}
_, err = s.rcGetter.ReplicationControllers(s.namespace).Patch(typedObj.Name, kapi.StrategicMergePatchType, patchBytes)
}
return err
}
示例15: updateResource
func updateResource(c *Client, target *resource.Info, currentObj runtime.Object, recreate bool) error {
encoder := api.Codecs.LegacyCodec(registered.EnabledVersions()...)
original, err := runtime.Encode(encoder, currentObj)
if err != nil {
return err
}
modified, err := runtime.Encode(encoder, target.Object)
if err != nil {
return err
}
if api.Semantic.DeepEqual(original, modified) {
return ErrAlreadyExists{target.Name}
}
patch, err := strategicpatch.CreateTwoWayMergePatch(original, modified, currentObj)
if err != nil {
return err
}
// send patch to server
helper := resource.NewHelper(target.Client, target.Mapping)
_, err = helper.Patch(target.Namespace, target.Name, api.StrategicMergePatchType, patch)
if err != nil {
return err
}
if recreate {
kind := target.Mapping.GroupVersionKind.Kind
client, _ := c.ClientSet()
switch kind {
case "ReplicationController":
rc := currentObj.(*v1.ReplicationController)
err = recreatePods(client, target.Namespace, rc.Spec.Selector)
case "DaemonSet":
daemonSet := currentObj.(*v1beta1.DaemonSet)
err = recreatePods(client, target.Namespace, daemonSet.Spec.Selector.MatchLabels)
case "StatefulSet":
petSet := currentObj.(*apps.StatefulSet)
err = recreatePods(client, target.Namespace, petSet.Spec.Selector.MatchLabels)
case "ReplicaSet":
replicaSet := currentObj.(*v1beta1.ReplicaSet)
err = recreatePods(client, target.Namespace, replicaSet.Spec.Selector.MatchLabels)
}
}
return err
}