Skip to content

Commit

Permalink
Add support for annotations on Deployment/StatefulSet resources in Dr…
Browse files Browse the repository at this point in the history
…uidNodeSpec (#145)

* Add support for annotations on Deployment/StatefulSet resources

* Support setting ReplicationControllerAnnotations at the cluster-level

* rename replicationControllerAnnotations to workloadAnnotations

* suggestions from code review
  • Loading branch information
SamWheating committed Mar 28, 2024
1 parent 09aeeb3 commit 6eb2937
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 8 deletions.
9 changes: 9 additions & 0 deletions apis/druid/v1alpha1/druid_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ type DruidSpec struct {
// +optional
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`

// WorkloadAnnotations annotations to be populated in StatefulSet or Deployment spec.
// if the same key is specified at both the DruidNodeSpec level and DruidSpec level, the DruidNodeSpec WorkloadAnnotations will take precedence.
// +optional
WorkloadAnnotations map[string]string `json:"workloadAnnotations,omitempty"`

// PodManagementPolicy
// +optional
// +kubebuilder:default:="Parallel"
Expand Down Expand Up @@ -433,6 +438,10 @@ type DruidNodeSpec struct {
// +optional
IngressAnnotations map[string]string `json:"ingressAnnotations,omitempty"`

// WorkloadAnnotations annotations to be populated in StatefulSet or Deployment spec.
// +optional
WorkloadAnnotations map[string]string `json:"workloadAnnotations,omitempty"`

// Ingress Kubernetes Native `Ingress` specification.
// +optional
Ingress *networkingv1.IngressSpec `json:"ingress,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions apis/druid/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions chart/templates/crds/druid.apache.org_druids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9025,6 +9025,12 @@ spec:
- name
type: object
type: array
workloadAnnotations:
additionalProperties:
type: string
description: WorkloadAnnotations annotations to be populated
in StatefulSet or Deployment spec.
type: object
required:
- druid.port
- nodeConfigMountPath
Expand Down Expand Up @@ -12094,6 +12100,14 @@ spec:
- name
type: object
type: array
workloadAnnotations:
additionalProperties:
type: string
description: WorkloadAnnotations annotations to be populated in StatefulSet
or Deployment spec. if the same key is specified at both the DruidNodeSpec
level and DruidSpec level, the DruidNodeSpec WorkloadAnnotations
will take precedence.
type: object
zookeeper:
description: 'Zookeeper IGNORED (Future API): In order to make Druid
dependency setup extensible from within Druid operator.'
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/druid.apache.org_druids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9025,6 +9025,12 @@ spec:
- name
type: object
type: array
workloadAnnotations:
additionalProperties:
type: string
description: WorkloadAnnotations annotations to be populated
in StatefulSet or Deployment spec.
type: object
required:
- druid.port
- nodeConfigMountPath
Expand Down Expand Up @@ -12094,6 +12100,14 @@ spec:
- name
type: object
type: array
workloadAnnotations:
additionalProperties:
type: string
description: WorkloadAnnotations annotations to be populated in StatefulSet
or Deployment spec. if the same key is specified at both the DruidNodeSpec
level and DruidSpec level, the DruidNodeSpec WorkloadAnnotations
will take precedence.
type: object
zookeeper:
description: 'Zookeeper IGNORED (Future API): In order to make Druid
dependency setup extensible from within Druid operator.'
Expand Down
30 changes: 24 additions & 6 deletions controllers/druid/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,10 @@ func makeStatefulSet(nodeSpec *v1alpha1.DruidNodeSpec, m *v1alpha1.Druid, ls map
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s", nodeSpecUniqueStr),
Namespace: m.Namespace,
Labels: ls,
Name: fmt.Sprintf("%s", nodeSpecUniqueStr),
Annotations: makeAnnotationsForWorkload(nodeSpec, m),
Namespace: m.Namespace,
Labels: ls,
},
Spec: makeStatefulSetSpec(nodeSpec, m, ls, nodeSpecUniqueStr, configMapSHA, serviceName),
}, nil
Expand All @@ -1022,9 +1023,10 @@ func makeDeployment(nodeSpec *v1alpha1.DruidNodeSpec, m *v1alpha1.Druid, ls map[
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s", nodeSpecUniqueStr),
Namespace: m.Namespace,
Labels: ls,
Name: fmt.Sprintf("%s", nodeSpecUniqueStr),
Annotations: makeAnnotationsForWorkload(nodeSpec, m),
Namespace: m.Namespace,
Labels: ls,
},
Spec: makeDeploymentSpec(nodeSpec, m, ls, nodeSpecUniqueStr, configMapSHA, serviceName),
}, nil
Expand Down Expand Up @@ -1287,6 +1289,22 @@ func makeLabelsForNodeSpec(nodeSpec *v1alpha1.DruidNodeSpec, m *v1alpha1.Druid,
return labels
}

// makeAnnotationsForWorkload returns the annotations for a Deployment or StatefulSet
// If a given key is set in both the DruidSpec and DruidNodeSpec, the node-scoped value will take precedence.
func makeAnnotationsForWorkload(nodeSpec *v1alpha1.DruidNodeSpec, m *v1alpha1.Druid) map[string]string {
var annotations = map[string]string{}

if m.Spec.WorkloadAnnotations != nil {
annotations = m.Spec.WorkloadAnnotations
}

for k, v := range nodeSpec.WorkloadAnnotations {
annotations[k] = v
}

return annotations
}

// addOwnerRefToObject appends the desired OwnerReference to the object
func addOwnerRefToObject(obj metav1.Object, ownerRef metav1.OwnerReference) {
obj.SetOwnerReferences(append(obj.GetOwnerReferences(), ownerRef))
Expand Down
5 changes: 4 additions & 1 deletion controllers/druid/testdata/broker-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ metadata:
nodeSpecUniqueStr: druid-druid-test-brokers
component: broker
annotations:
druidOpResourceHash: DYdfEwwDlLKgjTSZeuJb5nloe/w=
druidOpResourceHash: nQv/LmxctTSRsI5dtBe3jvN5WM8=
kubernetes.io/cluster-scoped-annotation: "cluster"
kubernetes.io/node-scoped-annotation: "broker"
kubernetes.io/override-annotation: "node-scoped-annotation"
spec:
replicas: 2
selector:
Expand Down
5 changes: 4 additions & 1 deletion controllers/druid/testdata/broker-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ metadata:
nodeSpecUniqueStr: druid-druid-test-brokers
component: broker
annotations:
druidOpResourceHash: lTyDduvMnbQ7O2Glf57R9c5bFtA=
druidOpResourceHash: LXsKmbxQkX+94LkevlKDy4wemRQ=
kubernetes.io/cluster-scoped-annotation: "cluster"
kubernetes.io/node-scoped-annotation: "broker"
kubernetes.io/override-annotation: "node-scoped-annotation"
spec:
podManagementPolicy: Parallel
replicas: 2
Expand Down
6 changes: 6 additions & 0 deletions controllers/druid/testdata/druid-test-cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ spec:
image: himanshu01/druid:druid-0.12.0-1
defaultProbes: true
priorityClassName: high-priority
workloadAnnotations:
kubernetes.io/cluster-scoped-annotation: "cluster"
kubernetes.io/override-annotation: "cluster-scoped-annotation"
podAnnotations:
key1: value1
key2: value2
Expand Down Expand Up @@ -109,6 +112,9 @@ spec:
nodes:
brokers:
nodeType: "broker"
workloadAnnotations:
kubernetes.io/node-scoped-annotation: "broker"
kubernetes.io/override-annotation: "node-scoped-annotation"
services:
- spec:
type: ClusterIP
Expand Down
Loading

0 comments on commit 6eb2937

Please sign in to comment.