Skip to content

Commit

Permalink
Annotation and label can be added with spec/template of RunnerPool (#57)
Browse files Browse the repository at this point in the history
* add label and annotation

Signed-off-by: kouki <[email protected]>
  • Loading branch information
kmdkuk authored Aug 5, 2021
1 parent 89280e0 commit 9b19ebd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
15 changes: 15 additions & 0 deletions api/v1alpha1/runnerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type SlackAgentConfig struct {
}

type RunnerPodTemplateSec struct {
// Standard object's metadata. Only `annotations` and `labels` are valid.
// +optional
ObjectMeta `json:"metadata"`

// Docker image name for the runner container.
// +optional
Image string `json:"image,omitempty"`
Expand Down Expand Up @@ -95,6 +99,17 @@ type RunnerPodTemplateSec struct {
ServiceAccountName string `json:"serviceAccountName,omitempty"`
}

// ObjectMeta is metadata of objects.
// This is partially copied from metav1.ObjectMeta.
type ObjectMeta struct {
// Labels is a map of string keys and values.
// +optional
Labels map[string]string `json:"labels,omitempty"`
// Annotations is a map of string keys and values.
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

// RunnerPoolStatus defines status of RunnerPool
type RunnerPoolStatus struct {
// Bound is true when the child Deployment is created.
Expand Down
30 changes: 30 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

15 changes: 15 additions & 0 deletions config/crd/bases/meows.cybozu.com_runnerpools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ spec:
type: string
type: object
type: array
metadata:
description: Standard object's metadata. Only `annotations` and
`labels` are valid.
properties:
annotations:
additionalProperties:
type: string
description: Annotations is a map of string keys and values.
type: object
labels:
additionalProperties:
type: string
description: Labels is a map of string keys and values.
type: object
type: object
resources:
description: Compute Resources required by the runner container.
properties:
Expand Down
5 changes: 4 additions & 1 deletion controllers/runnerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ func (r *RunnerPoolReconciler) reconcileDeployment(ctx context.Context, log logr
d.Spec.Selector = &metav1.LabelSelector{
MatchLabels: labelSet(rp, constants.AppComponentRunner),
}
d.Spec.Template.Labels = labelSetForRunnerPod(rp, r.organizationName)

d.Spec.Template.Labels = mergeMap(d.Spec.Template.GetLabels(), rp.Spec.Template.ObjectMeta.Labels)
d.Spec.Template.Labels = mergeMap(d.Spec.Template.GetLabels(), labelSetForRunnerPod(rp, r.organizationName))
d.Spec.Template.Annotations = mergeMap(d.Spec.Template.GetAnnotations(), rp.Spec.Template.ObjectMeta.Annotations)

d.Spec.Replicas = pointer.Int32Ptr(rp.Spec.Replicas)
d.Spec.Template.Spec.ServiceAccountName = rp.Spec.Template.ServiceAccountName
Expand Down
22 changes: 22 additions & 0 deletions controllers/runnerpool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ var _ = Describe("RunnerPool reconciler", func() {
rp.Spec.SetupCommand = []string{"command", "arg1", "args2"}
rp.Spec.SlackAgent.ServiceName = "slack-agent"
rp.Spec.SlackAgent.Channel = "#test"
rp.Spec.Template.ObjectMeta.Labels = map[string]string{
"test-label": "test",
constants.RunnerOrgLabelKey: "should-not-be-updated",
}
rp.Spec.Template.ObjectMeta.Annotations = map[string]string{
"test-annotation": "test",
}
rp.Spec.Template.Image = "sample:devel"
rp.Spec.Template.ImagePullPolicy = corev1.PullIfNotPresent
rp.Spec.Template.ImagePullSecrets = []corev1.LocalObjectReference{
Expand Down Expand Up @@ -310,6 +317,21 @@ var _ = Describe("RunnerPool reconciler", func() {
}))

// runner container spec
Expect(d.Spec.Template).To(MatchFields(IgnoreExtras, Fields{
"ObjectMeta": MatchFields(IgnoreExtras, Fields{
"Labels": MatchAllKeys(Keys{
constants.AppNameLabelKey: Equal(constants.AppName),
constants.AppComponentLabelKey: Equal(constants.AppComponentRunner),
constants.AppInstanceLabelKey: Equal(rp.Name),
constants.RunnerOrgLabelKey: Equal(organizationName),
constants.RunnerRepoLabelKey: Equal(rp.Spec.RepositoryName),
"test-label": Equal("test"),
}),
"Annotations": MatchAllKeys(Keys{
"test-annotation": Equal("test"),
}),
}),
}))
Expect(d.Spec.Template.Spec.Containers).To(HaveLen(1))
Expect(d.Spec.Template.Spec.Containers[0]).To(MatchFields(IgnoreExtras, Fields{
"Name": Equal(constants.RunnerContainerName),
Expand Down

0 comments on commit 9b19ebd

Please sign in to comment.