func()

in plugins/processors/ec2tagger/ec2tagger.go [302:387]


func (t *Tagger) Init() error {
	t.shutdownC = make(chan bool)
	t.ec2TagCache = map[string]string{}

	for _, tag := range t.EC2MetadataTags {
		switch tag {
		case mdKeyInstanceId:
			t.metadataLookup.instanceId = true
		case mdKeyImageId:
			t.metadataLookup.imageId = true
		case mdKeyInstaneType:
			t.metadataLookup.instanceType = true
		default:
			t.Log.Errorf("ec2tagger: Unsupported EC2 Metadata key: %s", tag)
		}
	}

	if !t.ec2metadata.Available() {
		msg := "ec2tagger: Unable to retrieve InstanceId. This plugin must only be used on an EC2 instance"
		t.Log.Errorf(msg)
		return errors.New(msg)
	}

	doc, err := t.ec2metadata.GetInstanceIdentityDocument()
	if nil != err {
		msg := fmt.Sprintf("ec2tagger: Unable to retrieve InstanceId : %+v", err.Error())
		t.Log.Errorf(msg)
		return errors.New(msg)
	}

	t.instanceId = doc.InstanceID
	t.region = doc.Region
	t.instanceType = doc.InstanceType
	t.imageId = doc.ImageID

	t.tagFilters = []*ec2.Filter{
		{
			Name:   aws.String("resource-type"),
			Values: aws.StringSlice([]string{"instance"}),
		},
		{
			Name:   aws.String("resource-id"),
			Values: aws.StringSlice([]string{t.instanceId}),
		},
	}

	useAllTags := len(t.EC2InstanceTagKeys) == 1 && t.EC2InstanceTagKeys[0] == "*"

	if !useAllTags && len(t.EC2InstanceTagKeys) > 0 {
		// if the customer said 'AutoScalingGroupName' (the CW dimension), do what they mean not what they said
		// and filter for the EC2 tag name called 'aws:autoscaling:groupName'
		for i, key := range t.EC2InstanceTagKeys {
			if cwDimensionASG == key {
				t.EC2InstanceTagKeys[i] = ec2InstanceTagKeyASG
			}
		}

		t.tagFilters = append(t.tagFilters, &ec2.Filter{
			Name:   aws.String("key"),
			Values: aws.StringSlice(t.EC2InstanceTagKeys),
		})
	}

	if len(t.EC2InstanceTagKeys) > 0 || len(t.EBSDeviceKeys) > 0 {
		ec2CredentialConfig := &configaws.CredentialConfig{
			Region:    t.region,
			AccessKey: t.AccessKey,
			SecretKey: t.SecretKey,
			RoleARN:   t.RoleARN,
			Profile:   t.Profile,
			Filename:  t.Filename,
			Token:     t.Token,
		}
		t.ec2 = t.ec2Provider(ec2CredentialConfig)
		go func() { //Async start of initial retrieval to prevent block of agent start
			t.initialRetrievalOfTagsAndVolumes()
			t.refreshLoopToUpdateTagsAndVolumes()
		}()
		t.Log.Infof("ec2tagger: EC2 tagger has started initialization.")

	} else {
		t.setStarted()
	}

	return nil
}