2019-03-14 14:56:06 +00:00
package k8s
import (
"fmt"
"regexp"
"strings"
2020-09-16 13:46:04 +00:00
"github.com/traefik/traefik/v2/pkg/log"
2019-03-14 14:56:06 +00:00
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
)
// MustParseYaml parses a YAML to objects.
func MustParseYaml ( content [ ] byte ) [ ] runtime . Object {
2021-10-04 13:46:08 +00:00
acceptedK8sTypes := regexp . MustCompile ( ` ^(Namespace|Deployment|Endpoints|Service|Ingress|IngressRoute|IngressRouteTCP|IngressRouteUDP|Middleware|MiddlewareTCP|Secret|TLSOption|TLSStore|TraefikService|IngressClass|ServersTransport|GatewayClass|Gateway|HTTPRoute|TCPRoute|TLSRoute)$ ` )
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
}
decode := scheme . 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 ) {
log . WithoutContext ( ) . Debugf ( "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
}