use universal decoder and add a check on default dns Policy of static pod for test

This commit is contained in:
Paco Xu 2023-09-10 21:41:01 +08:00
parent 2d86c333f5
commit 678b958567
4 changed files with 12 additions and 18 deletions

View File

@ -261,6 +261,9 @@ func TestCreateLocalEtcdStaticPodManifestFileWithPatches(t *testing.T) {
t.Errorf("Error executing ReadStaticPodFromDisk: %v", err) t.Errorf("Error executing ReadStaticPodFromDisk: %v", err)
return return
} }
if pod.Spec.DNSPolicy != "" {
t.Errorf("DNSPolicy should be empty but: %v", pod.Spec.DNSPolicy)
}
if _, ok := pod.ObjectMeta.Annotations["patched"]; !ok { if _, ok := pod.ObjectMeta.Annotations["patched"]; !ok {
t.Errorf("Patches were not applied to %s", kubeadmconstants.Etcd) t.Errorf("Patches were not applied to %s", kubeadmconstants.Etcd)

View File

@ -55,22 +55,13 @@ func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs s
} }
// UnmarshalFromYaml unmarshals yaml into an object. // UnmarshalFromYaml unmarshals yaml into an object.
func UnmarshalFromYaml(buffer []byte, gv schema.GroupVersion) (runtime.Object, error) { func UnmarshalFromYaml(buffer []byte) (runtime.Object, error) {
return UnmarshalFromYamlForCodecs(buffer, gv, clientsetscheme.Codecs) return UnmarshalFromYamlForCodecs(buffer, clientsetscheme.Codecs)
} }
// UnmarshalFromYamlForCodecs unmarshals yaml into an object using the specified codec // UnmarshalFromYamlForCodecs unmarshals yaml into an object using the universal deserializer
// TODO: Is specifying the gv really needed here? func UnmarshalFromYamlForCodecs(buffer []byte, codecs serializer.CodecFactory) (runtime.Object, error) {
// TODO: Can we support json out of the box easily here? obj, _, err := codecs.UniversalDeserializer().Decode(buffer, nil, nil)
func UnmarshalFromYamlForCodecs(buffer []byte, gv schema.GroupVersion, codecs serializer.CodecFactory) (runtime.Object, error) {
const mediaType = runtime.ContentTypeYAML
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType)
if !ok {
return nil, errors.Errorf("unsupported media type %q", mediaType)
}
decoder := codecs.DecoderToVersion(info.Serializer, gv)
obj, err := runtime.Decode(decoder, buffer)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to decode %s into runtime.Object", buffer) return nil, errors.Wrapf(err, "failed to decode %s into runtime.Object", buffer)
} }

View File

@ -84,7 +84,7 @@ func TestMarshalUnmarshalYaml(t *testing.T) {
t.Logf("\n%s", bytes) t.Logf("\n%s", bytes)
obj2, err := UnmarshalFromYaml(bytes, corev1.SchemeGroupVersion) obj2, err := UnmarshalFromYaml(bytes)
if err != nil { if err != nil {
t.Fatalf("unexpected error marshalling: %v", err) t.Fatalf("unexpected error marshalling: %v", err)
} }
@ -144,7 +144,7 @@ func TestMarshalUnmarshalToYamlForCodecs(t *testing.T) {
} }
t.Logf("\n%s", bytes) t.Logf("\n%s", bytes)
obj, err := UnmarshalFromYamlForCodecs(bytes, kubeadmapiv1.SchemeGroupVersion, codecs) obj, err := UnmarshalFromYamlForCodecs(bytes, codecs)
if err != nil { if err != nil {
t.Fatalf("unexpected error unmarshalling InitConfiguration: %v", err) t.Fatalf("unexpected error unmarshalling InitConfiguration: %v", err)
} }

View File

@ -190,7 +190,7 @@ func PatchStaticPod(pod *v1.Pod, patchesDir string, output io.Writer) (*v1.Pod,
return pod, err return pod, err
} }
obj, err := kubeadmutil.UnmarshalFromYaml(patchTarget.Data, v1.SchemeGroupVersion) obj, err := kubeadmutil.UnmarshalFromYaml(patchTarget.Data)
if err != nil { if err != nil {
return pod, errors.Wrap(err, "failed to unmarshal patched manifest from YAML") return pod, errors.Wrap(err, "failed to unmarshal patched manifest from YAML")
} }
@ -233,7 +233,7 @@ func ReadStaticPodFromDisk(manifestPath string) (*v1.Pod, error) {
return &v1.Pod{}, errors.Wrapf(err, "failed to read manifest for %q", manifestPath) return &v1.Pod{}, errors.Wrapf(err, "failed to read manifest for %q", manifestPath)
} }
obj, err := kubeadmutil.UnmarshalFromYaml(buf, v1.SchemeGroupVersion) obj, err := kubeadmutil.UnmarshalFromYaml(buf)
if err != nil { if err != nil {
return &v1.Pod{}, errors.Errorf("failed to unmarshal manifest for %q from YAML: %v", manifestPath, err) return &v1.Pod{}, errors.Errorf("failed to unmarshal manifest for %q from YAML: %v", manifestPath, err)
} }