株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

package main

import (
    "context"
    "fmt"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    "k8s.io/apimachinery/pkg/runtime/schema"
    "k8s.io/client-go/dynamic"
    "k8s.io/client-go/tools/clientcmd"
    "os"
    "path/filepath"
)

func main() {
    // Kubeconfigのファイルパスを指定
    kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        panic(err)
    }
    client, err := dynamic.NewForConfig(config)
    if err != nil {
        panic(err)
    }

    namespace := "default"

    deploymentRes := schema.GroupVersionResource{Group:"apps", Version: "v1", Resource: "deployments"}


    deployment := &unstructured.Unstructured{
        Object: map[string]interface{}{
            "apiVersion": "apps/v1",
            "kind":       "Deployment",
            "metadata": map[string]interface{}{
                "name": "ubuntu-deployment",
            },
            "spec": map[string]interface{}{
                "replicas": 2,
                "selector": map[string]interface{}{
                    "matchLabels": map[string]interface{}{
                        "app": "demo",
                    },
                },
                "template": map[string]interface{}{
                    "metadata": map[string]interface{}{
                        "labels": map[string]interface{}{
                            "app": "demo",
                        },
                    },

                    "spec": map[string]interface{}{
                        "containers": []map[string]interface{}{
                            {
                                "name":  "ubuntu",
                                "image": "ubuntu",
                                "command": []string{
                                    "sleep",
                                    "infinity",
                                },
                            },
                        },
                    },
                },
            },
        },
    }

    fmt.Println("Creating deployment...")
    result, err := client.Resource(deploymentRes).Namespace(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
    if err != nil {
        panic(err)
    }
    fmt.Printf("Created deployment %q.\n", result.GetName())

}

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)