示例#1
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{}
}
示例#2
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
}
示例#3
0
func setMachinesSeen(machines []string) {
	path := fmt.Sprintf("%s/keys/seen", Conf.EtcdAPIVersion)
	urlStr := getFullAPIURL(Conf.EtcdClientPort, path)
	data := ""

	switch machines {
	case nil:
		emptySlice := []string{}
		dataJSON, _ := json.Marshal(emptySlice)
		data = fmt.Sprintf("value=%s", dataJSON)
	default:
		dataJSON, _ := json.Marshal(machines)
		data = fmt.Sprintf("value=%s", dataJSON)
	}

	headers := map[string]string{
		"Content-Type":   "application/x-www-form-urlencoded",
		"Content-Length": strconv.Itoa(len(data)),
	}

	p := goutils.HttpRequestParams{
		HttpRequestType: "PUT",
		Url:             urlStr,
		Data:            data,
		Headers:         headers,
	}
	goutils.HttpCreateRequest(p)
}
示例#4
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})
		*/
	}
}
示例#5
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})

	}
}
示例#6
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
}
示例#7
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)
}
示例#8
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)
	}
}