-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
77 lines (63 loc) · 3.06 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package pkg
import (
appsv1beta1 "k8s.io/api/apps/v1beta1"
"k8s.io/client-go/kubernetes"
)
const (
// AnnotationToWatch is the annotation that Deployments need to have
AnnotationToWatch = "armesto.net/iam-role-annotator"
// Kube2IAMAnnotation is the standard kube2iam annotation used to specify the IAM Role to assume
Kube2IAMAnnotation = "iam.amazonaws.com/role"
)
// IamRoleAnnotatorInterface for the IamRoleAnnotator
type IamRoleAnnotatorInterface interface {
Annotate(deployment appsv1beta1.Deployment) (*appsv1beta1.Deployment, error)
}
// IamRoleAnnotator is simple annotator service.
type IamRoleAnnotator struct {
client kubernetes.Interface
awsAccountID string
logger Logger
}
// NewIamRoleAnnotator returns a new IamRoleAnnotator.
func NewIamRoleAnnotator(k8sCli kubernetes.Interface, awsAccountID string, logger Logger) *IamRoleAnnotator {
return &IamRoleAnnotator{
client: k8sCli,
awsAccountID: awsAccountID,
logger: logger,
}
}
// Annotate will add the kube2iam annotation to Deployment objects containing the special annotation
func (s *IamRoleAnnotator) Annotate(deployment appsv1beta1.Deployment) (*appsv1beta1.Deployment, error) {
newDeployment := deployment.DeepCopy()
_, deploymentExpectsToBeAnnotated := newDeployment.ObjectMeta.Annotations[AnnotationToWatch]
if !deploymentExpectsToBeAnnotated {
return newDeployment, nil
}
s.logger.Infof("Detected deploy/%s annotated with %s: %s", newDeployment.ObjectMeta.Name, AnnotationToWatch, newDeployment.ObjectMeta.Annotations[AnnotationToWatch])
_, alreadyContainsKube2IamAnnotation := newDeployment.Spec.Template.ObjectMeta.Annotations[Kube2IAMAnnotation]
if alreadyContainsKube2IamAnnotation {
s.logger.Infof("deploy/%s already contains %s:%s, skipping...", newDeployment.ObjectMeta.Name, Kube2IAMAnnotation, newDeployment.Spec.Template.ObjectMeta.Annotations[Kube2IAMAnnotation])
return newDeployment, nil
}
s.annotate(newDeployment, s.getRoleArn(newDeployment))
_, err := s.submitChangesToKubernetesAPI(newDeployment)
return newDeployment, err
}
// getRoleArn returns the role arn to put in the Deployment annotation.
func (s *IamRoleAnnotator) getRoleArn(deployment *appsv1beta1.Deployment) string {
return "arn:aws:iam::" + s.awsAccountID + ":role/" + deployment.ObjectMeta.Name
}
// annotate adds the annotation to the Deployment
func (s *IamRoleAnnotator) annotate(deployment *appsv1beta1.Deployment, role string) {
s.logger.Infof("Adding IAM Role annotation '%s' to the PodSpec of the Deployment '%s'", role, deployment.Name)
if deployment.Spec.Template.ObjectMeta.Annotations == nil {
deployment.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
}
deployment.Spec.Template.ObjectMeta.Annotations[Kube2IAMAnnotation] = role
}
// submitChangesToKubernetesAPI updates the Deployment object in the Kubernetes API
func (s *IamRoleAnnotator) submitChangesToKubernetesAPI(deployment *appsv1beta1.Deployment) (*appsv1beta1.Deployment, error) {
s.logger.Infof("Sending changes to k8s API")
return s.client.AppsV1beta1().Deployments(deployment.Namespace).Update(deployment)
}