Skip to content

Commit

Permalink
Review changes. Adds tests for generateDeploymentObjectMeta. Changes …
Browse files Browse the repository at this point in the history
…labels

Signed-off-by: Charlie Drage <charlie@charliedrage.com>
  • Loading branch information
cdrage committed Mar 16, 2022
1 parent e48a539 commit 7176dd0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
8 changes: 3 additions & 5 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (

"github.com/pkg/errors"

applabels "github.com/redhat-developer/odo/pkg/application/labels"
"github.com/redhat-developer/odo/pkg/devfile/location"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/devfile"
"github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
dfutil "github.com/devfile/library/pkg/util"
"github.com/redhat-developer/odo/pkg/devfile/location"

applabels "github.com/redhat-developer/odo/pkg/application/labels"
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/envinfo"
"github.com/redhat-developer/odo/pkg/kclient"
Expand Down Expand Up @@ -329,7 +328,6 @@ func Exists(client kclient.ClientInterface, componentName, applicationName strin
return false, nil
}

// GetComponentState ...
func GetComponentState(client kclient.ClientInterface, componentName, applicationName string) string {
// Check to see if the deployment has been pushed or not
c, err := GetPushedComponent(client, componentName, applicationName)
Expand Down Expand Up @@ -416,7 +414,7 @@ func getRemoteComponentMetadata(client kclient.ClientInterface, componentName st
component.Annotations = fromCluster.GetAnnotations()

// Mark the component status as pushed
component.Status.State = componentlabels.ComponentPushedName
component.Status.State = StateTypePushed

// Labels
component.Labels = fromCluster.GetLabels()
Expand Down
9 changes: 0 additions & 9 deletions pkg/component/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ const KubernetesNameLabel = "app.kubernetes.io/name"
// KubernetesManagedByLabel ...
const KubernetesManagedByLabel = "app.kubernetes.io/managed-by"

// ComponentUnknownLabel is the label that is used to display something we do not know
const ComponentUnknownLabel = "<unknown>"

// ComponentDevName ...
const ComponentDevName = "Dev"

// ComponentDeployName ...
const ComponentDeployName = "Deploy"

// ComponentNoneName ...
const ComponentNoneName = "None"

// ComponentPushedName ...
const ComponentPushedName = "Pushed"

// OdoModeLabel ...
const OdoModeLabel = "odo.dev/mode"

Expand Down
8 changes: 4 additions & 4 deletions pkg/component/pushed_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ func (d devfileComponent) GetName() string {

func getType(component provider) (string, error) {

// For backwards compatibility with previously deployed components that could be non-odo, check the label first
// then check to see if there is an annotation with the project type
if componentType, ok := component.GetLabels()[componentlabels.OdoProjectTypeAnnotation]; ok {
// For backwards compatibility with previously deployed components that could be non-odo, check the annotation first
// then check to see if there is a label with the project type
if componentType, ok := component.GetAnnotations()[componentlabels.OdoProjectTypeAnnotation]; ok {
return componentType, nil
} else if componentType, ok = component.GetAnnotations()[componentlabels.OdoProjectTypeAnnotation]; ok {
} else if componentType, ok = component.GetLabels()[componentlabels.OdoProjectTypeAnnotation]; ok {
return componentType, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {

execRequired, err := syncAdapter.SyncFiles(syncParams)
if err != nil {
return fmt.Errorf("failed to sync to component with name %s: %w", a.ComponentName, err)
return errors.Wrapf(err, "Failed to sync to component with name %s", a.ComponentName)
}
s.End(true)

Expand Down
26 changes: 22 additions & 4 deletions pkg/devfile/adapters/kubernetes/component/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/devfile/library/pkg/testingutil"
applabels "github.com/redhat-developer/odo/pkg/application/labels"
componentLabels "github.com/redhat-developer/odo/pkg/component/labels"
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
adaptersCommon "github.com/redhat-developer/odo/pkg/devfile/adapters/common"
"github.com/redhat-developer/odo/pkg/kclient"
odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil"
Expand Down Expand Up @@ -632,7 +633,8 @@ func TestAdapter_generateDeploymentObjectMeta(t *testing.T) {
deployment *v1.Deployment
}
type args struct {
labels map[string]string
labels map[string]string
annotations map[string]string
}
tests := []struct {
name string
Expand All @@ -649,7 +651,8 @@ func TestAdapter_generateDeploymentObjectMeta(t *testing.T) {
deployment: odoTestingUtil.CreateFakeDeployment("nodejs"),
},
args: args{
labels: odoTestingUtil.CreateFakeDeployment("nodejs").Labels,
labels: odoTestingUtil.CreateFakeDeployment("nodejs").Labels,
annotations: nil,
},
want: generator.GetObjectMeta("nodejs", "project-0", odoTestingUtil.CreateFakeDeployment("nodejs").Labels, nil),
wantErr: false,
Expand All @@ -662,11 +665,26 @@ func TestAdapter_generateDeploymentObjectMeta(t *testing.T) {
deployment: nil,
},
args: args{
labels: odoTestingUtil.CreateFakeDeployment("nodejs").Labels,
labels: odoTestingUtil.CreateFakeDeployment("nodejs").Labels,
annotations: nil,
},
want: generator.GetObjectMeta(namespacedKubernetesName, "project-0", odoTestingUtil.CreateFakeDeployment("nodejs").Labels, nil),
wantErr: false,
},
{
name: "case 3: deployment exists and there is annotations successfully passed in",
fields: fields{
componentName: "nodejs",
appName: "app",
deployment: odoTestingUtil.CreateFakeDeployment("nodejs"),
},
args: args{
labels: odoTestingUtil.CreateFakeDeployment("nodejs").Labels,
annotations: map[string]string{componentlabels.OdoModeLabel: componentlabels.ComponentDevName},
},
want: generator.GetObjectMeta("nodejs", "project-0", odoTestingUtil.CreateFakeDeployment("nodejs").Labels, map[string]string{componentlabels.OdoModeLabel: componentlabels.ComponentDevName}),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -683,7 +701,7 @@ func TestAdapter_generateDeploymentObjectMeta(t *testing.T) {
},
deployment: tt.fields.deployment,
}
got, err := a.generateDeploymentObjectMeta(tt.args.labels, nil)
got, err := a.generateDeploymentObjectMeta(tt.args.labels, tt.args.annotations)
if (err != nil) != tt.wantErr {
t.Errorf("generateDeploymentObjectMeta() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 7176dd0

Please sign in to comment.