Example #1
0
func getMachinesSeen() []string {
	var machinesSeenResult NodeResult

	path := fmt.Sprintf("%s/keys/seen", Conf.EtcdAPIVersion)
	urlStr := getFullAPIURL(Conf.EtcdClientPort, path)

	headers := map[string]string{
		"Content-Type": "application/json",
	}
	p := goutils.HttpRequestParams{
		HttpRequestType: "GET",
		Url:             urlStr,
		Headers:         headers,
	}

	_, jsonResponse, _ := goutils.HttpCreateRequest(p)
	err := json.Unmarshal(jsonResponse, &machinesSeenResult)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 1, Fatal: false})

	var machinesSeen []string
	var machinesSeenBytes []byte = []byte(machinesSeenResult.Node.Value)
	err = json.Unmarshal(machinesSeenBytes, &machinesSeen)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 1, Fatal: false})

	return machinesSeen
}
Example #2
0
func getUnitState(unitFile string) FleetUnitState {
	var fleetUnitStates FleetUnitStates
	filename := filepath.Base(unitFile)

	urlPath := fmt.Sprintf("fleet/%s/state", Conf.FleetAPIVersion)
	url := getFullAPIURL(Conf.FleetAPIPort, urlPath)

	headers := map[string]string{
		"Content-Type": "application/json",
	}
	p := goutils.HttpRequestParams{
		HttpRequestType: "GET",
		Url:             url,
		Headers:         headers,
	}

	_, jsonResponse, _ := goutils.HttpCreateRequest(p)
	err := json.Unmarshal(jsonResponse, &fleetUnitStates)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	for _, unitState := range fleetUnitStates.States {
		if unitState.Name == filename {
			return unitState
		}
	}
	return FleetUnitState{}
}
Example #3
0
func setupUnitFilesDeps() []map[string]string {
	unitPathInfo := getUnitPathInfo()
	perm := os.FileMode(os.ModeDir)

	for _, v := range unitPathInfo {
		err := os.RemoveAll(v["path"])
		goutils.PrintErrors(
			goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

		os.MkdirAll(v["path"], perm)
	}

	return unitPathInfo
	/*
		switch fleetMachine.Metadata["kubernetes_role"] {
		case "master":
			createdFiles = createMasterUnits(fleetMachine, unitPathInfo)
		case "minion":
			createdFiles = createMinionUnits(masterIPPort, fleetMachine, unitPathInfo)
		}

		log.Printf("Created all unit files for: %s\n", fleetMachine.ID)
		return createdFiles
	*/
}
Example #4
0
func waitForMetadata(
	resultNode *ResultNode,
	fleetMachine *FleetMachine,
) {

	// Issue request to get machines & parse it. Sleep if cluster not ready yet
	id := strings.Split(resultNode.Key, "fleet/machines/")[1]
	path := fmt.Sprintf(
		"%s/keys/_coreos.com/fleet/machines/%s/object", Conf.EtcdAPIVersion, id)

	url := getFullAPIURL(Conf.EtcdClientPort, path)

	headers := map[string]string{
		"Content-Type": "application/json",
	}

	p := goutils.HttpRequestParams{
		HttpRequestType: "GET",
		Url:             url,
		Headers:         headers,
	}

	_, jsonResponse, _ := goutils.HttpCreateRequest(p)

	var nodeResult NodeResult
	err := json.Unmarshal(jsonResponse, &nodeResult)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	err = json.Unmarshal(
		[]byte(nodeResult.Node.Value), &fleetMachine)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	for len(fleetMachine.Metadata) == 0 ||
		fleetMachine.Metadata["kubernetes_role"] == nil {
		log.Printf("Waiting for machine (%s) metadata to be available "+
			"in fleet...", fleetMachine.ID)
		time.Sleep(1 * time.Second)

		err = json.Unmarshal(
			[]byte(nodeResult.Node.Value), &fleetMachine)
		goutils.PrintErrors(
			goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	}
}
Example #5
0
func register(endpoint, addr string) error {
	status := v1.NodeStatus{}
	status.Addresses = []v1.NodeAddress{
		v1.NodeAddress{
			Address: addr,
			Type:    v1.NodeInternalIP,
		},
	}

	m := &PreregisteredKNode{
		Kind:       "Node",
		Metadata:   map[string]interface{}{"name": addr},
		Status:     status,
		APIVersion: Conf.KubernetesAPIVersion,
	}
	data, err := json.Marshal(m)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 1, Fatal: false})

	url := fmt.Sprintf("%s/api/%s/nodes", endpoint, Conf.KubernetesAPIVersion)

	headers := map[string]string{
		"Content-Type": "application/json",
	}

	p := goutils.HttpRequestParams{
		HttpRequestType: "POST",
		Url:             url,
		Data:            data,
		Headers:         headers,
	}

	statusCode, body, err := goutils.HttpCreateRequest(p)

	switch statusCode {
	case 200, 201, 202:
		log.Printf("Registered node with the Kubernetes master: %s\n", addr)
		return nil
	}
	log.Printf("%d\n%s", statusCode, body)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 1, Fatal: false})
	return nil
}
Example #6
0
// Get the IP address of the docker host as this is run from within container
func getDockerHostIP() string {
	cmd := fmt.Sprintf("netstat -nr | grep '^0\\.0\\.0\\.0' | awk '{print $2}'")
	out, err := exec.Command("sh", "-c", cmd).Output()
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	ip := string(out)
	ip = strings.Replace(ip, "\n", "", -1)
	return ip
}
Example #7
0
func startUnitFile(unitFile string) {
	filename := filepath.Base(unitFile)
	unitFilepath := fmt.Sprintf(
		"fleet/%s/units/%s", Conf.FleetAPIVersion, filename)
	url := getFullAPIURL(Conf.FleetAPIPort, unitFilepath)

	log.Printf("Starting unit file: %s", filename)

	statusCode := 0
	for statusCode != 204 {
		readfile, err := ioutil.ReadFile(unitFile)
		goutils.PrintErrors(
			goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

		content := string(readfile)
		u, _ := unit.NewUnitFile(content)

		options_bytes, _ := json.Marshal(u.Options)
		options_str := lowerCasingOfUnitOptionsStr(string(options_bytes))

		json_str := fmt.Sprintf(
			`{"name": "%s", "desiredState":"launched", "options": %s}`,
			filename,
			options_str)

		headers := map[string]string{
			"Content-Type": "application/json",
		}

		p := goutils.HttpRequestParams{
			HttpRequestType: "PUT",
			Url:             url,
			Data:            json_str,
			Headers:         headers,
		}
		statusCode, _, _ = goutils.HttpCreateRequest(p)

		time.Sleep(1 * time.Second)
		/*
			log.Printf(
				"curl -H \"Content-Type: application/json\" -X PUT "+
					"-d %q localhost:10001/v1-alpha/units/%s",
				json_str, filename)
			body, err := ioutil.ReadAll(resp.Body)
			log.Printf("Status Code: %s", statusCode)
			log.Printf("[Error] in HTTP Body: %s - %v", body, err)
			goutils.PrintErrors(
				goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
		*/
	}
}
Example #8
0
func getFleetMachines(fleetResult *Result) {
	path := fmt.Sprintf("%s/keys/_coreos.com/fleet/machines", Conf.EtcdAPIVersion)
	url := getFullAPIURL(Conf.EtcdClientPort, path)

	headers := map[string]string{
		"Content-Type": "application/json",
	}
	p := goutils.HttpRequestParams{
		HttpRequestType: "GET",
		Url:             url,
		Headers:         headers,
	}

	_, jsonResponse, _ := goutils.HttpCreateRequest(p)
	err := json.Unmarshal(jsonResponse, fleetResult)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	removeOverlord(&fleetResult.Node.Nodes)
}
Example #9
0
func registerKNodes(master *FleetMachine, node *FleetMachine) {

	// Get registered nodes, if any
	endpoint := fmt.Sprintf("http://%s:%s", master.PublicIP, Conf.KubernetesAPIPort)
	masterAPIurl := fmt.Sprintf("%s/api/%s/nodes", endpoint, Conf.KubernetesAPIVersion)

	headers := map[string]string{
		"Content-Type": "application/json",
	}
	p := goutils.HttpRequestParams{
		HttpRequestType: "GET",
		Url:             masterAPIurl,
		Headers:         headers,
	}

	_, jsonResponse, _ := goutils.HttpCreateRequest(p)

	var nodesResult KNodesResult
	err := json.Unmarshal(jsonResponse, &nodesResult)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	// See if nodes discovered have been registered. If not, register
	registered := false
	/*
		for _, registeredKNode := range nodesResult.Nodes {
			if registeredKNode.HostIP == node.PublicIP {
				registered = true
			}
		}
	*/

	if !registered {
		register(endpoint, node.PublicIP)
		time.Sleep(500 * time.Millisecond)
	}
}
Example #10
0
func createMasterUnits(fleetMachine *FleetMachine) []string {
	unitPathInfo := setupUnitFilesDeps()

	files := map[string]string{
		"api":        "[email protected]",
		"controller": "[email protected]",
		"scheduler":  "[email protected]",
		"download":   "[email protected]",
		"dns":        "[email protected]",
	}

	createdFiles := []string{}

	// Form download service file from template
	readfile, err := ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["download"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	download := string(readfile)
	download = strings.Replace(download, "<ID>", fleetMachine.ID, -1)
	download = strings.Replace(download, "<URL>", Conf.BinariesURL, -1)

	// Write download service file
	filename := strings.Replace(files["download"], "@", "@"+fleetMachine.ID, -1)
	download_file := fmt.Sprintf("%s/%s",
		unitPathInfo[0]["path"], filename)
	err = ioutil.WriteFile(download_file, []byte(download), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, download_file)

	// Form apiserver service file from template
	readfile, err = ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["api"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	apiserver := string(readfile)
	apiserver = strings.Replace(apiserver, "<ID>", fleetMachine.ID, -1)
	apiserver = strings.Replace(apiserver, "<IP_ADDR>", fleetMachine.PublicIP, -1)

	// Write apiserver service file
	filename = strings.Replace(files["api"], "@", "@"+fleetMachine.ID, -1)
	apiserver_file := fmt.Sprintf("%s/%s", unitPathInfo[1]["path"], filename)
	err = ioutil.WriteFile(apiserver_file, []byte(apiserver), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, apiserver_file)

	// Form controller service file from template
	readfile, err = ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["controller"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	controller := string(readfile)
	controller = strings.Replace(controller, "<ID>", fleetMachine.ID, -1)

	// Write controller service file
	filename = strings.Replace(files["controller"], "@", "@"+fleetMachine.ID, -1)
	controller_file := fmt.Sprintf("%s/%s", unitPathInfo[1]["path"], filename)
	err = ioutil.WriteFile(controller_file, []byte(controller), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, controller_file)

	// Form scheduler service file from template
	readfile, err = ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["scheduler"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	scheduler := string(readfile)
	scheduler = strings.Replace(scheduler, "<ID>", fleetMachine.ID, -1)

	// Write scheduler service file
	filename = strings.Replace(files["scheduler"], "@", "@"+fleetMachine.ID, -1)
	scheduler_file := fmt.Sprintf("%s/%s", unitPathInfo[1]["path"], filename)
	err = ioutil.WriteFile(scheduler_file, []byte(scheduler), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, scheduler_file)

	// Substitute the machine ID and file paths into systemd file
	filename = strings.Replace(files["dns"], "@", "@"+fleetMachine.ID, -1)
	dns_file := fmt.Sprintf("%s/%s", unitPathInfo[0]["path"], filename)
	readfile, err = ioutil.ReadFile(fmt.Sprintf("/templates/%s", files["dns"]))
	goutils.PrintErrors(goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})

	dns_content := strings.Replace(string(readfile), "<RC_URL>", Conf.SkyDNSRepContr, -1)
	dns_content = strings.Replace(dns_content, "<SVC_URL>", Conf.SkyDNSService, -1)
	dns_content = strings.Replace(dns_content, "<ID>", fleetMachine.ID, -1)
	dns_content = strings.Replace(dns_content, "<MASTER_IP>", fleetMachine.PublicIP, -1)

	err = ioutil.WriteFile(dns_file, []byte(dns_content), 0644)
	goutils.PrintErrors(goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, dns_file)

	log.Printf("Created all unit files for: %s\n", fleetMachine.ID)
	return createdFiles
}
Example #11
0
func createMinionUnits(masterFleetMachine,
	fleetMachine *FleetMachine) []string {

	masterIPPort := fmt.Sprintf("%s:%s",
		masterFleetMachine.PublicIP, Conf.KubernetesAPIPort)
	unitPathInfo := setupUnitFilesDeps()
	files := map[string]string{
		"kubelet":  "[email protected]",
		"proxy":    "[email protected]",
		"download": "[email protected]",
	}

	createdFiles := []string{}

	// Form download service file from template
	readfile, err := ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["download"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	download := string(readfile)
	download = strings.Replace(download, "<ID>", fleetMachine.ID, -1)
	download = strings.Replace(download, "<URL>", Conf.BinariesURL, -1)

	// Write download service file
	filename := strings.Replace(files["download"], "@", "@"+fleetMachine.ID, -1)
	download_file := fmt.Sprintf("%s/%s",
		unitPathInfo[0]["path"], filename)
	err = ioutil.WriteFile(download_file, []byte(download), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, download_file)

	// Form kubelet service file from template
	readfile, err = ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["kubelet"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	kubelet := string(readfile)
	kubelet = strings.Replace(kubelet, "<ID>", fleetMachine.ID, -1)
	kubelet = strings.Replace(kubelet, "<IP_ADDR>", fleetMachine.PublicIP, -1)
	kubelet = strings.Replace(kubelet, "<MASTER_IP_PORT>", masterIPPort, -1)

	// Write kubelet service file
	filename = strings.Replace(files["kubelet"], "@", "@"+fleetMachine.ID, -1)
	kubelet_file := fmt.Sprintf("%s/%s", unitPathInfo[1]["path"], filename)
	err = ioutil.WriteFile(kubelet_file, []byte(kubelet), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, kubelet_file)

	// Form proxy service file from template
	readfile, err = ioutil.ReadFile(
		fmt.Sprintf("/templates/%s", files["proxy"]))
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	proxy := string(readfile)
	proxy = strings.Replace(proxy, "<ID>", fleetMachine.ID, -1)
	proxy = strings.Replace(proxy, "<MASTER_IP_PORT>", masterIPPort, -1)

	// Write proxy service file
	filename = strings.Replace(files["proxy"], "@", "@"+fleetMachine.ID, -1)
	proxy_file := fmt.Sprintf("%s/%s", unitPathInfo[1]["path"], filename)
	err = ioutil.WriteFile(proxy_file, []byte(proxy), 0644)
	goutils.PrintErrors(
		goutils.ErrorParams{Err: err, CallerNum: 2, Fatal: false})
	createdFiles = append(createdFiles, proxy_file)

	log.Printf("Created all unit files for: %s\n", fleetMachine.ID)
	return createdFiles
}