Skip to content

Instantly share code, notes, and snippets.

@amy
Created April 20, 2020 18:06
Show Gist options
  • Save amy/fe530f5deb0f9447615a09024454e6b8 to your computer and use it in GitHub Desktop.
Save amy/fe530f5deb0f9447615a09024454e6b8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
yaml "gopkg.in/yaml.v2"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
restConfig, err := clientcmd.BuildConfigFromFlags("", "/Users/amychen/.kube/config")
if err != nil {
fmt.Println("HIT 1")
panic(err)
}
SchemeStuff()
restConfig.NegotiatedSerializer = serializer.NewCodecFactory(Scheme)
// create the dynamic client from kubeconfig
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
fmt.Println("HIT 2")
panic(err)
}
// convert the runtime.Object to unstructured.Unstructured
unstruct, err := ParseJSON()
if err != nil {
fmt.Println("HIT 3")
panic(err)
}
gvk := unstruct.GroupVersionKind()
restMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gvk.GroupVersion()})
restMapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
fmt.Println("HIT 4")
panic(err)
}
gvr := restMapping.Resource
_, err = dynamicClient.Resource(gvr).Namespace("default").Create(unstruct, metav1.CreateOptions{})
if err != nil {
fmt.Println("HIT 5")
panic(err)
}
}
func YAMLtoJSON() (map[string]interface{}, error) {
yamlText := `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: crontabs
# singular name to be used as an alias on the CLI and for display
singular: crontab
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: CronTab
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- ct
`
yamlConfig := make(map[interface{}]interface{})
err := yaml.Unmarshal([]byte(yamlText), yamlConfig)
if err != nil {
return nil, err
}
jsonConfig := make(map[string]interface{})
for key, value := range yamlConfig {
switch key := key.(type) {
case string:
jsonConfig[key] = value
}
}
return jsonConfig, nil
}
func ParseJSON() (*unstructured.Unstructured, error) {
//k8s.io/apimachinery/pkg/util/yamL
//https://github.com/kubernetes/client-go/issues/193
json, err := YAMLtoJSON()
if err != nil {
return nil, err
}
resource := &unstructured.Unstructured{
Object: json,
}
return resource, nil
}
var Scheme = runtime.NewScheme()
func SchemeStuff() {
utilruntime.Must(apiextensionsv1.AddToScheme(Scheme))
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment