Beispiel #1
0
func createTemplateCtx(meta *metadata.Client) (interface{}, error) {

	services, err := meta.GetServices()
	if err != nil {
		return nil, errors.Annotate(err, "get services")
	}
	containers, err := meta.GetContainers()
	if err != nil {
		return nil, errors.Annotate(err, "get containers")
	}

	containersW := make([]ContainerWrap, 0)
	for _, container := range containers {
		cw := ContainerWrap{container}
		containersW = append(containersW, cw)
	}

	servicesW := make([]ServiceWrap, 0)
	for _, service := range services {
		sw := ServiceWrap{service}
		servicesW = append(servicesW, sw)
	}

	ctx := map[string]interface{}{
		"Services":   servicesW,
		"Containers": containersW,
	}

	return ctx, nil
}
Beispiel #2
0
func getEnvironmentName(m *metadata.Client) (string, error) {
	timeout := 30 * time.Second
	var err error
	var stack metadata.Stack
	for i := 1 * time.Second; i < timeout; i *= time.Duration(2) {
		stack, err = m.GetSelfStack()
		if err != nil {
			logrus.Errorf("Error reading stack info: %v...will retry", err)
			time.Sleep(i)
		} else {
			return stack.EnvironmentName, nil
		}
	}
	return "", fmt.Errorf("Error reading stack info: %v", err)
}
Beispiel #3
0
func getContainersDnsRecords(m *metadata.Client, dnsEntries map[string]providers.DnsRecord, serviceName string, stackName string) error {
	containers, err := m.GetContainers()
	if err != nil {
		return err
	}

	for _, container := range containers {
		if len(container.ServiceName) == 0 {
			continue
		}

		if len(serviceName) != 0 {
			if serviceName != container.ServiceName {
				continue
			}
			if stackName != container.StackName {
				continue
			}
		}

		if len(container.Ports) == 0 {
			continue
		}

		hostUUID := container.HostUUID
		if len(hostUUID) == 0 {
			logrus.Debugf("Container's %v host_uuid is empty", container.Name)
			continue
		}
		host, err := m.GetHost(hostUUID)
		if err != nil {
			logrus.Infof("%v", err)
			continue
		}
		ip := host.AgentIP
		domainNameEntries := []string{container.ServiceName, container.StackName, EnvironmentName, providers.RootDomainName}
		domainName := strings.ToLower(strings.Join(domainNameEntries, "."))
		records := []string{ip}
		dnsEntry := providers.DnsRecord{domainName, records, "A", 300}

		addToDnsEntries(dnsEntry, dnsEntries)
	}

	return nil
}
Beispiel #4
0
func sync(kClient *kubernetesclient.Client, metadataClient *metadata.Client, c *cache.Cache) error {
	hosts, err := metadataClient.GetHosts()
	if err != nil {
		log.Errorf("Error reading host list from metadata service: [%v], retrying", err)
		return err
	}
	for _, host := range hosts {
		nodeInt, ok := c.Get(host.Hostname)
		if !ok {
			temp_node, err := getKubeNode(kClient, host.Hostname)
			if err != nil {
				log.Errorf("Error getting node: [%s] by name from kubernetes, err: [%v]", host.Hostname, err)
				// This node might not have been added to kuberentes cluster yet, so skip it
				continue
			}
			c.Set(host.Hostname, temp_node, 0)
			nodeInt = temp_node
		}
		node := nodeInt.(*model.Node)
		if node.Metadata.Annotations == nil {
			node.Metadata.Annotations = make(map[string]interface{})
		}
		rancherLabelsMetadataStore := node.Metadata.Annotations
		changed := false
		//check for new/updated labels
		for k, v1 := range host.Labels {
			if !isValidLabelValue(v1) {
				continue
			}
			if changed {
				break
			}
			v2, ok := node.Metadata.Labels[k]
			if !ok {
				// This label doesn't exist
				changed = true
			} else {
				v2String, ok := v2.(string)
				if !ok {
					changed = true
				} else if v1 != v2String {
					changed = true
				}
			}
		}
		for k := range node.Metadata.Labels {
			if changed {
				break
			}
			if _, ok := rancherLabelsMetadataStore[toKMetaLabel(k)]; !ok {
				// This is not a rancher managed label
				continue
			}
			if _, ok := host.Labels[k]; !ok {
				changed = true
			}
		}
		retryCount := 0
		maxRetryCount := 3
		for changed {
			node, err := getKubeNode(kClient, host.Hostname)
			if err != nil {
				log.Errorf("Error getting node: [%s] by name from kubernetes: [%v]", host.Hostname, err)
				continue
			}
			c.Set(host.Hostname, node, 0)
			if node.Metadata.Annotations == nil {
				node.Metadata.Annotations = make(map[string]interface{})
			}
			rancherLabelsMetadataStore := node.Metadata.Annotations
			for k, v1 := range host.Labels {
				if !isValidLabelValue(v1) {
					log.Infof("skipping invalid label %s=%s", k, v1)
					continue
				}
				node.Metadata.Labels[k] = v1
				rancherLabelsMetadataStore[toKMetaLabel(k)] = ""

			}
			for k := range node.Metadata.Labels {
				if _, ok := rancherLabelsMetadataStore[toKMetaLabel(k)]; !ok {
					// This is not a rancher managed label
					continue
				}
				if _, ok := host.Labels[k]; !ok {
					delete(node.Metadata.Labels, k)
					delete(rancherLabelsMetadataStore, toKMetaLabel(k))
				}
			}

			_, err = kClient.Node.ReplaceNode(node)
			if err != nil {
				log.Errorf("Error updating node [%s] with new host labels, err :[%v]", host.Hostname, err)
				if retryCount < maxRetryCount {
					retryCount = retryCount + 1
					continue
				}
			}
			changed = false
		}
	}
	return nil
}