traefik/pkg/provider/kubernetes/k8s/parser.go

38 lines
1.2 KiB
Go
Raw Normal View History

package k8s
import (
"fmt"
"regexp"
"strings"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/runtime"
2023-04-17 08:56:36 +00:00
kscheme "k8s.io/client-go/kubernetes/scheme"
)
// MustParseYaml parses a YAML to objects.
func MustParseYaml(content []byte) []runtime.Object {
acceptedK8sTypes := regexp.MustCompile(`^(Namespace|Deployment|Endpoints|Service|Ingress|IngressRoute|IngressRouteTCP|IngressRouteUDP|Middleware|MiddlewareTCP|Secret|TLSOption|TLSStore|TraefikService|IngressClass|ServersTransport|ServersTransportTCP|GatewayClass|Gateway|HTTPRoute|TCPRoute|TLSRoute)$`)
files := strings.Split(string(content), "---\n")
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
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)
} else {
retVal = append(retVal, obj)
}
}
return retVal
}