Skip to content

Instantly share code, notes, and snippets.

@arbarlow
Created February 12, 2018 21:22
Show Gist options
  • Save arbarlow/eb14f6b7189b674dfc160a5905004250 to your computer and use it in GitHub Desktop.
Save arbarlow/eb14f6b7189b674dfc160a5905004250 to your computer and use it in GitHub Desktop.
Remove istio
// Note: the example only works with the code within the same release/branch.
package main
import (
"flag"
"os"
"path/filepath"
"strings"
"go.uber.org/zap"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
func main() {
logger, _ := zap.NewDevelopment()
defer logger.Sync() // flushes buffer, if any
sugar := logger.Sugar()
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
dpls, err := clientset.AppsV1beta1().Deployments("default").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, d := range dpls.Items {
update := false
if os.Getenv("DEPLOYMENT") != "" && d.Name != os.Getenv("DEPLOYMENT") {
continue
}
conts := d.Spec.Template.Spec.Containers
for i, c := range d.Spec.Template.Spec.Containers {
if c.Name == "istio-proxy" {
conts = append(conts[:i], conts[i+1:]...)
update = true
}
}
vols := d.Spec.Template.Spec.Volumes
deleted := 0
for i, v := range d.Spec.Template.Spec.Volumes {
if strings.Contains(v.Name, "istio") {
j := i - deleted
vols = append(vols[:j], vols[j+1:]...)
deleted++
update = true
}
}
anons := d.Spec.Template.Annotations
for k, _ := range d.Spec.Template.Annotations {
if strings.Contains(k, "istio") {
delete(anons, k)
update = true
}
if strings.Contains(k, "init-containers") {
delete(anons, k)
update = true
}
}
if update {
sugar.Infof("Updating deployment %s", d.Name)
d.Spec.Template.Spec.Containers = conts
d.Spec.Template.Spec.Volumes = vols
d.Spec.Template.Annotations = anons
d.Spec.Template.Spec.InitContainers = []v1.Container{}
clientset.AppsV1beta1().Deployments("default").Update(&d)
} else {
sugar.Infof("Skipping deployment %s", d.Name)
}
}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment