2019-03-14 14:56:06 +00:00
|
|
|
package k8s
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2019-03-14 14:56:06 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2023-04-17 08:56:36 +00:00
|
|
|
kscheme "k8s.io/client-go/kubernetes/scheme"
|
2019-03-14 14:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MustParseYaml parses a YAML to objects.
|
|
|
|
func MustParseYaml(content []byte) []runtime.Object {
|
2024-10-21 08:10:04 +00:00
|
|
|
acceptedK8sTypes := regexp.MustCompile(`^(Namespace|Deployment|EndpointSlice|Node|Service|ConfigMap|Ingress|IngressRoute|IngressRouteTCP|IngressRouteUDP|Middleware|MiddlewareTCP|Secret|TLSOption|TLSStore|TraefikService|IngressClass|ServersTransport|ServersTransportTCP|GatewayClass|Gateway|GRPCRoute|HTTPRoute|TCPRoute|TLSRoute|ReferenceGrant|BackendTLSPolicy)$`)
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2021-10-07 13:40:05 +00:00
|
|
|
files := strings.Split(string(content), "---\n")
|
2019-03-14 14:56:06 +00:00
|
|
|
retVal := make([]runtime.Object, 0, len(files))
|
|
|
|
for _, file := range files {
|
|
|
|
if file == "\n" || file == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
decode := kscheme.Codecs.UniversalDeserializer().Decode
|
2019-03-14 14:56:06 +00:00
|
|
|
obj, groupVersionKind, err := decode([]byte(file), nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error while decoding YAML object. Err was: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !acceptedK8sTypes.MatchString(groupVersionKind.Kind) {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Msgf("The custom-roles configMap contained K8s object types which are not supported! Skipping object with type: %s", groupVersionKind.Kind)
|
2019-03-14 14:56:06 +00:00
|
|
|
} else {
|
|
|
|
retVal = append(retVal, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retVal
|
|
|
|
}
|