예제 #1
0
func TestDetectsRemoval(t *testing.T) {
	metadataClient := metadata.NewClient(fakeMetadataURL)
	kubeClient := kubernetesclient.NewClient(kubeURL, false)
	c := cache.New(1*time.Minute, 1*time.Minute)

	metadataHandler.hosts = []metadata.Host{
		{
			Name:     "test1",
			Hostname: "test1",
			Labels:   map[string]string{},
		},
	}

	kubeHandler.nodes["test1"] = &model.Node{
		Metadata: &model.ObjectMeta{
			Labels: map[string]interface{}{
				"test1": "val1",
			},
			Annotations: map[string]interface{}{
				"io.rancher.labels.test1": "",
			},
			Name: "test1",
		},
	}

	sync(kubeClient, metadataClient, c)

	if _, ok := kubeHandler.nodes["test1"].Metadata.Labels["test1"]; ok {
		t.Error("Label test1 was not detected as removed")
	}
}
func (mt *metadataBasedHealthCheck) populateHostMap() (map[string]string, error) {
	m := metadata.NewClient(mt.metadataUrl)

	version, err := m.GetVersion()
	if err != nil {
		return nil, err
	}
	if version == mt.version {
		return mt.prevHosts, nil
	}

	activeHosts := map[string]string{}
	timeStamp := time.Now().Format(time.RFC1123Z)
	stack, err := m.GetSelfStack()
	if err != nil {
		return nil, err
	}
	stackName := stack.Name

	services, err := m.GetServices()
	if err != nil {
		return nil, err
	}

	possibleServices := map[string]bool{}

	for _, service := range stack.Services {
		possibleServices[service] = true
	}

	possibleContainers := map[string]bool{}

	for _, service := range services {
		if _, ok := possibleServices[service.Name]; ok && service.StackName == stackName {
			for _, container := range service.Containers {
				possibleContainers[container] = true
			}
		}
	}

	containers, err := m.GetContainers()
	if err != nil {
		return nil, err
	}

	for _, container := range containers {
		_, okc := possibleContainers[container.Name]
		_, oks := possibleServices[container.ServiceName]
		if okc && container.StackName == stackName && oks {
			activeHosts[container.HostUUID] = timeStamp
		}
	}

	mt.prevHosts = activeHosts
	mt.version = version
	return activeHosts, nil
}
예제 #3
0
func getStackName() string {
	md := metadata.NewClient(metadataURL + "/2015-07-25")

	stackData, err := md.GetSelfStack()
	if err != nil {
		logrus.Errorf("Could not get Stack Name")
		return ""
	}

	return stackData.Name

}
예제 #4
0
func setEnv() {
	flag.Parse()
	provider = providers.GetProvider(*providerName)
	if *debug {
		logrus.SetLevel(logrus.DebugLevel)
	}
	if *logFile != "" {
		if output, err := os.OpenFile(*logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
			logrus.Fatalf("Failed to log to file %s: %v", *logFile, err)
		} else {
			logrus.SetOutput(output)
			formatter := &logrus.TextFormatter{
				FullTimestamp: true,
			}
			logrus.SetFormatter(formatter)
		}
	}

	// configure metadata client
	m = metadata.NewClient(metadataUrl)
	selfStack, err := m.GetSelfStack()
	if err != nil {
		logrus.Fatalf("Error reading stack info: %v", err)
	}
	EnvironmentName = selfStack.EnvironmentName

	cattleUrl := os.Getenv("CATTLE_URL")
	if len(cattleUrl) == 0 {
		logrus.Fatal("CATTLE_URL is not set")
	}

	cattleApiKey := os.Getenv("CATTLE_ACCESS_KEY")
	if len(cattleApiKey) == 0 {
		logrus.Fatal("CATTLE_ACCESS_KEY is not set")
	}

	cattleSecretKey := os.Getenv("CATTLE_SECRET_KEY")
	if len(cattleSecretKey) == 0 {
		logrus.Fatal("CATTLE_SECRET_KEY is not set")
	}

	//configure cattle client
	c, err = NewCattleClient(cattleUrl, cattleApiKey, cattleSecretKey)
	if err != nil {
		logrus.Fatalf("Failed to configure cattle client: %v", err)
	}
}
예제 #5
0
func main() {
	m := metadata.NewClient(metadataUrl)

	version := "init"

	for {
		newVersion, err := m.GetVersion()
		if err != nil {
			logrus.Errorf("Error reading metadata version: %v", err)
		} else if version == newVersion {
			logrus.Debug("No changes in metadata version")
		} else {
			logrus.Debugf("Metadata version has changed, oldVersion=[%s], newVersion=[%s]", version, newVersion)
			version = newVersion
		}
		time.Sleep(5 * time.Second)
	}
}
예제 #6
0
func TestDetectsChange(t *testing.T) {
	metadataClient := metadata.NewClient(fakeMetadataURL)
	kubeClient := kubernetesclient.NewClient(kubeURL, false)
	c := cache.New(1*time.Minute, 1*time.Minute)

	metadataHandler.hosts = []metadata.Host{
		{
			Name:     "test3",
			Hostname: "test3",
			Labels: map[string]string{
				"test3": "val3",
			},
		},
	}

	kubeHandler.nodes["test3"] = &model.Node{
		Metadata: &model.ObjectMeta{
			Labels: map[string]interface{}{
				"test3": "valx",
			},
			Annotations: map[string]interface{}{
				"io.rancher.labels.test3": "",
			},
			Name: "test3",
		},
	}

	sync(kubeClient, metadataClient, c)

	if val := kubeHandler.nodes["test3"].Metadata.Labels["test3"]; val != "val3" {
		t.Error("Label test3 was not detected as changed")
	}

	if _, ok := kubeHandler.nodes["test3"].Metadata.Annotations["io.rancher.labels.test3"]; !ok {
		t.Error("Annotation was not set on addition of new label")
	}
}
예제 #7
0
파일: longhorn.go 프로젝트: srenkens/convoy
func Init(root string, config map[string]string) (convoydriver.ConvoyDriver, error) {
	dev := &Device{
		Root: root,
	}
	exists, err := util.ObjectExists(dev)
	if err != nil {
		return nil, err
	}
	if exists {
		if err := util.ObjectLoad(dev); err != nil {
			return nil, err
		}
		dev.RancherURL = override(dev.RancherURL, config[LH_RANCHER_URL])
		dev.RancherAccessKey = override(dev.RancherAccessKey, config[LH_RANCHER_ACCESS_KEY])
		dev.RancherSecretKey = override(dev.RancherSecretKey, config[LH_RANCHER_SECRET_KEY])
	} else {
		if err := util.MkdirIfNotExists(root); err != nil {
			return nil, err
		}

		url := config[LH_RANCHER_URL]
		accessKey := config[LH_RANCHER_ACCESS_KEY]
		secretKey := config[LH_RANCHER_SECRET_KEY]

		if url == "" || accessKey == "" || secretKey == "" {
			return nil, fmt.Errorf("Missing required parameter. lh.rancherurl or lh.rancheraccesskey or lh.ranchersecretkey")
		}

		if _, exists := config[LH_DEFAULT_VOLUME_SIZE]; !exists {
			config[LH_DEFAULT_VOLUME_SIZE] = DEFAULT_VOLUME_SIZE
		}
		volumeSize, err := util.ParseSize(config[LH_DEFAULT_VOLUME_SIZE])
		if err != nil || volumeSize == 0 {
			return nil, fmt.Errorf("Illegal default volume size specified")
		}

		dev = &Device{
			Root:              root,
			RancherURL:        url,
			RancherAccessKey:  accessKey,
			RancherSecretKey:  secretKey,
			DefaultVolumeSize: volumeSize,
		}
	}

	containerName := config[LH_CONTAINER_NAME]
	if containerName == "" {
		handler := metadata.NewClient(RANCHER_METADATA_URL)
		container, err := handler.GetSelfContainer()
		if err != nil {
			return nil, err
		}
		containerName = container.UUID
	}

	log.Debugf("Try to connect to Rancher server at %s [%s:%s]", dev.RancherURL, dev.RancherAccessKey, dev.RancherSecretKey)
	client, err := rancherClient.NewRancherClient(&rancherClient.ClientOpts{
		Url:       dev.RancherURL,
		AccessKey: dev.RancherAccessKey,
		SecretKey: dev.RancherSecretKey,
	})
	if err != nil {
		return nil, fmt.Errorf("Failed to establish connection to Rancher server")
	}

	if err := util.ObjectSave(dev); err != nil {
		return nil, err
	}
	d := &Driver{
		client:        client,
		containerName: containerName,
		Device:        *dev,
	}

	return d, nil
}