func SplitYAMLDocuments()

in builder/kubebuilder-build/utils/acmsplit/main.go [88:149]


func SplitYAMLDocuments(yamlBytes []byte) ([]byte, []byte, error) {
	gvkListCluster := make([]byte, 0)
	gvkListOther := make([]byte, 0)
	errs := []error{}
	buf := bytes.NewBuffer(yamlBytes)
	reader := utilyaml.NewYAMLReader(bufio.NewReader(buf))
	for {
		typeMetaInfo := runtime.TypeMeta{}
		typeObjectInfo := corev1.ObjectReference{}
		// Read one YAML document at a time, until io.EOF is returned
		b, err := reader.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			return nil, nil, err
		}
		if len(b) == 0 {
			break
		}
		// Deserialize the TypeMeta information of this byte slice
		if err := yaml.Unmarshal(b, &typeMetaInfo); err != nil {
			return nil, nil, err
		}
		if err := yaml.Unmarshal(b, &typeObjectInfo); err != nil {
			return nil, nil, err
		}
		// Require TypeMeta information to be present
		if len(typeMetaInfo.APIVersion) == 0 || len(typeMetaInfo.Kind) == 0 {
			errs = append(errs, errors.New("invalid configuration: kind and apiVersion is mandatory information that needs to be specified in all YAML documents"))
			continue
		}

		fmt.Println("kind", typeMetaInfo.Kind, len(b))
		// Save the mapping between the gvk and the bytes that object consists of
		switch typeMetaInfo.Kind {
		case "Namespace":
		case "ConstraintTemplate", "Constraint":
			fallthrough
		case "ClusterRole":
			fallthrough
		case "ClusterRoleBinding":
			fallthrough
		case "CustomResourceDefinition":
			b = append([]byte("---\n"), b...)
			gvkListCluster = append(gvkListCluster, b...)
		case "MutatingWebhookConfiguration":
			fallthrough
		case "ValidatingWebhookConfiguration":
			configMapBytes, err := createConfigMapWithYAML(b)
			if err != nil {
				return nil, nil, err
			}
			gvkListOther = append(gvkListOther, configMapBytes...)
		default:
			b = append([]byte("---\n"), b...)
			gvkListOther = append(gvkListOther, b...)

		}
	}

	return gvkListCluster, gvkListOther, nil
}