Sample to start Ubuntu with Deployment with client-go

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() {
    //Specify the file path of 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())

}

Recommended Posts

Sample to start Ubuntu with Deployment with client-go
Deployment sample to launch ubuntu image in Kubernetes
Java, arrays to start with beginners
How to start Camunda with Docker
Introduced vscode with apt to docker: ubuntu
Sample to create PDF from Excel with Ruby
Write a dockerfile to start jupyter-lab on ubuntu
Rip a CD to MP3 with Ubuntu 18.04 LTS
How to use nfs protocol version 2 with ubuntu 18.04
Burn files to CD / DVD with Ubuntu 18.04 LTS
VNC connection to Ubuntu Desktop with Mac screen sharing.
[Introduction to Docker x ECS] ECS deployment with docker compose up
Pass User credential to deployment destination with Jenkins + Capistrano
`Failed to create the host-only adapter` with docker-machine start`
How to use RealSense with ubuntu 20.04 and ROS Noetic
I want to redirect sound from Ubuntu with xrdp
[Java] How to start a new line with StringBuilder
Install Gradle with ubuntu16.04
Wait for the container service to start with docker healthcheck
Sample to create GUI application with JavaFX + IntelliJ + Scene Builder
Sample code to parse date and time with Java SimpleDateFormat
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
A memo to start Java programming with VS Code (2020-04 version)