Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse a stream of kubernetes YAML on stdin into objects for reading
package main
import (
"fmt"
"io"
"os"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
"k8s.io/client-go/kubernetes/scheme"
)
// Parse a stream of kubernetes YAML on stdin into objects for reading
func main() {
yamlStreamReader := json.YAMLFramer.NewFrameReader(os.Stdin)
decoder := streaming.NewDecoder(yamlStreamReader, scheme.Codecs.UniversalDeserializer())
for {
object, _, err := decoder.Decode(nil, nil)
if err == io.EOF {
break
}
if deployment, isDeployment := object.(*appsv1.Deployment); isDeployment {
replicas := deployment.Spec.Replicas
if replicas != nil {
fmt.Printf("Deployment %s has %d replicas\n", deployment.ObjectMeta.Name, *replicas)
}
}
if configMap, isConfigMap := object.(*corev1.ConfigMap); isConfigMap {
fmt.Printf("ConfigMap %s has %d keys\n", configMap.ObjectMeta.Name, len(configMap.Data))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment