I am new to GoLang and looking to join two keys (rules) from two different 
Yamls.

I have been able to join the keys but I found that Unmarshal changes the 
keys (example apiVersion to APIVersion, and kind to Kind).

Overall, my goal is to have a valid Kubernetes YAML manifest. This is what 
I have attempted so far https://goplay.tools/snippet/zWYDjnTeA0Q 

## 
package main

import (
"fmt"

"github.com/InVisionApp/conjungo"
"github.com/ghodss/yaml"
)

type ClusterRole struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata struct {
Name string `yaml:"name"`
} `yaml:"metadata"`
Rules []struct {
APIGroups []string `yaml:"apiGroups"`
Resources []string `yaml:"resources"`
Verbs []string `yaml:"verbs"`
} `yaml:"rules"`
}

func main() {

yaml1 := `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: role1
rules:
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- apiGroups:
- apiregistration.k8s.io
resources:
- apiservices
verbs:
- create
- delete
`

yaml2 := `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: role2
rules:
- apiGroups:
- ""
resources:
- secrets
- events.k8s.io
verbs:
- list
- watch
- apiGroups:
- metrics.k8s.io
resources:
- nodes
- bindings
verbs:
- get
`

var role1 ClusterRole
yaml.Unmarshal([]byte(yaml1), &role1)
fmt.Printf("\n## ROLE1\n %v", role1)

var role2 ClusterRole
yaml.Unmarshal([]byte(yaml2), &role2)
fmt.Printf("\n\n## ROLE2\n %v", role2)

conjungo.Merge(&role1, role2, nil)

// Struct to Marshal
y, err := yaml.Marshal(role1)
if err != nil {
fmt.Printf("\nerr: %v\n", err)
return
}
fmt.Printf("\n\n## MERGED ROLE\n %v", string(y))
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20539ac3-f923-4a26-b598-27ade4961c46n%40googlegroups.com.

Reply via email to