simplified parsing & getting annotations from kubernetes svc resource

This commit is contained in:
2025-01-18 12:11:06 +01:00
parent ad64b7728b
commit 489e7df9ea
3 changed files with 30 additions and 60 deletions

View File

@@ -1,17 +1,24 @@
package converter
func DeploymentToKubernetes (filePath string) (*v1.Deployment, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
import (
"errors"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"os"
)
var cfg v1.Deployment
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return nil, err
// NOTE this is really not used, just wanted to convert
// to a kubernetes resource to our packets for practice.
func ServiceToKubernetes(filePath string) (*v1.Service, error) {
decode := scheme.Codecs.UniversalDeserializer().Decode
stream, _ := os.ReadFile(filePath)
// second param (gKV) is top level (GroupVersionKind) w/ group, version & kind
obj, gKV, _ := decode(stream, nil, nil)
// handle multiple resources split by ---
if gKV.Kind == "Service" {
return obj.(*v1.Service), nil
}
return &cfg, nil
return nil, errors.New("Unable to find service resource")
}