Skip to content

Instantly share code, notes, and snippets.

@mhrivnak
Created September 27, 2018 14:34
Show Gist options
  • Save mhrivnak/90776c00a54ace478aaef6b734fe75a8 to your computer and use it in GitHub Desktop.
Save mhrivnak/90776c00a54ace478aaef6b734fe75a8 to your computer and use it in GitHub Desktop.
Reads a yaml file and creates resources in k8s
[[constraint]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.11.2"
[[constraint]]
name = "k8s.io/client-go"
version = "kubernetes-1.11.2"
[[constraint]]
name = "sigs.k8s.io/controller-runtime"
version = "v0.1.4"
[prune]
go-tests = true
unused-packages = true
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/clientcmd"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func main() {
f, err := os.Open("manifest.yaml")
if err != nil {
panic(err.Error())
}
home := os.Getenv("HOME")
configpath := filepath.Join(home, ".kube/config")
config, err := clientcmd.BuildConfigFromFlags("", configpath)
if err != nil {
panic(err)
}
scheme := runtime.NewScheme()
client, err := crclient.New(config, crclient.Options{Scheme: scheme})
if err != nil {
panic(err)
}
decoder := yaml.NewYAMLOrJSONDecoder(f, 65536)
for {
u := unstructured.Unstructured{}
err = decoder.Decode(&u)
if err == io.EOF {
return
}
if err != nil {
panic(err.Error())
}
u.SetNamespace("default")
err = client.Create(context.TODO(), &u)
if err != nil {
panic(err)
}
fmt.Printf("created %v\n", u.GroupVersionKind())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment