package main
import (
"fmt"
)
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 := `
---
kind: ClusterRole
metadata:
name: role1
rules:
- apiGroups:
- ""
resources:
- services
verbs:
- create
- delete
- apiGroups:
resources:
- apiservices
verbs:
- create
- delete
`
yaml2 := `
---
kind: ClusterRole
metadata:
name: role2
rules:
- apiGroups:
- ""
resources:
- secrets
verbs:
- list
- watch
- apiGroups:
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))
}