Skip to content

Instantly share code, notes, and snippets.

@kayrus
Forked from zonorti/main.go
Created May 29, 2017 15:16
Show Gist options
  • Save kayrus/2a23d8767ec8ce3f5ce31172947219a0 to your computer and use it in GitHub Desktop.
Save kayrus/2a23d8767ec8ce3f5ce31172947219a0 to your computer and use it in GitHub Desktop.
http to k8s deployment patch. gorilla + k8s go client. For local run set KUBECONFIG path to config file
package main
import (
"flag"
"fmt"
"encoding/json"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
"io"
"io/ioutil"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
v1api "k8s.io/client-go/pkg/api/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"log"
"net/http"
"os"
)
type DeploySpec struct {
Name string `json:"name"`
ContainerName string `json:"container_name"`
ImageName string `json:"image_name"`
ImageTag string `json:"image_tag"`
Namespace string `json:"namespace"`
}
var client = create_client()
func print_image(podspec v1api.PodSpec) {
for _, container := range podspec.Containers {
fmt.Printf("Deployment is %s \n", container.Image)
}
}
func deploy(spec DeploySpec, clientset *kubernetes.Clientset) {
fmt.Println("Deploying ", spec.Name)
deployment, err := clientset.ExtensionsV1beta1().Deployments(spec.Namespace).Get(spec.Name, v1.GetOptions{})
if err != nil {
panic(err.Error())
}
print_image(deployment.Spec.Template.Spec)
format := `{"spec":{"template":{"spec":{"containers":[{"name":"%s","image":"%s:%s" }]}}}}`
patch := fmt.Sprintf(format,
spec.ContainerName, spec.ImageName, spec.ImageTag)
deployment_new, err := clientset.ExtensionsV1beta1().Deployments(spec.Namespace).Patch(
spec.Name,
types.StrategicMergePatchType,
[]byte(patch))
if err != nil {
panic(err.Error())
}
print_image(deployment_new.Spec.Template.Spec)
}
func buildConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func create_client() *kubernetes.Clientset {
config_path := os.Getenv("KUBECONFIG")
kubeconfig := flag.String("kubeconfig", config_path, "absolute path to the kubeconfig file")
flag.Parse()
config, err := buildConfig(*kubeconfig)
if err != nil {
panic(err)
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return clientset
}
func get_body(httpbody io.ReadCloser) []byte {
body, err := ioutil.ReadAll(io.LimitReader(httpbody, 1048576))
if err != nil {
panic(err)
}
if err := httpbody.Close(); err != nil {
panic(err)
}
return body
}
func check_auth(token string) {
log.Print(token)
}
func Handler(w http.ResponseWriter, r *http.Request) {
var ds DeploySpec
auth := r.Header.Get("Authorization")
if auth == "" {
http.Error(w, "Use Google Bearer Authorization", 401)
return
}
check_auth(auth)
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
body := get_body(r.Body)
if err := json.Unmarshal(body, &ds); err != nil {
http.Error(w, err.Error(), 400)
return
}
deploy(ds, client)
if err := json.NewEncoder(w).Encode(ds); err != nil {
panic(err)
}
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Handler)
loggedRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
log.Print("Starting http server")
log.Fatal(http.ListenAndServe(":18080", loggedRouter))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment