Skip to content

Commit

Permalink
Refactor labels (#5618)
Browse files Browse the repository at this point in the history
* refacto labels

* Getters

* Builder

* Do not export labels

* Move to pkg/labels + doc

* Fix rebase
  • Loading branch information
feloy authored Apr 28, 2022
1 parent dbfde00 commit feb4960
Show file tree
Hide file tree
Showing 41 changed files with 640 additions and 929 deletions.
61 changes: 0 additions & 61 deletions pkg/application/labels/labels.go

This file was deleted.

76 changes: 0 additions & 76 deletions pkg/application/labels/labels_test.go

This file was deleted.

27 changes: 10 additions & 17 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/devfile/library/pkg/devfile/parser"
dfutil "github.com/devfile/library/pkg/util"

componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog"
Expand Down Expand Up @@ -80,7 +80,7 @@ func Exists(client kclient.ClientInterface, componentName, applicationName strin

// GetOnePod gets a pod using the component and app name
func GetOnePod(client kclient.ClientInterface, componentName string, appName string) (*corev1.Pod, error) {
return client.GetOnePodFromSelector(componentlabels.GetSelector(componentName, appName))
return client.GetOnePodFromSelector(odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode))
}

// ComponentExists checks whether a deployment by the given name exists in the given app
Expand Down Expand Up @@ -149,31 +149,24 @@ func ListAllClusterComponents(client kclient.ClientInterface, namespace string)
// Figure out the correct name to use
// if there is no instance label, we SKIP the resource as
// it is not a component essential for Kubernetes.
var name string
if labels[componentlabels.KubernetesInstanceLabel] != "" {
name = labels[componentlabels.KubernetesInstanceLabel]
} else {
name := odolabels.GetComponentName(labels)
if name == "" {
continue
}

// Get the component type (if there is any..)
var componentType string
switch {
case annotations[componentlabels.OdoProjectTypeAnnotation] != "":
componentType = annotations[componentlabels.OdoProjectTypeAnnotation]
default:
componentType, err := odolabels.GetProjectType(nil, annotations)
if err != nil || componentType == "" {
componentType = StateTypeUnknown
}

// Get the managedBy label
// IMPORTANT. If "managed-by" label is BLANK, it is most likely an operator
// or a non-component. We do not want to show these in the list of components
// so we skip them if there is no "managed-by" label.
var managedBy string
switch {
case labels[componentlabels.KubernetesManagedByLabel] != "":
managedBy = labels[componentlabels.KubernetesManagedByLabel]
default:

managedBy := odolabels.GetManagedBy(labels)
if managedBy == "" {
continue
}

Expand All @@ -183,7 +176,7 @@ func ListAllClusterComponents(client kclient.ClientInterface, namespace string)
ManagedBy: managedBy,
Type: componentType,
}
mode := labels[componentlabels.OdoModeLabel]
mode := odolabels.GetMode(labels)
found := false
for v, otherCompo := range components {
if component.Name == otherCompo.Name {
Expand Down
16 changes: 8 additions & 8 deletions pkg/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
devfilepkg "github.com/devfile/api/v2/pkg/devfile"
"github.com/golang/mock/gomock"
"github.com/kylelemons/godebug/pretty"
"github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/labels"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -263,12 +263,12 @@ func getUnstructured(name, kind, apiVersion, managed, componentType, namespace s
u.SetKind(kind)
u.SetAPIVersion(apiVersion)
u.SetNamespace(namespace)
u.SetLabels(map[string]string{
labels.KubernetesInstanceLabel: name,
labels.KubernetesManagedByLabel: managed,
})
u.SetAnnotations(map[string]string{
labels.OdoProjectTypeAnnotation: componentType,
})
u.SetLabels(labels.Builder().
WithComponentName(name).
WithManager(managed).
Labels())
u.SetAnnotations(labels.Builder().
WithProjectType(componentType).
Labels())
return
}
9 changes: 3 additions & 6 deletions pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog"

applabels "github.com/redhat-developer/odo/pkg/application/labels"
"github.com/redhat-developer/odo/pkg/component"
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/util"
Expand All @@ -34,9 +33,7 @@ func NewDeleteComponentClient(kubeClient kclient.ClientInterface) *DeleteCompone
// It only returns resources not owned by another resource of the component, letting the garbage collector do its job
func (do *DeleteComponentClient) ListClusterResourcesToDelete(componentName string, namespace string) ([]unstructured.Unstructured, error) {
var result []unstructured.Unstructured
labels := componentlabels.GetLabels(componentName, "app", false)
labels[applabels.ManagedBy] = "odo"
selector := util.ConvertLabelsToSelector(labels)
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentAnyMode)
list, err := do.kubeClient.GetAllResourcesFromSelector(selector, namespace)
if err != nil {
return nil, err
Expand Down Expand Up @@ -149,7 +146,7 @@ func (do *DeleteComponentClient) ExecutePreStopEvents(devfileObj parser.DevfileO
klog.V(4).Infof("Gathering information for component: %q", componentName)

klog.V(3).Infof("Checking component status for %q", componentName)
selector := componentlabels.GetSelector(componentName, appName)
selector := odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode)
pod, err := do.kubeClient.GetOnePodFromSelector(selector)
if err != nil {
klog.V(1).Info("Component not found on the cluster.")
Expand Down
12 changes: 6 additions & 6 deletions pkg/component/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"
odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil"
"github.com/redhat-developer/odo/pkg/util"
)
Expand Down Expand Up @@ -498,7 +498,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
client := kclient.NewMockClientInterface(ctrl)

selector := componentlabels.GetSelector(componentName, "app")
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode)
client.EXPECT().GetOnePodFromSelector(selector).Return(&corev1.Pod{}, &kclient.PodNotFoundError{Selector: selector})
return client
},
Expand All @@ -515,7 +515,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
client := kclient.NewMockClientInterface(ctrl)

selector := componentlabels.GetSelector(componentName, "app")
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode)
client.EXPECT().GetOnePodFromSelector(selector).Return(nil, errors.New("some un-ignorable error"))
return client
},
Expand All @@ -532,7 +532,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
client := kclient.NewMockClientInterface(ctrl)

selector := componentlabels.GetSelector(componentName, "app")
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode)
client.EXPECT().GetOnePodFromSelector(selector).Return(odoTestingUtil.CreateFakePod(componentName, "runtime"), nil)

cmd := []string{"/bin/sh", "-c", "cd /projects/nodejs-starter && echo \"Hello World!\""}
Expand All @@ -553,7 +553,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
client := kclient.NewMockClientInterface(ctrl)

selector := componentlabels.GetSelector(componentName, "app")
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode)
pod := odoTestingUtil.CreateFakePod(componentName, "runtime")
pod.Status.Phase = corev1.PodFailed
client.EXPECT().GetOnePodFromSelector(selector).Return(pod, nil)
Expand All @@ -572,7 +572,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
client := kclient.NewMockClientInterface(ctrl)

selector := componentlabels.GetSelector(componentName, "app")
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode)
client.EXPECT().GetOnePodFromSelector(selector).Return(odoTestingUtil.CreateFakePod(componentName, "runtime"), nil)

cmd := []string{"/bin/sh", "-c", "cd /projects/nodejs-starter && echo \"Hello World!\""}
Expand Down
39 changes: 0 additions & 39 deletions pkg/component/labels/labels.go

This file was deleted.

Loading

0 comments on commit feb4960

Please sign in to comment.