Created
April 9, 2019 05:26
-
-
Save superbrothers/1b689412c38c3203109f335bea89924c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"path/filepath" | |
appsv1 "k8s.io/api/apps/v1" | |
apiv1 "k8s.io/api/core/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/apimachinery/pkg/types" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/kubernetes/scheme" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/homedir" | |
_ "k8s.io/client-go/plugin/pkg/client/auth" | |
) | |
var ( | |
kubeconfig = filepath.Join(homedir.HomeDir(), ".kube", "config") | |
) | |
func main() { | |
flag.StringVar(&kubeconfig, "kubeconfig", kubeconfig, "absolute path to the kubeconfig file") | |
flag.Parse() | |
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | |
if err != nil { | |
log.Fatal(err) | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
deployment := &appsv1.Deployment{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: "nginx", | |
}, | |
Spec: appsv1.DeploymentSpec{ | |
Selector: &metav1.LabelSelector{ | |
MatchLabels: map[string]string{ | |
"app": "nginx", | |
}, | |
}, | |
Template: apiv1.PodTemplateSpec{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Labels: map[string]string{ | |
"app": "nginx", | |
}, | |
}, | |
Spec: apiv1.PodSpec{ | |
Containers: []apiv1.Container{ | |
{ | |
Name: "nginx", | |
Image: "nginx", | |
}, | |
}, | |
}, | |
}, | |
}, | |
} | |
data, err := runtime.Encode(scheme.Codecs.LegacyCodec(appsv1.SchemeGroupVersion), deployment) | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = clientset.AppsV1().RESTClient().Patch(types.ApplyPatchType). | |
Namespace("default"). | |
Resource("deployments"). | |
Name("nginx"). | |
Param("fieldManager", "try-server-side-apply"). | |
Body(data). | |
Do(). | |
Get() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment