Skip to content

Commit 87481d9

Browse files
authored
Merge pull request #2813 from NikhilSharmaWe/deployImageLabels
✨deploy-image/v1-alpha: change labels to follow the standards
2 parents aba89c5 + 18efd3d commit 87481d9

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Controller struct {
3232
machinery.MultiGroupMixin
3333
machinery.BoilerplateMixin
3434
machinery.ResourceMixin
35+
machinery.ProjectNameMixin
3536

3637
ControllerRuntimeVersion string
3738

@@ -71,6 +72,7 @@ import (
7172
"k8s.io/apimachinery/pkg/types"
7273
7374
"context"
75+
"strings"
7476
"time"
7577
"fmt"
7678
"os"
@@ -285,8 +287,19 @@ func (r *{{ .Resource.Kind }}Reconciler) deploymentFor{{ .Resource.Kind }}(ctx c
285287
286288
// labelsFor{{ .Resource.Kind }} returns the labels for selecting the resources
287289
// belonging to the given {{ .Resource.Kind }} CR name.
290+
// Note that the labels follows the standards defined in: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
288291
func labelsFor{{ .Resource.Kind }}(name string) map[string]string {
289-
return map[string]string{"type": "{{ lower .Resource.Kind }}", "{{ lower .Resource.Kind }}_cr": name}
292+
var imageTag string
293+
image, err := imageFor{{ .Resource.Kind }}()
294+
if err == nil {
295+
imageTag = strings.Split(image, ":")[1]
296+
}
297+
return map[string]string{"app.kubernetes.io/name": "{{ .Resource.Kind }}",
298+
"app.kubernetes.io/instance": name,
299+
"app.kubernetes.io/version": imageTag,
300+
"app.kubernetes.io/part-of": "{{ .ProjectName }}",
301+
"app.kubernetes.io/created-by": "controller-manager",
302+
}
290303
}
291304
292305
// imageFor{{ .Resource.Kind }} gets the image for the resources belonging to the given {{ .Resource.Kind }} CR,

test/e2e/deployimage/plugin_cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func Run(kbc *utils.TestContext) {
252252
By("validating that pod(s) status.phase=Running")
253253
getMemcachedPodStatus := func() error {
254254
status, err := kbc.Kubectl.Get(true, "pods", "-l",
255-
fmt.Sprintf("type=%s", strings.ToLower(kbc.Kind)),
255+
fmt.Sprintf("app.kubernetes.io/name=%s", strings.ToLower(kbc.Kind)),
256256
"-o", "jsonpath={.items[*].status}",
257257
)
258258
ExpectWithOffset(2, err).NotTo(HaveOccurred())

testdata/project-v3-with-deploy-image/controllers/busybox_controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"os"
29+
"strings"
2930
"time"
3031

3132
"k8s.io/apimachinery/pkg/runtime"
@@ -252,8 +253,19 @@ func (r *BusyboxReconciler) deploymentForBusybox(ctx context.Context, busybox *e
252253

253254
// labelsForBusybox returns the labels for selecting the resources
254255
// belonging to the given Busybox CR name.
256+
// Note that the labels follows the standards defined in: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
255257
func labelsForBusybox(name string) map[string]string {
256-
return map[string]string{"type": "busybox", "busybox_cr": name}
258+
var imageTag string
259+
image, err := imageForBusybox()
260+
if err == nil {
261+
imageTag = strings.Split(image, ":")[1]
262+
}
263+
return map[string]string{"app.kubernetes.io/name": "Busybox",
264+
"app.kubernetes.io/instance": name,
265+
"app.kubernetes.io/version": imageTag,
266+
"app.kubernetes.io/part-of": "project-v3-with-deploy-image",
267+
"app.kubernetes.io/created-by": "controller-manager",
268+
}
257269
}
258270

259271
// imageForBusybox gets the image for the resources belonging to the given Busybox CR,

testdata/project-v3-with-deploy-image/controllers/memcached_controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"os"
29+
"strings"
2930
"time"
3031

3132
"k8s.io/apimachinery/pkg/runtime"
@@ -258,8 +259,19 @@ func (r *MemcachedReconciler) deploymentForMemcached(ctx context.Context, memcac
258259

259260
// labelsForMemcached returns the labels for selecting the resources
260261
// belonging to the given Memcached CR name.
262+
// Note that the labels follows the standards defined in: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
261263
func labelsForMemcached(name string) map[string]string {
262-
return map[string]string{"type": "memcached", "memcached_cr": name}
264+
var imageTag string
265+
image, err := imageForMemcached()
266+
if err == nil {
267+
imageTag = strings.Split(image, ":")[1]
268+
}
269+
return map[string]string{"app.kubernetes.io/name": "Memcached",
270+
"app.kubernetes.io/instance": name,
271+
"app.kubernetes.io/version": imageTag,
272+
"app.kubernetes.io/part-of": "project-v3-with-deploy-image",
273+
"app.kubernetes.io/created-by": "controller-manager",
274+
}
263275
}
264276

265277
// imageForMemcached gets the image for the resources belonging to the given Memcached CR,

testdata/project-v4-with-deploy-image/controllers/busybox_controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"os"
29+
"strings"
2930
"time"
3031

3132
"k8s.io/apimachinery/pkg/runtime"
@@ -252,8 +253,19 @@ func (r *BusyboxReconciler) deploymentForBusybox(ctx context.Context, busybox *e
252253

253254
// labelsForBusybox returns the labels for selecting the resources
254255
// belonging to the given Busybox CR name.
256+
// Note that the labels follows the standards defined in: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
255257
func labelsForBusybox(name string) map[string]string {
256-
return map[string]string{"type": "busybox", "busybox_cr": name}
258+
var imageTag string
259+
image, err := imageForBusybox()
260+
if err == nil {
261+
imageTag = strings.Split(image, ":")[1]
262+
}
263+
return map[string]string{"app.kubernetes.io/name": "Busybox",
264+
"app.kubernetes.io/instance": name,
265+
"app.kubernetes.io/version": imageTag,
266+
"app.kubernetes.io/part-of": "project-v4-with-deploy-image",
267+
"app.kubernetes.io/created-by": "controller-manager",
268+
}
257269
}
258270

259271
// imageForBusybox gets the image for the resources belonging to the given Busybox CR,

testdata/project-v4-with-deploy-image/controllers/memcached_controller.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"context"
2727
"fmt"
2828
"os"
29+
"strings"
2930
"time"
3031

3132
"k8s.io/apimachinery/pkg/runtime"
@@ -258,8 +259,19 @@ func (r *MemcachedReconciler) deploymentForMemcached(ctx context.Context, memcac
258259

259260
// labelsForMemcached returns the labels for selecting the resources
260261
// belonging to the given Memcached CR name.
262+
// Note that the labels follows the standards defined in: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
261263
func labelsForMemcached(name string) map[string]string {
262-
return map[string]string{"type": "memcached", "memcached_cr": name}
264+
var imageTag string
265+
image, err := imageForMemcached()
266+
if err == nil {
267+
imageTag = strings.Split(image, ":")[1]
268+
}
269+
return map[string]string{"app.kubernetes.io/name": "Memcached",
270+
"app.kubernetes.io/instance": name,
271+
"app.kubernetes.io/version": imageTag,
272+
"app.kubernetes.io/part-of": "project-v4-with-deploy-image",
273+
"app.kubernetes.io/created-by": "controller-manager",
274+
}
263275
}
264276

265277
// imageForMemcached gets the image for the resources belonging to the given Memcached CR,

0 commit comments

Comments
 (0)