Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
k8sutil: make pod annotations configurable (#1896)
Browse files Browse the repository at this point in the history
* k8sutil: make pod annotations configurable

Annotations are a way to attach metadata to Kubernetes objects. This
PR extends the operator configuration with an option to attach
annotations to pods that the operator creates for the cluster. A
use case for this is prometheus using annotations to identify pods from
which metrics can be scraped, including pod-specific scraping
configuration. The changes in this PR would enable users to configure
etcd pods created by the operator to be annotated for scraping by
prometheus.

* apis: updated generated files
  • Loading branch information
akrennmair authored and hongchaodeng committed Feb 6, 2018
1 parent ab04332 commit 0792635
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/user/spec_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,16 @@ spec:

For more information on working with TLS, see [Cluster TLS policy][cluster-tls].

## Custom pod annotations

```yaml
spec:
size: 3
pod:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "2379"
```


[cluster-tls]: cluster_tls.md
5 changes: 5 additions & 0 deletions pkg/apis/etcd/v1beta2/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ type PodPolicy struct {
// Note. This feature is in alpha stage. It is currently only used as non-stable storage,
// not the stable storage. Future work need to make it used as stable storage.
PersistentVolumeClaimSpec *v1.PersistentVolumeClaimSpec `json:"persistentVolumeClaimSpec,omitempty"`

// Annotations specifies the annotations to attach to pods the operator creates for the
// etcd cluster.
// The "etcd.version" annotation is reserved for the internal use of the etcd operator.
Annotations map[string]string `json:"annotations,omitempty"`
}

func (c *ClusterSpec) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/etcd/v1beta2/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,13 @@ func (in *PodPolicy) DeepCopyInto(out *PodPolicy) {
(*in).DeepCopyInto(*out)
}
}
if in.Annotations != nil {
in, out := &in.Annotations, &out.Annotations
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
return
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/util/k8sutil/pod_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func applyPodPolicy(clusterName string, pod *v1.Pod, policy *api.PodPolicy) {
for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i] = containerWithRequirements(pod.Spec.InitContainers[i], policy.Resources)
}

for key, value := range policy.Annotations {
pod.ObjectMeta.Annotations[key] = value
}
}

// IsPodReady returns false if the Pod Status is nil
Expand Down

0 comments on commit 0792635

Please sign in to comment.