summaryrefslogtreecommitdiffstats
path: root/clover/cloverctl/src/cloverctl/cmd/create_kubernetes.go
blob: 7311090b605d5b07820eb08db1234b518712e96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Authors of Clover
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Apache License, Version 2.0
// which accompanies this distribution, and is available at
// http://www.apache.org/licenses/LICENSE-2.0

package cmd

import (
    "fmt"
    "time"
    "io/ioutil"
    "strings"

    "gopkg.in/resty.v1"
    "github.com/ghodss/yaml"
    "github.com/spf13/cobra"
    "cloverkube"
)

type Kubernetes struct {
    Name        string
    ProviderVersion string
    KubeconfigFile string
    DockerRegistries []DockerRegistry
}

type DockerRegistry struct {
    AccountName string
}


var kubeproviderCmd = &cobra.Command{
    Use:   "kubernetes",
    Short: "Add one kubernete provider from yaml file to spinnaker",
    Long: ``,
    Run: func(cmd *cobra.Command, args []string) {
        createProvider()
    },
}

func init() {
    providercreateCmd.AddCommand(kubeproviderCmd)
    kubeproviderCmd.Flags().StringVarP(&cloverFile, "file", "f", "", "Input yaml file to add kubernetes provider")
    kubeproviderCmd.MarkFlagRequired("file")

}

func createProvider() {
    url := controllerIP + "/halyard/addkube"
    in, err := ioutil.ReadFile(cloverFile)
    if err != nil {
        fmt.Println("Please specify a valid rule definition yaml file")
        return
    }

    t := Kubernetes{}
    yaml.Unmarshal(in, &t)
    if(t.KubeconfigFile==""){
        fmt.Println("error")
        return;
    }
    filename := t.KubeconfigFile
    hal_path := "/home/spinnaker/config"
    timestamp := time.Now().Unix()
    tm := time.Unix(timestamp, 0)
    t.KubeconfigFile = hal_path + tm.Format("2006-01-02-15-04-05")
    dest_container := "spinnaker/spin-halyard/halyard-daemon"
    destPath := t.KubeconfigFile
    dest := strings.Join([]string{dest_container, destPath}, ":")
    cloverkube.CopyFileToPod(filename, dest)
    newconfig, _ := yaml.Marshal(&t)
    out_json, err := yaml.YAMLToJSON(newconfig)
    if err != nil {
        panic(err.Error())
    }
    resp, err := resty.R().
    SetHeader("Content-Type", "application/json").
    SetBody(out_json).
    Post(url)
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("\n%v\n", resp)

}